ensure all shells set/unset are gren with orginal code

pull/8334/head
Marcin Niemira 2020-05-31 13:14:25 +10:00
parent 81073a0561
commit a678b0fb19
No known key found for this signature in database
GPG Key ID: 053E25BDC33ED6A3
1 changed files with 23 additions and 12 deletions

View File

@ -43,20 +43,26 @@ func TestGenerateUsageHint(t *testing.T) {
func TestCfgSet(t *testing.T) { func TestCfgSet(t *testing.T) {
var testCases = []struct { var testCases = []struct {
plz, cmd string plz, cmd string
ec EnvConfig ec EnvConfig
suffixContains string expected string
}{ }{
{"", "eval", EnvConfig{"bash"}, "\n"}, {"", "eval", EnvConfig{""}, `"`},
{"", "eval", EnvConfig{""}, "\n"}, {"", "eval", EnvConfig{"bash"}, `"`},
{"", "eval", EnvConfig{"fish"}, "\n"}, {"", "eval", EnvConfig{"powershell"}, `"`},
{"", "eval", EnvConfig{"cmd"}, ``},
{"", "eval", EnvConfig{"emacs"}, `")`},
{"", "eval", EnvConfig{"none"}, ``},
{"", "eval", EnvConfig{"fish"}, `";`},
} }
for _, tc := range testCases { for _, tc := range testCases {
tc := tc tc := tc
t.Run(tc.ec.Shell, func(t *testing.T) { t.Run(tc.ec.Shell, func(t *testing.T) {
conf := CfgSet(tc.ec, tc.plz, tc.cmd) conf := CfgSet(tc.ec, tc.plz, tc.cmd)
if !strings.Contains(conf.Suffix, tc.suffixContains) { expected := strings.TrimSpace(tc.expected)
t.Errorf("Suffix doesn't contain expected string. Expected to find '%v' in '%v'", tc.suffixContains, conf.Suffix) got := strings.TrimSpace(conf.Suffix)
if expected != got {
t.Errorf("Expected suffix '%v' but got '%v'", expected, got)
} }
}) })
} }
@ -68,9 +74,13 @@ func TestUnsetScript(t *testing.T) {
ec EnvConfig ec EnvConfig
expected string expected string
}{ }{
{[]string{"foo"}, EnvConfig{"bash"}, `unset foo`}, {[]string{"baz"}, EnvConfig{""}, `unset baz`},
{[]string{"bar"}, EnvConfig{"powershell"}, `Remove-Item Env:\\bar`}, {[]string{"baz"}, EnvConfig{"bash"}, `unset baz`},
{[]string{"baz"}, EnvConfig{"powershell"}, `Remove-Item Env:\\baz`},
{[]string{"baz"}, EnvConfig{"cmd"}, `SET 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`},
} }
for _, tc := range testCases { for _, tc := range testCases {
tc := tc tc := tc
@ -80,8 +90,9 @@ func TestUnsetScript(t *testing.T) {
if err := UnsetScript(tc.ec, &b, tc.vars); err != nil { if err := UnsetScript(tc.ec, &b, tc.vars); err != nil {
t.Fatalf("Unexpected error when unseting script happen: %v", err) t.Fatalf("Unexpected error when unseting script happen: %v", err)
} else { } else {
writtenMessage := strings.Trim(b.String(), " \t\n") writtenMessage := strings.TrimSpace(b.String())
if writtenMessage != tc.expected { expected := strings.TrimSpace(tc.expected)
if writtenMessage != expected {
t.Fatalf("Unset script failed. Expected written '%v' but got '%v' ", tc.expected, writtenMessage) t.Fatalf("Unset script failed. Expected written '%v' but got '%v' ", tc.expected, writtenMessage)
} }
} }