Stop hardcoding runfiles prefix (#402)

bazelbuild/bazel@6a8ddb7 changed the prefix for runfiles from the main
repo when Bzlmod is enabled. Because all uses of rlocation were
hardcoded to use "bazel_skylib", tests requiring runfiles stopped
working with Bzlmod enabled. This commit updates calls to rlocation to
instead use the TEST_WORKSPACE env var to get the repo name.
This commit is contained in:
Nick Gooding 2022-10-14 23:20:48 +01:00 committed by GitHub
parent bc112d41fd
commit 8a6ab72c6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 49 additions and 42 deletions

View File

@ -41,7 +41,7 @@ else
fi fi
# --- end runfiles.bash initialization --- # --- end runfiles.bash initialization ---
source "$(rlocation bazel_skylib/tests/unittest.bash)" \ source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
function create_pkg() { function create_pkg() {
@ -58,7 +58,7 @@ EOF
exports_files(["*.bzl"]) exports_files(["*.bzl"])
EOF EOF
ln -sf "$(rlocation bazel_skylib/rules/analysis_test.bzl)" rules/analysis_test.bzl ln -sf "$(rlocation $TEST_WORKSPACE/rules/analysis_test.bzl)" rules/analysis_test.bzl
mkdir -p fakerules mkdir -p fakerules
cat > fakerules/rules.bzl <<EOF cat > fakerules/rules.bzl <<EOF

View File

@ -38,7 +38,7 @@ else
fi fi
# --- end runfiles.bash initialization --- # --- end runfiles.bash initialization ---
source "$(rlocation bazel_skylib/tests/unittest.bash)" \ source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
function create_volcano_pkg() { function create_volcano_pkg() {
@ -55,7 +55,7 @@ EOF
exports_files(["*.bzl"]) exports_files(["*.bzl"])
EOF EOF
ln -sf "$(rlocation bazel_skylib/rules/common_settings.bzl)" rules/common_settings.bzl ln -sf "$(rlocation $TEST_WORKSPACE/rules/common_settings.bzl)" rules/common_settings.bzl
mkdir -p volcano mkdir -p volcano
cat > volcano/rules.bzl <<EOF cat > volcano/rules.bzl <<EOF

View File

@ -37,43 +37,43 @@ else
fi fi
# --- end runfiles.bash initialization --- # --- end runfiles.bash initialization ---
source "$(rlocation bazel_skylib/tests/unittest.bash)" \ source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
function test_copy_dir_with_subdir__copies_a() { function test_copy_dir_with_subdir__copies_a() {
cat "$(rlocation bazel_skylib/tests/copy_directory/dir_copy)/a" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)/a" >"$TEST_log"
expect_log '^foo$' expect_log '^foo$'
} }
function test_copy_dir_with_subdir__copies_b() { function test_copy_dir_with_subdir__copies_b() {
cat "$(rlocation bazel_skylib/tests/copy_directory/dir_copy)/b" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)/b" >"$TEST_log"
expect_log '^bar$' expect_log '^bar$'
} }
function test_copy_dir_with_subdir__copies_c() { function test_copy_dir_with_subdir__copies_c() {
cat "$(rlocation bazel_skylib/tests/copy_directory/dir_copy)/subdir/c" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)/subdir/c" >"$TEST_log"
expect_log '^moocow$' expect_log '^moocow$'
} }
function test_copy_dir_with_subdir__correct_filecounts() { function test_copy_dir_with_subdir__correct_filecounts() {
local -r dir_filecount=$(ls "$(rlocation bazel_skylib/tests/copy_directory/dir_copy)" | wc -l) local -r dir_filecount=$(ls "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)" | wc -l)
assert_equals $dir_filecount 3 assert_equals $dir_filecount 3
local -r subdir_filecount=$(ls "$(rlocation bazel_skylib/tests/copy_directory/dir_copy)/subdir" | wc -l) local -r subdir_filecount=$(ls "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_copy)/subdir" | wc -l)
assert_equals $subdir_filecount 1 assert_equals $subdir_filecount 1
} }
function test_copy_empty_dir() { function test_copy_empty_dir() {
local -r filecount=$(ls "$(rlocation bazel_skylib/tests/copy_directory/empty_dir_copy)" | wc -l) local -r filecount=$(ls "$(rlocation $TEST_WORKSPACE/tests/copy_directory/empty_dir_copy)" | wc -l)
assert_equals $filecount 0 assert_equals $filecount 0
} }
function test_copy_dir_with_symlink__copies_file() { function test_copy_dir_with_symlink__copies_file() {
cat "$(rlocation bazel_skylib/tests/copy_directory/dir_with_symlink_copy)/file" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_with_symlink_copy)/file" >"$TEST_log"
expect_log '^foo$' expect_log '^foo$'
} }
function test_copy_dir_with_symlink__copies_symlink() { function test_copy_dir_with_symlink__copies_symlink() {
cat "$(rlocation bazel_skylib/tests/copy_directory/dir_with_symlink_copy)/symlink" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_directory/dir_with_symlink_copy)/symlink" >"$TEST_log"
expect_log '^foo$' expect_log '^foo$'
} }

