Skip to content

Commit cf21518

Browse files
committed
Add unit test
Signed-off-by: Reinhard Nägele <[email protected]>
1 parent af3597e commit cf21518

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
310310
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
311311
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
312312
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
313+
github.com/tj/assert v0.0.0-20171129193455-018094318fb0 h1:Rw8kxzWo1mr6FSaYXjQELRe88y2KdfynXdnK72rdjtA=
313314
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
314315
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
315316
github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao=

pkg/config/config.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,14 @@ func printCfg(cfg *Configuration) {
186186
}
187187

188188
func findConfigFile(fileName string) (string, error) {
189-
var cfgFile string
190189
if dir, ok := os.LookupEnv("CT_CONFIG_DIR"); ok {
191190
return filepath.Join(dir, fileName), nil
192191
}
193192

194193
for _, location := range configSearchLocations {
195194
filePath := filepath.Join(location, fileName)
196195
if util.FileExists(filePath) {
197-
return cfgFile, nil
196+
return filePath, nil
198197
}
199198
}
200199

pkg/config/config_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
package config
1616

1717
import (
18+
"os"
19+
"path/filepath"
1820
"testing"
1921

2022
"github.com/spf13/cobra"
23+
"github.com/stretchr/testify/assert"
2124
"github.com/stretchr/testify/require"
2225
)
2326

@@ -55,3 +58,56 @@ func loadAndAssertConfigFromFile(t *testing.T, configFile string) {
5558
require.Equal(t, "release", cfg.ReleaseLabel)
5659
require.Equal(t, true, cfg.ExcludeDeprecated)
5760
}
61+
62+
func Test_findConfigFile(t *testing.T) {
63+
tests := []struct {
64+
name string
65+
envVar string
66+
defaultDir string
67+
want string
68+
wantErr bool
69+
}{
70+
{
71+
name: "without env var",
72+
defaultDir: filepath.Join("testdata", "default"),
73+
want: filepath.Join("testdata", "default", "test.yaml"),
74+
},
75+
{
76+
name: "with env var",
77+
envVar: filepath.Join("testdata", "env"),
78+
want: filepath.Join("testdata", "env", "test.yaml"),
79+
},
80+
{
81+
name: "with env var and default location",
82+
envVar: filepath.Join("testdata", "env"),
83+
defaultDir: filepath.Join("testdata", "default"),
84+
want: filepath.Join("testdata", "env", "test.yaml"),
85+
},
86+
{
87+
name: "not found",
88+
wantErr: true,
89+
},
90+
}
91+
for _, tt := range tests {
92+
t.Run(tt.name, func(t *testing.T) {
93+
if tt.envVar != "" {
94+
err := os.Setenv("CT_CONFIG_DIR", tt.envVar)
95+
require.NoError(t, err)
96+
97+
t.Cleanup(func() {
98+
err := os.Unsetenv("CT_CONFIG_DIR")
99+
require.NoError(t, err)
100+
})
101+
}
102+
configSearchLocations = []string{tt.defaultDir}
103+
104+
got, err := findConfigFile("test.yaml")
105+
if tt.wantErr {
106+
assert.Error(t, err)
107+
} else {
108+
assert.NoError(t, err)
109+
assert.Equal(t, tt.want, got)
110+
}
111+
})
112+
}
113+
}

pkg/config/testdata/default/test.yaml

Whitespace-only changes.

pkg/config/testdata/env/test.yaml

Whitespace-only changes.

0 commit comments

Comments
 (0)