format: wrap nix command
This commit is contained in:
parent
0afb8284d6
commit
4874dc35c9
18
format.go
18
format.go
@ -1,5 +1,23 @@
|
||||
package nixbuild
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
const (
|
||||
nixExtraExperimentalFeatures = "--extra-experimental-features"
|
||||
nixExperimentalFeaturesFlakes = "nix-command flakes"
|
||||
)
|
||||
|
||||
// since flakes are supposedly experimental
|
||||
var nixEnableFlakes = []string{nixExtraExperimentalFeatures, nixExperimentalFeaturesFlakes}
|
||||
|
||||
// Nix returns the [exec.Cmd] struct to execute a nix command.
|
||||
func Nix(ctx context.Context, arg ...string) *exec.Cmd {
|
||||
return exec.CommandContext(ctx, "nix", append(nixEnableFlakes, arg...)...)
|
||||
}
|
||||
|
||||
const (
|
||||
nixosSuffix0 = "#nixosConfigurations."
|
||||
nixosSuffix1 = ".config.system.build.toplevel"
|
||||
|
@ -1,11 +1,34 @@
|
||||
package nixbuild_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"git.gensokyo.uk/yonah/nixbuild"
|
||||
)
|
||||
|
||||
func TestNix(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
arg []string
|
||||
want []string
|
||||
}{
|
||||
{"build", []string{"build"},
|
||||
[]string{"nix", "--extra-experimental-features", "nix-command flakes", "build"}},
|
||||
{"build workflow", []string{"build", `--out-link "result"`, "--print-out-paths", "--print-build-logs"},
|
||||
[]string{"nix", "--extra-experimental-features", "nix-command flakes", "build", `--out-link "result"`, "--print-out-paths", "--print-build-logs"}},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := nixbuild.Nix(t.Context(), tc.arg...)
|
||||
if !slices.Equal(got.Args, tc.want) {
|
||||
t.Errorf("Nix: %#v, want %#v",
|
||||
got.Args, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallable(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name, flake, host, want string
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os/exec"
|
||||
"path"
|
||||
"slices"
|
||||
"strings"
|
||||
@ -105,10 +104,7 @@ func EvalInstantiated(ctx context.Context, installable string) ([]string, error)
|
||||
c, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(c,
|
||||
"nix", "build", installable,
|
||||
// since flakes are supposedly experimental
|
||||
"--extra-experimental-features", "nix-command flakes",
|
||||
cmd := Nix(c, "build", installable,
|
||||
// 'instantiated' messages are only emitted when actually evaluating something
|
||||
"--option", "eval-cache", "false",
|
||||
// do not actually build anything
|
||||
|
@ -2,6 +2,7 @@ package nixbuild_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
@ -42,6 +43,12 @@ func TestDecodeInstantiated(t *testing.T) {
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if tc.wantErr != nil {
|
||||
w := nixbuild.Stderr
|
||||
nixbuild.Stderr = os.Stderr
|
||||
t.Cleanup(func() { nixbuild.Stderr = w })
|
||||
}
|
||||
|
||||
stderr := strings.NewReader(tc.out)
|
||||
got, err := nixbuild.DecodeInstantiated(stderr)
|
||||
if !errors.Is(err, tc.wantErr) {
|
||||
@ -70,6 +77,16 @@ func TestDecodeInstantiated(t *testing.T) {
|
||||
t.Error("unexpected MalformedInstantiatedError equivalence")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unreachable", func(t *testing.T) {
|
||||
defer func() {
|
||||
wantPanic := "unreachable"
|
||||
if r := recover(); r != wantPanic {
|
||||
t.Errorf("Error: panic = %q, want %q", r, wantPanic)
|
||||
}
|
||||
}()
|
||||
_ = (&nixbuild.MalformedInstantiatedError{Type: -1}).Error()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user