xref: /aosp_15_r20/external/bazel-skylib/tests/diff_test/diff_test_tests.sh (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
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 $TEST_WORKSPACE/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  mkdir -p "${repo}/lib"
47  touch "${repo}/lib/BUILD"
48  touch "${repo}/WORKSPACE"
49  ln -sf "$(rlocation $TEST_WORKSPACE/rules/diff_test.bzl)" \
50         "${repo}/rules/diff_test.bzl"
51  ln -sf "$(rlocation $TEST_WORKSPACE/lib/shell.bzl)" \
52         "${repo}/lib/shell.bzl"
53  echo "exports_files(['diff_test.bzl'])" > "${repo}/rules/BUILD"
54}
55
56function assert_simple_diff_test() {
57  local -r flags="$1"
58  local -r ws="${TEST_TMPDIR}/$2"
59  local -r subdir="$3"
60
61  import_diff_test "$ws"
62  touch "$ws/WORKSPACE"
63  mkdir -p "$ws/$subdir"
64  cat >"$ws/${subdir}BUILD" <<'eof'
65load("//rules:diff_test.bzl", "diff_test")
66
67diff_test(
68    name = "same",
69    file1 = "a.txt",
70    file2 = "a.txt",
71)
72
73diff_test(
74    name = "different",
75    file1 = "a.txt",
76    file2 = "b.txt",
77)
78eof
79  echo foo > "$ws/$subdir/a.txt"
80  echo bar > "$ws/$subdir/b.txt"
81
82  (cd "$ws" && \
83   bazel test ${flags} "//${subdir%/}:same" --test_output=errors 1>"$TEST_log" 2>&1 \
84     || fail "expected success")
85
86  (cd "$ws" && \
87   bazel test ${flags} "//${subdir%/}:different" --test_output=errors 1>"$TEST_log" 2>&1 \
88     && fail "expected failure" || true)
89  expect_log "FAIL: files \"${subdir}a.txt\" and \"${subdir}b.txt\" differ"
90}
91
92function assert_from_ext_repo() {
93  local -r flags="$1"
94  local -r ws="${TEST_TMPDIR}/$2"
95
96  # Import the rule to an external repository.
97  import_diff_test "$ws/bzl"
98  mkdir -p "$ws/ext1/foo" "$ws/main/ext1/foo" "$ws/ext2/foo" "$ws/main/ext2/foo"
99  cat >"$ws/main/WORKSPACE" <<'eof'
100local_repository(
101    name = "bzl",
102    path = "../bzl",
103)
104
105local_repository(
106    name = "ext1",
107    path = "../ext1",
108)
109
110local_repository(
111    name = "ext2",
112    path = "../ext2",
113)
114eof
115
116  # @ext1 has source files
117  touch "$ws/ext1/WORKSPACE"
118  echo 'exports_files(["foo.txt"])' >"$ws/ext1/foo/BUILD"
119  echo 'foo' > "$ws/ext1/foo/foo.txt"
120
121  # @//ext1/foo has different files than @ext1//foo
122  echo 'exports_files(["foo.txt"])' >"$ws/main/ext1/foo/BUILD"
123  echo 'not foo' > "$ws/main/ext1/foo/foo.txt"
124
125  # @ext2 has generated files
126  touch "$ws/ext2/WORKSPACE"
127  cat >"$ws/ext2/foo/BUILD" <<'eof'
128genrule(
129    name = "gen",
130    outs = [
131        "foo.txt",
132        "bar.txt",
133    ],
134    cmd = "echo 'foo' > $(location foo.txt) && echo 'bar' > $(location bar.txt)",
135    visibility = ["//visibility:public"],
136)
137eof
138
139  # @//ext2/foo has different files than @ext2//foo
140  cat >"$ws/main/ext2/foo/BUILD" <<'eof'
141genrule(
142    name = "gen",
143    outs = ["foo.txt"],
144    cmd = "echo 'not foo' > $@",
145    visibility = ["//visibility:public"],
146)
147eof
148
149  cat >"$ws/main/BUILD" <<'eof'
150load("@bzl//rules:diff_test.bzl", "diff_test")
151
152diff_test(
153    name = "same",
154    file1 = "@ext1//foo:foo.txt",
155    file2 = "@ext2//foo:foo.txt",
156)
157
158diff_test(
159    name = "different1",
160    file1 = "@ext1//foo:foo.txt",
161    file2 = "@ext2//foo:bar.txt",
162)
163
164diff_test(
165    name = "different2",
166    file1 = "@ext1//foo:foo.txt",
167    file2 = "//ext1/foo:foo.txt",
168)
169
170diff_test(
171    name = "different3",
172    file1 = "//ext2/foo:foo.txt",
173    file2 = "@ext2//foo:foo.txt",
174)
175eof
176
177  (cd "$ws/main" && \
178   bazel test ${flags} //:same --test_output=errors 1>"$TEST_log" 2>&1 \
179     || fail "expected success")
180
181  (cd "$ws/main" && \
182   bazel test ${flags} //:different1 --test_output=errors 1>"$TEST_log" 2>&1 \
183     && fail "expected failure" || true)
184  expect_log 'FAIL: files "external/ext1/foo/foo.txt" and "external/ext2/foo/bar.txt" differ'
185
186  (cd "$ws/main" && \
187   bazel test ${flags} //:different2 --test_output=errors 1>"$TEST_log" 2>&1 \
188     && fail "expected failure" || true)
189  expect_log 'FAIL: files "external/ext1/foo/foo.txt" and "ext1/foo/foo.txt" differ'
190
191  (cd "$ws/main" && \
192   bazel test ${flags} //:different3 --test_output=errors 1>"$TEST_log" 2>&1 \
193     && fail "expected failure" || true)
194  expect_log 'FAIL: files "ext2/foo/foo.txt" and "external/ext2/foo/foo.txt" differ'
195}
196
197function test_simple_diff_test_with_legacy_external_runfiles() {
198  assert_simple_diff_test "--enable_runfiles --legacy_external_runfiles" "${FUNCNAME[0]}" ""
199}
200
201function test_simple_diff_test_without_legacy_external_runfiles() {
202  assert_simple_diff_test "--enable_runfiles --nolegacy_external_runfiles" "${FUNCNAME[0]}" ""
203}
204
205function test_simple_diff_test_with_manifest() {
206  assert_simple_diff_test "--noenable_runfiles" "${FUNCNAME[0]}" ""
207}
208
209function test_directory_named_external_with_legacy_external_runfiles() {
210  assert_simple_diff_test "--enable_runfiles --legacy_external_runfiles" "${FUNCNAME[0]}" "path/to/directory/external/in/name/"
211}
212
213function test_directory_named_external_without_legacy_external_runfiles() {
214  assert_simple_diff_test "--enable_runfiles --nolegacy_external_runfiles" "${FUNCNAME[0]}" "path/to/directory/external/in/name/"
215}
216
217function test_directory_named_external_with_manifest() {
218  assert_simple_diff_test "--noenable_runfiles" "${FUNCNAME[0]}" "path/to/directory/external/in/name/"
219}
220
221function test_from_ext_repo_with_legacy_external_runfiles() {
222  assert_from_ext_repo "--enable_runfiles --legacy_external_runfiles" "${FUNCNAME[0]}"
223}
224
225function test_from_ext_repo_without_legacy_external_runfiles() {
226  assert_from_ext_repo "--enable_runfiles --nolegacy_external_runfiles" "${FUNCNAME[0]}"
227}
228
229function test_from_ext_repo_with_manifest() {
230  assert_from_ext_repo "--noenable_runfiles" "${FUNCNAME[0]}"
231}
232
233function test_failure_message() {
234  local -r ws="${TEST_TMPDIR}/${FUNCNAME[0]}"
235
236  import_diff_test "$ws"
237  touch "$ws/WORKSPACE"
238  cat >"$ws/BUILD" <<'eof'
239load("//rules:diff_test.bzl", "diff_test")
240
241diff_test(
242    name = "different_with_message",
243    failure_message = "This is an `$error`",  # TODO(arostovtsev): also test Windows cmd.exe escapes when https://github.com/bazelbuild/bazel-skylib/pull/363 is merged
244    file1 = "a.txt",
245    file2 = "b.txt",
246)
247
248diff_test(
249    name = "different_without_message",
250    file1 = "c.txt",
251    file2 = "d.txt",
252)
253eof
254  echo foo > "$ws/a.txt"
255  echo bar > "$ws/b.txt"
256  echo foo > "$ws/c.txt"
257  echo bar > "$ws/d.txt"
258
259  (cd "$ws" && \
260   bazel test //:different_with_message --test_output=errors 1>"$TEST_log" 2>&1 \
261     && fail "expected failure" || true)
262  # TODO(arostovtsev): also test Windows cmd.exe escapes when https://github.com/bazelbuild/bazel-skylib/pull/363 is merged
263  expect_log "FAIL: files \"a.txt\" and \"b.txt\" differ. This is an \`\$error\`"
264
265  (cd "$ws" && \
266   bazel test //:different_without_message --test_output=errors 1>"$TEST_log" 2>&1 \
267     && fail "expected failure" || true)
268  expect_log "FAIL: files \"c.txt\" and \"d.txt\" differ. $"
269}
270
271cd "$TEST_TMPDIR"
272run_suite "diff_test_tests test suite"
273