diff --git a/pkg/minikube/localpath/localpath.go b/pkg/minikube/localpath/localpath.go index 6bc9ef1239..1ac1172b6a 100644 --- a/pkg/minikube/localpath/localpath.go +++ b/pkg/minikube/localpath/localpath.go @@ -38,13 +38,14 @@ func ConfigFile() string { // MiniPath returns the path to the user's minikube dir func MiniPath() string { - if os.Getenv(MinikubeHome) == "" { + minikubeHomeEnv := os.Getenv(MinikubeHome) + if minikubeHomeEnv == "" { return filepath.Join(homedir.HomeDir(), ".minikube") } - if filepath.Base(os.Getenv(MinikubeHome)) == ".minikube" { - return os.Getenv(MinikubeHome) + if filepath.Base(minikubeHomeEnv) == ".minikube" { + return minikubeHomeEnv } - return filepath.Join(os.Getenv(MinikubeHome), ".minikube") + return filepath.Join(minikubeHomeEnv, ".minikube") } // MakeMiniPath is a utility to calculate a relative path to our directory. diff --git a/pkg/minikube/localpath/localpath_test.go b/pkg/minikube/localpath/localpath_test.go index 173ca5df88..81b40722dc 100644 --- a/pkg/minikube/localpath/localpath_test.go +++ b/pkg/minikube/localpath/localpath_test.go @@ -17,10 +17,15 @@ limitations under the License. package localpath import ( + "fmt" "io/ioutil" "os" + "path/filepath" "runtime" + "strings" "testing" + + "k8s.io/client-go/util/homedir" ) func TestReplaceWinDriveLetterToVolumeName(t *testing.T) { @@ -61,3 +66,37 @@ func TestHasWindowsDriveLetter(t *testing.T) { } } } + +func TestConfigFile(t *testing.T) { + configFile := ConfigFile() + if !strings.Contains(configFile, "config.json") { + t.Errorf("ConfigFile returned path without 'config.json': %s", configFile) + } +} + +func TestMiniPath(t *testing.T) { + var testCases = []struct { + env, basePath string + }{ + {"/tmp/.minikube", "/tmp/"}, + {"/tmp/", "/tmp"}, + {"", homedir.HomeDir()}, + } + for _, tc := range testCases { + originalEnv := os.Getenv(MinikubeHome) + defer func() { // revert to pre-test env var + err := os.Setenv(MinikubeHome, originalEnv) + if err != nil { + t.Fatalf("Error reverting env %s to its original value (%s) var after test ", MinikubeHome, originalEnv) + } + }() + t.Run(fmt.Sprintf("%s", tc.env), func(t *testing.T) { + expectedPath := filepath.Join(tc.basePath, ".minikube") + os.Setenv(MinikubeHome, tc.env) + path := MiniPath() + if path != expectedPath { + t.Errorf("MiniPath expected to return '%s', but got '%s'", expectedPath, path) + } + }) + } +}