github.com/tonkpils/snag@v1.2.1-0.20160221223445-7f8829737a1d/builder_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestNewBuilder(t *testing.T) { 13 testEnv := "foobar" 14 os.Setenv("TEST_ENV", testEnv) 15 c := config{ 16 Build: []string{"echo Hello World", "echo $$TEST_ENV"}, 17 Run: []string{"echo async here"}, 18 IgnoredItems: []string{"foo", "bar"}, 19 Verbose: true, 20 } 21 b, err := NewBuilder(c) 22 assert.NoError(t, err) 23 assert.NotNil(t, b) 24 25 require.Len(t, b.buildCmds, 2) 26 assert.Equal(t, c.Build[0], strings.Join(b.buildCmds[0], " ")) 27 assert.Equal(t, testEnv, b.buildCmds[1][1]) 28 29 require.Len(t, b.runCmds, 1) 30 assert.Equal(t, c.Run[0], strings.Join(b.runCmds[0], " ")) 31 32 assert.Equal(t, c.Verbose, b.verbose) 33 assert.Equal(t, c.IgnoredItems, b.ignoredItems) 34 } 35 36 func TestNewBuilder_CmdWithQuotes(t *testing.T) { 37 tests := []struct { 38 Command string 39 Chunks []string 40 }{ 41 { // one single quote pair 42 Command: `echo 'hello world' foo`, 43 Chunks: []string{`echo`, `'hello world'`, `foo`}, 44 }, 45 { // one double quote pair 46 Command: `echo "hello world" foo`, 47 Chunks: []string{`echo`, `"hello world"`, `foo`}, 48 }, 49 { // no ending double quote 50 Command: `echo "ga ga oh la la`, 51 Chunks: []string{`echo`, `"ga ga oh la la`}, 52 }, 53 { // no ending single quote 54 Command: `echo 'ga ga oh la la`, 55 Chunks: []string{`echo`, `'ga ga oh la la`}, 56 }, 57 { // multiple double quotes 58 Command: `echo "ga" "foo"`, 59 Chunks: []string{`echo`, `"ga"`, `"foo"`}, 60 }, 61 { // double quotes inside single quotes 62 Command: `echo -c 'foo "bar"'`, 63 Chunks: []string{`echo`, `-c`, `'foo "bar"'`}, 64 }, 65 { // single quotes inside double quotes 66 Command: `echo -c "foo 'bar'"`, 67 Chunks: []string{`echo`, `-c`, `"foo 'bar'"`}, 68 }, 69 } 70 71 for _, test := range tests { 72 c := config{ 73 Build: []string{test.Command}, 74 Run: []string{test.Command}, 75 } 76 77 b, err := NewBuilder(c) 78 require.NoError(t, err) 79 80 assert.Equal(t, test.Chunks, b.buildCmds[0]) 81 assert.Equal(t, test.Chunks, b.runCmds[0]) 82 } 83 } 84 85 func TestClose(t *testing.T) { 86 b, err := NewBuilder(config{}) 87 require.NoError(t, err) 88 89 err = b.Close() 90 assert.NoError(t, err) 91 92 _, ok := <-b.done 93 assert.False(t, ok, "channel 'done' was not closed") 94 }