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
This commit is contained in:
2026-11-16 11:42:56 +01:00
parent ae78fb039f
commit cdf168706b
5 changed files with 65 additions and 3 deletions
@@ -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
}
@@ -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 {
@@ -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)
}
}
@@ -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
}
@@ -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
}