-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_test.go
85 lines (77 loc) · 2 KB
/
command_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
78
79
80
81
82
83
84
85
package script
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLocalCommandAdd(t *testing.T) {
c := NewLocalCommand()
assert.Equal(t, "", c.Binary())
c.Add("ssh")
assert.Equal(t, "ssh", c.Binary())
c.Add("myhost")
assert.Equal(t, "ssh", c.Binary())
assert.Equal(t, 1, len(c.Args()))
assert.Equal(t, "myhost", c.Args()[0])
}
func TestLocalCommandAddAll(t *testing.T) {
c := NewLocalCommand()
assert.Equal(t, "", c.Binary())
c.AddAll("ssh", "myhost", "remotecommand")
assert.Equal(t, "ssh", c.Binary())
assert.Equal(t, 2, len(c.Args()))
assert.Equal(t, []string{"myhost", "remotecommand"}, c.Args())
}
func TestLocalCommandString(t *testing.T) {
tests := []struct {
Elements []string
ValidOutput string
}{
{
Elements: []string{"ls", "-al", "file"},
ValidOutput: `ls -al file`,
},
{
Elements: []string{"ls", `my file.txt`},
ValidOutput: `ls "my file.txt"`,
},
{
Elements: []string{"ls", `*.test`},
ValidOutput: `ls *.test`,
},
{
Elements: []string{"ls", `weird".file`},
ValidOutput: `ls weird\".file`,
},
{
Elements: []string{"ls", `'my custom file'`},
ValidOutput: `ls 'my custom file'`,
},
}
for _, test := range tests {
c := NewLocalCommand()
c.AddAll(test.Elements...)
assert.Equal(t, test.ValidOutput, c.String())
}
}
func TestSplitCommand(t *testing.T) {
tests := []struct {
input string
command string
args []string
}{
// simple cases
{"ls -la", "ls", []string{"-la"}},
{"./bin exit-code-error second_ARG", "./bin", []string{"exit-code-error", "second_ARG"}},
// special cases
{"", "", []string{}},
// quoting
{`"quoted bin" "fir st" 'sec ond'`, "quoted bin", []string{"fir st", "sec ond"}},
{`bin -p "fir st" "sec ond"`, "bin", []string{"-p", "fir st", "sec ond"}},
{`"\"bin" 'par am"'`, "\"bin", []string{"par am\""}},
}
for _, test := range tests {
command, args := SplitCommand(test.input)
assert.Equal(t, test.command, command)
assert.Equal(t, test.args, args)
}
}