Files
mesh-catalog/modules/mesh-build-go/cmd/mesh-build-go/digest_test.go
T

383 lines
17 KiB
Go

package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"git.novox.be/novox/mesh-sdk/go/forgekey"
)
// Issue 1291: the digests of a fixture module (testdata/repos/digest): a's tests import b and read a/testdata, c's
// tests read an embedded file, d is reached by no test. They run the real `go list` (it loads packages, it builds
// none), through the step runner; only the listing of the tree's go.mod files and postgres's version are answered.
// digestModule is a module over a copy of the fixture, in a workspace of its own.
func digestFixture(t *testing.T) (*Module, string) {
t.Helper()
work := t.TempDir()
tree := filepath.Join(work, "ws1", "tree")
if err := os.MkdirAll(tree, 0o755); err != nil {
t.Fatal(err)
}
if out, err := exec.Command("cp", "-r", filepath.Join("testdata", "repos", "digest")+"/.", tree).CombinedOutput(); err != nil {
t.Fatalf("%v %s", err, out)
}
m := moduleWith(newStates(), "go1.27.1")
m.Home, m.Work, m.Cache = t.TempDir(), work, t.TempDir()
real := StepRunner(m.Home)
m.Run = throughBubblewrap(t, func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
switch name {
case "git":
return forgekey.Ran{Stdout: "go.mod\n"}
case "postgres":
return forgekey.Ran{Stdout: "postgres (PostgreSQL) " + strconv.Itoa(m.Recipe.PostgresMajor) + ".1\n"}
}
return real(ctx, env, name, args...)
}, &[]shutOff{}) // go list runs shut off (1309); here the real go, unshut, reads the fixture
return m, tree
}
func digests(t *testing.T, m *Module, level string) DigestResult {
t.Helper()
out, err := m.digest(context.Background(), map[string]any{"workspace": "ws1", "level": level})
if err != nil {
t.Fatal(err)
}
return out.(DigestResult)
}
func edit(t *testing.T, tree, name, text string) {
t.Helper()
if err := os.WriteFile(filepath.Join(tree, name), []byte(text), 0o644); err != nil {
t.Fatal(err)
}
}
// 1291/AC1: a file of b changes: a's digest changes (its tests import b), c's does not.
func TestADependencysFileChangesTheDigestOfThePackageWhoseTestsImportIt(t *testing.T) {
t.Parallel()
m, tree := digestFixture(t)
before := digests(t, m, "unit")
for _, p := range []string{"fixture/digest/a", "fixture/digest/c"} {
if len(before.Packages[p]) != 64 {
t.Fatalf("no digest for %s: %+v", p, before)
}
}
edit(t, tree, "b/b.go", "package b\n\n// B is reached by a's tests.\nfunc B() int { return 2 + 0 }\n")
after := digests(t, m, "unit")
if after.Packages["fixture/digest/a"] == before.Packages["fixture/digest/a"] {
t.Errorf("a's digest did not change when b did")
}
if after.Packages["fixture/digest/c"] != before.Packages["fixture/digest/c"] {
t.Errorf("c's digest changed when b did")
}
if again := digests(t, m, "unit"); again.Packages["fixture/digest/a"] != after.Packages["fixture/digest/a"] {
t.Errorf("one tree has two digests")
}
}
// 1291/AC2: a file under a's testdata/ changes: a's digest changes. An embedded file does the same for c.
func TestATestdataFileAndAnEmbeddedFileChangeTheDigest(t *testing.T) {
t.Parallel()
m, tree := digestFixture(t)
before := digests(t, m, "unit")
edit(t, tree, "a/testdata/x.txt", "y\n")
after := digests(t, m, "unit")
if after.Packages["fixture/digest/a"] == before.Packages["fixture/digest/a"] {
t.Errorf("a's digest did not change with its testdata")
}
if after.Packages["fixture/digest/c"] != before.Packages["fixture/digest/c"] {
t.Errorf("c's digest changed with a's testdata")
}
edit(t, tree, "c/note.txt", "another note\n")
if again := digests(t, m, "unit"); again.Packages["fixture/digest/c"] == after.Packages["fixture/digest/c"] {
t.Errorf("c's digest did not change with its embedded file")
}
}
// 1291/AC3: one tree at the unit and the integration level: the digests differ; at the integration level a change of the
// recipe's postgres major (or of the nats-server version) changes it, at the unit level it does not.
func TestTheLevelTheStoreAndTheBusAreInTheDigest(t *testing.T) {
t.Parallel()
m, _ := digestFixture(t)
unit, integration := digests(t, m, "unit"), digests(t, m, "integration")
a := "fixture/digest/a"
if unit.Packages[a] == integration.Packages[a] {
t.Errorf("the unit and the integration digest of a are one")
}
m.Recipe.PostgresMajor++
if got := digests(t, m, "integration"); got.Packages[a] == integration.Packages[a] {
t.Errorf("a new postgres major left the integration digest as it was")
}
if got := digests(t, m, "unit"); got.Packages[a] != unit.Packages[a] {
t.Errorf("a new postgres major moved the unit digest")
}
m.Recipe.PostgresMajor--
m.Recipe.NatsServer = "9.9.9"
if got := digests(t, m, "integration"); got.Packages[a] == integration.Packages[a] {
t.Errorf("a new nats-server version left the integration digest as it was")
}
m.Recipe.NatsServer = "2.11.17"
}
// 1291/AC4: a change in a package no test of a reaches leaves a's digest as it was; d, with no tests, has none.
func TestAPackageNoTestReachesChangesNoDigest(t *testing.T) {
t.Parallel()
m, tree := digestFixture(t)
before := digests(t, m, "unit")
edit(t, tree, "d/d.go", "package d\n\n// D is reached by no test.\nfunc D() int { return 5 }\n")
after := digests(t, m, "unit")
for p, d := range before.Packages {
if after.Packages[p] != d {
t.Errorf("the digest of %s changed with a package no test of it reaches", p)
}
}
if _, has := after.Packages["fixture/digest/d"]; has || strings.Join(after.WithoutTests, " ") != "fixture/digest/b fixture/digest/d" {
t.Errorf("without tests = %v, packages = %v", after.WithoutTests, after.Packages)
}
}
// The digest does not depend on where the workspace lies.
func TestTheDigestIsTheSameInAnotherWorkspace(t *testing.T) {
t.Parallel()
m1, _ := digestFixture(t)
m2, _ := digestFixture(t)
if d1, d2 := digests(t, m1, "unit"), digests(t, m2, "unit"); d1.Packages["fixture/digest/a"] != d2.Packages["fixture/digest/a"] {
t.Errorf("one tree in two places has two digests")
}
}
// A module outside the tree counts by its version and checksum, not by its files: a package of a module that is not the
// mesh's, as go list gave it for this module's own dependency (testdata/captured/golist-external.json, which README
// describes). A package of the mesh's own forge counts by its files' content instead (ADR 0593 §5; the next test).
func TestAModuleOutsideTheTreeCountsByVersionAndChecksum(t *testing.T) {
raw, err := os.ReadFile(filepath.Join("testdata", "captured", "golist-external.json"))
if err != nil {
t.Fatal(err)
}
pkgs, err := parsePackages(string(raw))
if err != nil {
t.Fatal(err)
}
// The capture holds the one package go list was asked about, whichever package that was.
var ext *goPackage
for _, p := range pkgs {
ext = p
}
if ext == nil || ext.Module == nil || ext.Module.Sum == "" || ext.Module.Version == "" || ext.Module.Main {
t.Fatalf("the capture holds no external package with a version and a checksum: %+v", ext)
}
ext.Module.Path = "example.com/outside" // the capture's package, as a module that is not the mesh's
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "r.go"), []byte("package r\n"), 0o644)
root := &goPackage{ImportPath: "x/r", Dir: dir, Module: &goModule{Path: "x", Main: true}, GoFiles: []string{"r.go"}, TestGoFiles: []string{"r.go"}}
testMain := &goPackage{ImportPath: "x/r.test", Deps: []string{ext.ImportPath}}
pkgs[root.ImportPath] = root
var names []string
digest := func() string {
d, err := packageDigest(root, testMain, pkgs, dir, []string{"digest 1"}, nil, names, nil)
if err != nil {
t.Fatal(err)
}
return d
}
d1 := digest()
ext.Module.Sum = "h1:changed="
if d2 := digest(); d1 == d2 {
t.Errorf("a new checksum of a module outside the tree left the digest as it was")
}
ext.Module.Sum, ext.Module.Version = "h1:changed=", "v9.9.9"
if d3 := digest(); d3 == d1 {
t.Errorf("a new version left the digest as it was")
}
// A different node list misses the cache only for a package whose tests read MESH_NODE_NAMES (ADR 0567 §15).
names = []string{"node-names a"}
d4 := digest()
names = []string{"node-names a", "node-names b"}
if d5 := digest(); d5 != d4 {
t.Errorf("a package that does not read the node names changed with them")
}
os.WriteFile(filepath.Join(dir, "r.go"), []byte("package r\n// "+namesVariable+"\n"), 0o644)
names = []string{"node-names a"}
d6 := digest()
names = []string{"node-names a", "node-names b"}
if d7 := digest(); d7 == d6 {
t.Errorf("a new node left the digest of a package that reads the node names as it was")
}
}
// ADR 0593 §5 (C5): a package of the mesh's forge counts by the content of the files the tests reach, not by the version
// of its module. Two versions of mesh-sdk whose reached files are equal give one digest; a changed reached file changes it;
// a changed file of a package no test reaches does not.
func TestAMeshPackageCountsByTheContentOfTheFilesReached(t *testing.T) {
mesh := t.TempDir()
write := func(dir, name, text string) {
t.Helper()
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, name), []byte(text), 0o644); err != nil {
t.Fatal(err)
}
}
write(filepath.Join(mesh, "reached"), "a.go", "package reached\n")
write(filepath.Join(mesh, "unreached"), "b.go", "package unreached\n")
module := &goModule{Path: "git.novox.be/novox/mesh-sdk/go", Version: "v0.1.1", Sum: "h1:one="}
reached := &goPackage{ImportPath: module.Path + "/reached", Dir: filepath.Join(mesh, "reached"), Module: module, GoFiles: []string{"a.go"}}
unreached := &goPackage{ImportPath: module.Path + "/unreached", Dir: filepath.Join(mesh, "unreached"), Module: module, GoFiles: []string{"b.go"}}
own := t.TempDir()
write(own, "r.go", "package r\n")
root := &goPackage{ImportPath: "x/r", Dir: own, Module: &goModule{Path: "x", Main: true}, GoFiles: []string{"r.go"}, TestGoFiles: []string{"r.go"}}
pkgs := map[string]*goPackage{reached.ImportPath: reached, unreached.ImportPath: unreached, root.ImportPath: root}
testMain := &goPackage{ImportPath: "x/r.test", Deps: []string{reached.ImportPath}}
digest := func() string {
d, err := packageDigest(root, testMain, pkgs, own, []string{"digest 1"}, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
return d
}
d1 := digest()
module.Version, module.Sum = "v0.2.0", "h1:two=" // a new version of the module whose reached files are the same
if d2 := digest(); d2 != d1 {
t.Errorf("a new version of mesh-sdk with equal reached files changed the digest")
}
write(filepath.Join(mesh, "unreached"), "b.go", "package unreached\n\nconst B = 1\n")
if d3 := digest(); d3 != d1 {
t.Errorf("a changed package no test reaches changed the digest")
}
write(filepath.Join(mesh, "reached"), "a.go", "package reached\n\nconst A = 1\n")
if d4 := digest(); d4 == d1 {
t.Errorf("a changed reached file left the digest as it was")
}
}
// Issue 1935, 1936: a package whose reads-outside marker lists nothing it can trust is never kept: its digest is one no
// kept verdict can have (it differs on every call, though nothing changed), and the answer names it. A marker that lists
// what the tests read is kept: a change to a file it names changes the package's digest, a change elsewhere does not.
func TestAPackageWithTheReadsOutsideMarkerIsKeptOnlyByWhatItLists(t *testing.T) {
t.Parallel()
m, tree := digestFixture(t)
plain := digests(t, m, "unit")
if len(plain.ReadsOutside) != 0 {
t.Fatalf("a package carries the marker in the fixture: %v", plain.ReadsOutside)
}
c, a := "fixture/digest/c", "fixture/digest/a"
if err := os.MkdirAll(filepath.Join(tree, "docs"), 0o755); err != nil {
t.Fatal(err)
}
edit(t, tree, "docs/one.md", "one\n")
edit(t, tree, "other.md", "other\n")
for name, marker := range map[string]string{
"an empty marker": "# nothing listed\n\n",
"a sentence": "the tests read ../docs\n",
"a path climbing out": "../docs\n",
"a path matching nothing": "docs/none.md\n",
"an absolute path": "/etc/passwd\n",
"a path that is not clean": "docs/./one.md\n",
"a good line and a bad line": "docs\nnope/*.md\n",
} {
edit(t, tree, "c/"+ReadsOutsideMarker, marker)
first, second := digests(t, m, "unit"), digests(t, m, "unit")
if len(first.Packages[c]) != 64 || first.Packages[c] == second.Packages[c] || first.Packages[c] == plain.Packages[c] {
t.Errorf("%s: c's digest is stable: %s, %s, before %s", name, first.Packages[c], second.Packages[c], plain.Packages[c])
}
if strings.Join(first.ReadsOutside, ",") != c {
t.Errorf("%s: the answer does not name the package that reads outside: %v", name, first.ReadsOutside)
}
if first.Packages[a] != plain.Packages[a] || first.Packages[a] != second.Packages[a] {
t.Errorf("%s: a has no marker and its digest moved: %s, %s, before %s", name, first.Packages[a], second.Packages[a], plain.Packages[a])
}
}
// A list: a glob, a directory read whole, and a path whose absence is read.
edit(t, tree, "c/"+ReadsOutsideMarker, "# what c's tests read\ndocs/*.md\ndocs\n?gone\n")
before := digests(t, m, "unit")
if len(before.ReadsOutside) != 0 || len(before.Packages[c]) != 64 || before.Packages[c] == plain.Packages[c] {
t.Fatalf("a listed marker is not kept: %v, %s (no marker %s)", before.ReadsOutside, before.Packages[c], plain.Packages[c])
}
if again := digests(t, m, "unit"); again.Packages[c] != before.Packages[c] {
t.Errorf("nothing changed and c's digest moved")
}
for _, step := range []struct {
file, text string
moves bool
}{
{"other.md", "changed\n", false}, // outside the list
{"d/d.go", "package d\n\n// D.\n", false}, // a package c's tests do not reach
{"docs/one.md", "changed\n", true}, // a listed file
{"docs/two.md", "new\n", true}, // a new file the glob matches
{"gone", "here\n", true}, // a path whose absence was read
{"c/note.txt", "changed\n", true}, // the package's own file that is not Go
{"c/x_integration_test.go", "//go:build integration\n\npackage c\n", true}, // a Go file its build tags leave out (go list's IgnoredGoFiles)
} {
edit(t, tree, step.file, step.text)
after := digests(t, m, "unit")
if moved := after.Packages[c] != before.Packages[c]; moved != step.moves {
t.Errorf("a change to %s: c's digest moved %v, want %v", step.file, moved, step.moves)
}
if after.Packages[a] != plain.Packages[a] {
t.Errorf("a change to %s moved a's digest", step.file)
}
before = after
}
// Issue 890/AC3: a package whose directory carries the needs marker is named with its needs at the integration level,
// comments and blank lines not read, and at the unit level not at all.
edit(t, tree, "a/"+NeedsMarker, "# its integration tests copy the installed Node toolchain\n\nnode-build-node\n")
if n := digests(t, m, LevelIntegration).Needs; len(n) != 1 || n[0].Package != a || strings.Join(n[0].Needs, ",") != "node-build-node" {
t.Errorf("the integration level's needs = %+v, want %s needing node-build-node", n, a)
}
if n := digests(t, m, LevelUnit).Needs; len(n) != 0 {
t.Errorf("the unit level names needs: %+v", n)
}
}
// Issue 1935: a catalogue module's packages read ../../module.json and the module's files: a change to them changes the
// digest of the module's package, and a change to another module's files does not.
func TestACatalogueModulesFilesChangeTheDigestOfItsPackagesOnly(t *testing.T) {
t.Parallel()
m, tree := digestFixture(t)
for _, mod := range []string{"mod1", "mod2"} {
dir := filepath.Join(tree, mod, "cmd", "p")
if err := os.MkdirAll(filepath.Join(tree, mod, "files"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
edit(t, tree, mod+"/module.json", `{"module":"`+mod+`"}`)
edit(t, tree, mod+"/README.md", "# "+mod+"\n")
edit(t, tree, mod+"/files/f.env", "A=1\n")
edit(t, tree, mod+"/cmd/p/p.go", "package p\n")
edit(t, tree, mod+"/cmd/p/p_test.go", "package p\n\nimport (\n\t\"os\"\n\t\"testing\"\n)\n\nfunc TestReads(t *testing.T) {\n\tif _, err := os.ReadFile(\"../../module.json\"); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n")
}
p1, p2 := "fixture/digest/mod1/cmd/p", "fixture/digest/mod2/cmd/p"
before := digests(t, m, "unit")
if len(before.Packages[p1]) != 64 || len(before.Packages[p2]) != 64 {
t.Fatalf("no digest for the module packages: %+v", before.Packages)
}
if before.Packages[p1] == before.Packages[p2] {
t.Errorf("two modules, one digest")
}
for _, file := range []string{"mod1/module.json", "mod1/README.md", "mod1/files/f.env"} {
edit(t, tree, file, "changed "+file+"\n")
after := digests(t, m, "unit")
if after.Packages[p1] == before.Packages[p1] {
t.Errorf("a change to %s did not change the module's digest", file)
}
if after.Packages[p2] != before.Packages[p2] {
t.Errorf("a change to %s changed the other module's digest", file)
}
before = after
}
if got := digests(t, m, "unit"); got.Packages["fixture/digest/c"] == "" {
t.Errorf("a package outside any module lost its digest")
}
}