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