Add TestMiniPath test

pull/7294/head
Marcin Niemira 2020-03-25 22:44:40 +11:00
parent 5689d3b8e1
commit f42572f29c
No known key found for this signature in database
GPG Key ID: 053E25BDC33ED6A3
2 changed files with 44 additions and 4 deletions

View File

@ -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.

View File

@ -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)
}
})
}
}