Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 package exec | |
| 2 | |
| 3 import ( | |
| 4 "bytes" | |
| 5 "fmt" | |
| 6 "io" | |
| 7 "io/ioutil" | |
| 8 "os" | |
| 9 "path/filepath" | |
| 10 "strings" | |
| 11 "testing" | |
| 12 "time" | |
| 13 | |
| 14 expect "github.com/stretchr/testify/assert" | |
| 15 assert "github.com/stretchr/testify/require" | |
| 16 ) | |
| 17 | |
| 18 func TestParseCommand(t *testing.T) { | |
| 19 test := func(input string, expected Command) { | |
| 20 expect.Equal(t, expected, ParseCommand(input)) | |
| 21 } | |
| 22 test("", Command{Name: "", Args: []string{}}) | |
| 23 test("foo", Command{Name: "foo", Args: []string{}}) | |
| 24 test("foo bar", Command{Name: "foo", Args: []string{"bar"}}) | |
| 25 test("foo_bar baz", Command{Name: "foo_bar", Args: []string{"baz"}}) | |
| 26 test("foo-bar baz", Command{Name: "foo-bar", Args: []string{"baz"}}) | |
| 27 test("foo --bar --baz", Command{Name: "foo", Args: []string{"--bar", "-- baz"}}) | |
| 28 // Doesn't work. | |
| 29 //test("foo 'bar baz'", Command{Name: "foo", Args: []string{"bar baz"}}) | |
| 30 } | |
| 31 | |
| 32 func TestSquashWriters(t *testing.T) { | |
| 33 test := func(input ...*bytes.Buffer) { | |
| 34 writers := make([]io.Writer, len(input)) | |
| 35 for i, buffer := range input { | |
| 36 if buffer != nil { | |
| 37 writers[i] = buffer | |
| 38 } | |
| 39 } | |
| 40 squashed := squashWriters(writers...) | |
| 41 assert.NotNil(t, squashed) | |
| 42 testString1, testString2 := "foobar", "baz" | |
| 43 n, err := squashed.Write([]byte(testString1)) | |
| 44 expect.Equal(t, len(testString1), n) | |
| 45 expect.NoError(t, err) | |
| 46 n, err = squashed.Write([]byte(testString2)) | |
| 47 expect.Equal(t, len(testString2), n) | |
| 48 expect.NoError(t, err) | |
| 49 for _, buffer := range input { | |
| 50 if buffer != nil { | |
| 51 expect.Equal(t, testString1+testString2, string( buffer.Bytes())) | |
| 52 } | |
| 53 } | |
| 54 } | |
| 55 expect.Equal(t, nil, squashWriters()) | |
| 56 expect.Equal(t, nil, squashWriters(nil)) | |
| 57 expect.Equal(t, nil, squashWriters(nil, nil)) | |
| 58 test(&bytes.Buffer{}) | |
| 59 test(&bytes.Buffer{}, &bytes.Buffer{}) | |
| 60 test(&bytes.Buffer{}, nil) | |
| 61 test(nil, &bytes.Buffer{}) | |
| 62 test(&bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}) | |
| 63 test(&bytes.Buffer{}, nil, nil) | |
| 64 test(nil, &bytes.Buffer{}, nil) | |
| 65 test(nil, nil, &bytes.Buffer{}) | |
| 66 test(&bytes.Buffer{}, nil, &bytes.Buffer{}) | |
| 67 } | |
| 68 | |
| 69 func TestBasic(t *testing.T) { | |
| 70 dir, err := ioutil.TempDir("", "exec_test") | |
|
jcgregorio
2015/08/14 13:41:30
Clean up tempdirs after you are done with them. He
dogben
2015/08/14 14:03:29
Done. Sorry, I'm used to TempPath in C++ tests.
| |
| 71 assert.NoError(t, err) | |
| 72 file := filepath.Join(dir, "ran") | |
| 73 assert.NoError(t, Run(&Command{ | |
| 74 Name: "touch", | |
| 75 Args: []string{file}, | |
| 76 })) | |
| 77 _, err = os.Stat(file) | |
| 78 expect.NoError(t, err) | |
| 79 } | |
| 80 | |
| 81 func WriteScript(path, script string) error { | |
| 82 return ioutil.WriteFile(path, []byte(script), 0777) | |
| 83 } | |
| 84 | |
| 85 const SimpleScript = `#!/bin/bash | |
| 86 touch "${EXEC_TEST_FILE}" | |
| 87 ` | |
| 88 | |
| 89 func TestEnv(t *testing.T) { | |
| 90 dir, err := ioutil.TempDir("", "exec_test") | |
| 91 assert.NoError(t, err) | |
| 92 script := filepath.Join(dir, "simple_script.sh") | |
| 93 assert.NoError(t, WriteScript(script, SimpleScript)) | |
| 94 file := filepath.Join(dir, "ran") | |
| 95 assert.NoError(t, Run(&Command{ | |
| 96 Name: script, | |
| 97 Env: []string{fmt.Sprintf("EXEC_TEST_FILE=%s", file)}, | |
| 98 })) | |
| 99 _, err = os.Stat(file) | |
| 100 expect.NoError(t, err) | |
| 101 } | |
| 102 | |
| 103 const PathScript = `#!/bin/bash | |
| 104 echo "${PATH}" > "${EXEC_TEST_FILE}" | |
| 105 ` | |
| 106 | |
| 107 func TestInheritPath(t *testing.T) { | |
| 108 dir, err := ioutil.TempDir("", "exec_test") | |
| 109 assert.NoError(t, err) | |
| 110 script := filepath.Join(dir, "path_script.sh") | |
| 111 assert.NoError(t, WriteScript(script, PathScript)) | |
| 112 file := filepath.Join(dir, "ran") | |
| 113 assert.NoError(t, Run(&Command{ | |
| 114 Name: script, | |
| 115 Env: []string{fmt.Sprintf("EXEC_TEST_FILE=%s", file)}, | |
| 116 InheritPath: true, | |
| 117 })) | |
| 118 contents, err := ioutil.ReadFile(file) | |
| 119 assert.NoError(t, err) | |
| 120 expect.Equal(t, os.Getenv("PATH"), strings.TrimSpace(string(contents))) | |
| 121 } | |
| 122 | |
| 123 const HelloScript = `#!/bin/bash | |
| 124 echo "Hello World!" > output.txt | |
| 125 ` | |
| 126 | |
| 127 func TestDir(t *testing.T) { | |
| 128 dir1, err := ioutil.TempDir("", "exec_test1") | |
| 129 assert.NoError(t, err) | |
| 130 script := filepath.Join(dir1, "hello_script.sh") | |
| 131 assert.NoError(t, WriteScript(script, HelloScript)) | |
| 132 dir2, err := ioutil.TempDir("", "exec_test2") | |
| 133 assert.NoError(t, err) | |
| 134 assert.NoError(t, Run(&Command{ | |
| 135 Name: script, | |
| 136 Dir: dir2, | |
| 137 })) | |
| 138 file := filepath.Join(dir2, "output.txt") | |
| 139 _, err = os.Stat(file) | |
| 140 expect.NoError(t, err) | |
| 141 } | |
| 142 | |
| 143 func TestSimpleIO(t *testing.T) { | |
| 144 inputString := "foo\nbar\nbaz\n" | |
| 145 output := bytes.Buffer{} | |
| 146 assert.NoError(t, Run(&Command{ | |
| 147 Name: "grep", | |
| 148 Args: []string{"-e", "^ba"}, | |
| 149 Stdin: bytes.NewReader([]byte(inputString)), | |
| 150 Stdout: &output, | |
| 151 })) | |
| 152 expect.Equal(t, "bar\nbaz\n", string(output.Bytes())) | |
| 153 } | |
| 154 | |
| 155 func TestError(t *testing.T) { | |
| 156 dir, err := ioutil.TempDir("", "exec_test") | |
| 157 assert.NoError(t, err) | |
| 158 output := bytes.Buffer{} | |
| 159 err = Run(&Command{ | |
| 160 Name: "cp", | |
| 161 Args: []string{filepath.Join(dir, "doesnt_exist"), | |
| 162 filepath.Join(dir, "dest")}, | |
| 163 Stderr: &output, | |
| 164 }) | |
| 165 expect.Error(t, err) | |
| 166 expect.Contains(t, err.Error(), "exit status 1") | |
| 167 expect.Contains(t, string(output.Bytes()), "No such file or directory") | |
| 168 } | |
| 169 | |
| 170 const CombinedOutputScript = `#!/bin/bash | |
| 171 echo "roses" | |
| 172 >&2 echo "red" | |
| 173 echo "violets" | |
| 174 >&2 echo "blue" | |
| 175 ` | |
| 176 | |
| 177 func TestCombinedOutput(t *testing.T) { | |
| 178 dir, err := ioutil.TempDir("", "exec_test") | |
| 179 assert.NoError(t, err) | |
| 180 script := filepath.Join(dir, "combined_output_script.sh") | |
| 181 assert.NoError(t, WriteScript(script, CombinedOutputScript)) | |
| 182 combined := bytes.Buffer{} | |
| 183 assert.NoError(t, Run(&Command{ | |
| 184 Name: script, | |
| 185 CombinedOutput: &combined, | |
| 186 })) | |
| 187 expect.Equal(t, "roses\nred\nviolets\nblue\n", string(combined.Bytes())) | |
| 188 } | |
| 189 | |
| 190 const SleeperScript = `#!/bin/bash | |
| 191 sleep 3 | |
| 192 touch ran | |
| 193 ` | |
| 194 | |
| 195 func TestTimeoutNotReached(t *testing.T) { | |
| 196 dir, err := ioutil.TempDir("", "exec_test") | |
| 197 assert.NoError(t, err) | |
| 198 script := filepath.Join(dir, "sleeper_script.sh") | |
| 199 assert.NoError(t, WriteScript(script, SleeperScript)) | |
| 200 assert.NoError(t, Run(&Command{ | |
| 201 Name: script, | |
| 202 Dir: dir, | |
| 203 Timeout: time.Minute, | |
| 204 })) | |
| 205 file := filepath.Join(dir, "ran") | |
| 206 _, err = os.Stat(file) | |
| 207 expect.NoError(t, err) | |
| 208 } | |
| 209 | |
| 210 func TestTimeoutExceeded(t *testing.T) { | |
| 211 dir, err := ioutil.TempDir("", "exec_test") | |
| 212 assert.NoError(t, err) | |
| 213 script := filepath.Join(dir, "sleeper_script.sh") | |
| 214 assert.NoError(t, WriteScript(script, SleeperScript)) | |
| 215 err = Run(&Command{ | |
| 216 Name: script, | |
| 217 Dir: dir, | |
| 218 Timeout: time.Second, | |
| 219 }) | |
| 220 expect.Error(t, err) | |
| 221 expect.Contains(t, err.Error(), "Command killed") | |
| 222 file := filepath.Join(dir, "ran") | |
| 223 _, err = os.Stat(file) | |
| 224 expect.True(t, os.IsNotExist(err)) | |
| 225 } | |
| 226 | |
| 227 func TestInjection(t *testing.T) { | |
| 228 var actualCommand *Command | |
| 229 SetRunForTesting(func(command *Command) error { | |
| 230 actualCommand = command | |
| 231 return nil | |
| 232 }) | |
| 233 defer SetRunForTesting(DefaultRun) | |
| 234 | |
| 235 dir, err := ioutil.TempDir("", "exec_test") | |
| 236 assert.NoError(t, err) | |
| 237 file := filepath.Join(dir, "ran") | |
| 238 assert.NoError(t, Run(&Command{ | |
| 239 Name: "touch", | |
| 240 Args: []string{file}, | |
| 241 })) | |
| 242 _, err = os.Stat(file) | |
| 243 expect.True(t, os.IsNotExist(err)) | |
| 244 | |
| 245 expect.Equal(t, "touch", actualCommand.Name) | |
| 246 expect.Equal(t, 1, len(actualCommand.Args)) | |
| 247 expect.Equal(t, file, actualCommand.Args[0]) | |
| 248 } | |
| 249 | |
| 250 func TestRunSimple(t *testing.T) { | |
| 251 output, err := RunSimple(`echo "Hello Go!"`) | |
| 252 assert.NoError(t, err) | |
| 253 expect.Equal(t, "\"Hello Go!\"\n", output) | |
| 254 } | |
| OLD | NEW |