From 00805727b867d33fd922e63ca82b0d9825ad79fe Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 11 Feb 2021 10:19:42 -0800 Subject: [PATCH] Use a runfiles path when initiating tests. This makes the test setup logic compatible with the sibling repository layout. PiperOrigin-RevId: 357003859 --- .../lib/analysis/test/TestRunnerAction.java | 2 +- .../build/lib/analysis/test/TestStrategy.java | 2 +- src/test/py/bazel/test_wrapper_test.py | 33 +++++++++++-------- src/test/shell/bazel/bazel_test_test.sh | 26 +++++++++++++++ src/test/shell/unittest_utils.sh | 12 +++++++ tools/test/generate-xml.sh | 1 + tools/test/test-setup.sh | 1 + tools/test/windows/tw.cc | 8 +++-- 8 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java b/src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java index 1d1ad7b78404a5..4f72b50f204ebc 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java @@ -584,7 +584,7 @@ public void setupEnvVariables(Map 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. diff --git a/src/main/java/com/google/devtools/build/lib/analysis/test/TestStrategy.java b/src/main/java/com/google/devtools/build/lib/analysis/test/TestStrategy.java index 8c3f20624b6051..30d011475df032 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/test/TestStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/test/TestStrategy.java @@ -186,7 +186,7 @@ public static ImmutableList 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); } diff --git a/src/test/py/bazel/test_wrapper_test.py b/src/test/py/bazel/test_wrapper_test.py index 788b76f767e65e..709010ecfc4a0b 100644 --- a/src/test/py/bazel/test_wrapper_test.py +++ b/src/test/py/bazel/test_wrapper_test.py @@ -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([ diff --git a/src/test/shell/bazel/bazel_test_test.sh b/src/test/shell/bazel/bazel_test_test.sh index 662b27bdc8e44d..b3d9481efa5a87 100755 --- a/src/test/shell/bazel/bazel_test_test.sh +++ b/src/test/shell/bazel/bazel_test_test.sh @@ -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 < 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 " +# genfiles_dir +# testlogs_dir + +testlogs_dir() { + echo $(bazel info bazel-testlogs | sed "s|bazel-out|bazel-out/$1|") +} diff --git a/tools/test/generate-xml.sh b/tools/test/generate-xml.sh index cb292ec32fe1ff..d4a2ee959dc00e 100755 --- a/tools/test/generate-xml.sh +++ b/tools/test/generate-xml.sh @@ -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 diff --git a/tools/test/test-setup.sh b/tools/test/test-setup.sh index 89be46aa5b127d..247056dc1a17e6 100755 --- a/tools/test/test-setup.sh +++ b/tools/test/test-setup.sh @@ -197,6 +197,7 @@ function write_xml_output_file { error_msg="" 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)) diff --git a/tools/test/windows/tw.cc b/tools/test/windows/tw.cc index d0338a2f030572..5cac93e7233e35 100644 --- a/tools/test/windows/tw.cc +++ b/tools/test/windows/tw.cc @@ -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; } @@ -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