Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
n0npax committed May 31, 2020
1 parent 168f67b commit ae2ceee
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
41 changes: 20 additions & 21 deletions pkg/minikube/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -114,7 +114,10 @@ var shellConfigMap = map[string]shellData{
"\n",
"=",
func(s ...interface{}) string {
return ""
return fmt.Sprintf(`
# %s
# eval $(%s)
`, s...)
},
},
}
Expand Down Expand Up @@ -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
}
Expand Down
48 changes: 33 additions & 15 deletions pkg/minikube/shell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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
Expand All @@ -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")
}
Expand All @@ -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")
}
}

0 comments on commit ae2ceee

Please sign in to comment.