Repository: helm/chart-testing
Pull request #291 (2020-10-31 18:38:44)

Introduce environment variable for custom config dir
Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>


torstenwalter: For me this looks good!

Patch:
diff --git a/go.sum b/go.sum
index 64c1511..1edcfcb 100644
--- a/go.sum
+++ b/go.sum
@@ -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=
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 10a6e2c..466534c 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -88,8 +88,12 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*C
 		v.SetConfigFile(cfgFile)
 	} else {
 		v.SetConfigName("ct")
-		for _, searchLocation := range configSearchLocations {
-			v.AddConfigPath(searchLocation)
+		if cfgFile, ok := os.LookupEnv("CT_CONFIG_DIR"); ok {
+			v.AddConfigPath(cfgFile)
+		} else {
+			for _, searchLocation := range configSearchLocations {
+				v.AddConfigPath(searchLocation)
+			}
 		}
 	}
 
@@ -182,11 +186,16 @@ func printCfg(cfg *Configuration) {
 }
 
 func findConfigFile(fileName string) (string, error) {
+	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 filePath, nil
 		}
 	}
+
 	return "", errors.New(fmt.Sprintf("Config file not found: %s", fileName))
 }


Test patch:
diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go
index b5c47bb..b9b74de 100644
--- a/pkg/config/config_test.go
+++ b/pkg/config/config_test.go
@@ -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"
 )
 
@@ -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)
+			}
+		})
+	}
+}