Skip to content

Commit

Permalink
Re-write flag values to place generated files in undeclared outputs d…
Browse files Browse the repository at this point in the history
…ir (#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.
  • Loading branch information
amartani authored Aug 7, 2023
1 parent 4a4fa85 commit 5b8b76a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python_pytest/pytest_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@
if os.environ.get("XML_OUTPUT_FILE"):
pytest_args.append("--junitxml={xml_output_file}".format(xml_output_file=os.environ.get("XML_OUTPUT_FILE")))

# Handle plugins that generate reports - if they are provided with relative paths (via args),
# re-write it under bazel's test undeclared outputs dir.
if os.environ.get("TEST_UNDECLARED_OUTPUTS_DIR"):
undeclared_output_dir = os.environ.get("TEST_UNDECLARED_OUTPUTS_DIR")

# Flags that take file paths as value.
path_flags = [
"--report-log", # pytest-reportlog
"--json-report-file", # pytest-json-report
"--html", # pytest-html
]
for i, arg in enumerate(args):
for flag in path_flags:
if arg.startswith(f"{flag}="):
arg_split = arg.split("=", 1)
if len(arg_split) == 2 and not os.path.isabs(arg_split[1]):
args[i] = f"{flag}={undeclared_output_dir}/{arg_split[1]}"

if os.environ.get("TESTBRIDGE_TEST_ONLY"):
# TestClass.test_fn -> TestClass::test_fn
module_name = os.environ.get("TESTBRIDGE_TEST_ONLY").replace(".", "::")
Expand Down

0 comments on commit 5b8b76a

Please sign in to comment.