Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Reinhard Nägele <[email protected]>
  • Loading branch information
unguiculus committed Nov 2, 2020
1 parent af3597e commit cf21518
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tj/assert v0.0.0-20171129193455-018094318fb0 h1:Rw8kxzWo1mr6FSaYXjQELRe88y2KdfynXdnK72rdjtA=
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao=
Expand Down
3 changes: 1 addition & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,14 @@ func printCfg(cfg *Configuration) {
}

func findConfigFile(fileName string) (string, error) {
var cfgFile string
if dir, ok := os.LookupEnv("CT_CONFIG_DIR"); ok {
return filepath.Join(dir, fileName), nil
}

for _, location := range configSearchLocations {
filePath := filepath.Join(location, fileName)
if util.FileExists(filePath) {
return cfgFile, nil
return filePath, nil
}
}

Expand Down
56 changes: 56 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -55,3 +58,56 @@ func loadAndAssertConfigFromFile(t *testing.T, configFile string) {
require.Equal(t, "release", cfg.ReleaseLabel)
require.Equal(t, true, cfg.ExcludeDeprecated)
}

func Test_findConfigFile(t *testing.T) {
tests := []struct {
name string
envVar string
defaultDir string
want string
wantErr bool
}{
{
name: "without env var",
defaultDir: filepath.Join("testdata", "default"),
want: filepath.Join("testdata", "default", "test.yaml"),
},
{
name: "with env var",
envVar: filepath.Join("testdata", "env"),
want: filepath.Join("testdata", "env", "test.yaml"),
},
{
name: "with env var and default location",
envVar: filepath.Join("testdata", "env"),
defaultDir: filepath.Join("testdata", "default"),
want: filepath.Join("testdata", "env", "test.yaml"),
},
{
name: "not found",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envVar != "" {
err := os.Setenv("CT_CONFIG_DIR", tt.envVar)
require.NoError(t, err)

t.Cleanup(func() {
err := os.Unsetenv("CT_CONFIG_DIR")
require.NoError(t, err)
})
}
configSearchLocations = []string{tt.defaultDir}

got, err := findConfigFile("test.yaml")
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}
Empty file.
Empty file.

0 comments on commit cf21518

Please sign in to comment.