-
Notifications
You must be signed in to change notification settings - Fork 4
/
builder_test.go
94 lines (81 loc) · 2.21 KB
/
builder_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
package main
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewBuilder(t *testing.T) {
testEnv := "foobar"
os.Setenv("TEST_ENV", testEnv)
c := config{
Build: []string{"echo Hello World", "echo $$TEST_ENV"},
Run: []string{"echo async here"},
IgnoredItems: []string{"foo", "bar"},
Verbose: true,
}
b, err := NewBuilder(c)
assert.NoError(t, err)
assert.NotNil(t, b)
require.Len(t, b.buildCmds, 2)
assert.Equal(t, c.Build[0], strings.Join(b.buildCmds[0], " "))
assert.Equal(t, testEnv, b.buildCmds[1][1])
require.Len(t, b.runCmds, 1)
assert.Equal(t, c.Run[0], strings.Join(b.runCmds[0], " "))
assert.Equal(t, c.Verbose, b.verbose)
assert.Equal(t, c.IgnoredItems, b.ignoredItems)
}
func TestNewBuilder_CmdWithQuotes(t *testing.T) {
tests := []struct {
Command string
Chunks []string
}{
{ // one single quote pair
Command: `echo 'hello world' foo`,
Chunks: []string{`echo`, `'hello world'`, `foo`},
},
{ // one double quote pair
Command: `echo "hello world" foo`,
Chunks: []string{`echo`, `"hello world"`, `foo`},
},
{ // no ending double quote
Command: `echo "ga ga oh la la`,
Chunks: []string{`echo`, `"ga ga oh la la`},
},
{ // no ending single quote
Command: `echo 'ga ga oh la la`,
Chunks: []string{`echo`, `'ga ga oh la la`},
},
{ // multiple double quotes
Command: `echo "ga" "foo"`,
Chunks: []string{`echo`, `"ga"`, `"foo"`},
},
{ // double quotes inside single quotes
Command: `echo -c 'foo "bar"'`,
Chunks: []string{`echo`, `-c`, `'foo "bar"'`},
},
{ // single quotes inside double quotes
Command: `echo -c "foo 'bar'"`,
Chunks: []string{`echo`, `-c`, `"foo 'bar'"`},
},
}
for _, test := range tests {
c := config{
Build: []string{test.Command},
Run: []string{test.Command},
}
b, err := NewBuilder(c)
require.NoError(t, err)
assert.Equal(t, test.Chunks, b.buildCmds[0])
assert.Equal(t, test.Chunks, b.runCmds[0])
}
}
func TestClose(t *testing.T) {
b, err := NewBuilder(config{})
require.NoError(t, err)
err = b.Close()
assert.NoError(t, err)
_, ok := <-b.done
assert.False(t, ok, "channel 'done' was not closed")
}