57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package nix_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"slices"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"gensokyo.uk/nix"
|
|
)
|
|
|
|
func TestNixWriteStdin(t *testing.T) {
|
|
t.Run("already set", func(t *testing.T) {
|
|
ctx := nix.New(t.Context(), nil, os.Stdout, os.Stderr)
|
|
cmd := exec.CommandContext(t.Context(), nonexistent)
|
|
cmd.Stdin = os.Stdin
|
|
if _, err := ctx.WriteStdin(cmd, nil, nil); err == nil {
|
|
t.Fatal("WriteStdinCommand unexpectedly succeeded")
|
|
}
|
|
})
|
|
|
|
t.Run("f returns error", func(t *testing.T) {
|
|
stubNixCommand(t)
|
|
ctx := newStubContext(t.Context(), nil, os.Stdout, os.Stderr)
|
|
cmd := ctx.Nix(t.Context(), "true")
|
|
if _, err := ctx.WriteStdin(
|
|
cmd,
|
|
slices.Values(make([]string, 0)),
|
|
func() error { return syscall.ENOSYS },
|
|
); !errors.Is(err, syscall.ENOSYS) {
|
|
t.Fatalf("WriteStdinCommand: error = %v, want %v", err, syscall.ENOSYS)
|
|
}
|
|
})
|
|
|
|
t.Run("exit before cancel", func(t *testing.T) {
|
|
stubNixCommand(t)
|
|
ctx := newStubContext(t.Context(), nil, os.Stdout, os.Stderr)
|
|
c, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
cmd := ctx.Nix(c, "true")
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatalf("Start: error = %v", err)
|
|
}
|
|
// Cancel is skipped after exec.Cmd.Wait completes
|
|
if _, err := cmd.Process.Wait(); err != nil {
|
|
t.Fatalf("Wait: error = %v", err)
|
|
}
|
|
cancel()
|
|
if cmd.Err != nil {
|
|
t.Fatalf("Err = %v", cmd.Err)
|
|
}
|
|
})
|
|
}
|