Skip to content

Commit

Permalink
Fix java tests for --legacy_external_runfiles
Browse files Browse the repository at this point in the history
Migrate Bazel for bazelbuild#23574

PiperOrigin-RevId: 704687334
Change-Id: I47fe34775959d1b4ab4e6d931ddc6df256f6e59a
  • Loading branch information
meteorcloudy authored and copybara-github committed Dec 10, 2024
1 parent 5c7ada4 commit b09335b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ sh_test(
"@bazel_tools//tools/jdk:current_java_runtime",
],
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)

java_binary(
Expand Down Expand Up @@ -159,6 +162,9 @@ sh_test(
"@bazel_tools//tools/jdk:current_java_runtime",
],
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)

exports_files([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,33 @@

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

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
# shellcheck disable=SC1090
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

# Load the unit-testing framework
source "$1" || \
{ echo "Failed to load unit-testing framework $1" >&2; exit 1; }

set +o errexit

unset TEST_PREMATURE_EXIT_FILE
JAVA_HOME="$2"
JAVABASE="$2"
if [[ "$TEST_WORKSPACE" == "_main" ]]; then
# For Bazel
RUNFILES_JAVABASE=${JAVABASE#external/}
else
# For Blaze
RUNFILES_JAVABASE=${TEST_WORKSPACE}/${JAVABASE}
fi
TESTBED_JAR="${PWD}/$3"

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,38 @@
source "$1" || \
{ echo "Failed to load unit-testing framework $1" >&2; exit 1; }

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
# shellcheck disable=SC1090
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

set +o errexit

PROGRAM_THAT_CALLS_SYSTEM_EXIT_JAR="$2"
readonly PROGRAM_THAT_CALLS_SYSTEM_EXIT_JAR
JAVA_HOME="$3"
readonly JAVA_HOME
JAVABASE="$3"
if [[ "$TEST_WORKSPACE" == "_main" ]]; then
# For Bazel
RUNFILES_JAVABASE=${JAVABASE#external/}
else
# For Blaze
RUNFILES_JAVABASE=${TEST_WORKSPACE}/${JAVABASE}
fi
EXPECTED_STACK_FILE="$4"
readonly EXPECTED_STACK_FILE

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

"${JAVA_HOME}/bin/java" -jar "${PROGRAM_THAT_CALLS_SYSTEM_EXIT_JAR}" \
JAVA=$(rlocation "${RUNFILES_JAVABASE}/bin/java")
"${JAVA}" -jar "${PROGRAM_THAT_CALLS_SYSTEM_EXIT_JAR}" \
2> "${output_file}"
assert_equals 121 $?

Expand Down
15 changes: 12 additions & 3 deletions src/test/shell/integration/java_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ source "${CURRENT_DIR}/java_integration_test_utils.sh" \
|| { echo "java_integration_test_utils.sh not found!" >&2; exit 1; }
set -eu

declare -r runfiles_relative_javabase="$1"
JAVABASE="$1"
if [[ "$TEST_WORKSPACE" == "_main" ]]; then
# For Bazel
RUNFILES_JAVABASE=${JAVABASE#external/}
RUNFILES_JAVABASE="$(dirname $(dirname $(rlocation $RUNFILES_JAVABASE/bin/java)))"
else
# For Blaze
RUNFILES_JAVABASE=${BAZEL_RUNFILES}/${JAVABASE}
fi

add_to_bazelrc "build --package_path=%workspace%"

function set_up() {
Expand All @@ -39,7 +48,7 @@ function set_up() {

function setup_local_jdk() {
local -r dest="$1"
local -r src="${BAZEL_RUNFILES}/${runfiles_relative_javabase}"
local -r src="${RUNFILES_JAVABASE}"

mkdir -p "$dest" || fail "mkdir -p $dest"
cp -LR "${src}"/* "$dest" || fail "cp -LR \"${src}\"/* \"$dest\""
Expand Down Expand Up @@ -260,7 +269,7 @@ function assert_singlejar_works() {
ln -s "my_jdk" "$pkg/my_jdk.symlink"
local -r javabase="$(get_real_path "$pkg/my_jdk.symlink")"
else
local -r javabase="${BAZEL_RUNFILES}/${runfiles_relative_javabase}"
local -r javabase="${RUNFILES_JAVABASE}"
fi

mkdir -p "$pkg/jvm"
Expand Down
10 changes: 7 additions & 3 deletions src/tools/singlejar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ cc_test(
"output_jar_simple_test.cc",
],
copts = select({
"//src/conditions:windows": ["-DJAR_TOOL_PATH=\\\"io_bazel/$(JAVABASE)/bin/jar.exe\\\""],
"//conditions:default": ["-DJAR_TOOL_PATH=\\\"io_bazel/$(JAVABASE)/bin/jar\\\""],
"//src/conditions:windows": ["-DJAR_TOOL_PATH=\\\"local_jdk/bin/jar.exe\\\""],
"//conditions:default": ["-DJAR_TOOL_PATH=\\\"local_jdk/bin/jar\\\""],
}),
data = [
":data1",
Expand All @@ -297,7 +297,11 @@ cc_test(
":test1",
":test2",
"@rules_java//toolchains:current_java_runtime",
],
] + select({
# TODO: Use the current java runtime instead of local_jdk
"//src/conditions:windows": ["@local_jdk//:bin/jar.exe"],
"//conditions:default": ["@local_jdk//:bin/jar"],
}),
toolchains = ["@rules_java//toolchains:current_java_runtime"],
deps = [
":input_jar",
Expand Down
6 changes: 2 additions & 4 deletions third_party/ijar/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ sh_test(
size = "enormous",
srcs = ["ijar_test.sh"],
args = [
"$(JAVABASE)/bin/javac",
"$(JAVABASE)/bin/java",
"$(JAVABASE)/bin/jar",
"$(JAVABASE)/bin/javap",
"$(JAVABASE)",
"$(location //third_party/ijar)",
# We assume unzip and zip to be on the path
"unzip",
Expand Down Expand Up @@ -46,6 +43,7 @@ sh_test(
# wrong.
"libwrongcentraldir.jar",
":dynamic_constant.jar",
"@bazel_tools//tools/bash/runfiles",
"@bazel_tools//tools/jdk:current_java_runtime",
],
shard_count = 5,
Expand Down
32 changes: 25 additions & 7 deletions third_party/ijar/test/ijar_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,29 @@
# TODO(bazel-team) test that modifying the source in a non-interface
# changing way results in the same -interface.jar.

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
# shellcheck disable=SC1090
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

## Inputs
JAVAC=$1
shift
JAVA=$1
shift
JAR=$1
shift
JAVAP=$1
JAVABASE="$1"
if [[ "$TEST_WORKSPACE" == "_main" ]]; then
# For Bazel
RUNFILES_JAVABASE=${JAVABASE#external/}
else
# For Blaze
RUNFILES_JAVABASE=${TEST_WORKSPACE}/${JAVABASE}
fi
shift
IJAR=$1
shift
Expand All @@ -44,6 +57,11 @@ function cleanup() {

trap cleanup EXIT

JAVAC=$(rlocation "$RUNFILES_JAVABASE/bin/javac")
JAVA=$(rlocation "$RUNFILES_JAVABASE/bin/java")
JAR=$(rlocation "$RUNFILES_JAVABASE/bin/jar")
JAVAP=$(rlocation "$RUNFILES_JAVABASE/bin/javap")

## Tools
# Ensure that tooling path is absolute if not in PATH.
[[ "$JAVAC" =~ ^(/|[^/]+$) ]] || JAVAC="$PWD/$JAVAC"
Expand Down

0 comments on commit b09335b

Please sign in to comment.