blob: c26ff63b212b2bc74495326ebc1dc379524d528f [file] [log] [blame]
Fabian Meumertzheim31872502024-06-05 11:45:25 -07001#!/bin/bash
2#
3# Copyright 2024 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# bash_completion_test.sh: tests of bash command completion.
18
19# --- begin runfiles.bash initialization v3 ---
20# Copy-pasted from the Bazel Bash runfiles library v3.
21set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
22source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
23 source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
24 source "$0.runfiles/$f" 2>/dev/null || \
25 source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
26 source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
27 { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
28# --- end runfiles.bash initialization v3 ---
29
30: ${DIR:=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}
31source ${DIR}/testenv.sh || { echo "testenv.sh not found!" >&2; exit 1; }
32
33JQ_SCRIPT_FILE="$(rlocation io_bazel/scripts/bazel-lockfile-merge.jq)"
34JQ="$(rlocation $JQ_RLOCATIONPATH)"
35
36function do_merge() {
37 local base="$1"
38 local left="$2"
39 local right="$3"
40
41 assert_not_contains "'" "$JQ_SCRIPT_FILE"
42 # Simulate the setup of a git merge driver, which can only be configured as a
43 # single command passed to sh and overwrites the "left" version. The check
44 # above verifies that wrapping the jq script in single quotes is sufficient to
45 # escape it here.
46 jq_script="$(cat "$JQ_SCRIPT_FILE")"
47 merge_cmd="\"$JQ\" -s '${jq_script}' -- $base $left $right > ${left}.jq_tmp && mv ${left}.jq_tmp ${left}"
48 sh -c "${merge_cmd}" || fail "merge failed"
49}
50
51function test_synthetic_merge() {
52 cat > base <<'EOF'
53{
54 "lockFileVersion": 10,
55 "registryFileHashes": {
56 "https://example.org/modules/bar/0.9/MODULE.bazel": "1234",
57 "https://example.org/modules/foo/1.0/MODULE.bazel": "1234"
58 },
59 "selectedYankedVersions": {},
60 "moduleExtensions": {
61 "//:rbe_extensions.bzl%bazel_rbe_deps": {
62 "general": {
63 "bzlTransitiveDigest": "3Qxu4ylcYD3RTWLhk5k/59p/CwZ4tLdSgYnmBXYgAtc=",
64 "recordedFileInputs": {},
65 "recordedDirentsInputs": {},
66 "envVariables": {},
67 "generatedRepoSpecs": {
68 "rbe_ubuntu2004": {
Xdng Yng60924fd2024-07-31 01:46:57 -070069 "bzlFile": "@@+bazel_test_deps+bazelci_rules//:rbe_repo.bzl",
Fabian Meumertzheim31872502024-06-05 11:45:25 -070070 "ruleClassName": "rbe_preconfig",
71 "attributes": {
72 "toolchain": "ubuntu2004"
73 }
74 }
75 },
76 "recordedRepoMappingEntries": [
77 [
78 "",
79 "bazelci_rules",
Xdng Yng60924fd2024-07-31 01:46:57 -070080 "+bazel_test_deps+bazelci_rules"
Fabian Meumertzheim31872502024-06-05 11:45:25 -070081 ]
82 ]
83 }
84 },
Xdng Yng60924fd2024-07-31 01:46:57 -070085 "@@rules_python+//python/extensions:python.bzl%python": {
Fabian Meumertzheim31872502024-06-05 11:45:25 -070086 "general": {
87 "repo1": "old_args"
88 }
89 }
90 }
91}
92EOF
93 cat > left <<'EOF'
94{
95 "lockFileVersion": 10,
96 "registryFileHashes": {
97 "https://example.org/modules/bar/0.9/MODULE.bazel": "1234",
98 "https://example.org/modules/baz/2.0/MODULE.bazel": "1234",
99 "https://example.org/modules/foo/1.0/MODULE.bazel": "1234"
100 },
101 "selectedYankedVersions": {
102 "bbb@1.0": "also dubious"
103 },
104 "moduleExtensions": {
Xdng Yng60924fd2024-07-31 01:46:57 -0700105 "@@rules_python+//python/extensions:python.bzl%python": {
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700106 "general": {
107 "repo1": "new_args"
108 }
109 },
Xdng Yng60924fd2024-07-31 01:46:57 -0700110 "@@rules_python+//python/extensions/private:internal_deps.bzl%internal_deps": {
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700111 "os:linux,arch:aarch64": {
112 "repo2": "aarch64_args"
113 }
114 }
115 }
116}
117EOF
118 cat > right <<'EOF'
119{
120 "lockFileVersion": 10,
121 "registryFileHashes": {
122 "https://example.org/modules/bar/0.9/MODULE.bazel": "1234",
123 "https://example.org/modules/bar/1.0/MODULE.bazel": "1234",
124 "https://example.org/modules/foo/1.0/MODULE.bazel": "1234"
125 },
126 "selectedYankedVersions": {
127 "aaa@1.0": "dubious"
128 },
129 "moduleExtensions": {
130 "//:rbe_extensions.bzl%bazel_rbe_deps": {
131 "general": {
132 "bzlTransitiveDigest": "changed",
133 "recordedFileInputs": {},
134 "recordedDirentsInputs": {},
135 "envVariables": {},
136 "generatedRepoSpecs": {
137 "rbe_ubuntu2004": {
Xdng Yng60924fd2024-07-31 01:46:57 -0700138 "bzlFile": "@@+bazel_test_deps+bazelci_rules//:rbe_repo.bzl",
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700139 "ruleClassName": "rbe_preconfig",
140 "attributes": {
141 "toolchain": "ubuntu2004"
142 }
143 }
144 },
145 "recordedRepoMappingEntries": [
146 [
147 "",
148 "bazelci_rules",
Xdng Yng60924fd2024-07-31 01:46:57 -0700149 "+bazel_test_deps+bazelci_rules"
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700150 ]
151 ]
152 }
153 },
Xdng Yng60924fd2024-07-31 01:46:57 -0700154 "@@rules_python+//python/extensions:python.bzl%python": {
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700155 "general": {
156 "repo1": "old_args"
157 }
158 },
Xdng Yng60924fd2024-07-31 01:46:57 -0700159 "@@rules_python+//python/extensions/private:internal_deps.bzl%internal_deps": {
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700160 "os:linux,arch:amd64": {
161 "repo2": "amd64_args"
162 }
163 }
164 }
165}
166EOF
167 cat > expected <<'EOF'
168{
169 "lockFileVersion": 10,
170 "registryFileHashes": {
171 "https://example.org/modules/bar/0.9/MODULE.bazel": "1234",
172 "https://example.org/modules/bar/1.0/MODULE.bazel": "1234",
173 "https://example.org/modules/baz/2.0/MODULE.bazel": "1234",
174 "https://example.org/modules/foo/1.0/MODULE.bazel": "1234"
175 },
176 "selectedYankedVersions": {
177 "aaa@1.0": "dubious",
178 "bbb@1.0": "also dubious"
179 },
180 "moduleExtensions": {
181 "//:rbe_extensions.bzl%bazel_rbe_deps": {
182 "general": {
183 "bzlTransitiveDigest": "changed",
184 "recordedFileInputs": {},
185 "recordedDirentsInputs": {},
186 "envVariables": {},
187 "generatedRepoSpecs": {
188 "rbe_ubuntu2004": {
Xdng Yng60924fd2024-07-31 01:46:57 -0700189 "bzlFile": "@@+bazel_test_deps+bazelci_rules//:rbe_repo.bzl",
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700190 "ruleClassName": "rbe_preconfig",
191 "attributes": {
192 "toolchain": "ubuntu2004"
193 }
194 }
195 },
196 "recordedRepoMappingEntries": [
197 [
198 "",
199 "bazelci_rules",
Xdng Yng60924fd2024-07-31 01:46:57 -0700200 "+bazel_test_deps+bazelci_rules"
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700201 ]
202 ]
203 }
204 },
Xdng Yng60924fd2024-07-31 01:46:57 -0700205 "@@rules_python+//python/extensions:python.bzl%python": {
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700206 "general": {
207 "repo1": "new_args"
208 }
209 },
Xdng Yng60924fd2024-07-31 01:46:57 -0700210 "@@rules_python+//python/extensions/private:internal_deps.bzl%internal_deps": {
Fabian Meumertzheim31872502024-06-05 11:45:25 -0700211 "os:linux,arch:aarch64": {
212 "repo2": "aarch64_args"
213 },
214 "os:linux,arch:amd64": {
215 "repo2": "amd64_args"
216 }
217 }
218 }
219}
220EOF
221
222 do_merge base left right
223 diff -u expected left || fail "output differs"
224}
225
226function test_complex_identity_merge() {
227 test_lockfile="$(rlocation io_bazel/src/test/tools/bzlmod/MODULE.bazel.lock)"
228 cp "$test_lockfile" base
229 cp "$test_lockfile" left
230 cp "$test_lockfile" right
231
232 do_merge base left right
233 diff -u $test_lockfile left || fail "output differs"
234}
235
236function test_merge_across_versions() {
237 test_lockfile="$(rlocation io_bazel/src/test/tools/bzlmod/MODULE.bazel.lock)"
238 cp "$test_lockfile" base
239 cp "$test_lockfile" left
240 cat > right <<'EOF'
241{
242 "lockFileVersion": 9,
243 "weirdField": {}
244}
245EOF
246
247 do_merge base left right
248 diff -u $test_lockfile left || fail "output differs"
249}
250
251function test_outdated_versions_only() {
252 cat > base <<'EOF'
253{
254 "lockFileVersion": 9,
255 "weirdField": {}
256}
257EOF
258 cat > left <<'EOF'
259{
260 "lockFileVersion": 8
261}
262EOF
263 cat > right <<'EOF'
264{
265 "lockFileVersion": 7
266}
267EOF
268
269 do_merge base left right
270 diff -u base left || fail "output differs"
271}
272
273run_suite "Tests of bash completion of 'blaze' command."