-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmain_test.go
76 lines (70 loc) · 1.37 KB
/
main_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
package main
import (
"bytes"
"io"
"os"
"testing"
)
func Test_initConfig(t *testing.T) {
t.Parallel()
tests := []struct {
name string
xdg string
cfgFile string
wantErr bool
}{
{
name: "test-ov.yaml",
xdg: "",
cfgFile: "ov.yaml",
wantErr: false,
},
{
name: "test-ov-less.yaml",
xdg: "",
cfgFile: "ov-less.yaml",
wantErr: false,
},
{
name: "no-file.yaml",
xdg: "",
cfgFile: "no-file.yaml",
wantErr: true,
},
{
name: "not found",
xdg: "dummy",
cfgFile: "",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.xdg != "" {
os.Setenv("XDG_CONFIG_HOME", tt.xdg)
}
cfgFile = tt.cfgFile
// Backup original stderr
origStderr := os.Stderr
// Create a buffer to capture stderr output
r, w, _ := os.Pipe()
os.Stderr = w
initConfig()
w.Close()
// Restore original stderr
os.Stderr = origStderr
// Read captured stderr output
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
t.Fatal(err)
}
capturedStderr := buf.String()
// Now you can assert capturedStderr
// For example, check if it contains a specific error message
got := len(capturedStderr) > 0
if got != tt.wantErr {
t.Errorf("initConfig() error = %v, wantErr %v", capturedStderr, tt.wantErr)
}
})
}
}