| #!/bin/bash | |
| # | |
| # Copyright 2018 The Bazel Authors. All rights reserved. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| set -euo pipefail | |
| function _log_base() { | |
| prefix=$1 | |
| shift | |
| echo >&2 "${prefix}[$(basename "${BASH_SOURCE[0]}"):${BASH_LINENO[1]} ($(date "+%H:%M:%S %z"))] $*" | |
| } | |
| function fail() { | |
| _log_base "FAILED" "$@" | |
| exit 1 | |
| } | |
| function log_fail() { | |
| # non-fatal version of fail() | |
| _log_base "FAILED" $* | |
| } | |
| function log_info() { | |
| _log_base "INFO" $* | |
| } | |
| which uname >&/dev/null || fail "cannot locate GNU coreutils" | |
| case "$(uname -s | tr [:upper:] [:lower:])" in | |
| msys*|mingw*|cygwin*) | |
| function is_windows() { true; } | |
| ;; | |
| *) | |
| function is_windows() { false; } | |
| ;; | |
| esac | |
| _RUNFILES_LIBRARY_FILE="${RUNFILES_LIBRARY_FILE:-bazel_tools/tools/bash/runfiles/runfiles.bash}" | |
| function find_runfiles_lib() { | |
| # Unset existing definitions of the functions we want to test. | |
| if type rlocation >&/dev/null; then | |
| unset rlocation | |
| unset runfiles_export_envvars | |
| fi | |
| if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | |
| if [[ -f "$0.runfiles_manifest" ]]; then | |
| export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" | |
| elif [[ -f "$0.runfiles/MANIFEST" ]]; then | |
| export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" | |
| elif [[ -f "$0.runfiles/${_RUNFILES_LIBRARY_FILE}" ]]; then | |
| export RUNFILES_DIR="$0.runfiles" | |
| fi | |
| fi | |
| if [[ -f "${RUNFILES_DIR:-/dev/null}/${_RUNFILES_LIBRARY_FILE}" ]]; then | |
| echo "${RUNFILES_DIR}/${_RUNFILES_LIBRARY_FILE}" | |
| elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | |
| grep -m1 "^${_RUNFILES_LIBRARY_FILE} " \ | |
| "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2- | |
| else | |
| echo >&2 "ERROR: cannot find //shell/runfiles:runfiles.bash" | |
| exit 1 | |
| fi | |
| } | |
| function test_rlocation_call_requires_no_envvars() { | |
| export RUNFILES_DIR=mock/runfiles | |
| export RUNFILES_MANIFEST_FILE= | |
| export RUNFILES_MANIFEST_ONLY= | |
| source "$runfiles_lib_path" || fail | |
| } | |
| function test_rlocation_argument_validation() { | |
| export RUNFILES_DIR= | |
| export RUNFILES_MANIFEST_FILE= | |
| export RUNFILES_MANIFEST_ONLY= | |
| source "$runfiles_lib_path" | |
| # Test invalid inputs to make sure rlocation catches these. | |
| if rlocation "../foo" >&/dev/null; then | |
| fail | |
| fi | |
| if rlocation "foo/.." >&/dev/null; then | |
| fail | |
| fi | |
| if rlocation "foo/../bar" >&/dev/null; then | |
| fail | |
| fi | |
| if rlocation "./foo" >&/dev/null; then | |
| fail | |
| fi | |
| if rlocation "foo/." >&/dev/null; then | |
| fail | |
| fi | |
| if rlocation "foo/./bar" >&/dev/null; then | |
| fail | |
| fi | |
| if rlocation "//foo" >&/dev/null; then | |
| fail | |
| fi | |
| if rlocation "foo//" >&/dev/null; then | |
| fail | |
| fi | |
| if rlocation "foo//bar" >&/dev/null; then | |
| fail | |
| fi | |
| if rlocation "\\foo" >&/dev/null; then | |
| fail | |
| fi | |
| } | |
| function test_rlocation_abs_path() { | |
| export RUNFILES_DIR= | |
| export RUNFILES_MANIFEST_FILE= | |
| export RUNFILES_MANIFEST_ONLY= | |
| source "$runfiles_lib_path" | |
| if is_windows; then | |
| [[ "$(rlocation "c:/Foo" || echo failed)" == "c:/Foo" ]] || fail | |
| [[ "$(rlocation "c:\\Foo" || echo failed)" == "c:\\Foo" ]] || fail | |
| else | |
| [[ "$(rlocation "/Foo" || echo failed)" == "/Foo" ]] || fail | |
| fi | |
| } | |
| function test_init_manifest_based_runfiles() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| cat > $tmpdir/foo.runfiles_manifest << EOF | |
| a/b $tmpdir/c/d | |
| e/f $tmpdir/g h | |
| y $tmpdir/y | |
| c/dir $tmpdir/dir | |
| unresolved $tmpdir/unresolved | |
| h/\si $tmpdir/ j k | |
| h/\s\bi $tmpdir/ j k b | |
| h/\n\bi $tmpdir/ \bnj k \na | |
| dir\swith\sspaces $tmpdir/dir with spaces | |
| space\snewline\nbackslash\b_dir $tmpdir/space newline\nbackslash\ba | |
| EOF | |
| mkdir "${tmpdir}/c" | |
| mkdir "${tmpdir}/y" | |
| mkdir -p "${tmpdir}/dir/deeply/nested" | |
| touch "${tmpdir}/c/d" "${tmpdir}/g h" | |
| touch "${tmpdir}/dir/file" | |
| ln -s /does/not/exist "${tmpdir}/dir/unresolved" | |
| touch "${tmpdir}/dir/deeply/nested/file" | |
| touch "${tmpdir}/dir/deeply/nested/file with spaces" | |
| ln -s /does/not/exist "${tmpdir}/unresolved" | |
| touch "${tmpdir}/ j k" | |
| touch "${tmpdir}/ j k b" | |
| mkdir -p "${tmpdir}/dir with spaces/nested" | |
| touch "${tmpdir}/dir with spaces/nested/file" | |
| if ! is_windows; then | |
| touch "${tmpdir}/ \nj k "$'\n'a | |
| mkdir -p "${tmpdir}/space newline"$'\n'"backslash\a" | |
| touch "${tmpdir}/space newline"$'\n'"backslash\a/f i\le" | |
| fi | |
| export RUNFILES_DIR= | |
| export RUNFILES_MANIFEST_FILE=$tmpdir/foo.runfiles_manifest | |
| source "$runfiles_lib_path" | |
| [[ -z "$(rlocation a || echo failed)" ]] || fail | |
| [[ -z "$(rlocation c/d || echo failed)" ]] || fail | |
| [[ "$(rlocation a/b || echo failed)" == "$tmpdir/c/d" ]] || fail | |
| [[ "$(rlocation e/f || echo failed)" == "$tmpdir/g h" ]] || fail | |
| [[ "$(rlocation y || echo failed)" == "$tmpdir/y" ]] || fail | |
| [[ -z "$(rlocation c || echo failed)" ]] || fail | |
| [[ -z "$(rlocation c/di || echo failed)" ]] || fail | |
| [[ "$(rlocation c/dir || echo failed)" == "$tmpdir/dir" ]] || fail | |
| [[ "$(rlocation c/dir/file || echo failed)" == "$tmpdir/dir/file" ]] || fail | |
| [[ -z "$(rlocation c/dir/unresolved || echo failed)" ]] || fail | |
| [[ "$(rlocation c/dir/deeply/nested/file || echo failed)" == "$tmpdir/dir/deeply/nested/file" ]] || fail | |
| [[ "$(rlocation "c/dir/deeply/nested/file with spaces" || echo failed)" == "$tmpdir/dir/deeply/nested/file with spaces" ]] || fail | |
| [[ -z "$(rlocation unresolved || echo failed)" ]] || fail | |
| [[ "$(rlocation "h/ i" || echo failed)" == "$tmpdir/ j k" ]] || fail | |
| [[ "$(rlocation "h/ \i" || echo failed)" == "$tmpdir/ j k b" ]] || fail | |
| [[ "$(rlocation "dir with spaces" || echo failed)" == "$tmpdir/dir with spaces" ]] || fail | |
| [[ "$(rlocation "dir with spaces/nested/file" || echo failed)" == "$tmpdir/dir with spaces/nested/file" ]] || fail | |
| if ! is_windows; then | |
| [[ "$(rlocation $'h/\n\\i' || echo failed)" == "$tmpdir/ \nj k "$'\n'a ]] || fail | |
| [[ "$(rlocation "space newline"$'\n'"backslash\_dir/f i\le" || echo failed)" == "${tmpdir}/space newline"$'\n'"backslash\a/f i\le" ]] || fail | |
| fi | |
| rm -r "$tmpdir/c/d" "$tmpdir/g h" "$tmpdir/y" "$tmpdir/dir" "$tmpdir/unresolved" "$tmpdir/ j k" "$tmpdir/dir with spaces" | |
| if ! is_windows; then | |
| rm -r "$tmpdir/ \nj k "$'\n'a "${tmpdir}/space newline"$'\n'"backslash\a" | |
| [[ -z "$(rlocation $'h/\n\\i' || echo failed)" ]] || fail | |
| [[ -z "$(rlocation "space newline"$'\n'"backslash\_dir/f i\le" || echo failed)" ]] || fail | |
| fi | |
| [[ -z "$(rlocation a/b || echo failed)" ]] || fail | |
| [[ -z "$(rlocation e/f || echo failed)" ]] || fail | |
| [[ -z "$(rlocation y || echo failed)" ]] || fail | |
| [[ -z "$(rlocation c/dir || echo failed)" ]] || fail | |
| [[ -z "$(rlocation c/dir/file || echo failed)" ]] || fail | |
| [[ -z "$(rlocation c/dir/deeply/nested/file || echo failed)" ]] || fail | |
| [[ -z "$(rlocation "h/ i" || echo failed)" ]] || fail | |
| [[ -z "$(rlocation "dir with spaces" || echo failed)" ]] || fail | |
| [[ -z "$(rlocation "dir with spaces/nested/file" || echo failed)" ]] || fail | |
| } | |
| # Writes a runfiles layout containing unresolved symlinks with relative targets, both as a manifest | |
| # pointing at the original files and as a materialized runfiles directory. | |
| function write_relative_symlink_target_layout() { | |
| local -r tmpdir="$1" | |
| local -r dir="$tmpdir/foo.runfiles" | |
| mkdir -p "$tmpdir/original/dir/deeply/nested" | |
| echo file > "$tmpdir/original/file" | |
| echo nested_file > "$tmpdir/original/dir/deeply/nested/file" | |
| # Lies next to the runfiles directory and is thus only reachable through a relative target that | |
| # leaves the runfiles tree. | |
| echo outside > "$tmpdir/outside" | |
| cat > "$tmpdir/foo.runfiles_manifest" << EOF | |
| _main/pkg/file $tmpdir/original/file | |
| _main/pkg/dir $tmpdir/original/dir | |
| _main/pkg/link ../pkg/file | |
| _main/pkg/nested/link ../../pkg/link | |
| _main/pkg/dir_link ./dir | |
| _main/pkg/dangling ../pkg/missing | |
| _main/pkg/escaping ../../../outside | |
| _main/pkg/loop_a loop_b | |
| _main/pkg/loop_b loop_a | |
| _main/pkg/link\swith\sspaces ../pkg/file | |
| EOF | |
| mkdir -p "$dir/_main/pkg/nested" | |
| ln -s "$tmpdir/original/file" "$dir/_main/pkg/file" | |
| ln -s "$tmpdir/original/dir" "$dir/_main/pkg/dir" | |
| ln -s ../pkg/file "$dir/_main/pkg/link" | |
| ln -s ../../pkg/link "$dir/_main/pkg/nested/link" | |
| ln -s ./dir "$dir/_main/pkg/dir_link" | |
| ln -s ../pkg/missing "$dir/_main/pkg/dangling" | |
| ln -s ../../../outside "$dir/_main/pkg/escaping" | |
| ln -s loop_b "$dir/_main/pkg/loop_a" | |
| ln -s loop_a "$dir/_main/pkg/loop_b" | |
| ln -s ../pkg/file "$dir/_main/pkg/link with spaces" | |
| } | |
| # Asserts that the given rlocation path resolves to a file with the given contents. Only the | |
| # contents are compared since the path itself necessarily differs between the two lookup modes: a | |
| # materialized runfiles directory resolves to the entry in that directory, not to the file it | |
| # points at. | |
| function assert_rlocation_contents() { | |
| local -r resolved="$(rlocation "$1" || echo failed)" | |
| [[ -f "$resolved" ]] || fail "$1 did not resolve to a file, got: $resolved" | |
| [[ "$(cat "$resolved")" == "$2" ]] || fail "$1 resolved to $resolved with unexpected contents" | |
| } | |
| # Asserts that the given rlocation path does not resolve, which rlocation reports as an empty result | |
| # when it uses the manifest and as a non-zero exit code when it uses the runfiles directory (see the | |
| # FIXME on runfiles_rlocation_checked). | |
| function assert_no_rlocation() { | |
| local -r resolved="$(rlocation "$1" || echo failed)" | |
| [[ -z "$resolved" || "$resolved" == failed ]] || fail "$1 unexpectedly resolved to $resolved" | |
| } | |
| # The lookups whose outcome must not depend on whether the manifest or the runfiles directory backs | |
| # them: resolving a relative target against the manifest has to arrive at the same file that the | |
| # file system arrives at when resolving the corresponding symlink in the runfiles directory. | |
| function assert_relative_symlink_target_lookups() { | |
| assert_rlocation_contents _main/pkg/link file | |
| assert_rlocation_contents _main/pkg/nested/link file | |
| assert_rlocation_contents "_main/pkg/link with spaces" file | |
| # A relative target that resolves to a directory runfile also resolves paths underneath it. | |
| [[ -d "$(rlocation _main/pkg/dir_link || echo failed)" ]] || fail | |
| assert_rlocation_contents _main/pkg/dir_link/deeply/nested/file nested_file | |
| # A target that doesn't resolve to an existing file behaves like a missing runfile. | |
| assert_no_rlocation _main/pkg/dangling | |
| assert_no_rlocation _main/pkg/dir_link/does/not/exist | |
| # A cycle terminates instead of looping forever. | |
| assert_no_rlocation _main/pkg/loop_a | |
| } | |
| function test_manifest_based_relative_symlink_targets() { | |
| local -r tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| write_relative_symlink_target_layout "$tmpdir" | |
| export RUNFILES_DIR= | |
| export RUNFILES_MANIFEST_FILE=$tmpdir/foo.runfiles_manifest | |
| source "$runfiles_lib_path" | |
| assert_relative_symlink_target_lookups | |
| # The one lookup the manifest cannot reproduce: a relative target that leaves the runfiles tree | |
| # can only be resolved against a materialized runfiles directory, whose existence the manifest | |
| # does not imply. Bazel does not generate such a runfile. | |
| assert_no_rlocation _main/pkg/escaping | |
| } | |
| function test_directory_based_relative_symlink_targets() { | |
| # MSYS2 may materialize symlinks as copies, which does not preserve relative targets. | |
| if is_windows; then | |
| return 0 | |
| fi | |
| local -r tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| write_relative_symlink_target_layout "$tmpdir" | |
| export RUNFILES_DIR="$tmpdir/foo.runfiles" | |
| export RUNFILES_MANIFEST_FILE= | |
| source "$runfiles_lib_path" | |
| assert_relative_symlink_target_lookups | |
| assert_rlocation_contents _main/pkg/escaping outside | |
| } | |
| function test_manifest_based_envvars() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| echo "a b" > $tmpdir/foo.runfiles_manifest | |
| export RUNFILES_DIR= | |
| export RUNFILES_MANIFEST_FILE=$tmpdir/foo.runfiles_manifest | |
| mkdir -p $tmpdir/foo.runfiles | |
| source "$runfiles_lib_path" | |
| runfiles_export_envvars | |
| [[ "${RUNFILES_DIR:-}" == "$tmpdir/foo.runfiles" ]] || fail | |
| [[ "${RUNFILES_MANIFEST_FILE:-}" == "$tmpdir/foo.runfiles_manifest" ]] || fail | |
| } | |
| function test_init_directory_based_runfiles() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| export RUNFILES_DIR=${tmpdir}/mock/runfiles | |
| export RUNFILES_MANIFEST_FILE= | |
| source "$runfiles_lib_path" | |
| mkdir -p "$RUNFILES_DIR/a" | |
| touch "$RUNFILES_DIR/a/b" "$RUNFILES_DIR/c d" | |
| [[ "$(rlocation a || echo failed)" == "$RUNFILES_DIR/a" ]] || fail | |
| [[ "$(rlocation c/d || echo failed)" == failed ]] || fail | |
| [[ "$(rlocation a/b || echo failed)" == "$RUNFILES_DIR/a/b" ]] || fail | |
| [[ "$(rlocation "c d" || echo failed)" == "$RUNFILES_DIR/c d" ]] || fail | |
| [[ "$(rlocation "c" || echo failed)" == failed ]] || fail | |
| rm -r "$RUNFILES_DIR/a" "$RUNFILES_DIR/c d" | |
| [[ "$(rlocation a || echo failed)" == failed ]] || fail | |
| [[ "$(rlocation a/b || echo failed)" == failed ]] || fail | |
| [[ "$(rlocation "c d" || echo failed)" == failed ]] || fail | |
| } | |
| function test_directory_based_runfiles_with_repo_mapping_from_main() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| export RUNFILES_DIR=${tmpdir}/mock/runfiles | |
| mkdir -p "$RUNFILES_DIR" | |
| cat > "$RUNFILES_DIR/_repo_mapping" <<EOF | |
| ,config.json,config.json+1.2.3 | |
| ,my_module,_main | |
| ,my_protobuf,protobuf+3.19.2 | |
| ,my_workspace,_main | |
| protobuf+3.19.2,protobuf,protobuf+3.19.2 | |
| protobuf+3.19.2,config.json,config.json+1.2.3 | |
| EOF | |
| export RUNFILES_MANIFEST_FILE= | |
| source "$runfiles_lib_path" | |
| mkdir -p "$RUNFILES_DIR/_main/bar" | |
| touch "$RUNFILES_DIR/_main/bar/runfile" | |
| mkdir -p "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted" | |
| touch "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/file" | |
| touch "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" | |
| mkdir -p "$RUNFILES_DIR/protobuf+3.19.2/foo" | |
| touch "$RUNFILES_DIR/protobuf+3.19.2/foo/runfile" | |
| touch "$RUNFILES_DIR/config.json" | |
| [[ "$(rlocation "my_module/bar/runfile" "" || echo failed)" == "$RUNFILES_DIR/_main/bar/runfile" ]] || fail | |
| [[ "$(rlocation "my_workspace/bar/runfile" "" || echo failed)" == "$RUNFILES_DIR/_main/bar/runfile" ]] || fail | |
| [[ "$(rlocation "my_protobuf/foo/runfile" "" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/foo/runfile" ]] || fail | |
| [[ "$(rlocation "my_protobuf/bar/dir" "" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir" ]] || fail | |
| [[ "$(rlocation "my_protobuf/bar/dir/file" "" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/file" ]] || fail | |
| [[ "$(rlocation "my_protobuf/bar/dir/de eply/nes ted/fi+le" "" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" ]] || fail | |
| [[ "$(rlocation "protobuf/foo/runfile" "" || echo failed)" == failed ]] || fail | |
| [[ "$(rlocation "protobuf/bar/dir/dir/de eply/nes ted/fi+le" "" || echo failed)" == failed ]] || fail | |
| [[ "$(rlocation "_main/bar/runfile" "" || echo failed)" == "$RUNFILES_DIR/_main/bar/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/foo/runfile" "" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/foo/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir" "" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir/file" "" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/file" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" "" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" ]] || fail | |
| [[ "$(rlocation "config.json" "" || echo failed)" == "$RUNFILES_DIR/config.json" ]] || fail | |
| } | |
| function test_directory_based_runfiles_with_repo_mapping_from_other_repo() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| export RUNFILES_DIR=${tmpdir}/mock/runfiles | |
| mkdir -p "$RUNFILES_DIR" | |
| cat > "$RUNFILES_DIR/_repo_mapping" <<EOF | |
| ,config.json,config.json+1.2.3 | |
| ,my_module,_main | |
| ,my_protobuf,protobuf+3.19.2 | |
| ,my_workspace,_main | |
| protobuf+3.19.2,protobuf,protobuf+3.19.2 | |
| protobuf+3.19.2,config.json,config.json+1.2.3 | |
| EOF | |
| export RUNFILES_MANIFEST_FILE= | |
| source "$runfiles_lib_path" | |
| mkdir -p "$RUNFILES_DIR/_main/bar" | |
| touch "$RUNFILES_DIR/_main/bar/runfile" | |
| mkdir -p "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted" | |
| touch "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/file" | |
| touch "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" | |
| mkdir -p "$RUNFILES_DIR/protobuf+3.19.2/foo" | |
| touch "$RUNFILES_DIR/protobuf+3.19.2/foo/runfile" | |
| touch "$RUNFILES_DIR/config.json" | |
| [[ "$(rlocation "protobuf/foo/runfile" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/foo/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf/bar/dir" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir" ]] || fail | |
| [[ "$(rlocation "protobuf/bar/dir/file" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/file" ]] || fail | |
| [[ "$(rlocation "protobuf/bar/dir/de eply/nes ted/fi+le" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" ]] || fail | |
| [[ "$(rlocation "my_module/bar/runfile" "protobuf+3.19.2" || echo failed)" == failed ]] || fail | |
| [[ "$(rlocation "my_protobuf/bar/dir/de eply/nes ted/fi+le" "protobuf+3.19.2" || echo failed)" == failed ]] || fail | |
| [[ "$(rlocation "_main/bar/runfile" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/_main/bar/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/foo/runfile" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/foo/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir/file" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/file" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" ]] || fail | |
| [[ "$(rlocation "config.json" "protobuf+3.19.2" || echo failed)" == "$RUNFILES_DIR/config.json" ]] || fail | |
| } | |
| function test_directory_based_runfiles_with_repo_mapping_from_extension_repo() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| export RUNFILES_DIR=${tmpdir}/mock/runfiles | |
| mkdir -p "$RUNFILES_DIR" | |
| cat > "$RUNFILES_DIR/_repo_mapping" <<EOF | |
| ,config.json,config.json+1.2.3 | |
| ,my_module,_main | |
| ,my_protobuf,protobuf+3.19.2 | |
| ,my_workspace,_main | |
| my_module++ex+*,my_module,my_module+ | |
| my_module++ext+*,my_module,my_module+ | |
| my_module++ext+*,repo1,my_module++ext+repo1 | |
| my_module++ext1+*,my_module,my_module+ | |
| EOF | |
| export RUNFILES_MANIFEST_FILE= | |
| source "$runfiles_lib_path" | |
| mkdir -p "$RUNFILES_DIR/_main/bar" | |
| touch "$RUNFILES_DIR/_main/bar/runfile" | |
| mkdir -p "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted" | |
| touch "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/file" | |
| touch "$RUNFILES_DIR/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" | |
| mkdir -p "$RUNFILES_DIR/protobuf+3.19.2/foo" | |
| touch "$RUNFILES_DIR/protobuf+3.19.2/foo/runfile" | |
| touch "$RUNFILES_DIR/config.json" | |
| mkdir -p "$RUNFILES_DIR/my_module+/foo" | |
| touch "$RUNFILES_DIR/my_module+/foo/runfile" | |
| mkdir -p "$RUNFILES_DIR/my_module++ext+repo1/foo" | |
| touch "$RUNFILES_DIR/my_module++ext+repo1/foo/runfile" | |
| mkdir -p "$RUNFILES_DIR/repo2+/foo" | |
| touch "$RUNFILES_DIR/repo2+/foo/runfile" | |
| [[ "$(rlocation "my_module/foo/runfile" "my_module++ext+repo1" || echo failed)" == "$RUNFILES_DIR/my_module+/foo/runfile" ]] || fail | |
| [[ "$(rlocation "repo1/foo/runfile" "my_module++ext+repo1" || echo failed)" == "$RUNFILES_DIR/my_module++ext+repo1/foo/runfile" ]] || fail | |
| [[ "$(rlocation "repo2+/foo/runfile" "my_module++ext+repo1" || echo failed)" == "$RUNFILES_DIR/repo2+/foo/runfile" ]] || fail | |
| } | |
| function test_manifest_based_runfiles_with_repo_mapping_from_main() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| cat > "$tmpdir/foo.repo_mapping" <<EOF | |
| ,config.json,config.json+1.2.3 | |
| ,my_module,_main | |
| ,my_protobuf,protobuf+3.19.2 | |
| ,my_workspace,_main | |
| protobuf+3.19.2,protobuf,protobuf+3.19.2 | |
| protobuf+3.19.2,config.json,config.json+1.2.3 | |
| EOF | |
| export RUNFILES_DIR= | |
| export RUNFILES_MANIFEST_FILE="$tmpdir/foo.runfiles_manifest" | |
| cat > "$RUNFILES_MANIFEST_FILE" << EOF | |
| _repo_mapping $tmpdir/foo.repo_mapping | |
| config.json $tmpdir/config.json | |
| protobuf+3.19.2/foo/runfile $tmpdir/protobuf+3.19.2/foo/runfile | |
| _main/bar/runfile $tmpdir/_main/bar/runfile | |
| protobuf+3.19.2/bar/dir $tmpdir/protobuf+3.19.2/bar/dir | |
| EOF | |
| source "$runfiles_lib_path" | |
| mkdir -p "$tmpdir/_main/bar" | |
| touch "$tmpdir/_main/bar/runfile" | |
| mkdir -p "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted" | |
| touch "$tmpdir/protobuf+3.19.2/bar/dir/file" | |
| touch "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" | |
| mkdir -p "$tmpdir/protobuf+3.19.2/foo" | |
| touch "$tmpdir/protobuf+3.19.2/foo/runfile" | |
| touch "$tmpdir/config.json" | |
| [[ "$(rlocation "my_module/bar/runfile" "" || echo failed)" == "$tmpdir/_main/bar/runfile" ]] || fail | |
| [[ "$(rlocation "my_workspace/bar/runfile" "" || echo failed)" == "$tmpdir/_main/bar/runfile" ]] || fail | |
| [[ "$(rlocation "my_protobuf/foo/runfile" "" || echo failed)" == "$tmpdir/protobuf+3.19.2/foo/runfile" ]] || fail | |
| [[ "$(rlocation "my_protobuf/bar/dir" "" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir" ]] || fail | |
| [[ "$(rlocation "my_protobuf/bar/dir/file" "" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir/file" ]] || fail | |
| [[ "$(rlocation "my_protobuf/bar/dir/de eply/nes ted/fi+le" "" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" ]] || fail | |
| [[ -z "$(rlocation "protobuf/foo/runfile" "" || echo failed)" ]] || fail | |
| [[ -z "$(rlocation "protobuf/bar/dir/dir/de eply/nes ted/fi+le" "" || echo failed)" ]] || fail | |
| [[ "$(rlocation "_main/bar/runfile" "" || echo failed)" == "$tmpdir/_main/bar/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/foo/runfile" "" || echo failed)" == "$tmpdir/protobuf+3.19.2/foo/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir" "" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir/file" "" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir/file" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" "" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" ]] || fail | |
| [[ "$(rlocation "config.json" "" || echo failed)" == "$tmpdir/config.json" ]] || fail | |
| } | |
| function test_manifest_based_runfiles_with_repo_mapping_from_other_repo() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| cat > "$tmpdir/foo.repo_mapping" <<EOF | |
| ,config.json,config.json+1.2.3 | |
| ,my_module,_main | |
| ,my_protobuf,protobuf+3.19.2 | |
| ,my_workspace,_main | |
| protobuf+3.19.2,protobuf,protobuf+3.19.2 | |
| protobuf+3.19.2,config.json,config.json+1.2.3 | |
| EOF | |
| export RUNFILES_DIR= | |
| export RUNFILES_MANIFEST_FILE="$tmpdir/foo.runfiles_manifest" | |
| cat > "$RUNFILES_MANIFEST_FILE" << EOF | |
| _repo_mapping $tmpdir/foo.repo_mapping | |
| config.json $tmpdir/config.json | |
| protobuf+3.19.2/foo/runfile $tmpdir/protobuf+3.19.2/foo/runfile | |
| _main/bar/runfile $tmpdir/_main/bar/runfile | |
| protobuf+3.19.2/bar/dir $tmpdir/protobuf+3.19.2/bar/dir | |
| EOF | |
| source "$runfiles_lib_path" | |
| mkdir -p "$tmpdir/_main/bar" | |
| touch "$tmpdir/_main/bar/runfile" | |
| mkdir -p "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted" | |
| touch "$tmpdir/protobuf+3.19.2/bar/dir/file" | |
| touch "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" | |
| mkdir -p "$tmpdir/protobuf+3.19.2/foo" | |
| touch "$tmpdir/protobuf+3.19.2/foo/runfile" | |
| touch "$tmpdir/config.json" | |
| [[ "$(rlocation "protobuf/foo/runfile" "protobuf+3.19.2" || echo failed)" == "$tmpdir/protobuf+3.19.2/foo/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf/bar/dir" "protobuf+3.19.2" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir" ]] || fail | |
| [[ "$(rlocation "protobuf/bar/dir/file" "protobuf+3.19.2" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir/file" ]] || fail | |
| [[ "$(rlocation "protobuf/bar/dir/de eply/nes ted/fi+le" "protobuf+3.19.2" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" ]] || fail | |
| [[ -z "$(rlocation "my_module/bar/runfile" "protobuf+3.19.2" || echo failed)" ]] || fail | |
| [[ -z "$(rlocation "my_protobuf/bar/dir/de eply/nes ted/fi+le" "protobuf+3.19.2" || echo failed)" ]] || fail | |
| [[ "$(rlocation "_main/bar/runfile" "protobuf+3.19.2" || echo failed)" == "$tmpdir/_main/bar/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/foo/runfile" "protobuf+3.19.2" || echo failed)" == "$tmpdir/protobuf+3.19.2/foo/runfile" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir" "protobuf+3.19.2" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir/file" "protobuf+3.19.2" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir/file" ]] || fail | |
| [[ "$(rlocation "protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" "protobuf+3.19.2" || echo failed)" == "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" ]] || fail | |
| [[ "$(rlocation "config.json" "protobuf+3.19.2" || echo failed)" == "$tmpdir/config.json" ]] || fail | |
| } | |
| function test_manifest_based_runfiles_with_repo_mapping_from_extension_repo() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| cat > "$tmpdir/foo.repo_mapping" <<EOF | |
| ,config.json,config.json+1.2.3 | |
| ,my_module,_main | |
| ,my_protobuf,protobuf+3.19.2 | |
| ,my_workspace,_main | |
| my_module++ex+*,my_module,my_module+ | |
| my_module++ext+*,my_module,my_module+ | |
| my_module++ext+*,repo1,my_module++ext+repo1 | |
| my_module++ext1+*,my_module,my_module+ | |
| EOF | |
| export RUNFILES_DIR= | |
| export RUNFILES_MANIFEST_FILE="$tmpdir/foo.runfiles_manifest" | |
| cat > "$RUNFILES_MANIFEST_FILE" << EOF | |
| _repo_mapping $tmpdir/foo.repo_mapping | |
| config.json $tmpdir/config.json | |
| protobuf+3.19.2/foo/runfile $tmpdir/protobuf+3.19.2/foo/runfile | |
| _main/bar/runfile $tmpdir/_main/bar/runfile | |
| protobuf+3.19.2/bar/dir $tmpdir/protobuf+3.19.2/bar/dir | |
| my_module+/foo/runfile $tmpdir/my_module+/runfile | |
| my_module++ext+repo1/foo/runfile $tmpdir/my_module++ext+repo1/runfile | |
| repo2+/foo/runfile $tmpdir/repo2+/runfile | |
| EOF | |
| source "$runfiles_lib_path" | |
| mkdir -p "$tmpdir/_main/bar" | |
| touch "$tmpdir/_main/bar/runfile" | |
| mkdir -p "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted" | |
| touch "$tmpdir/protobuf+3.19.2/bar/dir/file" | |
| touch "$tmpdir/protobuf+3.19.2/bar/dir/de eply/nes ted/fi+le" | |
| mkdir -p "$tmpdir/protobuf+3.19.2/foo" | |
| touch "$tmpdir/protobuf+3.19.2/foo/runfile" | |
| touch "$tmpdir/config.json" | |
| mkdir -p "$tmpdir/my_module+" | |
| touch "$tmpdir/my_module+/runfile" | |
| mkdir -p "$tmpdir/my_module++ext+repo1" | |
| touch "$tmpdir/my_module++ext+repo1/runfile" | |
| mkdir -p "$tmpdir/repo2+" | |
| touch "$tmpdir/repo2+/runfile" | |
| [[ "$(rlocation "my_module/foo/runfile" "my_module++ext+repo1" || echo failed)" == "$tmpdir/my_module+/runfile" ]] || fail | |
| [[ "$(rlocation "repo1/foo/runfile" "my_module++ext+repo1" || echo failed)" == "$tmpdir/my_module++ext+repo1/runfile" ]] || fail | |
| [[ "$(rlocation "repo2+/foo/runfile" "my_module++ext+repo1" || echo failed)" == "$tmpdir/repo2+/runfile" ]] || fail | |
| } | |
| # Writes a script that reports the repository it lies in via the given function. | |
| # The function must not be defined by more than one script in a single test, as | |
| # runfiles_current_repository resolves the path of the script that defines it. | |
| function write_current_repository_lib() { | |
| local -r path="$1" | |
| local -r func="$2" | |
| local -r result="${3:-}" | |
| mkdir -p "$(dirname "$path")" | |
| if [[ -n "$result" ]]; then | |
| # A stale copy that must never be sourced. | |
| cat > "$path" << EOF | |
| function $func() { | |
| echo "$result" | |
| } | |
| EOF | |
| else | |
| cat > "$path" << EOF | |
| function $func() { | |
| runfiles_current_repository 1 | |
| } | |
| EOF | |
| fi | |
| } | |
| function test_current_repository_directory_based() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| export RUNFILES_DIR="${tmpdir}/mock/runfiles" | |
| export RUNFILES_MANIFEST_FILE= | |
| write_current_repository_lib "$RUNFILES_DIR/protobuf+3.19.2/foo/lib.sh" repo_of_other | |
| write_current_repository_lib "$RUNFILES_DIR/_main/bar/lib.sh" repo_of_main | |
| source "$runfiles_lib_path" | |
| source "$(rlocation "protobuf+3.19.2/foo/lib.sh" "")" || fail | |
| [[ "$(repo_of_other || echo failed)" == "protobuf+3.19.2" ]] || fail | |
| source "$(rlocation "_main/bar/lib.sh" "")" || fail | |
| [[ "$(repo_of_main || echo failed)" == "" ]] || fail | |
| } | |
| function test_current_repository_manifest_based() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| export RUNFILES_DIR= | |
| export RUNFILES_MANIFEST_FILE="$tmpdir/foo.runfiles_manifest" | |
| write_current_repository_lib "$tmpdir/protobuf+3.19.2/foo/lib.sh" repo_of_other | |
| write_current_repository_lib "$tmpdir/_main/bar/lib.sh" repo_of_main | |
| cat > "$RUNFILES_MANIFEST_FILE" << EOF | |
| protobuf+3.19.2/foo/lib.sh $tmpdir/protobuf+3.19.2/foo/lib.sh | |
| _main/bar/lib.sh $tmpdir/_main/bar/lib.sh | |
| EOF | |
| source "$runfiles_lib_path" | |
| source "$(rlocation "protobuf+3.19.2/foo/lib.sh" "")" || fail | |
| [[ "$(repo_of_other || echo failed)" == "protobuf+3.19.2" ]] || fail | |
| source "$(rlocation "_main/bar/lib.sh" "")" || fail | |
| [[ "$(repo_of_main || echo failed)" == "" ]] || fail | |
| } | |
| # Both envvars are set at the same time e.g. on Windows with --enable_runfiles, | |
| # where the runfiles directory contains the MANIFEST file that | |
| # runfiles_export_envvars promotes to RUNFILES_MANIFEST_FILE. The manifest maps | |
| # rlocation paths to the locations of the *original* files, so a caller that had | |
| # been looked up in the runfiles directory could never be found in it. | |
| function test_current_repository_directory_and_manifest_based() { | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| export RUNFILES_DIR="${tmpdir}/mock/runfiles" | |
| export RUNFILES_MANIFEST_FILE="$RUNFILES_DIR/MANIFEST" | |
| write_current_repository_lib "$tmpdir/protobuf+3.19.2/foo/lib.sh" repo_of_other | |
| write_current_repository_lib "$tmpdir/_main/bar/lib.sh" repo_of_main | |
| # The runfiles directory may hold stale contents, so the manifest wins. | |
| mkdir -p "$RUNFILES_DIR" | |
| write_current_repository_lib "$RUNFILES_DIR/protobuf+3.19.2/foo/lib.sh" repo_of_other stale | |
| write_current_repository_lib "$RUNFILES_DIR/_main/bar/lib.sh" repo_of_main stale | |
| cat > "$RUNFILES_MANIFEST_FILE" << EOF | |
| protobuf+3.19.2/foo/lib.sh $tmpdir/protobuf+3.19.2/foo/lib.sh | |
| _main/bar/lib.sh $tmpdir/_main/bar/lib.sh | |
| EOF | |
| source "$runfiles_lib_path" | |
| [[ "$(rlocation "protobuf+3.19.2/foo/lib.sh" "" || echo failed)" == "$tmpdir/protobuf+3.19.2/foo/lib.sh" ]] || fail | |
| source "$(rlocation "protobuf+3.19.2/foo/lib.sh" "")" || fail | |
| [[ "$(repo_of_other || echo failed)" == "protobuf+3.19.2" ]] || fail | |
| [[ "$(rlocation "_main/bar/lib.sh" "" || echo failed)" == "$tmpdir/_main/bar/lib.sh" ]] || fail | |
| source "$(rlocation "_main/bar/lib.sh" "")" || fail | |
| [[ "$(repo_of_main || echo failed)" == "" ]] || fail | |
| } | |
| function test_directory_based_envvars() { | |
| export RUNFILES_DIR=mock/runfiles | |
| export RUNFILES_MANIFEST_FILE= | |
| source "$runfiles_lib_path" | |
| runfiles_export_envvars | |
| [[ "${RUNFILES_DIR:-}" == "mock/runfiles" ]] || fail | |
| [[ -z "${RUNFILES_MANIFEST_FILE:-}" ]] || fail | |
| } | |
| # Through the wrapper, awk is on unless the caller has decided: AWK wins if | |
| # set, RULES_SHELL_RUNFILES_USE_AWK=0 keeps the shell loops. The wrapper must | |
| # not touch AWK or leave temporaries behind. Only meaningful when the library | |
| # under test is reached through the wrapper. | |
| function test_runfiles_bash_awk_default() { | |
| if [[ "$_RUNFILES_LIBRARY_FILE" != *runfiles.bash ]]; then | |
| return 0 | |
| fi | |
| export RUNFILES_DIR=mock/runfiles | |
| export RUNFILES_MANIFEST_FILE= | |
| ( | |
| unset AWK RULES_SHELL_RUNFILES_USE_AWK | |
| source "$runfiles_lib_path" || fail | |
| if command -v awk >&/dev/null; then | |
| [[ "${_RULES_SHELL_RUNFILES_AWK:-}" == awk ]] \ | |
| || fail "expected awk by default, got '${_RULES_SHELL_RUNFILES_AWK:-}'" | |
| else | |
| [[ -z "${_RULES_SHELL_RUNFILES_AWK:-}" ]] \ | |
| || fail "expected the shell loops without awk on PATH" | |
| fi | |
| [[ -z "${AWK+x}" ]] || fail "the wrapper leaked AWK='${AWK}' into the environment" | |
| [[ -z "${_rf_bash_dir+x}" ]] \ | |
| || fail "the wrapper leaked its temporaries into the environment" | |
| ) || fail | |
| ( | |
| unset AWK | |
| export RULES_SHELL_RUNFILES_USE_AWK=0 | |
| source "$runfiles_lib_path" || fail | |
| [[ -z "${_RULES_SHELL_RUNFILES_AWK:-}" ]] \ | |
| || fail "expected RULES_SHELL_RUNFILES_USE_AWK=0 to keep the shell loops" | |
| ) || fail | |
| ( | |
| unset RULES_SHELL_RUNFILES_USE_AWK | |
| export AWK="$(command -v awk || echo /no/such/awk)" | |
| source "$runfiles_lib_path" || fail | |
| [[ "$AWK" == /no/such/awk || "${_RULES_SHELL_RUNFILES_AWK:-}" == "$AWK" ]] \ | |
| || fail "expected a set AWK to be used as is, got '${_RULES_SHELL_RUNFILES_AWK:-}'" | |
| [[ -n "${AWK+x}" ]] || fail "the wrapper must not unset AWK" | |
| ) || fail | |
| } | |
| # The public API stays exported through the wrapper, so that a script which | |
| # inherited it across exec keeps finding it. Checked from a child bash, which | |
| # only sees what was exported. | |
| function test_runfiles_bash_exports_public_api() { | |
| if [[ "$_RUNFILES_LIBRARY_FILE" != *runfiles.bash ]]; then | |
| return 0 | |
| fi | |
| export RUNFILES_DIR=mock/runfiles | |
| export RUNFILES_MANIFEST_FILE= | |
| source "$runfiles_lib_path" | |
| local f | |
| for f in rlocation runfiles_export_envvars runfiles_current_repository \ | |
| runfiles_rlocation_checked; do | |
| bash -c "[[ \"\$(type -t $f)\" == function ]]" \ | |
| || fail "expected $f to be exported as a function" | |
| done | |
| bash -c '[[ -n "${RUNFILES_REPO_MAPPING+x}" ]]' \ | |
| || fail "expected RUNFILES_REPO_MAPPING to be exported" | |
| } | |
| # The v4 snippet, taken from the wrapper's own header, and the wrapper have to | |
| # work on a host without awk: the snippet's awk attempt fails and its shell | |
| # loop takes over, and runfiles.sh keeps its shell loops instead of the awk the | |
| # wrapper asks for. | |
| function test_runfiles_bash_survives_without_awk() { | |
| if [[ "$_RUNFILES_LIBRARY_FILE" != *runfiles.bash ]]; then | |
| return 0 | |
| fi | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| local lib_dir="$tmpdir/lib/bazel_tools/tools/bash/runfiles" | |
| mkdir -p "$lib_dir" | |
| cp "$runfiles_lib_path" "$lib_dir/runfiles.bash" | |
| cp "$(dirname "$runfiles_lib_path")/runfiles.sh" "$lib_dir/runfiles.sh" | |
| touch "$tmpdir/f" | |
| printf 'r/f %s\nbazel_tools/tools/bash/runfiles/runfiles.bash %s\n' "$tmpdir/f" \ | |
| "$lib_dir/runfiles.bash" > "$tmpdir/manifest" | |
| { | |
| echo '#!/usr/bin/env bash' | |
| sed -n '/^# *# --- begin runfiles.bash initialization v4 ---/,/^# *# --- end runfiles.bash initialization v4 ---/p' \ | |
| "$runfiles_lib_path" | sed 's/^# //' | |
| echo 'rlocation r/f' | |
| } > "$tmpdir/script.sh" | |
| local actual | |
| actual=$(RUNFILES_DIR= RUNFILES_MANIFEST_FILE="$tmpdir/manifest" PATH=/nonexistent "$BASH" "$tmpdir/script.sh") | |
| [[ "$actual" == "$tmpdir/f" ]] || fail "expected $tmpdir/f without awk, got: $actual" | |
| } | |
| # The v4 snippet sources the first library it finds, so a library that fails | |
| # to load has to abort the script rather than leave it without rlocation. A | |
| # runfiles directory from before runfiles.sh existed has runfiles.bash and | |
| # nothing for it to source. | |
| function test_runfiles_bash_init_aborts_when_library_fails() { | |
| if [[ "$_RUNFILES_LIBRARY_FILE" != *runfiles.bash ]]; then | |
| return 0 | |
| fi | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| mkdir -p "$tmpdir/stale/bazel_tools/tools/bash/runfiles" | |
| cp "$runfiles_lib_path" "$tmpdir/stale/bazel_tools/tools/bash/runfiles/runfiles.bash" | |
| touch "$tmpdir/f" | |
| printf 'r/f %s\n' "$tmpdir/f" > "$tmpdir/manifest" | |
| { | |
| echo '#!/usr/bin/env bash' | |
| sed -n '/^# *# --- begin runfiles.bash initialization v4 ---/,/^# *# --- end runfiles.bash initialization v4 ---/p' \ | |
| "$runfiles_lib_path" | sed 's/^# //' | |
| echo 'echo reached' | |
| } > "$tmpdir/script.sh" | |
| local output | |
| if output=$(RUNFILES_DIR="$tmpdir/stale" RUNFILES_MANIFEST_FILE="$tmpdir/manifest" "$BASH" "$tmpdir/script.sh" 2>&1); then | |
| fail "expected the snippet to abort, got: $output" | |
| fi | |
| [[ "$output" != *reached* ]] || fail "expected the script not to run past the snippet" | |
| } | |
| # awk takes an operand of the form name=value as an assignment, so the v4 | |
| # snippet has to reach a manifest with such a path some other way. | |
| function test_runfiles_bash_manifest_path_with_equals_sign() { | |
| if [[ "$_RUNFILES_LIBRARY_FILE" != *runfiles.bash ]]; then | |
| return 0 | |
| fi | |
| local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)" | |
| local lib_dir="$tmpdir/lib/bazel_tools/tools/bash/runfiles" | |
| mkdir -p "$lib_dir" | |
| cp "$runfiles_lib_path" "$lib_dir/runfiles.bash" | |
| cp "$(dirname "$runfiles_lib_path")/runfiles.sh" "$lib_dir/runfiles.sh" | |
| touch "$tmpdir/f" | |
| printf 'r/f %s\nbazel_tools/tools/bash/runfiles/runfiles.bash %s\n' "$tmpdir/f" \ | |
| "$lib_dir/runfiles.bash" > "$tmpdir/out=1.manifest" | |
| { | |
| echo '#!/usr/bin/env bash' | |
| sed -n '/^# *# --- begin runfiles.bash initialization v4 ---/,/^# *# --- end runfiles.bash initialization v4 ---/p' \ | |
| "$runfiles_lib_path" | sed 's/^# //' | |
| echo 'rlocation r/f' | |
| } > "$tmpdir/script.sh" | |
| local actual | |
| actual=$(RUNFILES_DIR= RUNFILES_MANIFEST_FILE="$tmpdir/out=1.manifest" "$BASH" "$tmpdir/script.sh") | |
| [[ "$actual" == "$tmpdir/f" ]] || fail "expected the snippet to find the library through out=1.manifest, got: $actual" | |
| } | |
| function test_with_grep_env_vars_set() { | |
| # These influence how grep behaves. | |
| export GREP_COLOR='1;35;40' | |
| export GREP_OPTIONS='--color=always' | |
| test_init_manifest_based_runfiles | |
| } | |
| function main() { | |
| local -r manifest_file="${RUNFILES_MANIFEST_FILE:-}" | |
| local -r dir="${RUNFILES_DIR:-}" | |
| local -r runfiles_lib_path=$(find_runfiles_lib) | |
| local -r tests=$(declare -F | grep " -f test" | awk '{print $3}') | |
| local failure=0 | |
| for t in $tests; do | |
| export RUNFILES_MANIFEST_FILE="$manifest_file" | |
| export RUNFILES_DIR="$dir" | |
| if ! ($t); then | |
| failure=1 | |
| fi | |
| done | |
| return $failure | |
| } | |
| main |