Fabian Meumertzheim | 3187250 | 2024-06-05 11:45:25 -0700 | [diff] [blame] | 1 | #!/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. |
| 21 | set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash |
| 22 | source "${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)} |
| 31 | source ${DIR}/testenv.sh || { echo "testenv.sh not found!" >&2; exit 1; } |
| 32 | |
| 33 | JQ_SCRIPT_FILE="$(rlocation io_bazel/scripts/bazel-lockfile-merge.jq)" |
| 34 | JQ="$(rlocation $JQ_RLOCATIONPATH)" |
| 35 | |
| 36 | function 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 | |
| 51 | function 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": { |
| 69 | "bzlFile": "@@_main~bazel_test_deps~bazelci_rules//:rbe_repo.bzl", |
| 70 | "ruleClassName": "rbe_preconfig", |
| 71 | "attributes": { |
| 72 | "toolchain": "ubuntu2004" |
| 73 | } |
| 74 | } |
| 75 | }, |
| 76 | "recordedRepoMappingEntries": [ |
| 77 | [ |
| 78 | "", |
| 79 | "bazelci_rules", |
| 80 | "_main~bazel_test_deps~bazelci_rules" |
| 81 | ] |
| 82 | ] |
| 83 | } |
| 84 | }, |
| 85 | "@@rules_python~//python/extensions:python.bzl%python": { |
| 86 | "general": { |
| 87 | "repo1": "old_args" |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | EOF |
| 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": { |
| 105 | "@@rules_python~//python/extensions:python.bzl%python": { |
| 106 | "general": { |
| 107 | "repo1": "new_args" |
| 108 | } |
| 109 | }, |
| 110 | "@@rules_python~//python/extensions/private:internal_deps.bzl%internal_deps": { |
| 111 | "os:linux,arch:aarch64": { |
| 112 | "repo2": "aarch64_args" |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | EOF |
| 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": { |
| 138 | "bzlFile": "@@_main~bazel_test_deps~bazelci_rules//:rbe_repo.bzl", |
| 139 | "ruleClassName": "rbe_preconfig", |
| 140 | "attributes": { |
| 141 | "toolchain": "ubuntu2004" |
| 142 | } |
| 143 | } |
| 144 | }, |
| 145 | "recordedRepoMappingEntries": [ |
| 146 | [ |
| 147 | "", |
| 148 | "bazelci_rules", |
| 149 | "_main~bazel_test_deps~bazelci_rules" |
| 150 | ] |
| 151 | ] |
| 152 | } |
| 153 | }, |
| 154 | "@@rules_python~//python/extensions:python.bzl%python": { |
| 155 | "general": { |
| 156 | "repo1": "old_args" |
| 157 | } |
| 158 | }, |
| 159 | "@@rules_python~//python/extensions/private:internal_deps.bzl%internal_deps": { |
| 160 | "os:linux,arch:amd64": { |
| 161 | "repo2": "amd64_args" |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | EOF |
| 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": { |
| 189 | "bzlFile": "@@_main~bazel_test_deps~bazelci_rules//:rbe_repo.bzl", |
| 190 | "ruleClassName": "rbe_preconfig", |
| 191 | "attributes": { |
| 192 | "toolchain": "ubuntu2004" |
| 193 | } |
| 194 | } |
| 195 | }, |
| 196 | "recordedRepoMappingEntries": [ |
| 197 | [ |
| 198 | "", |
| 199 | "bazelci_rules", |
| 200 | "_main~bazel_test_deps~bazelci_rules" |
| 201 | ] |
| 202 | ] |
| 203 | } |
| 204 | }, |
| 205 | "@@rules_python~//python/extensions:python.bzl%python": { |
| 206 | "general": { |
| 207 | "repo1": "new_args" |
| 208 | } |
| 209 | }, |
| 210 | "@@rules_python~//python/extensions/private:internal_deps.bzl%internal_deps": { |
| 211 | "os:linux,arch:aarch64": { |
| 212 | "repo2": "aarch64_args" |
| 213 | }, |
| 214 | "os:linux,arch:amd64": { |
| 215 | "repo2": "amd64_args" |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | EOF |
| 221 | |
| 222 | do_merge base left right |
| 223 | diff -u expected left || fail "output differs" |
| 224 | } |
| 225 | |
| 226 | function 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 | |
| 236 | function 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 | } |
| 245 | EOF |
| 246 | |
| 247 | do_merge base left right |
| 248 | diff -u $test_lockfile left || fail "output differs" |
| 249 | } |
| 250 | |
| 251 | function test_outdated_versions_only() { |
| 252 | cat > base <<'EOF' |
| 253 | { |
| 254 | "lockFileVersion": 9, |
| 255 | "weirdField": {} |
| 256 | } |
| 257 | EOF |
| 258 | cat > left <<'EOF' |
| 259 | { |
| 260 | "lockFileVersion": 8 |
| 261 | } |
| 262 | EOF |
| 263 | cat > right <<'EOF' |
| 264 | { |
| 265 | "lockFileVersion": 7 |
| 266 | } |
| 267 | EOF |
| 268 | |
| 269 | do_merge base left right |
| 270 | diff -u base left || fail "output differs" |
| 271 | } |
| 272 | |
| 273 | run_suite "Tests of bash completion of 'blaze' command." |