Skip to content

Commit ea5c23d

Browse files
authored
Absolutify $(rootpath) env vars (#146)
The default test runner `cd`s into the workspace directory, this ensures that in-repo paths are properly preserved across this cd boundary.
1 parent bfce78d commit ea5c23d

File tree

11 files changed

+95
-2
lines changed

11 files changed

+95
-2
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# This lets us glob() up all the files inside the examples to make them inputs to tests
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, run tools/update_deleted_packages.sh
6-
build --deleted_packages=examples/custom_test_runner,examples/custom_test_runner/Sources/CustomTestRunner,examples/custom_test_runner/Sources/CustomTestRunnerLib,examples/custom_test_runner/Tests/CustomTestRunnerTests,examples/custom_test_runner/integration_tests,examples/custom_test_runner/integration_tests/workspace,examples/simple,examples/simple/mockascript,examples/simple/mockascript/private,examples/simple/tests,tests/params_tests/workspace
7-
query --deleted_packages=examples/custom_test_runner,examples/custom_test_runner/Sources/CustomTestRunner,examples/custom_test_runner/Sources/CustomTestRunnerLib,examples/custom_test_runner/Tests/CustomTestRunnerTests,examples/custom_test_runner/integration_tests,examples/custom_test_runner/integration_tests/workspace,examples/simple,examples/simple/mockascript,examples/simple/mockascript/private,examples/simple/tests,tests/params_tests/workspace
6+
build --deleted_packages=examples/custom_test_runner,examples/custom_test_runner/Sources/CustomTestRunner,examples/custom_test_runner/Sources/CustomTestRunnerLib,examples/custom_test_runner/Tests/CustomTestRunnerTests,examples/custom_test_runner/integration_tests,examples/custom_test_runner/integration_tests/workspace,examples/env_var_with_rootpath/sample_workspace,examples/simple,examples/simple/mockascript,examples/simple/mockascript/private,examples/simple/tests,tests/params_tests/workspace
7+
query --deleted_packages=examples/custom_test_runner,examples/custom_test_runner/Sources/CustomTestRunner,examples/custom_test_runner/Sources/CustomTestRunnerLib,examples/custom_test_runner/Tests/CustomTestRunnerTests,examples/custom_test_runner/integration_tests,examples/custom_test_runner/integration_tests/workspace,examples/env_var_with_rootpath/sample_workspace,examples/simple,examples/simple/mockascript,examples/simple/mockascript/private,examples/simple/tests,tests/params_tests/workspace
88

99
# Import Shared settings
1010
import %workspace%/shared.bazelrc

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ test_suite(
8383
tags = integration_test_utils.DEFAULT_INTEGRATION_TEST_TAGS,
8484
tests = [
8585
"//examples:integration_tests",
86+
"//examples/env_var_with_rootpath:integration_tests",
8687
"//release:archive_test",
8788
"//tests/params_tests:integration_tests",
8889
"//tests/py_tests:integration_tests",

MODULE.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ bazel_binaries = use_extension(
2020
bazel_binaries.download(version_file = "//:.bazelversion")
2121
bazel_binaries.download(version = "7.0.0-pre.20230215.2")
2222
use_repo(bazel_binaries, "bazel_binaries")
23+
24+
download_sample_file = use_extension(
25+
"//examples/env_var_with_rootpath:sample_file_extension.bzl",
26+
"download_sample_file",
27+
dev_dependency = True,
28+
)
29+
30+
use_repo(download_sample_file, "sample_file")

WORKSPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ go_rules_dependencies()
5454
go_register_toolchains(version = "1.19.5")
5555

5656
gazelle_dependencies()
57+
58+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
59+
60+
http_file(
61+
name = "sample_file",
62+
sha256 = "0baf36f9c3ef9d8b4833833e1d633707965c9850f69f04dd96712672b9e75cc0",
63+
url = "https://raw.githubusercontent.com/bazel-contrib/rules_bazel_integration_test/v0.12.0/.bazelversion",
64+
)

bazel_integration_test/private/bazel_integration_test.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ def bazel_integration_test(
168168

169169
env_inherit = env_inherit + additional_env_inherit
170170

171+
env_vars_are_rootpaths = []
172+
for k, v in env.items():
173+
if v.startswith("$(rootpath") and v.index(")") == len(v) - 1:
174+
env_vars_are_rootpaths.append(k)
175+
if env_vars_are_rootpaths:
176+
env["ENV_VARS_TO_ABSOLUTIFY"] = " ".join(env_vars_are_rootpaths)
177+
171178
native.sh_test(
172179
name = name,
173180
srcs = [

bazel_integration_test/private/default_test_runner.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ done
4242
[[ -n "${workspace_dir:-}" ]] || exit_with_msg "Must specify the path of the workspace directory."
4343
[[ ${#bazel_cmds[@]} > 0 ]] || exit_with_msg "No Bazel commands were specified."
4444

45+
for var_name in ${ENV_VARS_TO_ABSOLUTIFY:-}; do
46+
export "${var_name}=$(pwd)/$(printenv "${var_name}")"
47+
done
48+
4549
cd "${workspace_dir}"
4650

4751
for cmd in "${bazel_cmds[@]}" ; do
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load("@bazel_binaries//:defs.bzl", "bazel_binaries")
2+
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")
3+
load(
4+
"@rules_bazel_integration_test//bazel_integration_test:defs.bzl",
5+
"bazel_integration_test",
6+
"default_test_runner",
7+
"integration_test_utils",
8+
)
9+
10+
bzlformat_pkg(name = "bzlformat")
11+
12+
default_test_runner(
13+
name = "simple_test_runner",
14+
)
15+
16+
bazel_integration_test(
17+
name = "simple_bazel_test",
18+
bazel_binaries = bazel_binaries,
19+
bazel_version = bazel_binaries.versions.current,
20+
data = ["@sample_file//file"],
21+
env = {
22+
"SAMPLE_FILE": "$(rootpath @sample_file//file)",
23+
},
24+
test_runner = ":simple_test_runner",
25+
workspace_path = "sample_workspace",
26+
)
27+
28+
# MARK: - Test Suite
29+
30+
test_suite(
31+
name = "integration_tests",
32+
# If you don't apply the test tags to the test suite, the test suite will
33+
# be found when `bazel test //...` is executed.
34+
tags = integration_test_utils.DEFAULT_INTEGRATION_TEST_TAGS,
35+
tests = [":simple_bazel_test"],
36+
visibility = ["//:__subpackages__"],
37+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Implementation for `download_sample_file`, used for testing this repo itself."""
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
4+
5+
def _download_sample_file_impl(_mctx):
6+
http_file(
7+
name = "sample_file",
8+
url = "https://raw.githubusercontent.com/bazel-contrib/rules_bazel_integration_test/v0.12.0/.bazelversion",
9+
sha256 = "0baf36f9c3ef9d8b4833833e1d633707965c9850f69f04dd96712672b9e75cc0",
10+
)
11+
12+
download_sample_file = module_extension(
13+
_download_sample_file_impl,
14+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sh_test(
2+
name = "test",
3+
srcs = ["test.sh"],
4+
env_inherit = ["SAMPLE_FILE"],
5+
)

examples/env_var_with_rootpath/sample_workspace/WORKSPACE.bazel

Whitespace-only changes.

0 commit comments

Comments
 (0)