Skip to content

Commit 85543d6

Browse files
authored
add helper tests (#472)
Signed-off-by: yxxhero <[email protected]>
1 parent 95b1ea0 commit 85543d6

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

cmd/helpers_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
"os/exec"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func captureStdout(f func()) (string, error) {
14+
old := os.Stdout
15+
r, w, err := os.Pipe()
16+
if err != nil {
17+
return "", err
18+
}
19+
os.Stdout = w
20+
21+
defer func() {
22+
os.Stdout = old
23+
}()
24+
25+
f()
26+
27+
w.Close()
28+
29+
var buf bytes.Buffer
30+
_, err = io.Copy(&buf, r)
31+
if err != nil {
32+
return "", err
33+
}
34+
return buf.String(), nil
35+
}
36+
37+
func TestCaptureStdout(t *testing.T) {
38+
output, err := captureStdout(func() {
39+
_, _ = os.Stdout.Write([]byte("test"))
40+
})
41+
require.NoError(t, err)
42+
require.Equal(t, "test", output)
43+
}
44+
45+
func TestIsDebug(t *testing.T) {
46+
tests := []struct {
47+
name string
48+
envValue string
49+
expected bool
50+
}{
51+
{
52+
name: "HELM_DEBUG is true",
53+
envValue: "true",
54+
expected: true,
55+
},
56+
{
57+
name: "HELM_DEBUG is false",
58+
envValue: "false",
59+
expected: false,
60+
},
61+
}
62+
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
t.Setenv("HELM_DEBUG", tt.envValue)
66+
require.Equalf(t, tt.expected, isDebug(), "Expected %v but got %v", tt.expected, isDebug())
67+
})
68+
}
69+
}
70+
71+
func TestDebugPrint(t *testing.T) {
72+
tests := []struct {
73+
name string
74+
envValue string
75+
expected string
76+
}{
77+
{
78+
name: "non-empty when HELM_DEBUG is true",
79+
envValue: "true",
80+
expected: "test\n",
81+
},
82+
{
83+
name: "empty when HELM_DEBUG is false",
84+
envValue: "false",
85+
expected: "",
86+
},
87+
}
88+
89+
for _, tt := range tests {
90+
t.Run(tt.name, func(t *testing.T) {
91+
t.Setenv("HELM_DEBUG", tt.envValue)
92+
output, err := captureStdout(func() {
93+
debugPrint("test")
94+
})
95+
require.NoError(t, err)
96+
require.Equalf(t, tt.expected, output, "Expected %v but got %v", tt.expected, output)
97+
})
98+
}
99+
}
100+
101+
func TestOutputWithRichError(t *testing.T) {
102+
tests := []struct {
103+
name string
104+
envValue string
105+
cmd *exec.Cmd
106+
expected string
107+
expectedStdout string
108+
}{
109+
{
110+
name: "debug output in stdout when HELM_DEBUG is true",
111+
envValue: "true",
112+
cmd: exec.Command("echo", "test1"),
113+
expected: "test1\n",
114+
expectedStdout: "Executing echo test1\n",
115+
},
116+
{
117+
name: "non-debug output in stdout when HELM_DEBUG is false",
118+
envValue: "false",
119+
cmd: exec.Command("echo", "test2"),
120+
expected: "test2\n",
121+
expectedStdout: "",
122+
},
123+
}
124+
125+
for _, tt := range tests {
126+
t.Run(tt.name, func(t *testing.T) {
127+
t.Setenv("HELM_DEBUG", tt.envValue)
128+
var (
129+
stdoutString string
130+
outBytes []byte
131+
funcErr, captureErr error
132+
)
133+
stdoutString, captureErr = captureStdout(func() {
134+
outBytes, funcErr = outputWithRichError(tt.cmd)
135+
})
136+
require.NoError(t, captureErr)
137+
require.NoError(t, funcErr)
138+
require.Equalf(t, tt.expected, string(outBytes), "Expected %v but got %v", tt.expected, string(outBytes))
139+
require.Equalf(t, tt.expectedStdout, stdoutString, "Expected %v but got %v", tt.expectedStdout, stdoutString)
140+
})
141+
}
142+
}

0 commit comments

Comments
 (0)