format: wrap nix command
This commit is contained in:
parent
a9d0130474
commit
edac902c7f
18
format.go
18
format.go
@ -1,5 +1,23 @@
|
|||||||
package nixbuild
|
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 (
|
const (
|
||||||
nixosSuffix0 = "#nixosConfigurations."
|
nixosSuffix0 = "#nixosConfigurations."
|
||||||
nixosSuffix1 = ".config.system.build.toplevel"
|
nixosSuffix1 = ".config.system.build.toplevel"
|
||||||
|
@ -1,11 +1,34 @@
|
|||||||
package nixbuild_test
|
package nixbuild_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.gensokyo.uk/yonah/nixbuild"
|
"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) {
|
func TestInstallable(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name, flake, host, want string
|
name, flake, host, want string
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
|
||||||
"path"
|
"path"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
@ -105,11 +104,8 @@ func EvalInstantiated(ctx context.Context, installable string) ([]string, error)
|
|||||||
c, cancel := context.WithCancel(ctx)
|
c, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
cmd := exec.CommandContext(c,
|
cmd := Nix(c, "build", installable,
|
||||||
"nix", "build", installable,
|
// 'instantiated' messages are only emitted when actually evaluating something
|
||||||
// since flakes are supposedly experimental
|
|
||||||
"--extra-experimental-features", "nix-command flakes",
|
|
||||||
// 'instantiated' messages are only emitted when actually evaluating something
|
|
||||||
"--option", "eval-cache", "false",
|
"--option", "eval-cache", "false",
|
||||||
// do not actually build anything
|
// do not actually build anything
|
||||||
"--dry-run",
|
"--dry-run",
|
||||||
|
@ -2,6 +2,7 @@ package nixbuild_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
@ -42,6 +43,12 @@ func TestDecodeInstantiated(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
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)
|
stderr := strings.NewReader(tc.out)
|
||||||
got, err := nixbuild.DecodeInstantiated(stderr)
|
got, err := nixbuild.DecodeInstantiated(stderr)
|
||||||
if !errors.Is(err, tc.wantErr) {
|
if !errors.Is(err, tc.wantErr) {
|
||||||
@ -70,6 +77,16 @@ func TestDecodeInstantiated(t *testing.T) {
|
|||||||
t.Error("unexpected MalformedInstantiatedError equivalence")
|
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