| 17 | } |
| 18 | |
| 19 | func TestRunScript(t *testing.T) { |
| 20 | t.Run("outputs", func(t *testing.T) { |
| 21 | testDir := t.TempDir() |
| 22 | require.NoError(t, os.WriteFile(filepath.Join(testDir, "testdirfile"), []byte("testdirfile\n"), 0644)) |
| 23 | strptr := func(s string) *string { return &s } |
| 24 | err := (&ZTest{ |
| 25 | Script: ` |
| 26 | echo stdout |
| 27 | echo stderr >&2 |
| 28 | touch empty |
| 29 | echo notempty > notempty |
| 30 | echo regexp > regexp |
| 31 | echo testdirfile > testdirfile |
| 32 | echo testdirfile > testdirfile2 |
| 33 | `, |
| 34 | Outputs: []File{ |
| 35 | {Name: "stdout", Data: strptr("stdout\n")}, |
| 36 | {Name: "stderr", Data: strptr("stderr\n")}, |
| 37 | {Name: "empty", Data: strptr("")}, |
| 38 | {Name: "notempty", Data: strptr("notempty\n")}, |
| 39 | {Name: "regexp", Re: "^re"}, |
| 40 | {Name: "testdirfile"}, |
| 41 | {Name: "testdirfile2", Source: "testdirfile"}, |
| 42 | }, |
| 43 | }).RunScript(t.Context(), "", testDir, t.TempDir) |
| 44 | assert.NoError(t, err) |
| 45 | }) |
| 46 | t.Run("error", func(t *testing.T) { |
| 47 | err := (&ZTest{ |
| 48 | Script: "echo 1; echo 2 >&2; exit 3", |
| 49 | Outputs: []File{}, |
| 50 | }).RunScript(t.Context(), "", "", func() string { return "" }) |
| 51 | assert.EqualError(t, err, "script failed: exit status 3\n=== stdout ===\n1\n=== stderr ===\n2\n") |
| 52 | }) |
| 53 | } |