View File

@ -37,50 +37,50 @@ else
fi fi
# --- end runfiles.bash initialization --- # --- end runfiles.bash initialization ---
source "$(rlocation bazel_skylib/tests/unittest.bash)" \ source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
function test_copy_src() { function test_copy_src() {
cat "$(rlocation bazel_skylib/tests/copy_file/out/a-out.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/out/a-out.txt)" >"$TEST_log"
expect_log '^#!/usr/bin/env bash$' expect_log '^#!/usr/bin/env bash$'
expect_log '^echo aaa$' expect_log '^echo aaa$'
} }
function test_copy_src_symlink() { function test_copy_src_symlink() {
cat "$(rlocation bazel_skylib/tests/copy_file/out/a-out-symlink.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/out/a-out-symlink.txt)" >"$TEST_log"
expect_log '^#!/usr/bin/env bash$' expect_log '^#!/usr/bin/env bash$'
expect_log '^echo aaa$' expect_log '^echo aaa$'
} }
function test_copy_gen() { function test_copy_gen() {
cat "$(rlocation bazel_skylib/tests/copy_file/out/gen-out.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/out/gen-out.txt)" >"$TEST_log"
expect_log '^#!/usr/bin/env bash$' expect_log '^#!/usr/bin/env bash$'
expect_log '^echo potato$' expect_log '^echo potato$'
} }
function test_copy_gen_symlink() { function test_copy_gen_symlink() {
cat "$(rlocation bazel_skylib/tests/copy_file/out/gen-out-symlink.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/out/gen-out-symlink.txt)" >"$TEST_log"
expect_log '^#!/usr/bin/env bash$' expect_log '^#!/usr/bin/env bash$'
expect_log '^echo potato$' expect_log '^echo potato$'
} }
function test_copy_xsrc() { function test_copy_xsrc() {
cat "$(rlocation bazel_skylib/tests/copy_file/xsrc-out.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/xsrc-out.txt)" >"$TEST_log"
expect_log '^aaa$' expect_log '^aaa$'
} }
function test_copy_xsrc_symlink() { function test_copy_xsrc_symlink() {
cat "$(rlocation bazel_skylib/tests/copy_file/xsrc-out-symlink.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/xsrc-out-symlink.txt)" >"$TEST_log"
expect_log '^aaa$' expect_log '^aaa$'
} }
function test_copy_xgen() { function test_copy_xgen() {
cat "$(rlocation bazel_skylib/tests/copy_file/xgen-out.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/xgen-out.txt)" >"$TEST_log"
expect_log '^potato$' expect_log '^potato$'
} }
function test_copy_xgen_symlink() { function test_copy_xgen_symlink() {
cat "$(rlocation bazel_skylib/tests/copy_file/xgen-out-symlink.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/copy_file/xgen-out-symlink.txt)" >"$TEST_log"
expect_log '^potato$' expect_log '^potato$'
} }

View File

