Skip to content

Commit b9ee523

Browse files
committed
PSMDB-1880 Automatically set RUNFILES_DIR for unittests executed via resmoke
So far we have single test affected by this change: extensions_api_test With this change we can succefully run it this way: buildscripts/resmoke.py run --suite unittests bazel-bin/install/bin/extensions_api_test
1 parent fa5cca4 commit b9ee523

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

buildscripts/resmokelib/testing/testcases/cpp_unittest.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""The unittest.TestCase for C++ unit tests."""
22

3+
import os
34
from typing import Optional
45

56
from buildscripts.resmokelib import core, logging, utils
@@ -26,6 +27,44 @@ def __init__(
2627
self.program_options = utils.default_if_none(program_options, {}).copy()
2728

2829
interface.append_process_tracking_options(self.program_options, self._id)
30+
31+
# Automatically set RUNFILES_DIR for Bazel tests if not already set
32+
self._set_runfiles_dir_if_needed()
33+
34+
def _set_runfiles_dir_if_needed(self):
35+
"""Set RUNFILES_DIR environment variable for Bazel tests if needed."""
36+
# Check if RUNFILES_DIR is already set in program_options
37+
env_vars = self.program_options.get("env_vars", {})
38+
if "RUNFILES_DIR" in env_vars:
39+
return # Already set, don't override
40+
41+
# Check if this looks like a Bazel binary (in bazel-bin directory)
42+
executable_path = os.path.abspath(self.program_executable)
43+
if "bazel-bin" not in os.path.normpath(executable_path).split(os.sep):
44+
return # Not a Bazel binary, skip
45+
46+
# Check if executable path is a symlink and resolve it into resolved_path
47+
resolved_path = executable_path
48+
if os.path.islink(executable_path):
49+
resolved_path = os.path.realpath(executable_path)
50+
51+
# Check if .runfiles directory exists next to the binary but take binary name from the symlink name
52+
# Take base dir from resolved_path
53+
base_dir = os.path.dirname(resolved_path)
54+
# Take binary name from executable_path
55+
binary_name = os.path.basename(executable_path)
56+
57+
runfiles_dir = os.path.join(base_dir, binary_name + ".runfiles")
58+
if os.path.isdir(runfiles_dir):
59+
# Set RUNFILES_DIR in env_vars
60+
if "env_vars" not in self.program_options:
61+
self.program_options["env_vars"] = {}
62+
self.program_options["env_vars"]["RUNFILES_DIR"] = runfiles_dir
63+
self.logger.debug(
64+
"Automatically set RUNFILES_DIR=%s for Bazel test %s",
65+
runfiles_dir,
66+
executable_path,
67+
)
2968

3069
def _make_process(self):
3170
return core.programs.make_process(

0 commit comments

Comments
 (0)