-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_test.go
164 lines (152 loc) · 3.31 KB
/
run_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"io/ioutil"
"os"
"testing"
"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"
)
func TestGetConfigOrPanic(t *testing.T) {
exampleConf := Config{}
bytes, err := ioutil.ReadFile("./example_conf.toml")
assert.Nil(t, err)
exampleToml := string(bytes)
_, err = toml.Decode(exampleToml, &exampleConf)
assert.Nil(t, err)
tcs := []struct {
desc string
configFilePath string
configEnvValue string
expectedPanic bool
expectedConfig Config
}{
{
desc: "no config file exists and not env var set => panic",
configFilePath: "./nonExistingConfigFile.toml",
configEnvValue: "",
expectedPanic: true,
},
{
desc: "takes config from file if set",
configFilePath: "./example_conf.toml",
configEnvValue: "",
expectedConfig: exampleConf,
},
{
desc: "takes config from env if set",
configFilePath: "./nonExistingConfigFile.toml",
configEnvValue: exampleToml,
expectedConfig: exampleConf,
},
{
desc: "config file toml broken => panic",
configFilePath: "",
configEnvValue: "",
expectedPanic: true,
},
{
desc: "env toml broken => panic",
configFilePath: "./broken_conf.toml",
configEnvValue: "invalid toml",
expectedPanic: true,
},
{
desc: "env variable overwrites config file",
configFilePath: "./example_conf.toml",
configEnvValue: `prog = "overwritten_env_config"`,
expectedConfig: Config{
Prog: "overwritten_env_config",
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
os.Setenv(CONFIG_ENV_VAR, tc.configEnvValue)
if tc.expectedPanic {
assert.Panics(t, func() { _ = getConfigOrPanic(tc.configFilePath) }, "expected panic")
} else {
config := getConfigOrPanic(tc.configFilePath)
assert.Equal(t, tc.expectedConfig, config)
}
})
}
}
func TestGetLevelOrPanic(t *testing.T) {
tcs := []struct {
desc string
config Config
expectedPanic bool
expectedLevel Level
}{
{
desc: "default level",
config: Config{
Level: nil,
},
expectedLevel: Always,
},
{
desc: "explicit always",
config: Config{
Level: ptrToStr("Always"),
},
expectedLevel: Always,
},
{
desc: "error_or_output",
config: Config{
Level: ptrToStr("Error_Or_Output"),
},
expectedLevel: Error_Or_Output,
},
{
desc: "error",
config: Config{
Level: ptrToStr("Error"),
},
expectedLevel: Error,
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
if tc.expectedPanic {
assert.Panics(t, func() { _ = getLevelOrPanic(tc.config) }, "expected panic")
} else {
lvl := getLevelOrPanic(tc.config)
assert.Equal(t, tc.expectedLevel, lvl)
}
})
}
}
func TestGetProgName(t *testing.T) {
tcs := []struct {
desc string
conf *Config
expectedName string
}{
{
desc: "no name in config",
conf: &Config{
Prog: "ls",
},
expectedName: "`ls`",
},
{
desc: "name in config",
conf: &Config{
Name: ptrToStr("list files"),
Prog: "ls",
},
expectedName: "list files",
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
result := getProgName(tc.conf)
assert.Equal(t, tc.expectedName, result)
})
}
}
func ptrToStr(str string) *string {
return &str
}