@ -37,7 +37,7 @@ else
fi fi
# --- end runfiles.bash initialization --- # --- end runfiles.bash initialization ---
source "$(rlocation bazel_skylib/tests/unittest.bash)" \ source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
function import_diff_test() { function import_diff_test() {
@ -46,9 +46,9 @@ function import_diff_test() {
mkdir -p "${repo}/lib" mkdir -p "${repo}/lib"
touch "${repo}/lib/BUILD" touch "${repo}/lib/BUILD"
touch "${repo}/WORKSPACE" touch "${repo}/WORKSPACE"
ln -sf "$(rlocation bazel_skylib/rules/diff_test.bzl)" \ ln -sf "$(rlocation $TEST_WORKSPACE/rules/diff_test.bzl)" \
"${repo}/rules/diff_test.bzl" "${repo}/rules/diff_test.bzl"
ln -sf "$(rlocation bazel_skylib/lib/shell.bzl)" \ ln -sf "$(rlocation $TEST_WORKSPACE/lib/shell.bzl)" \
"${repo}/lib/shell.bzl" "${repo}/lib/shell.bzl"
echo "exports_files(['diff_test.bzl'])" > "${repo}/rules/BUILD" echo "exports_files(['diff_test.bzl'])" > "${repo}/rules/BUILD"
} }

View File

@ -25,11 +25,11 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 --- # --- end runfiles.bash initialization v2 ---
source "$(rlocation bazel_skylib/tests/unittest.bash)" \ source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
function test_expand_template() { function test_expand_template() {
cat "$(rlocation bazel_skylib/tests/expand_template/foo/test.yaml)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/expand_template/foo/test.yaml)" >"$TEST_log"
expect_log 'name: test' expect_log 'name: test'
expect_log 'version: 1.1.1' expect_log 'version: 1.1.1'
} }

View File

@ -30,10 +30,17 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
char* workspace = getenv("TEST_WORKSPACE");
if (workspace == nullptr) {
fprintf(stderr, "ERROR(" __FILE__ ":%d): envvar TEST_WORKSPACE is undefined\n",
__LINE__);
return 1;
}
// This should have runfiles, either from the binary itself or from the // This should have runfiles, either from the binary itself or from the
// native_test. // native_test.
std::string path = std::string path =
runfiles->Rlocation("bazel_skylib/tests/native_binary/testdata.txt"); runfiles->Rlocation(std::string(workspace) + "/tests/native_binary/testdata.txt");
FILE *f = fopen(path.c_str(), "rt"); FILE *f = fopen(path.c_str(), "rt");
if (!f) { if (!f) {
fprintf(stderr, "ERROR(" __FILE__ ":%d): Could not find runfile '%s'\n", fprintf(stderr, "ERROR(" __FILE__ ":%d): Could not find runfile '%s'\n",

View File

@ -42,7 +42,7 @@ else
fi fi
# --- end runfiles.bash initialization --- # --- end runfiles.bash initialization ---
source "$(rlocation bazel_skylib/tests/unittest.bash)" \ source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
function create_pkg() { function create_pkg() {
@ -64,22 +64,22 @@ EOF
cat > tests/BUILD <<EOF cat > tests/BUILD <<EOF
exports_files(["*.bzl"]) exports_files(["*.bzl"])
EOF EOF
ln -sf "$(rlocation bazel_skylib/tests/unittest_tests.bzl)" tests/unittest_tests.bzl ln -sf "$(rlocation $TEST_WORKSPACE/tests/unittest_tests.bzl)" tests/unittest_tests.bzl
mkdir -p lib mkdir -p lib
touch lib/BUILD touch lib/BUILD
cat > lib/BUILD <<EOF cat > lib/BUILD <<EOF
exports_files(["*.bzl"]) exports_files(["*.bzl"])
EOF EOF
ln -sf "$(rlocation bazel_skylib/lib/dicts.bzl)" lib/dicts.bzl ln -sf "$(rlocation $TEST_WORKSPACE/lib/dicts.bzl)" lib/dicts.bzl
ln -sf "$(rlocation bazel_skylib/lib/new_sets.bzl)" lib/new_sets.bzl ln -sf "$(rlocation $TEST_WORKSPACE/lib/new_sets.bzl)" lib/new_sets.bzl
ln -sf "$(rlocation bazel_skylib/lib/partial.bzl)" lib/partial.bzl ln -sf "$(rlocation $TEST_WORKSPACE/lib/partial.bzl)" lib/partial.bzl
ln -sf "$(rlocation bazel_skylib/lib/sets.bzl)" lib/sets.bzl ln -sf "$(rlocation $TEST_WORKSPACE/lib/sets.bzl)" lib/sets.bzl
ln -sf "$(rlocation bazel_skylib/lib/types.bzl)" lib/types.bzl ln -sf "$(rlocation $TEST_WORKSPACE/lib/types.bzl)" lib/types.bzl
ln -sf "$(rlocation bazel_skylib/lib/unittest.bzl)" lib/unittest.bzl ln -sf "$(rlocation $TEST_WORKSPACE/lib/unittest.bzl)" lib/unittest.bzl
mkdir -p toolchains/unittest mkdir -p toolchains/unittest
ln -sf "$(rlocation bazel_skylib/toolchains/unittest/BUILD)" toolchains/unittest/BUILD ln -sf "$(rlocation $TEST_WORKSPACE/toolchains/unittest/BUILD)" toolchains/unittest/BUILD
# Create test files. # Create test files.
mkdir -p testdir mkdir -p testdir

View File

@ -37,7 +37,7 @@ else
fi fi
# --- end runfiles.bash initialization --- # --- end runfiles.bash initialization ---
source "$(rlocation bazel_skylib/tests/unittest.bash)" \ source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
function assert_empty_file() { function assert_empty_file() {
@ -47,21 +47,21 @@ function assert_empty_file() {
} }
function test_write_empty_text() { function test_write_empty_text() {
assert_empty_file "$(rlocation bazel_skylib/tests/write_file/out/empty.txt)" assert_empty_file "$(rlocation $TEST_WORKSPACE/tests/write_file/out/empty.txt)"
} }
function test_write_nonempty_text() { function test_write_nonempty_text() {
cat "$(rlocation bazel_skylib/tests/write_file/out/nonempty.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/write_file/out/nonempty.txt)" >"$TEST_log"
expect_log '^aaa' expect_log '^aaa'
expect_log '^bbb' expect_log '^bbb'
} }
function test_write_empty_bin() { function test_write_empty_bin() {
assert_empty_file "$(rlocation bazel_skylib/tests/write_file/empty-bin-out.txt)" assert_empty_file "$(rlocation $TEST_WORKSPACE/tests/write_file/empty-bin-out.txt)"
} }
function test_write_nonempty_bin() { function test_write_nonempty_bin() {
cat "$(rlocation bazel_skylib/tests/write_file/nonempty-bin-out.txt)" >"$TEST_log" cat "$(rlocation $TEST_WORKSPACE/tests/write_file/nonempty-bin-out.txt)" >"$TEST_log"
expect_log '^potato' expect_log '^potato'
} }