Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ func TestGenerateAndCompile(t *testing.T) {
// local copy, `go get` will try to fetch the unreleased
// version remotely and some tests will fail.
func TestReplaceStatementsAreComplete(t *testing.T) {
workspaceDir := getWorkspaceDir()
replaceMods := map[string]bool{}

for _, suffix := range replaceModules {
replaceMods[modulePrefix+suffix] = false
}

for _, mod := range replaceModules {
verifyGoMod(t, workspaceDir+mod, replaceMods)
}

var err error
dir := t.TempDir()
cfg := NewDefaultConfig()
Expand Down Expand Up @@ -401,6 +412,14 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
err = GenerateAndCompile(cfg)
require.NoError(t, err)

verifyGoMod(t, dir, replaceMods)

for k, v := range replaceMods {
assert.Truef(t, v, "Module not used: %s", k)
}
}

func verifyGoMod(t *testing.T, dir string, replaceMods map[string]bool) {
gomodpath := path.Join(dir, "go.mod")
// #nosec G304 We control this path and generate the file inside, so we can assume it is safe.
gomod, err := os.ReadFile(gomodpath)
Expand All @@ -409,12 +428,6 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
mod, err := modfile.Parse(gomodpath, gomod, nil)
require.NoError(t, err)

replaceMods := map[string]bool{}

for _, suffix := range replaceModules {
replaceMods[modulePrefix+suffix] = false
}

for _, req := range mod.Require {
if !strings.HasPrefix(req.Mod.Path, modulePrefix) {
continue
Expand All @@ -425,15 +438,6 @@ func TestReplaceStatementsAreComplete(t *testing.T) {

replaceMods[req.Mod.Path] = true
}

// those are modules that should be part of the replaces, but are not components
for _, unused := range []string{"/pdata/testdata"} {
replaceMods[modulePrefix+unused] = true
}

for k, v := range replaceMods {
assert.Truef(t, v, "Module not used: %s", k)
}
}

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

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

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

return replaces
}

func getWorkspaceDir() string {
// This is dependent on the current file structure.
// The goal is find the root of the repo so we can replace the root module.
_, thisFile, _, _ := runtime.Caller(0)
return filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(thisFile)))))
}