minor cleanup

上级 168f67bf
...@@ -48,9 +48,9 @@ var shellConfigMap = map[string]shellData{ ...@@ -48,9 +48,9 @@ var shellConfigMap = map[string]shellData{
UnsetDelimiter: "", UnsetDelimiter: "",
usageHint: func(s ...interface{}) string { usageHint: func(s ...interface{}) string {
return fmt.Sprintf(` return fmt.Sprintf(`
# %s # %s
# %s | source # %s | source
`, s...) `, s...)
}, },
}, },
"powershell": shellData{ "powershell": shellData{
...@@ -62,8 +62,8 @@ var shellConfigMap = map[string]shellData{ ...@@ -62,8 +62,8 @@ var shellConfigMap = map[string]shellData{
"", "",
func(s ...interface{}) string { func(s ...interface{}) string {
return fmt.Sprintf(`# %s return fmt.Sprintf(`# %s
# & %s | Invoke-Expression # & %s | Invoke-Expression
`, s...) `, s...)
}, },
}, },
"cmd": shellData{ "cmd": shellData{
...@@ -75,8 +75,8 @@ var shellConfigMap = map[string]shellData{ ...@@ -75,8 +75,8 @@ var shellConfigMap = map[string]shellData{
"=", "=",
func(s ...interface{}) string { func(s ...interface{}) string {
return fmt.Sprintf(`REM %s return fmt.Sprintf(`REM %s
REM @FOR /f "tokens=*" %%i IN ('%s') DO @%%i REM @FOR /f "tokens=*" %%i IN ('%s') DO @%%i
`, s...) `, s...)
}, },
}, },
"emacs": shellData{ "emacs": shellData{
...@@ -88,8 +88,8 @@ var shellConfigMap = map[string]shellData{ ...@@ -88,8 +88,8 @@ var shellConfigMap = map[string]shellData{
"\" nil", "\" nil",
func(s ...interface{}) string { func(s ...interface{}) string {
return fmt.Sprintf(`;; %s return fmt.Sprintf(`;; %s
;; (with-temp-buffer (shell-command "%s" (current-buffer)) (eval-buffer)) ;; (with-temp-buffer (shell-command "%s" (current-buffer)) (eval-buffer))
`, s...) `, s...)
}, },
}, },
"bash": shellData{ "bash": shellData{
...@@ -101,9 +101,9 @@ var shellConfigMap = map[string]shellData{ ...@@ -101,9 +101,9 @@ var shellConfigMap = map[string]shellData{
"", "",
func(s ...interface{}) string { func(s ...interface{}) string {
return fmt.Sprintf(` return fmt.Sprintf(`
# %s # %s
# eval $(%s) # eval $(%s)
`, s...) `, s...)
}, },
}, },
"none": shellData{ "none": shellData{
...@@ -114,7 +114,10 @@ var shellConfigMap = map[string]shellData{ ...@@ -114,7 +114,10 @@ var shellConfigMap = map[string]shellData{
"\n", "\n",
"=", "=",
func(s ...interface{}) string { func(s ...interface{}) string {
return "" return fmt.Sprintf(`
# %s
# eval $(%s)
`, s...)
}, },
}, },
} }
...@@ -149,18 +152,14 @@ func generateUsageHint(sh, usgPlz, usgCmd string) string { ...@@ -149,18 +152,14 @@ func generateUsageHint(sh, usgPlz, usgCmd string) string {
// CfgSet generates context variables for shell // CfgSet generates context variables for shell
func CfgSet(ec EnvConfig, plz, cmd string) *Config { func CfgSet(ec EnvConfig, plz, cmd string) *Config {
shellCfg, ok := shellConfigMap[ec.Shell]
shellKey, s := ec.Shell, &Config{}
shellCfg, ok := shellConfigMap[shellKey]
if !ok { if !ok {
shellCfg = defaultShell shellCfg = defaultShell
} }
s.Suffix, s.Prefix, s.Delimiter = shellCfg.Suffix, shellCfg.Prefix, shellCfg.Delimiter
if shellKey != "none" { s := &Config{}
s.UsageHint = generateUsageHint(ec.Shell, plz, cmd) s.Suffix, s.Prefix, s.Delimiter = shellCfg.Suffix, shellCfg.Prefix, shellCfg.Delimiter
} s.UsageHint = generateUsageHint(ec.Shell, plz, cmd)
return s return s
} }
......
...@@ -24,18 +24,30 @@ import ( ...@@ -24,18 +24,30 @@ import (
func TestGenerateUsageHint(t *testing.T) { func TestGenerateUsageHint(t *testing.T) {
var testCases = []struct { var testCases = []struct {
shellType, hintContains string shellType, expected string
}{ }{
{"", "eval"}, {"", `# foo
{"powershell", "Invoke-Expression"}, # eval $(bar)`},
{"bash", "eval"}, {"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 { for _, tc := range testCases {
tc := tc tc := tc
t.Run(tc.shellType, func(t *testing.T) { t.Run(tc.shellType, func(t *testing.T) {
hint := generateUsageHint(tc.shellType, "foo", "bar") got := strings.TrimSpace(generateUsageHint(tc.shellType, "foo", "bar"))
if !strings.Contains(hint, tc.hintContains) { expected := strings.TrimSpace(tc.expected)
t.Errorf("Hint doesn't contain expected string. Expected to find '%v' in '%v'", tc.hintContains, hint) if got != expected {
t.Errorf("Expected '%v' but got '%v'", expected, got)
} }
}) })
} }
...@@ -74,13 +86,16 @@ func TestUnsetScript(t *testing.T) { ...@@ -74,13 +86,16 @@ func TestUnsetScript(t *testing.T) {
ec EnvConfig ec EnvConfig
expected string expected string
}{ }{
{[]string{"baz"}, EnvConfig{""}, `unset baz`}, {[]string{"baz", "bar"}, EnvConfig{""}, `unset baz bar`},
{[]string{"baz"}, EnvConfig{"bash"}, `unset baz`}, {[]string{"baz", "bar"}, EnvConfig{"bash"}, `unset baz bar`},
{[]string{"baz"}, EnvConfig{"powershell"}, `Remove-Item Env:\\baz`}, {[]string{"baz", "bar"}, EnvConfig{"powershell"}, `Remove-Item Env:\\baz Env:\\bar`},
{[]string{"baz"}, EnvConfig{"cmd"}, `SET baz=`}, {[]string{"baz", "bar"}, EnvConfig{"cmd"}, `SET baz=
{[]string{"baz"}, EnvConfig{"fish"}, `set -e baz;`}, SET bar=`},
{[]string{"baz"}, EnvConfig{"emacs"}, `(setenv "baz" nil)`}, {[]string{"baz", "bar"}, EnvConfig{"fish"}, `set -e baz;
{[]string{"baz"}, EnvConfig{"none"}, `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 { for _, tc := range testCases {
tc := tc tc := tc
...@@ -102,7 +117,7 @@ func TestUnsetScript(t *testing.T) { ...@@ -102,7 +117,7 @@ func TestUnsetScript(t *testing.T) {
func TestDetect(t *testing.T) { func TestDetect(t *testing.T) {
if s, err := Detect(); err != nil { 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 == "" { } else if s == "" {
t.Fatalf("Detected shell expected to be non empty string") t.Fatalf("Detected shell expected to be non empty string")
} }
...@@ -117,4 +132,7 @@ func TestSetScript(t *testing.T) { ...@@ -117,4 +132,7 @@ func TestSetScript(t *testing.T) {
if w.String() != "foo" { if w.String() != "foo" {
t.Fatalf("Expected foo writed by SetScript, but got '%v'", w.String()) t.Fatalf("Expected foo writed by SetScript, but got '%v'", w.String())
} }
if ec.Shell == "" {
t.Fatalf("Expected no empty shell")
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册