package link import ( "os" "os/exec" "path/filepath" "strings" "testing" ) // Where the conformance fixtures are read from, held on a small SDK repository of the test's own: two // commits, the clone checked out at the older as the build seat leaves it at the running controller's pin // (novox/hq issue 449). The contents are the test's, because what is judged is which commit is read. type sdkBed struct { root, goMod, captured, capturedLog string older, newer string } const fixtureName = "events/module-event.json" func newSDKBed(t *testing.T) sdkBed { t.Helper() if _, err := exec.LookPath("git"); err != nil { t.Fatalf("git is needed to read the SDK at a pin, as a merge check does: %v", err) } b := sdkBed{root: t.TempDir()} clone := filepath.Join(b.root, "mesh-sdk") git := func(args ...string) string { t.Helper() cmd := exec.Command("git", append([]string{"-C", clone, "-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"}, args...)...) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("git %v: %v: %s", args, err, out) } return strings.TrimSpace(string(out)) } write := func(dir, body string) { t.Helper() file := filepath.Join(dir, "conformance", filepath.FromSlash(fixtureName)) if err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(file, []byte(body), 0o644); err != nil { t.Fatal(err) } } if err := os.MkdirAll(clone, 0o755); err != nil { t.Fatal(err) } git("init", "--quiet") write(clone, "older\n") git("add", "-A") git("commit", "--quiet", "-m", "older") b.older = git("rev-parse", "HEAD") write(clone, "newer\n") git("commit", "--quiet", "-am", "newer") b.newer = git("rev-parse", "HEAD") git("checkout", "--quiet", b.older) other := t.TempDir() b.captured = filepath.Join(other, "mesh-sdk") write(b.captured, "older\n") b.capturedLog = filepath.Join(other, "CAPTURED") if err := os.WriteFile(b.capturedLog, []byte("mesh-sdk "+b.older+" conformance/events\n"), 0o644); err != nil { t.Fatal(err) } b.goMod = filepath.Join(other, "go.mod") b.pin(t, b.newer) return b } func (b sdkBed) pin(t *testing.T, commit string) { t.Helper() mod := "module x\n\nrequire (\n\t" + sdkModule + " v0.1.11-0.20261009143344-" + commit[:12] + "\n)\n" if err := os.WriteFile(b.goMod, []byte(mod), 0o644); err != nil { t.Fatal(err) } } // A pull request moving the SDK is judged against the SDK it moves to: the clone is read at the tree's own // pin, not at what it is checked out at, the running controller's. func TestAMergeCheckReadsTheSDKAtTheTreesOwnPin(t *testing.T) { b := newSDKBed(t) raw, from, err := sdkFixture(b.root, b.goMod, b.captured, b.capturedLog, fixtureName) if err != nil { t.Fatal(err) } if string(raw) != "newer\n" { t.Errorf("read %q from %s; the tree pins the newer commit", raw, from) } } // A captured copy that is not the SDK's at the commit CAPTURED names fails a merge check. func TestAMergeCheckFailsACapturedCopyEditedByHand(t *testing.T) { b := newSDKBed(t) if err := os.WriteFile(filepath.Join(b.captured, "conformance", filepath.FromSlash(fixtureName)), []byte("edited\n"), 0o644); err != nil { t.Fatal(err) } if _, _, err := sdkFixture(b.root, b.goMod, b.captured, b.capturedLog, fixtureName); err == nil || !strings.Contains(err.Error(), "is not mesh-sdk's at") { t.Errorf("a hand-edited captured copy was accepted: %v", err) } } // A pin the clone does not hold fails loudly and names it. func TestAMergeCheckFailsWithoutTheSDKAtItsPin(t *testing.T) { b := newSDKBed(t) b.pin(t, "0123456789ab0123456789ab0123456789ab0123") if _, _, err := sdkFixture(b.root, b.goMod, b.captured, b.capturedLog, fixtureName); err == nil || !strings.Contains(err.Error(), "0123456789ab") { t.Errorf("a pin the clone lacks was not named: %v", err) } } // Until mesh-controller #220 has rolled out the seat places no mesh-sdk beside a check: the captured copy is // read, and where it was read from says plainly that the clone did not judge it (novox/hq issue 449). func TestAMergeCheckWithoutTheCloneSaysItReadTheCapturedCopy(t *testing.T) { b := newSDKBed(t) raw, from, err := sdkFixture(t.TempDir(), b.goMod, b.captured, b.capturedLog, fixtureName) if err != nil || string(raw) != "older\n" { t.Fatalf("read %q, %v; the captured copy holds the older", raw, err) } if !strings.HasPrefix(from, "NOT JUDGED AGAINST THE CLONE") || !strings.Contains(from, b.older) { t.Errorf("the log does not say the clone was missing and which copy was read: %q", from) } } // Away from a merge check the captured copy is read, and nothing else. func TestAwayFromACheckTheCapturedCopyIsRead(t *testing.T) { b := newSDKBed(t) raw, _, err := sdkFixture("", b.goMod, b.captured, b.capturedLog, fixtureName) if err != nil || string(raw) != "older\n" { t.Errorf("read %q, %v; the captured copy holds the older", raw, err) } }