Skip to content

Commit

Permalink
Use a runfiles path when initiating tests. This makes the test setup …
Browse files Browse the repository at this point in the history
…logic compatible with the sibling repository layout.

PiperOrigin-RevId: 357003859
  • Loading branch information
Googler authored and copybara-github committed Feb 11, 2021
1 parent caf1355 commit 0080572
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ public void setupEnvVariables(Map<String, String> env, Duration timeout) {
env.put("TEST_WORKSPACE", getRunfilesPrefix());
env.put(
"TEST_BINARY",
getExecutionSettings().getExecutable().getRootRelativePath().getCallablePathString());
getExecutionSettings().getExecutable().getRunfilesPath().getCallablePathString());

// When we run test multiple times, set different TEST_RANDOM_SEED values for each run.
// Don't override any previous setting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static ImmutableList<String> expandedArgsFromAction(TestRunnerAction test
}

// Execute the test using the alias in the runfiles tree, as mandated by the Test Encyclopedia.
args.add(execSettings.getExecutable().getRootRelativePath().getCallablePathString());
args.add(execSettings.getExecutable().getRunfilesPath().getCallablePathString());
Iterables.addAll(args, execSettings.getArgs().arguments());
return ImmutableList.copyOf(args);
}
Expand Down
33 changes: 20 additions & 13 deletions src/test/py/bazel/test_wrapper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,19 +656,26 @@ def testRunningTestFromExternalRepo(self):
self.ScratchFile('a/x.py')

for flag in ['--legacy_external_runfiles', '--nolegacy_external_runfiles']:
for target in ['//:x', '@a//:x']:
exit_code, _, stderr = self.RunBazel([
'test',
'-t-',
'--shell_executable=',
'--test_output=errors',
'--verbose_failures',
flag,
target,
])
self.AssertExitCode(
exit_code, 0,
['flag=%s' % flag, 'target=%s' % target] + stderr)
for layout in [
'--experimental_sibling_repository_layout',
'--noexperimental_sibling_repository_layout',
]:
for target in ['//:x', '@a//:x']:
exit_code, _, stderr = self.RunBazel([
'test',
'-t-',
'--shell_executable=',
'--test_output=errors',
'--verbose_failures',
flag,
layout,
target,
])
self.AssertExitCode(exit_code, 0, [
'flag=%s' % flag,
'layout=%s' % layout,
'target=%s' % target,
] + stderr)

def _AssertAddCurrentDirectoryToPathTest(self, flags):
exit_code, _, stderr = self.RunBazel([
Expand Down
26 changes: 26 additions & 0 deletions src/test/shell/bazel/bazel_test_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,30 @@ EOF
expect_log "cannot run local tests with --nobuild_runfile_manifests"
}

function test_run_from_external_repo_sibling_repository_layout() {
cat <<EOF > WORKSPACE
local_repository(
name = "a",
path = "./a",
)
EOF

mkdir -p a
touch a/WORKSPACE
cat <<'EOF' > a/BUILD
py_test(
name = 'x',
srcs = ['x.py'],
)
EOF
touch a/x.py

bazel test --experimental_sibling_repository_layout @a//:x &> $TEST_log \
|| fail "expected success"

cp $(testlogs_dir a)/x/test.xml $TEST_log
expect_log "<testsuite name=\"a/x\""
expect_log "<testcase name=\"a/x\""
}

run_suite "bazel test tests"
12 changes: 12 additions & 0 deletions src/test/shell/unittest_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,15 @@ capture_test_stderr () {
if [[ -z "${XML_OUTPUT_FILE:-}" ]]; then
XML_OUTPUT_FILE=${TEST_TMPDIR}/ouput.xml
fi

# Functions to provide easy access to external repository outputs in the sibling
# repository layout.
#
# Usage:
# bin_dir <repository name>
# genfiles_dir <repository name>
# testlogs_dir <repository name>

testlogs_dir() {
echo $(bazel info bazel-testlogs | sed "s|bazel-out|bazel-out/$1|")
}
1 change: 1 addition & 0 deletions tools/test/generate-xml.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ if [ "$TEST_LOG" == "-" ]; then
fi

test_name="${TEST_BINARY#./}"
test_name="${TEST_BINARY#../}"
errors=0
error_msg=""
if (( $EXIT_CODE != 0 )); then
Expand Down
1 change: 1 addition & 0 deletions tools/test/test-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ function write_xml_output_file {
error_msg="<error message=\"exited with error code $exitCode\"></error>"
fi
test_name="${TEST_BINARY#./}"
test_name="${TEST_BINARY#../}"
# Ensure that test shards have unique names in the xml output.
if [[ -n "${TEST_TOTAL_SHARDS+x}" ]] && ((TEST_TOTAL_SHARDS != 0)); then
((shard_num=TEST_SHARD_INDEX+1))
Expand Down
8 changes: 6 additions & 2 deletions tools/test/windows/tw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1118,8 +1118,9 @@ inline void ComputeRunfilePath(const std::wstring& test_workspace,
if (s->size() >= 2 && (*s)[0] == L'.' && (*s)[1] == L'/') {
s->erase(0, 2);
}
if (s->find(L"external/") == 0) {
s->erase(0, 9);
// Runfiles paths of external tests start with "../".
if (s->find(L"../") == 0) {
s->erase(0, 3);
} else {
*s = test_workspace + L"/" + *s;
}
Expand Down Expand Up @@ -1569,6 +1570,9 @@ bool GetTestName(std::wstring* result) {
}
if (result->size() >= 2 && (*result)[0] == '.' && (*result)[1] == '/') {
result->erase(0, 2);
} else if (result->size() >= 3 && (*result)[0] == '.' &&
(*result)[1] == '.' && (*result)[2] == '/') {
result->erase(0, 3);
}

// Ensure that test shards have unique names in the xml output, by including
Expand Down

0 comments on commit 0080572

Please sign in to comment.