2023-09-27 09:04:44 +00:00
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
2024-03-26 07:57:48 +00:00
|
|
|
"bufio"
|
2023-09-27 09:04:44 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-03-26 07:57:48 +00:00
|
|
|
func splitLines(b []byte) (lines []string) {
|
|
|
|
scan := bufio.NewScanner(bytes.NewReader(b))
|
|
|
|
for scan.Scan() {
|
|
|
|
line := scan.Text()
|
|
|
|
if line != "" {
|
|
|
|
lines = append(lines, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2023-09-27 09:04:44 +00:00
|
|
|
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))
|
2024-03-26 07:57:48 +00:00
|
|
|
|
|
|
|
// The order of stdout and stderr being read into the buffer
|
|
|
|
// for combined output is not deterministic due to scheduling
|
|
|
|
// of the underlying goroutines that consume them.
|
|
|
|
// That's why this asserts on the contents and not the order.
|
|
|
|
assert.ElementsMatch(t, []string{"1", "2"}, splitLines(buf.Bytes()))
|
2023-09-27 09:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2024-03-26 07:57:48 +00:00
|
|
|
assert.ElementsMatch(t, []string{"1", "2"}, splitLines(buf.Bytes()))
|
2023-09-27 09:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|