Skip to content

Commit 57995d6

Browse files
committed
test(integration): Add tests focused on integration into workspaces
Our current test setup is biased towards bzlmod. The aspect integration tests are using WORKSPACE for a single version and all others use bzlmod. The examples integration tests are executed only for a single Bazel version. Due to this, we overlooked until now that our WORKSPACE setup is no longer working for all Bazel versions. These tests should help finding again a working setup. If we achieve again a fully working setup, we will add these new tests to our CI.
1 parent 837855d commit 57995d6

File tree

12 files changed

+129
-1
lines changed

12 files changed

+129
-1
lines changed

test/aspect/execute_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
VERSION_SPECIFIC_ARGS = {
3232
# We test Bazel 6 once with bzlmod and once with legacy WORKSPACE setup. Newer Bazel versions are only tested
33-
# with bzlmod. bzlmod does not work for us before Bazel 6.2.
33+
# with bzlmod. bzlmod does not work for with rules_python before Bazel 6.2.
3434
"--enable_bzlmod=false": CompatibleVersions(minimum="6.0.0", before="6.2.0"),
3535
"--enable_bzlmod=true": CompatibleVersions(minimum="6.2.0", before="7.0.0"),
3636
# Experimental changes we want to be compatible for

test/support/bazel.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import os
12
import subprocess
3+
from copy import deepcopy
24
from pathlib import Path
35
from shutil import which
46

@@ -22,6 +24,24 @@ def get_bazel_binary() -> Path:
2224
raise RuntimeError("No bazelisk binary or bazel symlink towards bazelisk available on your system")
2325

2426

27+
def get_bazel_rolling_version(bazel_bin: Path) -> str:
28+
process = subprocess.run(
29+
[bazel_bin, "--version"],
30+
env=make_bazel_version_env("rolling"),
31+
shell=False,
32+
check=True,
33+
capture_output=True,
34+
text=True,
35+
)
36+
return process.stdout.split("bazel")[1].strip()
37+
38+
39+
def make_bazel_version_env(version: str) -> os._Environ:
40+
run_env = deepcopy(os.environ)
41+
run_env["USE_BAZEL_VERSION"] = version
42+
return run_env
43+
44+
2545
def get_current_workspace(bazel_bin: Path) -> Path:
2646
cmd = [
2747
str(bazel_bin),

test/workspace_integration/.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# The symlinks have no value for us, but clutter the project view in the IDE
2+
build --experimental_convenience_symlinks=clean
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.4.1

test/workspace_integration/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cc_library(
2+
name = "valid_target",
3+
hdrs = ["valid_target.h"],
4+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module(name = "dwyu_workspace_integration_tests")
2+
3+
bazel_dep(name = "depend_on_what_you_use", dev_dependency = True)
4+
local_path_override(
5+
module_name = "depend_on_what_you_use",
6+
path = "../../",
7+
)

test/workspace_integration/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
These tests ensure that DWYU is integrable as advertised in the documentation for all supported Bazel versions.
2+
3+
We explicitly do not add further dependencies to ensure no other modules and their transitive dependencies overwrite the dependencies defined by DWYU.

test/workspace_integration/WORKSPACE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
workspace(name = "dwyu_examples")
2+
3+
local_repository(
4+
name = "depend_on_what_you_use",
5+
path = "../../",
6+
)
7+
8+
load("@depend_on_what_you_use//:setup_step_1.bzl", "setup_step_1")
9+
10+
setup_step_1()
11+
12+
load("@depend_on_what_you_use//:setup_step_2.bzl", "setup_step_2")
13+
14+
setup_step_2()

test/workspace_integration/WORKSPACE.bzlmod

Whitespace-only changes.

test/workspace_integration/aspect.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("@depend_on_what_you_use//:defs.bzl", "dwyu_aspect_factory")
2+
3+
dwyu_aspect = dwyu_aspect_factory()

test/workspace_integration/test.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python3
2+
from __future__ import annotations
3+
4+
import logging
5+
import subprocess
6+
import sys
7+
from os import chdir
8+
from pathlib import Path
9+
from shlex import join as shlex_join
10+
11+
# Allow importing test support code. Relative imports do not work in our case.
12+
# We do this centrally here, so all code we import while executing this knows the extended PYTHONPATH
13+
# ruff: noqa: E402
14+
WORKSPACE_ROOT = Path(__file__).resolve().parent.parent.parent
15+
sys.path.insert(0, str(WORKSPACE_ROOT))
16+
17+
from test.support.bazel import get_bazel_binary, get_bazel_rolling_version, make_bazel_version_env
18+
19+
logging.basicConfig(format="%(message)s", level=logging.INFO)
20+
21+
BAZEL_VERSIONS_UNDER_TEST = [
22+
"6.0.0",
23+
"6.5.0",
24+
"7.0.0",
25+
"7.4.1",
26+
"8.0.0",
27+
"rolling",
28+
]
29+
30+
ASPECT_TEST_CMD = ["--aspects=//:aspect.bzl%dwyu_aspect", "--output_groups=dwyu", "//:valid_target"]
31+
32+
33+
def run_tests(is_bzlmod: bool, bazel_versions: list[str]) -> list[str]:
34+
bzlmod_arg = ["--enable_bzlmod=true"] if is_bzlmod else ["--enable_bzlmod=false"]
35+
workspace_arg = [] if is_bzlmod else ["--enable_workspace=true"]
36+
mode = "bzlmod" if is_bzlmod else "WORKSPACE"
37+
failures = []
38+
39+
logging.info(f"\n##\n## Testing {mode} setup\n##")
40+
for bazel_version in bazel_versions:
41+
logging.info(f"\n## Testing {mode} with Bazel '{bazel_version}'")
42+
43+
env = make_bazel_version_env(bazel_version)
44+
enable_workspace = workspace_arg if bazel_version >= "8.0.0" else []
45+
cmd = ["bazel", "--max_idle_secs=10", "build", *bzlmod_arg, *enable_workspace, *ASPECT_TEST_CMD]
46+
logging.info(f"## Executing: {shlex_join(cmd)}\n")
47+
if subprocess.run(cmd, check=False, env=env).returncode != 0:
48+
failures.append(f"WORKSPACE: {bazel_version}")
49+
50+
return failures
51+
52+
53+
def main() -> int:
54+
bazel_bin = get_bazel_binary()
55+
rolling_bazel = get_bazel_rolling_version(bazel_bin)
56+
bazel_versions = [v if v != "rolling" else rolling_bazel for v in BAZEL_VERSIONS_UNDER_TEST]
57+
58+
failures = run_tests(is_bzlmod=True, bazel_versions=bazel_versions)
59+
failures.extend(run_tests(is_bzlmod=False, bazel_versions=bazel_versions))
60+
61+
if failures:
62+
logging.info("\nSome workspace integration tests FAILED")
63+
logging.info("\n".join(f"- {fail}" for fail in sorted(failures)))
64+
return 1
65+
66+
logging.info("\nAll workspace integration tests succeeded")
67+
return 0
68+
69+
70+
if __name__ == "__main__":
71+
# Ensure we can invoke the script from various places
72+
chdir(Path(__file__).parent)
73+
74+
sys.exit(main())

test/workspace_integration/valid_target.h

Whitespace-only changes.

0 commit comments

Comments
 (0)