nixbuild/exec_test.go
Ophestra d3a8aed237
exec: replace global state with interface
This is cleaner, and finally enables writing tests for the nix invoking functions.
2025-07-17 16:53:00 +09:00

38 lines
894 B
Go

package nixbuild_test
import (
"errors"
"os"
"os/exec"
"slices"
"syscall"
"testing"
"git.gensokyo.uk/yonah/nixbuild"
)
func TestNixWriteStdin(t *testing.T) {
ctx := nixbuild.New(t.Context(), nil, os.Stdout, os.Stderr)
t.Run("already set", func(t *testing.T) {
cmd := exec.CommandContext(t.Context(), "/proc/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)
}
})
}