-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun_test.go
150 lines (145 loc) · 3.33 KB
/
run_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"io/ioutil"
"os"
"reflect"
"runtime"
"sync"
"testing"
)
var runTests = []struct {
args []string
in string
expected string
}{
{
args: []string{"echo", "Hello,", "world!"},
in: ``,
expected: `echo Hello, "world!"`,
},
{
args: []string{"echo", "{{foo}},", "{{bar}}"},
in: `Hello test
world test!
`,
expected: `echo "Hello test," "world test!"`,
},
{
args: []string{"echo", "{{foo-bar_baz}}", "{{FOO-9:BAR_2}}", "{{X}}"},
in: `Foo bar
FOO BAR
X
`,
expected: `echo "Foo bar" "FOO BAR" X`,
},
{
args: []string{"echo", "{{foo}},", "{{bar}},", "{{foo}}-{{bar}}-{{baz}}"},
in: `Hello
wonderful
world!
`,
expected: `echo Hello, wonderful, "Hello-wonderful-world!"`,
},
{
args: []string{"echo", "{{foo:bar}},", "{{foo:bar}},", "{{foo:baz}}"},
in: `Hello
example world!
`,
expected: `echo Hello, Hello, "example world!"`,
},
{
args: []string{"echo", "{{foo:bar}}", "{{foo:baz}}", "{{foo:baz}}"},
in: `Hello, world!
`,
expected: `echo Hello "world!" "world!"`,
},
{
args: []string{"echo", "{{foo:bar}}", "{{foo:bar}}", "{{foo:baz}}"},
in: `Hello,\ world!, test,\ for,\ comma!
`,
expected: `echo "Hello, world!" "Hello, world!" "test, for, comma!"`,
},
{
args: []string{"echo", "[[foo:bar]]", "[[foo:bar]]", "[[foo:baz]]"},
in: `Hello, world, oops!
Hello,
world?
`,
expected: `echo Hello, Hello, "world?"`,
},
{
args: []string{"echo", "{{foo}},", "[[bar]]", "{{baz}}"},
in: `こんにちは
世界
+。:.゚٩(๑>◡<๑)۶:.。+゚
`,
expected: `echo こんにちは, 世界 "+。:.゚٩(๑>◡<๑)۶:.。+゚"`,
},
{
args: []string{"echo", "{{foo}}", "|", "echo", "||", "echo", "&&", "echo", ">", "/dev/null", "</dev/null", "2>&1", "1", ">&2", ">>", "foo", ">>/dev/null"},
in: `Hello world!
`,
expected: `echo "Hello world!" | echo || echo && echo > /dev/null </dev/null 2>&1 1 >&2 >> foo >>/dev/null`,
},
{
args: []string{"echo", "{{foo}}", "{{bar}}"},
in: `\'"${[]}|&;<>()*?!
foo bar baz
`,
expected: `echo "\\'\"${[]}|&;<>()*?!" "\tfoo bar baz"`,
},
{
args: []string{"echo", "{{foo}}"},
in: "\a\b\f'\r\t\v\\\n",
expected: `echo "\a\b\f'\r\t\v\\"`,
},
{
args: []string{"echo $(cat {{foo}} {{bar}})"},
in: `sample1.txt
sample2.txt
`,
expected: `echo $(cat sample1.txt sample2.txt)`,
},
}
func TestRun(t *testing.T) {
dir, err := ioutil.TempDir("", "fillin-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
for _, test := range runTests {
cmd, err := Run(dir, test.args, newTestPrompt(test.in))
if err != nil {
t.Errorf("error occurred unexpectedly: %+v", err)
}
if !reflect.DeepEqual(cmd, test.expected) {
t.Errorf("command not correct (expected: %+v, got: %+v)", test.expected, cmd)
}
}
}
func TestRun_concurrently(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skip test on Windows")
}
dir, err := ioutil.TempDir("", "fillin-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
test := runTests[1]
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
cmd, err := Run(dir, test.args, newTestPrompt(test.in))
if err != nil {
t.Errorf("error occurred unexpectedly: %+v", err)
}
if !reflect.DeepEqual(cmd, test.expected) {
t.Errorf("command not correct (expected: %+v, got: %+v)", test.expected, cmd)
}
}()
}
wg.Wait()
}