53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/buildstep"
|
|
)
|
|
|
|
func fixture(t *testing.T) string {
|
|
t.Helper()
|
|
d := t.TempDir()
|
|
os.MkdirAll(filepath.Join(d, "sub"), 0o755)
|
|
os.WriteFile(filepath.Join(d, "a.txt"), []byte("alpha"), 0o644)
|
|
os.WriteFile(filepath.Join(d, "sub", "run"), []byte("#!/bin/sh\n"), 0o755)
|
|
return d
|
|
}
|
|
|
|
func TestPackFixtureTwiceGivesBuildstepsDigest(t *testing.T) {
|
|
d := fixture(t)
|
|
_, want, err := buildstep.Pack(d)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
got, err := run([]string{"-pack", d, "-out", filepath.Join(t.TempDir(), "x.tgz")})
|
|
if err != nil || got != want {
|
|
t.Fatalf("run %d: %q, %v; want %q", i, got, err, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildTwiceGivesOneDigestAndRecipeArgs(t *testing.T) {
|
|
m := t.TempDir()
|
|
os.WriteFile(filepath.Join(m, "go.mod"), []byte("module x\n\ngo 1.21\n"), 0o644)
|
|
os.MkdirAll(filepath.Join(m, "cmd", "p"), 0o755)
|
|
os.WriteFile(filepath.Join(m, "cmd", "p", "main.go"), []byte("package main\n\nvar builtFor string\n\nfunc main() { println(builtFor) }\n"), 0o644)
|
|
args := []string{"-dir", m, "-from", "cmd/p", "-binary", "p", "-system", "arch"}
|
|
a, err := run(args)
|
|
if err != nil {
|
|
t.Skipf("no go toolchain that builds offline: %v", err)
|
|
}
|
|
b, err := run(args)
|
|
if err != nil || a != b {
|
|
t.Fatalf("two builds: %q, %q, %v", a, b, err)
|
|
}
|
|
got := Args("/d", "./cmd/p/", "/o", "p", "arch")
|
|
if got[len(got)-1] != "./cmd/p" || got[4] != "-s -w -X main.builtFor=arch" {
|
|
t.Fatalf("args %v", got)
|
|
}
|
|
}
|