From cdf168706ba9164dd52b44529916f40c744b8692 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 16 Nov 2026 11:42:56 +0100 Subject: [PATCH] Go steps test a module's own directories alone, not what its go.mod replaces from outside (A-447c) Builds: design 47 G (G-22 lead answers 2) A-447c --- .../mesh-build-go/cmd/mesh-build-go/digest.go | 2 +- .../mesh-build-go/cmd/mesh-build-go/fetch.go | 21 ++++++++++ .../cmd/mesh-build-go/gomoddirs_test.go | 41 +++++++++++++++++++ .../cmd/mesh-build-go/integration.go | 2 +- .../mesh-build-go/cmd/mesh-build-go/test.go | 2 +- 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/modules/mesh-build-go/cmd/mesh-build-go/digest.go b/modules/mesh-build-go/cmd/mesh-build-go/digest.go index fe7c06c79..9aa9c4616 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/digest.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/digest.go @@ -260,7 +260,7 @@ func (m *Module) digestHere(ctx context.Context, workspace, level string, names namesHeader = []string{"node-names " + strings.Join(sortedNames, ",")} } - dirs, err := m.goModDirs(ctx, tree) + dirs, err := m.testDirs(ctx, tree) if err != nil { return DigestResult{}, err } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go index cd61cc20a..04a18aadf 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/fetch.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/fetch.go @@ -476,6 +476,27 @@ func (m *Module) goModDirs(ctx context.Context, tree string) ([]string, error) { return dirs, nil } +// testDirs are the directories the unit and integration steps and the digest take: the chosen go.mod directories at or +// under the module's own directory. A directory the module's go.mod replaces from outside its own stays for download and +// resolution alone; its tests are another module's (design 47 G, A-447c). A workspace fetched with no dir keeps every go.mod. +func (m *Module) testDirs(ctx context.Context, tree string) ([]string, error) { + all, err := m.goModDirs(ctx, tree) + if err != nil { + return nil, err + } + own, ok := moduleDirOf(tree) + if !ok || own == "." || own == "" { + return all, nil + } + out := []string{} + for _, d := range all { + if d == own || strings.HasPrefix(d, own+"/") { + out = append(out, d) + } + } + return out, nil +} + // inTestdata says a path lies under a directory named testdata, whose go.mod files are fixtures Go ignores, not modules // of the tree (issue 1810). func inTestdata(path string) bool { diff --git a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go index f08f77218..1a7a8d429 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/gomoddirs_test.go @@ -108,3 +108,44 @@ func TestAFetchWithNoDirectoryChoosesEveryGoModOfTheTree(t *testing.T) { t.Fatalf("a fetch with no dir ended %s: %q %s", st.State, st.Reason, st.Result) } } + +// A-447c: the unit level of a module whose go.mod replaces ../lib tests the module alone; lib is downloaded and resolved +// for it, and its tests are another module's. +func TestTheUnitLevelTestsOnlyTheModulesOwnDirectoriesNotWhatItReplaces(t *testing.T) { + f := &fakeStep{} + m, _ := fetchModule(t, f) + root := filepath.Join(m.Work, "ws1") + tree := filepath.Join(root, "tree") + for d, body := range map[string]string{"mod": "module example.org/mod\nreplace example.org/lib => ../lib\n", "lib": "module example.org/lib\n"} { + if err := os.MkdirAll(filepath.Join(tree, d), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(tree, d, "go.mod"), []byte(body), 0o644); err != nil { + t.Fatal(err) + } + } + for name, body := range map[string]string{scopeMark: "lib\nmod", moduleDirMark: "mod"} { + if err := os.WriteFile(filepath.Join(root, name), []byte(body), 0o600); err != nil { + t.Fatal(err) + } + } + dirs, err := m.testDirs(context.Background(), tree) + if err != nil || !reflect.DeepEqual(dirs, []string{"mod"}) { + t.Fatalf("the test directories = %v, %v; want mod alone", dirs, err) + } + if _, err := m.runLevel(context.Background(), tree, levelOpts{workspace: "ws1", level: LevelUnit}); err != nil { + t.Fatal(err) + } + for _, l := range f.lines() { + if strings.Contains(l, "/tree/lib test") { + t.Errorf("the module's unit level tested lib: %s", l) + } + } + // No dir asked: every go.mod of the tree stays. + if err := os.Remove(filepath.Join(root, moduleDirMark)); err != nil { + t.Fatal(err) + } + if dirs, _ = m.testDirs(context.Background(), tree); len(dirs) != 2 { + t.Errorf("a workspace with no module directory tests %v, want every go.mod", dirs) + } +} diff --git a/modules/mesh-build-go/cmd/mesh-build-go/integration.go b/modules/mesh-build-go/cmd/mesh-build-go/integration.go index 86c9d6db5..cfa51ba98 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/integration.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/integration.go @@ -58,7 +58,7 @@ func (m *Module) integrationHere(ctx context.Context, workspace, tree string, pa defer os.RemoveAll(run) began := m.clock() - dirs, err := m.goModDirs(ctx, tree) + dirs, err := m.testDirs(ctx, tree) if err != nil { return nil, err } diff --git a/modules/mesh-build-go/cmd/mesh-build-go/test.go b/modules/mesh-build-go/cmd/mesh-build-go/test.go index 108c73940..e76b9b2c5 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/test.go @@ -256,7 +256,7 @@ type levelOpts struct { // runLevel runs `go test -json` in each go.mod directory of the tree and judges the whole against the bounds. func (m *Module) runLevel(ctx context.Context, tree string, o levelOpts) (*TestResult, error) { - dirs, err := m.goModDirs(ctx, tree) + dirs, err := m.testDirs(ctx, tree) if err != nil { return nil, err }