Skip to content

Commit 5b8b76a

Browse files
authored
Re-write flag values to place generated files in undeclared outputs dir (#7)
Some common pytest plugins, such as pytest-reportlog, pytest-json-report and pytest-html, allow the user to generate reports, accepting a file path as a flag. In Bazel, these generated files can only be accessed if placed inside the test undeclared outputs dir. However, bazel doesn't provide any way to expand environment variable values provided in args. This commit rewrites flag values for the plugins mentioned above, prepend the test undeclared output dir folder to the value of the corresponding flags.
1 parent 4a4fa85 commit 5b8b76a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

python_pytest/pytest_shim.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@
1616
if os.environ.get("XML_OUTPUT_FILE"):
1717
pytest_args.append("--junitxml={xml_output_file}".format(xml_output_file=os.environ.get("XML_OUTPUT_FILE")))
1818

19+
# Handle plugins that generate reports - if they are provided with relative paths (via args),
20+
# re-write it under bazel's test undeclared outputs dir.
21+
if os.environ.get("TEST_UNDECLARED_OUTPUTS_DIR"):
22+
undeclared_output_dir = os.environ.get("TEST_UNDECLARED_OUTPUTS_DIR")
23+
24+
# Flags that take file paths as value.
25+
path_flags = [
26+
"--report-log", # pytest-reportlog
27+
"--json-report-file", # pytest-json-report
28+
"--html", # pytest-html
29+
]
30+
for i, arg in enumerate(args):
31+
for flag in path_flags:
32+
if arg.startswith(f"{flag}="):
33+
arg_split = arg.split("=", 1)
34+
if len(arg_split) == 2 and not os.path.isabs(arg_split[1]):
35+
args[i] = f"{flag}={undeclared_output_dir}/{arg_split[1]}"
36+
1937
if os.environ.get("TESTBRIDGE_TEST_ONLY"):
2038
# TestClass.test_fn -> TestClass::test_fn
2139
module_name = os.environ.get("TESTBRIDGE_TEST_ONLY").replace(".", "::")

0 commit comments

Comments
 (0)