-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathbackground_test.go
More file actions
91 lines (81 loc) · 2.53 KB
/
Copy pathbackground_test.go
File metadata and controls
91 lines (81 loc) · 2.53 KB
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
package process
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBackgroundUnwrapsNotFound(t *testing.T) {
ctx := context.Background()
_, err := Background(ctx, []string{"/bin/meeecho", "1"})
assert.ErrorIs(t, err, os.ErrNotExist)
}
func TestBackground(t *testing.T) {
ctx := context.Background()
res, err := Background(ctx, []string{"echo", "1"}, WithDir("/"))
assert.NoError(t, err)
assert.Equal(t, "1", strings.TrimSpace(res))
}
func TestBackgroundOnlyStdoutGetsoutOnSuccess(t *testing.T) {
ctx := context.Background()
res, err := Background(ctx, []string{
"python3", "-c", "import sys; sys.stderr.write('1'); sys.stdout.write('2')",
})
assert.NoError(t, err)
assert.Equal(t, "2", res)
}
func TestBackgroundCombinedOutput(t *testing.T) {
ctx := context.Background()
buf := bytes.Buffer{}
res, err := Background(ctx, []string{
"python3", "-c", "import sys, time; " +
`sys.stderr.write("1\n"); sys.stderr.flush(); ` +
"time.sleep(0.001); " +
"print('2', flush=True); sys.stdout.flush(); " +
"time.sleep(0.001)",
}, WithCombinedOutput(&buf))
assert.NoError(t, err)
assert.Equal(t, "2", strings.TrimSpace(res))
assert.Equal(t, "1\n2\n", strings.ReplaceAll(buf.String(), "\r", ""))
}
func TestBackgroundCombinedOutputFailure(t *testing.T) {
ctx := context.Background()
buf := bytes.Buffer{}
res, err := Background(ctx, []string{
"python3", "-c", "import sys, time; " +
`sys.stderr.write("1\n"); sys.stderr.flush(); ` +
"time.sleep(0.001); " +
"print('2', flush=True); sys.stdout.flush(); " +
"time.sleep(0.001); " +
"sys.exit(42)",
}, WithCombinedOutput(&buf))
var processErr *ProcessError
if assert.ErrorAs(t, err, &processErr) {
assert.Equal(t, "1", strings.TrimSpace(processErr.Stderr))
assert.Equal(t, "2", strings.TrimSpace(processErr.Stdout))
}
assert.Equal(t, "2", strings.TrimSpace(res))
assert.Equal(t, "1\n2\n", strings.ReplaceAll(buf.String(), "\r", ""))
}
func TestBackgroundNoStdin(t *testing.T) {
ctx := context.Background()
res, err := Background(ctx, []string{"cat"})
assert.NoError(t, err)
assert.Equal(t, "", res)
}
func TestBackgroundFails(t *testing.T) {
ctx := context.Background()
_, err := Background(ctx, []string{"ls", "/dev/null/x"})
assert.NotNil(t, err)
}
func TestBackgroundFailsOnOption(t *testing.T) {
ctx := context.Background()
_, err := Background(ctx, []string{"ls", "/dev/null/x"}, func(_ context.Context, c *exec.Cmd) error {
return fmt.Errorf("nope")
})
assert.EqualError(t, err, "nope")
}