This is primarily for chroot stores. This might not be useful however since chroot store breaks builds.
85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package nix_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"slices"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"gensokyo.uk/nix"
|
|
)
|
|
|
|
func TestNixWriteStdin(t *testing.T) {
|
|
t.Run("store", func(t *testing.T) {
|
|
ctx := nix.New(t.Context(), &nix.BinaryCache{
|
|
Compression: "none",
|
|
ParallelCompression: false,
|
|
Bucket: "example",
|
|
Endpoint: "s3.example.org",
|
|
Region: "us-east-1",
|
|
Scheme: "http",
|
|
CredentialsPath: "/dev/null",
|
|
KeyPath: nonexistent,
|
|
}, nil, nil, nil)
|
|
cmd := ctx.Nix(t.Context(), nix.FlagVersion)
|
|
|
|
wantArgs := []string{
|
|
nix.Nix,
|
|
nix.FlagStore,
|
|
"s3://example?compression=none¶llel-compression=false®ion=us-east-1&scheme=http&endpoint=s3.example.org&secret-key=/proc/nonexistent",
|
|
nix.FlagVersion}
|
|
if !slices.Equal(cmd.Args, wantArgs) {
|
|
t.Errorf("Args = %#v, want %#v", cmd.Args, wantArgs)
|
|
}
|
|
|
|
wantEnv := []string{nix.EnvAwsSharedCredentialsFile + "=/dev/null"}
|
|
if !slices.Equal(cmd.Env, wantEnv) {
|
|
t.Errorf("Env = %#v, want %#v", cmd.Env, wantEnv)
|
|
}
|
|
})
|
|
|
|
t.Run("already set", func(t *testing.T) {
|
|
ctx := nix.New(t.Context(), nil, 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)
|
|
}
|
|
})
|
|
}
|