-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckenv_test.go
86 lines (76 loc) · 1.65 KB
/
checkenv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"os"
"testing"
)
func TestCheckEnv(t *testing.T) {
// Define test cases
testCases := []struct {
name string
directories []string
extensions []string
ignoreDirs []string
expectedOutput string
setup func()
teardown func()
}{
{
name: "missing environment variable",
directories: []string{"testdata"},
extensions: []string{".js"},
ignoreDirs: []string{},
expectedOutput: "Missing variable: TEST_VAR\n",
},
{
name: "environment variable set",
directories: []string{"testdata"},
extensions: []string{".js"},
ignoreDirs: []string{},
expectedOutput: "",
setup: func() {
os.Setenv("TEST_VAR", "test")
},
teardown: func() {
os.Unsetenv("TEST_VAR")
},
},
}
// Run test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Set up test case
if tc.setup != nil {
tc.setup()
}
defer func() {
if tc.teardown != nil {
tc.teardown()
}
}()
// Run function and capture output
output := captureOutput(func() {
checkEnv(tc.directories, tc.extensions, tc.ignoreDirs, scanFiles)
})
// Check output
if output != tc.expectedOutput {
t.Errorf("Expected output: %s, but got: %s", tc.expectedOutput, output)
}
})
}
}
// captureOutput captures the output of a function
func captureOutput(f func()) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
os.Stdout = old
output := make(chan string)
go func() {
buf := make([]byte, 1024)
n, _ := r.Read(buf)
output <- string(buf[:n])
}()
return <-output
}