diff --git a/pkg/minikube/shell/shell.go b/pkg/minikube/shell/shell.go index 7835754b290e4a239170f57b6946c590aab9f2e0..4ac414fed7c700319a27ef7e1167115fd97224ed 100644 --- a/pkg/minikube/shell/shell.go +++ b/pkg/minikube/shell/shell.go @@ -48,9 +48,9 @@ var shellConfigMap = map[string]shellData{ UnsetDelimiter: "", usageHint: func(s ...interface{}) string { return fmt.Sprintf(` - # %s - # %s | source - `, s...) +# %s +# %s | source +`, s...) }, }, "powershell": shellData{ @@ -62,8 +62,8 @@ var shellConfigMap = map[string]shellData{ "", func(s ...interface{}) string { return fmt.Sprintf(`# %s - # & %s | Invoke-Expression - `, s...) +# & %s | Invoke-Expression +`, s...) }, }, "cmd": shellData{ @@ -75,8 +75,8 @@ var shellConfigMap = map[string]shellData{ "=", func(s ...interface{}) string { return fmt.Sprintf(`REM %s - REM @FOR /f "tokens=*" %%i IN ('%s') DO @%%i - `, s...) +REM @FOR /f "tokens=*" %%i IN ('%s') DO @%%i +`, s...) }, }, "emacs": shellData{ @@ -88,8 +88,8 @@ var shellConfigMap = map[string]shellData{ "\" nil", func(s ...interface{}) string { return fmt.Sprintf(`;; %s - ;; (with-temp-buffer (shell-command "%s" (current-buffer)) (eval-buffer)) - `, s...) +;; (with-temp-buffer (shell-command "%s" (current-buffer)) (eval-buffer)) +`, s...) }, }, "bash": shellData{ @@ -101,9 +101,9 @@ var shellConfigMap = map[string]shellData{ "", func(s ...interface{}) string { return fmt.Sprintf(` - # %s - # eval $(%s) - `, s...) +# %s +# eval $(%s) +`, s...) }, }, "none": shellData{ @@ -114,7 +114,10 @@ var shellConfigMap = map[string]shellData{ "\n", "=", func(s ...interface{}) string { - return "" + return fmt.Sprintf(` +# %s +# eval $(%s) +`, s...) }, }, } @@ -149,18 +152,14 @@ func generateUsageHint(sh, usgPlz, usgCmd string) string { // CfgSet generates context variables for shell func CfgSet(ec EnvConfig, plz, cmd string) *Config { - - shellKey, s := ec.Shell, &Config{} - - shellCfg, ok := shellConfigMap[shellKey] + shellCfg, ok := shellConfigMap[ec.Shell] if !ok { shellCfg = defaultShell } - s.Suffix, s.Prefix, s.Delimiter = shellCfg.Suffix, shellCfg.Prefix, shellCfg.Delimiter - if shellKey != "none" { - s.UsageHint = generateUsageHint(ec.Shell, plz, cmd) - } + s := &Config{} + s.Suffix, s.Prefix, s.Delimiter = shellCfg.Suffix, shellCfg.Prefix, shellCfg.Delimiter + s.UsageHint = generateUsageHint(ec.Shell, plz, cmd) return s } diff --git a/pkg/minikube/shell/shell_test.go b/pkg/minikube/shell/shell_test.go index dafcbe3bd67f0406e596e5852394134e3fc30be1..00e4e25293f5622cbb6edc2b4f63c34ff0b9e1bc 100644 --- a/pkg/minikube/shell/shell_test.go +++ b/pkg/minikube/shell/shell_test.go @@ -24,18 +24,30 @@ import ( func TestGenerateUsageHint(t *testing.T) { var testCases = []struct { - shellType, hintContains string + shellType, expected string }{ - {"", "eval"}, - {"powershell", "Invoke-Expression"}, - {"bash", "eval"}, + {"", `# foo +# eval $(bar)`}, + {"powershell", `# foo +# & bar | Invoke-Expression`}, + {"bash", `# foo +# eval $(bar)`}, + {"powershell", `# foo +# & bar | Invoke-Expression`}, + {"emacs", `;; foo +;; (with-temp-buffer (shell-command "bar" (current-buffer)) (eval-buffer))`}, + {"fish", `# foo +# bar | source`}, + {"none", `# foo +# eval $(bar)`}, } for _, tc := range testCases { tc := tc t.Run(tc.shellType, func(t *testing.T) { - hint := generateUsageHint(tc.shellType, "foo", "bar") - if !strings.Contains(hint, tc.hintContains) { - t.Errorf("Hint doesn't contain expected string. Expected to find '%v' in '%v'", tc.hintContains, hint) + got := strings.TrimSpace(generateUsageHint(tc.shellType, "foo", "bar")) + expected := strings.TrimSpace(tc.expected) + if got != expected { + t.Errorf("Expected '%v' but got '%v'", expected, got) } }) } @@ -74,13 +86,16 @@ func TestUnsetScript(t *testing.T) { ec EnvConfig expected string }{ - {[]string{"baz"}, EnvConfig{""}, `unset baz`}, - {[]string{"baz"}, EnvConfig{"bash"}, `unset baz`}, - {[]string{"baz"}, EnvConfig{"powershell"}, `Remove-Item Env:\\baz`}, - {[]string{"baz"}, EnvConfig{"cmd"}, `SET baz=`}, - {[]string{"baz"}, EnvConfig{"fish"}, `set -e baz;`}, - {[]string{"baz"}, EnvConfig{"emacs"}, `(setenv "baz" nil)`}, - {[]string{"baz"}, EnvConfig{"none"}, `baz`}, + {[]string{"baz", "bar"}, EnvConfig{""}, `unset baz bar`}, + {[]string{"baz", "bar"}, EnvConfig{"bash"}, `unset baz bar`}, + {[]string{"baz", "bar"}, EnvConfig{"powershell"}, `Remove-Item Env:\\baz Env:\\bar`}, + {[]string{"baz", "bar"}, EnvConfig{"cmd"}, `SET baz= +SET bar=`}, + {[]string{"baz", "bar"}, EnvConfig{"fish"}, `set -e baz; +set -e bar;`}, + {[]string{"baz", "bar"}, EnvConfig{"emacs"}, `(setenv "baz" nil) +(setenv "bar" nil)`}, + {[]string{"baz", "bar"}, EnvConfig{"none"}, `baz bar`}, } for _, tc := range testCases { tc := tc @@ -102,7 +117,7 @@ func TestUnsetScript(t *testing.T) { func TestDetect(t *testing.T) { if s, err := Detect(); err != nil { - t.Fatalf("unexpected error: '%v' during shell detection", err) + t.Fatalf("unexpected error: '%v' during shell detection. Returned shell: %s", err, s) } else if s == "" { t.Fatalf("Detected shell expected to be non empty string") } @@ -117,4 +132,7 @@ func TestSetScript(t *testing.T) { if w.String() != "foo" { t.Fatalf("Expected foo writed by SetScript, but got '%v'", w.String()) } + if ec.Shell == "" { + t.Fatalf("Expected no empty shell") + } }