Skip to content

Commit 6fcafb7

Browse files
authored
test: Return root module root dir when using go.work (#1125)
**What problem does this PR solve?**: Fixes the test helper when `go.work` is used locally. Previously, the helper returned all the output of `go list -m`, and when using go.work, the output contains multiple lines, with the directory of the root module on the first line. Now, the helper returns only the first line. The previous behavior caused the test to fail: ``` === Failed === FAIL: pkg/handlers/generic/lifecycle/ccm/nutanix Test_templateValues (0.00s) handler_test.go:71: Error Trace: /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/ccm/nutanix/handler_test.go:193 /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/ccm/nutanix/handler_test.go:71 Error: Received unexpected error: open /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix/api /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix/common /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix/docs /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix/hack/third-party/caaph /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix/hack/third-party/capa /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix/hack/third-party/capx /home/dlipovetsky/projects/cluster-api-runtime-extensions-nutanix/hack/tools/charts/cluster-api-runtime-extensions-nutanix/addons/ccm/nutanix/values-template.yaml: no such file or directory Test: Test_templateValues DONE 446 tests, 1 failure in 35.288s make: *** [make/go.mk:49: test.root] Error 1 ``` **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> The test passes. My `go.work`: ``` go 1.24.2 use ( . ./api ./common ./docs ./hack/third-party/caaph ./hack/third-party/capa ./hack/third-party/capx ./hack/tools ) ``` **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. -->
1 parent f9c2f95 commit 6fcafb7

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

pkg/handlers/generic/lifecycle/ccm/nutanix/handler_test.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,21 @@ secretName: nutanix-ccm-credentials
5555
`
5656
)
5757

58-
var valuesTemplateFile = filepath.Join(
59-
moduleRootDir(),
60-
"charts",
61-
"cluster-api-runtime-extensions-nutanix",
62-
"addons",
63-
"ccm",
64-
"nutanix",
65-
"values-template.yaml",
66-
)
58+
var valuesTemplateFile = func() string {
59+
dir, err := moduleRootDir()
60+
if err != nil {
61+
panic(err)
62+
}
63+
return filepath.Join(
64+
dir,
65+
"charts",
66+
"cluster-api-runtime-extensions-nutanix",
67+
"addons",
68+
"ccm",
69+
"nutanix",
70+
"values-template.yaml",
71+
)
72+
}()
6773

6874
func Test_templateValues(t *testing.T) {
6975
t.Parallel()
@@ -194,20 +200,21 @@ func readCCMValuesTemplateFromProjectHelmChart(t *testing.T) string {
194200
return string(bs)
195201
}
196202

197-
func moduleRootDir() string {
203+
func moduleRootDir() (string, error) {
198204
cmd := exec.Command("go", "list", "-m", "-f", "{{ .Dir }}")
199205
out, err := cmd.CombinedOutput()
200-
if err != nil {
206+
if err != nil || len(out) == 0 {
201207
// We include the combined output because the error is usually
202208
// an exit code, which does not explain why the command failed.
203-
panic(
204-
fmt.Sprintf("cmd.Dir=%q, cmd.Env=%q, cmd.Args=%q, err=%q, output=%q",
205-
cmd.Dir,
206-
cmd.Env,
207-
cmd.Args,
208-
err,
209-
out),
210-
)
209+
return "", fmt.Errorf("cmd.Dir=%q, cmd.Env=%q, cmd.Args=%q, err=%q, output=%q",
210+
cmd.Dir,
211+
cmd.Env,
212+
cmd.Args,
213+
err,
214+
out)
211215
}
212-
return strings.TrimSpace(string(out))
216+
// The first line is the module root directory. When go workspaces are used,
217+
// the first line is the "root" module root directory.
218+
dir, _, _ := strings.Cut(string(out), "\n")
219+
return dir, nil
213220
}

0 commit comments

Comments
 (0)