Skip to content

Commit bb72f49

Browse files
authored
test-unit: run Go unit tests in nested modules (#1469)
go list ./... from the repo root stops at nested module boundaries, so tests in modules with their own go.mod (dev/tools, olm, and the per-example modules) were never run in CI. Discover every tracked go.mod and run gotestsum in each module, writing a per-module junit file; the root module keeps its historical junit filename. Modules with no Go packages (e.g. site/) are skipped.
1 parent 6f601f8 commit bb72f49

1 file changed

Lines changed: 46 additions & 16 deletions

File tree

‎dev/tools/test-unit‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,53 @@ PYTHON_TEST_SUITES = [
120120
]
121121

122122

123+
def find_go_modules(repo_root):
124+
""" returns the repo-relative directories of all Go modules tracked in
125+
the repository: "" for the repo root, plus any nested modules (e.g. the
126+
examples that carry their own go.mod) """
127+
# :(glob) gives ** its explicit meaning: go.mod at any depth, including
128+
# the repo root.
129+
output = subprocess.check_output(
130+
["git", "ls-files", "--", ":(glob)**/go.mod"], cwd=repo_root, text=True)
131+
return sorted(os.path.dirname(path) for path in output.strip().splitlines())
132+
133+
123134
def run_go_tests(repo_root, artifact_dir):
124-
""" runs Go unit tests and returns the exit code """
125-
go_list_cmd = ["go", "list", "./..."]
126-
go_list_output = subprocess.check_output(go_list_cmd, cwd=repo_root, text=True)
127-
packages = go_list_output.strip().split('\n')
128-
129-
filtered_packages = [pkg for pkg in packages if "test/e2e" not in pkg]
130-
131-
result = subprocess.run(utils.go_tool_args(
132-
"gotestsum",
133-
f"--junitfile={os.path.join(artifact_dir, 'junit_unit-go.xml')}",
134-
"--",
135-
"-race",
136-
*filtered_packages
137-
), cwd=repo_root)
138-
139-
return result.returncode
135+
""" runs Go unit tests in every Go module and returns the exit code """
136+
returncode = 0
137+
138+
for module_rel in find_go_modules(repo_root):
139+
module_dir = os.path.join(repo_root, module_rel) if module_rel else repo_root
140+
141+
go_list_output = subprocess.check_output(
142+
["go", "list", "./..."], cwd=module_dir, text=True)
143+
packages = [
144+
pkg for pkg in go_list_output.strip().split('\n')
145+
if pkg and "test/e2e" not in pkg
146+
]
147+
if not packages:
148+
# e.g. site/ is a module only to pin the docs theme.
149+
print(f"No Go packages in module {module_rel or '.'}; skipping")
150+
continue
151+
152+
# Keep the historical junit filename for the root module.
153+
junit_name = "junit_unit-go.xml"
154+
if module_rel:
155+
junit_name = f"junit_unit-go-{module_rel.replace(os.sep, '-')}.xml"
156+
157+
print(f"\n=== Running Go unit tests: module {module_rel or '(repo root)'} ===")
158+
result = subprocess.run(utils.go_tool_args(
159+
"gotestsum",
160+
f"--junitfile={os.path.join(artifact_dir, junit_name)}",
161+
"--",
162+
"-race",
163+
*packages
164+
), cwd=module_dir)
165+
166+
if result.returncode != 0:
167+
returncode = result.returncode
168+
169+
return returncode
140170

141171

142172
def run_python_tests(repo_root, artifact_dir):

0 commit comments

Comments
 (0)