-
Notifications
You must be signed in to change notification settings - Fork 68
/
shell_parser_test.go
77 lines (74 loc) · 2.44 KB
/
shell_parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package imagebuilder
import (
"os/exec"
"testing"
"github.com/stretchr/testify/require"
)
func TestProcessWord(t *testing.T) {
envs := []string{
"EDITOR=vim",
"FOUREYES=iiii",
"XMODIFIERS=@im=ibus",
}
testCases := []struct {
pattern, expected string
expectError bool
}{
{"A", "A", false},
{"${EDITOR}", "vim", false},
{"${EDITOR:+emacs}", "emacs", false},
{"${EDITOR:-emacs}", "vim", false},
{"${EDITOR:-${FOUREYES}}", "vim", false},
{"${EDITOR:+${FOUREYES}}", "iiii", false},
{"${XMODIFIERS#*i}", "m=ibus", false},
{"${XMODIFIERS##*i}", "bus", false},
{"${XMODIFIERS#i*}", "@im=ibus", false},
{"${XMODIFIERS##i*}", "@im=ibus", false},
{"${XMODIFIERS%i*}", "@im=", false},
{"${XMODIFIERS%%i*}", "@", false},
{"${XMODIFIERS%*i}", "@im=ibus", false},
{"${XMODIFIERS%%*i}", "@im=ibus", false},
{"${XMODIFIERS/i/I}", "@Im=ibus", false},
{"${XMODIFIERS//i/I}", "@Im=Ibus", false},
{"${XMODIFIERS/#i/I}", "@im=ibus", false},
{"${XMODIFIERS/%i/I}", "@im=ibus", false},
{"${XMODIFIERS/#@/AT}", "ATim=ibus", false},
{"${XMODIFIERS/#@}", "im=ibus", false},
{"${XMODIFIERS/%s/S}", "@im=ibuS", false},
{"${XMODIFIERS/%s}", "@im=ibu", false},
{"${XMODIFIERS//i/aye}", "@ayem=ayebus", false},
{"${XMODIFIERS//b/BEE}", "@im=iBEEus", false},
{"${EDITOR/${EDITOR}/}", "", false},
{"${EDITOR/${EDITOR}}", "", false},
{"${EDITOR//${EDITOR}/}", "", false},
{"${EDITOR//${EDITOR}}", "", false},
{"${FOUREYES/ii/${EDITOR}}", "vimii", false},
{"${FOUREYES//i/${EDITOR}}", "vimvimvimvim", false},
{"${FOUREYES//ii/${EDITOR}}", "vimvim", false},
{"${FOUREYES//iii/${EDITOR}}", "vimi", false},
{"${FOUREYES//iii/${EDITOR}", "vimi", true},
}
for _, testCase := range testCases {
t.Run(testCase.pattern, func(t *testing.T) {
actual, err := ProcessWord(testCase.pattern, envs)
if testCase.expectError {
require.Error(t, err)
t.Logf("got expected error %v", err)
} else {
require.NoError(t, err)
require.Equal(t, testCase.expected, actual)
}
// We're probably not as flexible as the shell, but at least don't be incompatible with it
cmd := exec.Command("bash", "-c", "echo -n "+testCase.pattern)
cmd.Env = append(cmd.Env, envs...)
output, err := cmd.CombinedOutput()
if testCase.expectError {
require.Error(t, err)
t.Logf("got expected error %v (%s)", err, string(output))
} else {
require.NoError(t, err)
require.Equal(t, testCase.expected, string(output))
}
})
}
}