-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaults_internals_test.go
102 lines (99 loc) · 2.76 KB
/
defaults_internals_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package cmd_toolkit
import (
"fmt"
"github.com/majohn-r/output"
"github.com/spf13/afero"
"path/filepath"
"testing"
)
func Test_reportInvalidConfigurationData(t *testing.T) {
type args struct {
s string
e error
}
tests := map[string]struct {
args
output.WantedRecording
}{
"simple": {
args: args{s: "defaults", e: fmt.Errorf("illegal value")},
WantedRecording: output.WantedRecording{
Error: "" +
"The configuration file \"defaults.yaml\" contains an invalid value for \"defaults\": " +
"'illegal value'.\n",
Log: "" +
"level='error' " +
"error='illegal value' " +
"section='defaults' " +
"msg='invalid content in configuration file'\n",
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
o := output.NewRecorder()
reportInvalidConfigurationData(o, tt.args.s, tt.args.e)
o.Report(t, "reportInvalidConfigurationData()", tt.WantedRecording)
})
}
}
func Test_verifyDefaultConfigFileExists(t *testing.T) {
originalFileSystem := fileSystem
defer func() {
fileSystem = originalFileSystem
}()
fileSystem = afero.NewMemMapFs()
tests := map[string]struct {
preTest func()
path string
wantOk bool
wantErr bool
output.WantedRecording
}{
"path is a directory": {
preTest: func() {
_ = fileSystem.Mkdir("testPath", StdDirPermissions)
},
path: "testPath",
wantErr: true,
WantedRecording: output.WantedRecording{
Error: "" +
"The configuration file \"testPath\" is a directory.\n" +
"What to do:\n" +
"Delete the directory \"testPath\" from \".\" and restart the application.\n",
Log: "level='error' directory='.' fileName='testPath' msg='file is a directory'\n",
},
},
"path does not exist": {
preTest: func() {},
path: filepath.Join(".", "non-existent-file.yaml"),
WantedRecording: output.WantedRecording{
Log: "level='info' directory='.' fileName='non-existent-file.yaml' msg='file does not exist'\n",
},
},
"path is a valid file": {
preTest: func() {
path := "testPath2"
_ = fileSystem.Mkdir(path, StdDirPermissions)
_ = afero.WriteFile(fileSystem, filepath.Join(path, "happy.yaml"), []byte("boo"), StdFilePermissions)
},
path: filepath.Join("testPath2", "happy.yaml"),
wantOk: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
tt.preTest()
o := output.NewRecorder()
gotOk, gotErr := verifyDefaultConfigFileExists(o, tt.path)
if (gotErr != nil) != tt.wantErr {
t.Errorf("verifyDefaultConfigFileExists() error = %v, wantErr %v", gotErr, tt.wantErr)
return
}
if gotOk != tt.wantOk {
t.Errorf("verifyDefaultConfigFileExists() = %v, want %v", gotOk, tt.wantOk)
}
o.Report(t, "verifyDefaultConfigFileExists()", tt.WantedRecording)
})
}
}