Skip to content

Commit b09335b

Browse files
meteorcloudycopybara-github
authored andcommitted
Fix java tests for --legacy_external_runfiles
Migrate Bazel for #23574 PiperOrigin-RevId: 704687334 Change-Id: I47fe34775959d1b4ab4e6d931ddc6df256f6e59a
1 parent 5c7ada4 commit b09335b

File tree

7 files changed

+95
-22
lines changed

7 files changed

+95
-22
lines changed

src/java_tools/junitrunner/javatests/com/google/testing/junit/runner/BUILD

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ sh_test(
132132
"@bazel_tools//tools/jdk:current_java_runtime",
133133
],
134134
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
135+
deps = [
136+
"@bazel_tools//tools/bash/runfiles",
137+
],
135138
)
136139

137140
java_binary(
@@ -159,6 +162,9 @@ sh_test(
159162
"@bazel_tools//tools/jdk:current_java_runtime",
160163
],
161164
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
165+
deps = [
166+
"@bazel_tools//tools/bash/runfiles",
167+
],
162168
)
163169

164170
exports_files([

src/java_tools/junitrunner/javatests/com/google/testing/junit/runner/deploy_jar_integration_tests.sh

+21-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,33 @@
2424

2525
[ -z "$TEST_SRCDIR" ] && { echo "TEST_SRCDIR not set!" >&2; exit 1; }
2626

27+
# --- begin runfiles.bash initialization v3 ---
28+
# Copy-pasted from the Bazel Bash runfiles library v3.
29+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
30+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
31+
# shellcheck disable=SC1090
32+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
33+
source "$0.runfiles/$f" 2>/dev/null || \
34+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
35+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
36+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
37+
# --- end runfiles.bash initialization v3 ---
38+
2739
# Load the unit-testing framework
2840
source "$1" || \
2941
{ echo "Failed to load unit-testing framework $1" >&2; exit 1; }
3042

3143
set +o errexit
3244

3345
unset TEST_PREMATURE_EXIT_FILE
34-
JAVA_HOME="$2"
46+
JAVABASE="$2"
47+
if [[ "$TEST_WORKSPACE" == "_main" ]]; then
48+
# For Bazel
49+
RUNFILES_JAVABASE=${JAVABASE#external/}
50+
else
51+
# For Blaze
52+
RUNFILES_JAVABASE=${TEST_WORKSPACE}/${JAVABASE}
53+
fi
3554
TESTBED_JAR="${PWD}/$3"
3655

3756
shift 3
@@ -41,7 +60,7 @@ shift 3
4160
# Test that we see a warning about missing the test suite Java system property
4261
function test_Warning() {
4362
test_pid=""
44-
${JAVA_HOME}/bin/java -jar $TESTBED_JAR >& $TEST_log && test_pid=$!
63+
$(rlocation $RUNFILES_JAVABASE/bin/java) -jar $TESTBED_JAR >& $TEST_log && test_pid=$!
4564

4665
expect_log "The test suite Java system property .* is required but missing"
4766

src/java_tools/junitrunner/javatests/com/google/testing/junit/runner/system_exit_detecting_test.sh

+22-3
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,38 @@
2323
source "$1" || \
2424
{ echo "Failed to load unit-testing framework $1" >&2; exit 1; }
2525

26+
# --- begin runfiles.bash initialization v3 ---
27+
# Copy-pasted from the Bazel Bash runfiles library v3.
28+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
29+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
30+
# shellcheck disable=SC1090
31+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
32+
source "$0.runfiles/$f" 2>/dev/null || \
33+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
34+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
35+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
36+
# --- end runfiles.bash initialization v3 ---
37+
2638
set +o errexit
2739

2840
PROGRAM_THAT_CALLS_SYSTEM_EXIT_JAR="$2"
2941
readonly PROGRAM_THAT_CALLS_SYSTEM_EXIT_JAR
30-
JAVA_HOME="$3"
31-
readonly JAVA_HOME
42+
JAVABASE="$3"
43+
if [[ "$TEST_WORKSPACE" == "_main" ]]; then
44+
# For Bazel
45+
RUNFILES_JAVABASE=${JAVABASE#external/}
46+
else
47+
# For Blaze
48+
RUNFILES_JAVABASE=${TEST_WORKSPACE}/${JAVABASE}
49+
fi
3250
EXPECTED_STACK_FILE="$4"
3351
readonly EXPECTED_STACK_FILE
3452

3553
function test_prints_stack_trace_on_system_exit() {
3654
local output_file="${TEST_TMPDIR}/output.txt"
3755

38-
"${JAVA_HOME}/bin/java" -jar "${PROGRAM_THAT_CALLS_SYSTEM_EXIT_JAR}" \
56+
JAVA=$(rlocation "${RUNFILES_JAVABASE}/bin/java")
57+
"${JAVA}" -jar "${PROGRAM_THAT_CALLS_SYSTEM_EXIT_JAR}" \
3958
2> "${output_file}"
4059
assert_equals 121 $?
4160

src/test/shell/integration/java_integration_test.sh

+12-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ source "${CURRENT_DIR}/java_integration_test_utils.sh" \
2828
|| { echo "java_integration_test_utils.sh not found!" >&2; exit 1; }
2929
set -eu
3030

31-
declare -r runfiles_relative_javabase="$1"
31+
JAVABASE="$1"
32+
if [[ "$TEST_WORKSPACE" == "_main" ]]; then
33+
# For Bazel
34+
RUNFILES_JAVABASE=${JAVABASE#external/}
35+
RUNFILES_JAVABASE="$(dirname $(dirname $(rlocation $RUNFILES_JAVABASE/bin/java)))"
36+
else
37+
# For Blaze
38+
RUNFILES_JAVABASE=${BAZEL_RUNFILES}/${JAVABASE}
39+
fi
40+
3241
add_to_bazelrc "build --package_path=%workspace%"
3342

3443
function set_up() {
@@ -39,7 +48,7 @@ function set_up() {
3948

4049
function setup_local_jdk() {
4150
local -r dest="$1"
42-
local -r src="${BAZEL_RUNFILES}/${runfiles_relative_javabase}"
51+
local -r src="${RUNFILES_JAVABASE}"
4352

4453
mkdir -p "$dest" || fail "mkdir -p $dest"
4554
cp -LR "${src}"/* "$dest" || fail "cp -LR \"${src}\"/* \"$dest\""
@@ -260,7 +269,7 @@ function assert_singlejar_works() {
260269
ln -s "my_jdk" "$pkg/my_jdk.symlink"
261270
local -r javabase="$(get_real_path "$pkg/my_jdk.symlink")"
262271
else
263-
local -r javabase="${BAZEL_RUNFILES}/${runfiles_relative_javabase}"
272+
local -r javabase="${RUNFILES_JAVABASE}"
264273
fi
265274

266275
mkdir -p "$pkg/jvm"

src/tools/singlejar/BUILD

+7-3
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ cc_test(
287287
"output_jar_simple_test.cc",
288288
],
289289
copts = select({
290-
"//src/conditions:windows": ["-DJAR_TOOL_PATH=\\\"io_bazel/$(JAVABASE)/bin/jar.exe\\\""],
291-
"//conditions:default": ["-DJAR_TOOL_PATH=\\\"io_bazel/$(JAVABASE)/bin/jar\\\""],
290+
"//src/conditions:windows": ["-DJAR_TOOL_PATH=\\\"local_jdk/bin/jar.exe\\\""],
291+
"//conditions:default": ["-DJAR_TOOL_PATH=\\\"local_jdk/bin/jar\\\""],
292292
}),
293293
data = [
294294
":data1",
@@ -297,7 +297,11 @@ cc_test(
297297
":test1",
298298
":test2",
299299
"@rules_java//toolchains:current_java_runtime",
300-
],
300+
] + select({
301+
# TODO: Use the current java runtime instead of local_jdk
302+
"//src/conditions:windows": ["@local_jdk//:bin/jar.exe"],
303+
"//conditions:default": ["@local_jdk//:bin/jar"],
304+
}),
301305
toolchains = ["@rules_java//toolchains:current_java_runtime"],
302306
deps = [
303307
":input_jar",

third_party/ijar/test/BUILD

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ sh_test(
99
size = "enormous",
1010
srcs = ["ijar_test.sh"],
1111
args = [
12-
"$(JAVABASE)/bin/javac",
13-
"$(JAVABASE)/bin/java",
14-
"$(JAVABASE)/bin/jar",
15-
"$(JAVABASE)/bin/javap",
12+
"$(JAVABASE)",
1613
"$(location //third_party/ijar)",
1714
# We assume unzip and zip to be on the path
1815
"unzip",
@@ -46,6 +43,7 @@ sh_test(
4643
# wrong.
4744
"libwrongcentraldir.jar",
4845
":dynamic_constant.jar",
46+
"@bazel_tools//tools/bash/runfiles",
4947
"@bazel_tools//tools/jdk:current_java_runtime",
5048
],
5149
shard_count = 5,

third_party/ijar/test/ijar_test.sh

+25-7
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,29 @@
1515
# TODO(bazel-team) test that modifying the source in a non-interface
1616
# changing way results in the same -interface.jar.
1717

18+
# --- begin runfiles.bash initialization v3 ---
19+
# Copy-pasted from the Bazel Bash runfiles library v3.
20+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
21+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
22+
# shellcheck disable=SC1090
23+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
24+
source "$0.runfiles/$f" 2>/dev/null || \
25+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
26+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
27+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
28+
# --- end runfiles.bash initialization v3 ---
29+
1830
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
1931

2032
## Inputs
21-
JAVAC=$1
22-
shift
23-
JAVA=$1
24-
shift
25-
JAR=$1
26-
shift
27-
JAVAP=$1
33+
JAVABASE="$1"
34+
if [[ "$TEST_WORKSPACE" == "_main" ]]; then
35+
# For Bazel
36+
RUNFILES_JAVABASE=${JAVABASE#external/}
37+
else
38+
# For Blaze
39+
RUNFILES_JAVABASE=${TEST_WORKSPACE}/${JAVABASE}
40+
fi
2841
shift
2942
IJAR=$1
3043
shift
@@ -44,6 +57,11 @@ function cleanup() {
4457

4558
trap cleanup EXIT
4659

60+
JAVAC=$(rlocation "$RUNFILES_JAVABASE/bin/javac")
61+
JAVA=$(rlocation "$RUNFILES_JAVABASE/bin/java")
62+
JAR=$(rlocation "$RUNFILES_JAVABASE/bin/jar")
63+
JAVAP=$(rlocation "$RUNFILES_JAVABASE/bin/javap")
64+
4765
## Tools
4866
# Ensure that tooling path is absolute if not in PATH.
4967
[[ "$JAVAC" =~ ^(/|[^/]+$) ]] || JAVAC="$PWD/$JAVAC"

0 commit comments

Comments
 (0)