1#!/usr/bin/env bash 2 3# Copyright 2019 The Bazel Authors. All rights reserved. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# --- begin runfiles.bash initialization --- 18# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). 19set -euo pipefail 20if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then 21 if [[ -f "$0.runfiles_manifest" ]]; then 22 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" 23 elif [[ -f "$0.runfiles/MANIFEST" ]]; then 24 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" 25 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then 26 export RUNFILES_DIR="$0.runfiles" 27 fi 28fi 29if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then 30 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" 31elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then 32 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ 33 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" 34else 35 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" 36 exit 1 37fi 38# --- end runfiles.bash initialization --- 39 40source "$(rlocation bazel_skylib/tests/unittest.bash)" \ 41 || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } 42 43function import_diff_test() { 44 local -r repo="$1" 45 mkdir -p "${repo}/rules" 46 touch "${repo}/WORKSPACE" 47 ln -sf "$(rlocation bazel_skylib/rules/diff_test.bzl)" \ 48 "${repo}/rules/diff_test.bzl" 49 echo "exports_files(['diff_test.bzl'])" > "${repo}/rules/BUILD" 50} 51 52function assert_simple_diff_test() { 53 local -r flag="$1" 54 local -r ws="${TEST_TMPDIR}/$2" 55 local -r subdir="$3" 56 57 import_diff_test "$ws" 58 touch "$ws/WORKSPACE" 59 mkdir -p "$ws/$subdir" 60 cat >"$ws/${subdir}BUILD" <<'eof' 61load("//rules:diff_test.bzl", "diff_test") 62 63diff_test( 64 name = "same", 65 file1 = "a.txt", 66 file2 = "a.txt", 67) 68 69diff_test( 70 name = "different", 71 file1 = "a.txt", 72 file2 = "b.txt", 73) 74eof 75 echo foo > "$ws/$subdir/a.txt" 76 echo bar > "$ws/$subdir/b.txt" 77 78 (cd "$ws" && \ 79 bazel test "$flag" "//${subdir%/}:same" --test_output=errors 1>"$TEST_log" 2>&1 \ 80 || fail "expected success") 81 82 (cd "$ws" && \ 83 bazel test "$flag" "//${subdir%/}:different" --test_output=errors 1>"$TEST_log" 2>&1 \ 84 && fail "expected failure" || true) 85 expect_log "FAIL: files \"${subdir}a.txt\" and \"${subdir}b.txt\" differ" 86} 87 88function assert_from_ext_repo() { 89 local -r flag="$1" 90 local -r ws="${TEST_TMPDIR}/$2" 91 92 # Import the rule to an external repository. 93 import_diff_test "$ws/bzl" 94 mkdir -p "$ws/ext1/foo" "$ws/main/ext1/foo" "$ws/ext2/foo" "$ws/main/ext2/foo" 95 cat >"$ws/main/WORKSPACE" <<'eof' 96local_repository( 97 name = "bzl", 98 path = "../bzl", 99) 100 101local_repository( 102 name = "ext1", 103 path = "../ext1", 104) 105 106local_repository( 107 name = "ext2", 108 path = "../ext2", 109) 110eof 111 112 # @ext1 has source files 113 touch "$ws/ext1/WORKSPACE" 114 echo 'exports_files(["foo.txt"])' >"$ws/ext1/foo/BUILD" 115 echo 'foo' > "$ws/ext1/foo/foo.txt" 116 117 # @//ext1/foo has different files than @ext1//foo 118 echo 'exports_files(["foo.txt"])' >"$ws/main/ext1/foo/BUILD" 119 echo 'not foo' > "$ws/main/ext1/foo/foo.txt" 120 121 # @ext2 has generated files 122 touch "$ws/ext2/WORKSPACE" 123 cat >"$ws/ext2/foo/BUILD" <<'eof' 124genrule( 125 name = "gen", 126 outs = [ 127 "foo.txt", 128 "bar.txt", 129 ], 130 cmd = "echo 'foo' > $(location foo.txt) && echo 'bar' > $(location bar.txt)", 131 visibility = ["//visibility:public"], 132) 133eof 134 135 # @//ext2/foo has different files than @ext2//foo 136 cat >"$ws/main/ext2/foo/BUILD" <<'eof' 137genrule( 138 name = "gen", 139 outs = ["foo.txt"], 140 cmd = "echo 'not foo' > $@", 141 visibility = ["//visibility:public"], 142) 143eof 144 145 cat >"$ws/main/BUILD" <<'eof' 146load("@bzl//rules:diff_test.bzl", "diff_test") 147 148diff_test( 149 name = "same", 150 file1 = "@ext1//foo:foo.txt", 151 file2 = "@ext2//foo:foo.txt", 152) 153 154diff_test( 155 name = "different1", 156 file1 = "@ext1//foo:foo.txt", 157 file2 = "@ext2//foo:bar.txt", 158) 159 160diff_test( 161 name = "different2", 162 file1 = "@ext1//foo:foo.txt", 163 file2 = "//ext1/foo:foo.txt", 164) 165 166diff_test( 167 name = "different3", 168 file1 = "//ext2/foo:foo.txt", 169 file2 = "@ext2//foo:foo.txt", 170) 171eof 172 173 (cd "$ws/main" && \ 174 bazel test "$flag" //:same --test_output=errors 1>"$TEST_log" 2>&1 \ 175 || fail "expected success") 176 177 (cd "$ws/main" && \ 178 bazel test "$flag" //:different1 --test_output=errors 1>"$TEST_log" 2>&1 \ 179 && fail "expected failure" || true) 180 expect_log 'FAIL: files "external/ext1/foo/foo.txt" and "external/ext2/foo/bar.txt" differ' 181 182 (cd "$ws/main" && \ 183 bazel test "$flag" //:different2 --test_output=errors 1>"$TEST_log" 2>&1 \ 184 && fail "expected failure" || true) 185 expect_log 'FAIL: files "external/ext1/foo/foo.txt" and "ext1/foo/foo.txt" differ' 186 187 (cd "$ws/main" && \ 188 bazel test "$flag" //:different3 --test_output=errors 1>"$TEST_log" 2>&1 \ 189 && fail "expected failure" || true) 190 expect_log 'FAIL: files "ext2/foo/foo.txt" and "external/ext2/foo/foo.txt" differ' 191} 192 193function test_simple_diff_test_with_legacy_external_runfiles() { 194 assert_simple_diff_test "--legacy_external_runfiles" "${FUNCNAME[0]}" "" 195} 196 197function test_simple_diff_test_without_legacy_external_runfiles() { 198 assert_simple_diff_test "--nolegacy_external_runfiles" "${FUNCNAME[0]}" "" 199} 200 201function test_directory_named_external_with_legacy_external_runfiles() { 202 assert_simple_diff_test "--legacy_external_runfiles" "${FUNCNAME[0]}" "path/to/direcotry/external/in/name/" 203} 204 205function test_directory_named_external_without_legacy_external_runfiles() { 206 assert_simple_diff_test "--nolegacy_external_runfiles" "${FUNCNAME[0]}" "path/to/direcotry/external/in/name/" 207} 208 209function test_from_ext_repo_with_legacy_external_runfiles() { 210 assert_from_ext_repo "--legacy_external_runfiles" "${FUNCNAME[0]}" 211} 212 213function test_from_ext_repo_without_legacy_external_runfiles() { 214 assert_from_ext_repo "--nolegacy_external_runfiles" "${FUNCNAME[0]}" 215} 216 217cd "$TEST_TMPDIR" 218run_suite "diff_test_tests test suite" 219