Skip to content

Commit 51bfa46

Browse files
evan-bradleyandrzej-stencel
authored andcommitted
[chore][cmd/builder] Improve missing replace statements test (open-telemetry#10196)
Updates `TestReplaceStatementsAreComplete` to check the modules used in replace statements to ensure that their dependencies also have replace statements. This will catch the error that happened in open-telemetry#10188 before a release is started. The one caveat here is that the test may need to be run multiple times if there are modules deep in the dependency tree that haven't been added to the list of replace statement modules. In essence, the user has to do a BFS walk of the dependency tree themselves by running the tests until all missing modules are caught. We could automate this process with additional code to report all missing modules at once regardless of depth, but I figure it's not worth the extra complexity in the test for such a small gain. #### Testing I tested this on the open-telemetry#10188 branch by removing the `pdata/testdata` module from the replace statements list and seeing that the failure is easier to understand: ``` --- FAIL: TestReplaceStatementsAreComplete (0.60s) Error: Should be true Test: TestReplaceStatementsAreComplete Messages: Module missing from replace statements list: go.opentelemetry.io/collector/pdata/testdata ```
1 parent 6dc9d14 commit 51bfa46

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

cmd/builder/internal/builder/main_test.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ func TestGenerateAndCompile(t *testing.T) {
341341
// local copy, `go get` will try to fetch the unreleased
342342
// version remotely and some tests will fail.
343343
func TestReplaceStatementsAreComplete(t *testing.T) {
344+
workspaceDir := getWorkspaceDir()
345+
replaceMods := map[string]bool{}
346+
347+
for _, suffix := range replaceModules {
348+
replaceMods[modulePrefix+suffix] = false
349+
}
350+
351+
for _, mod := range replaceModules {
352+
verifyGoMod(t, workspaceDir+mod, replaceMods)
353+
}
354+
344355
var err error
345356
dir := t.TempDir()
346357
cfg := NewDefaultConfig()
@@ -401,6 +412,14 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
401412
err = GenerateAndCompile(cfg)
402413
require.NoError(t, err)
403414

415+
verifyGoMod(t, dir, replaceMods)
416+
417+
for k, v := range replaceMods {
418+
assert.Truef(t, v, "Module not used: %s", k)
419+
}
420+
}
421+
422+
func verifyGoMod(t *testing.T, dir string, replaceMods map[string]bool) {
404423
gomodpath := path.Join(dir, "go.mod")
405424
// #nosec G304 We control this path and generate the file inside, so we can assume it is safe.
406425
gomod, err := os.ReadFile(gomodpath)
@@ -409,12 +428,6 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
409428
mod, err := modfile.Parse(gomodpath, gomod, nil)
410429
require.NoError(t, err)
411430

412-
replaceMods := map[string]bool{}
413-
414-
for _, suffix := range replaceModules {
415-
replaceMods[modulePrefix+suffix] = false
416-
}
417-
418431
for _, req := range mod.Require {
419432
if !strings.HasPrefix(req.Mod.Path, modulePrefix) {
420433
continue
@@ -425,15 +438,6 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
425438

426439
replaceMods[req.Mod.Path] = true
427440
}
428-
429-
// those are modules that should be part of the replaces, but are not components
430-
for _, unused := range []string{"/pdata/testdata"} {
431-
replaceMods[modulePrefix+unused] = true
432-
}
433-
434-
for k, v := range replaceMods {
435-
assert.Truef(t, v, "Module not used: %s", k)
436-
}
437441
}
438442

439443
func makeModule(dir string, fileContents []byte) error {
@@ -454,10 +458,7 @@ func makeModule(dir string, fileContents []byte) error {
454458
}
455459

456460
func generateReplaces() []string {
457-
// This test is dependent on the current file structure.
458-
// The goal is find the root of the repo so we can replace the root module.
459-
_, thisFile, _, _ := runtime.Caller(0)
460-
workspaceDir := filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(thisFile)))))
461+
workspaceDir := getWorkspaceDir()
461462
modules := replaceModules
462463
replaces := make([]string, len(modules))
463464

@@ -467,3 +468,10 @@ func generateReplaces() []string {
467468

468469
return replaces
469470
}
471+
472+
func getWorkspaceDir() string {
473+
// This is dependent on the current file structure.
474+
// The goal is find the root of the repo so we can replace the root module.
475+
_, thisFile, _, _ := runtime.Caller(0)
476+
return filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(thisFile)))))
477+
}

0 commit comments

Comments
 (0)