forked from carolynvs/stingoftheviper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
93 lines (81 loc) · 2.92 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPrecedence(t *testing.T) {
// Run the tests in a temporary directory
tmpDir, err := os.MkdirTemp("", "stingoftheviper")
require.NoError(t, err, "error creating a temporary test directory")
testDir, err := os.Getwd()
require.NoError(t, err, "error getting the current working directory")
defer os.Chdir(testDir)
err = os.Chdir(tmpDir)
require.NoError(t, err, "error changing to the temporary test directory")
// Set favorite-color with the config file
t.Run("config file", func(t *testing.T) {
testcases := []struct {
name string
configFile string
replaceHyphenWithCamelCase bool
}{
{name: "hyphen", configFile: "testdata/config-hyphen.toml"},
{name: "camelCase", configFile: "testdata/config-camel.toml", replaceHyphenWithCamelCase: true},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
replaceHyphenWithCamelCase = tc.replaceHyphenWithCamelCase
defer func() { replaceHyphenWithCamelCase = false }()
// Copy the config file into our temporary test directory
configB, err := os.ReadFile(filepath.Join(testDir, tc.configFile))
require.NoError(t, err, "error reading test config file")
err = os.WriteFile(filepath.Join(tmpDir, "stingoftheviper.toml"), configB, 0644)
require.NoError(t, err, "error writing test config file")
defer os.Remove(filepath.Join(tmpDir, "stingoftheviper.toml"))
// Run ./stingoftheviper
cmd := NewRootCommand()
output := &bytes.Buffer{}
cmd.SetOut(output)
cmd.Execute()
gotOutput := output.String()
wantOutput := `Your favorite color is: blue
The magic number is: 7
`
assert.Equal(t, wantOutput, gotOutput, "expected the color from the config file and the number from the flag default")
})
}
})
// Set favorite-color with an environment variable
t.Run("env var", func(t *testing.T) {
// Run STING_FAVORITE_COLOR=purple ./stingoftheviper
os.Setenv("STING_FAVORITE_COLOR", "purple")
defer os.Unsetenv("STING_FAVORITE_COLOR")
cmd := NewRootCommand()
output := &bytes.Buffer{}
cmd.SetOut(output)
cmd.Execute()
gotOutput := output.String()
wantOutput := `Your favorite color is: purple
The magic number is: 7
`
assert.Equal(t, wantOutput, gotOutput, "expected the color to use the environment variable value and the number to use the flag default")
})
// Set number with a flag
t.Run("flag", func(t *testing.T) {
// Run ./stingoftheviper --number 2
cmd := NewRootCommand()
output := &bytes.Buffer{}
cmd.SetOut(output)
cmd.SetArgs([]string{"--number", "2"})
cmd.Execute()
gotOutput := output.String()
wantOutput := `Your favorite color is: red
The magic number is: 2
`
assert.Equal(t, wantOutput, gotOutput, "expected the number to use the flag value and the color to use the flag default")
})
}