From 32072877ae0161ac82f185e0b57f6cdf93e08cc6 Mon Sep 17 00:00:00 2001 From: Jordan Bonser Date: Mon, 21 Jul 2025 15:04:46 +0100 Subject: [PATCH] feat: adds support for TEST_UNDECLARED_OUTPUT_DIR the runner now supports the bazel environment variable and ensures copying back the folder so that bazel can expose them in the `bazel-testlogs` --- bazel/labgrid/driver/localhost.py | 2 +- bazel/labgrid/runner/runner.py | 8 ++++++++ e2e/.bazelrc | 3 +++ e2e/.bazelrc.ci | 3 +++ e2e/test/BUILD.bazel | 12 ++++++++++++ e2e/test/undeclared_outputs_dir.py | 13 +++++++++++++ 6 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 e2e/test/undeclared_outputs_dir.py diff --git a/bazel/labgrid/driver/localhost.py b/bazel/labgrid/driver/localhost.py index e97a307f..4330acdd 100644 --- a/bazel/labgrid/driver/localhost.py +++ b/bazel/labgrid/driver/localhost.py @@ -55,6 +55,6 @@ class LocalhostDriver(CommandMixin, Driver, CommandProtocol, FileTransferProtoco if not path.exists(): raise ExecutionError("File {} doesn't exist".format(path)) if path.is_dir(): - copytree(source, destination) + copytree(source, destination, dirs_exist_ok=True) else: copy(source, destination) diff --git a/bazel/labgrid/runner/runner.py b/bazel/labgrid/runner/runner.py index d3789e53..3ea5ed18 100644 --- a/bazel/labgrid/runner/runner.py +++ b/bazel/labgrid/runner/runner.py @@ -68,6 +68,7 @@ def runner(): class Runner: _xml_output_file = "results.xml" _test_shard_status_file = "test.shard" + _test_outputs_dir = "test.outputs" def __init__(self, transfer, shell): self._transfer = transfer @@ -131,6 +132,11 @@ class Runner: self._exec_root = self._read_path(mktemp.cmd("-d", "--suffix=-EXECROOT")) + # Location on remote to support the Bazel test spec for `TEST_UNDECLARED_OUTPUTS_DIR`. + self._run( + f"{self._tools.mkdir(self._exec_root / PurePath(self._test_outputs_dir))}" + ) + self._paths_to_remove_on_exit.extend( [self._exec_root, mktemp.remote, tools_root] ) @@ -193,6 +199,7 @@ class Runner: env = { "XML_OUTPUT_FILE": Runner._xml_output_file, "TEST_SHARD_STATUS_FILE": Runner._test_shard_status_file, + "TEST_UNDECLARED_OUTPUTS_DIR": Runner._test_outputs_dir, } return {k: environ.get(k) for k in inherit_env if k in environ} | { @@ -204,6 +211,7 @@ class Runner: get = { "XML_OUTPUT_FILE": Runner._xml_output_file, "TEST_SHARD_STATUS_FILE": Runner._test_shard_status_file, + "TEST_UNDECLARED_OUTPUTS_DIR": Runner._test_outputs_dir, } return [ diff --git a/e2e/.bazelrc b/e2e/.bazelrc index 7c8908fb..69717571 100644 --- a/e2e/.bazelrc +++ b/e2e/.bazelrc @@ -26,5 +26,8 @@ common --@rules_python//python/config_settings:venvs_use_declare_symlink=no # Enable built Zstandard toolchain common --extra_toolchains=@rules_zstd//zstd/toolchain/zstd:built +# Do not zip the test outputs dir +test --nozip_undeclared_test_outputs + # User-specific .bazelrc try-import %workspace%/.bazelrc.user diff --git a/e2e/.bazelrc.ci b/e2e/.bazelrc.ci index f8ae2e0c..8dfe7c6a 100644 --- a/e2e/.bazelrc.ci +++ b/e2e/.bazelrc.ci @@ -32,6 +32,9 @@ test --test_verbose_timeout_warnings # Validate that the lockfile is correct common --lockfile_mode=${CI_LOCKFILE_MODE} +# Do not zip the test outputs dir +test --nozip_undeclared_test_outputs + # These locations are cached on the CI build:local --disk_cache=${CI_PROJECT_DIR}/.cache/bazel/disk build:local --repository_cache=${CI_PROJECT_DIR}/.cache/bazel/repo diff --git a/e2e/test/BUILD.bazel b/e2e/test/BUILD.bazel index 2ae4267d..1ff1f4c4 100644 --- a/e2e/test/BUILD.bazel +++ b/e2e/test/BUILD.bazel @@ -164,3 +164,15 @@ labgrid_test( src = ":inner-sharding", shard_count = 2, ) + +py_binary( + name = "inner-undeclared-outputs-dir", + srcs = ["undeclared_outputs_dir.py"], + main = "undeclared_outputs_dir.py", +) + +labgrid_test( + name = "undeclared-outputs-dir", + size = "small", + src = ":inner-undeclared-outputs-dir", +) diff --git a/e2e/test/undeclared_outputs_dir.py b/e2e/test/undeclared_outputs_dir.py new file mode 100644 index 00000000..49a29e95 --- /dev/null +++ b/e2e/test/undeclared_outputs_dir.py @@ -0,0 +1,13 @@ +from os import environ +from pathlib import Path + +output_dir = environ.get("TEST_UNDECLARED_OUTPUTS_DIR") +assert output_dir is not None + +# Create file in root of the directory. +Path(f"{output_dir}/file.txt").write_text("some_text") + +# Create a file in a subdirectory, to test recursive copying. +subdir_file = Path(f"{output_dir}/subdir/other_file.txt") +subdir_file.parent.mkdir(parents=True) +subdir_file.write_text("some_other_text") -- GitLab