Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2018 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 | # modify_execution_info_test.sh: tests of the --modify_execution_info flag. |
| 18 | |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 19 | # --- begin runfiles.bash initialization --- |
| 20 | # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). |
| 21 | set -euo pipefail |
| 22 | if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then |
| 23 | if [[ -f "$0.runfiles_manifest" ]]; then |
| 24 | export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" |
| 25 | elif [[ -f "$0.runfiles/MANIFEST" ]]; then |
| 26 | export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" |
| 27 | elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then |
| 28 | export RUNFILES_DIR="$0.runfiles" |
| 29 | fi |
| 30 | fi |
| 31 | if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then |
| 32 | source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" |
| 33 | elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then |
| 34 | source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ |
| 35 | "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" |
| 36 | else |
| 37 | echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" |
| 38 | exit 1 |
| 39 | fi |
| 40 | # --- end runfiles.bash initialization --- |
| 41 | |
| 42 | source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \ |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 43 | || { echo "integration_test_setup.sh not found!" >&2; exit 1; } |
| 44 | |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 45 | case "$(uname -s | tr [:upper:] [:lower:])" in |
| 46 | msys*|mingw*|cygwin*) |
| 47 | declare -r is_windows=true |
| 48 | ;; |
| 49 | *) |
| 50 | declare -r is_windows=false |
| 51 | ;; |
| 52 | esac |
| 53 | |
| 54 | if "$is_windows"; then |
| 55 | export MSYS_NO_PATHCONV=1 |
| 56 | export MSYS2_ARG_CONV_EXCL="*" |
| 57 | fi |
| 58 | |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 59 | #### HELPER FUNCTIONS ################################################## |
| 60 | |
Laszlo Csomor | f57c306 | 2019-01-16 05:06:00 -0800 | [diff] [blame] | 61 | if ! type try_with_timeout >&/dev/null; then |
| 62 | # Bazel's testenv.sh defines try_with_timeout but the Google-internal version |
| 63 | # uses a different testenv.sh. |
| 64 | function try_with_timeout() { $* ; } |
| 65 | fi |
| 66 | |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 67 | function set_up() { |
| 68 | cd ${WORKSPACE_DIR} |
| 69 | } |
| 70 | |
| 71 | function tear_down() { |
Laszlo Csomor | f57c306 | 2019-01-16 05:06:00 -0800 | [diff] [blame] | 72 | try_with_timeout bazel shutdown |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | #### TESTS ############################################################# |
| 76 | |
| 77 | function test_aquery_respects_modify_execution_info_changes { |
| 78 | local pkg="${FUNCNAME[0]}" |
| 79 | mkdir -p "$pkg" || fail "mkdir -p $pkg" |
| 80 | cat > "$pkg/BUILD" <<'EOF' |
| 81 | genrule(name = "bar", outs = ["bar_out.txt"], cmd = "touch $(OUTS)") |
| 82 | EOF |
| 83 | bazel aquery --output=text "//$pkg:bar" \ |
| 84 | --modify_execution_info=Genrule=+requires-x \ |
| 85 | > output1 2> "$TEST_log" || fail "Expected success" |
| 86 | assert_contains "ExecutionInfo: {requires-x: ''}" output1 |
| 87 | |
| 88 | bazel aquery --output=text "//$pkg:bar" \ |
| 89 | --modify_execution_info=Genrule=+requires-y \ |
| 90 | > output2 2> "$TEST_log" || fail "Expected success" |
| 91 | assert_contains "ExecutionInfo: {requires-y: ''}" output2 |
| 92 | } |
| 93 | |
| 94 | function test_modify_execution_info_multiple { |
| 95 | local pkg="${FUNCNAME[0]}" |
| 96 | mkdir -p "$pkg" || fail "mkdir -p $pkg" |
| 97 | cat > "$pkg/BUILD" <<'EOF' |
| 98 | genrule( |
| 99 | name = "bar", |
| 100 | outs = ["bar_out.txt"], |
| 101 | cmd = "touch $(OUTS)", |
| 102 | tags = ["requires-x"], |
| 103 | ) |
| 104 | cc_binary(name="zero", srcs=["zero.cc"]) |
| 105 | EOF |
| 106 | echo "int main(void) {}" > "$pkg/zero.cc" |
| 107 | |
| 108 | # multiple elements in the value list that match the same mnemonic. |
| 109 | bazel aquery --output=text "//$pkg:bar" \ |
| 110 | --modify_execution_info=Genrule=+requires-y,Genrule=+requires-z \ |
| 111 | > output 2> "$TEST_log" || fail "Expected success" |
| 112 | assert_contains "ExecutionInfo: {requires-x: '', requires-y: '', "\ |
| 113 | "requires-z: ''}" output |
| 114 | |
| 115 | # multiple elements in the value list, the first of which adds an |
| 116 | # ExecutionInfo and the second of which removes it. |
| 117 | bazel aquery --output=text "//$pkg:bar" \ |
| 118 | --modify_execution_info=Genrule=+requires-z,.*=-requires-z \ |
| 119 | > output 2> "$TEST_log" || fail "Expected success" |
| 120 | assert_contains "ExecutionInfo: {requires-x: ''}" output |
| 121 | |
| 122 | # multiple elements in the value list, the first of which removes an |
| 123 | # ExecutionInfo (previously absent) and the second of which adds it. |
| 124 | bazel aquery --output=text "//$pkg:bar" \ |
| 125 | --modify_execution_info=Genrule=-requires-z,.*=+requires-z \ |
| 126 | > output 2> "$TEST_log" || fail "Expected success" |
| 127 | assert_contains "ExecutionInfo: {requires-x: '', requires-z: ''}" output |
| 128 | |
| 129 | # multiple elements in the value list, the first of which removes an |
| 130 | # ExecutionInfo (previously present) and the second of which adds it back. |
| 131 | bazel aquery --output=text "//$pkg:bar" \ |
| 132 | --modify_execution_info=Genrule=-requires-x,.*=+requires-x \ |
| 133 | > output 2> "$TEST_log" || fail "Expected success" |
| 134 | assert_contains "ExecutionInfo: {requires-x: ''}" output |
| 135 | |
| 136 | # multiple elements with multiple values |
| 137 | bazel aquery --output=text "//$pkg:all" \ |
| 138 | --modify_execution_info=Genrule=-requires-x,Genrule=+requires-z,\ |
| 139 | Genrule=+requires-a,CppCompile=+requires-b,CppCompile=+requires-c \ |
| 140 | > output 2> "$TEST_log" || fail "Expected success" |
| 141 | assert_contains "ExecutionInfo: {requires-a: '', requires-z: ''}" output |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 142 | assert_contains "ExecutionInfo: {requires-b: '', requires-c: ''" output |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 143 | |
| 144 | # negative lookahead |
| 145 | bazel aquery --output=text "//$pkg:all" \ |
| 146 | --modify_execution_info='(?!Genrule).*=+requires-a,(?!CppCompile).*=+requires-z' \ |
| 147 | > output 2> "$TEST_log" || fail "Expected success" |
| 148 | assert_contains "ExecutionInfo: {requires-x: '', requires-z: ''}" output |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 149 | assert_contains "ExecutionInfo: {requires-a: ''" output |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | function test_modify_execution_info_various_types() { |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 153 | if [[ "$PRODUCT_NAME" = "bazel" ]]; then |
Tony Aiuto | 320bc94 | 2021-01-14 07:43:21 -0800 | [diff] [blame] | 154 | cat "$(rlocation "io_bazel/src/test/shell/integration/rules_proto_stanza.txt")" >>WORKSPACE |
| 155 | cat >> WORKSPACE << EOF |
Yannic Bonenberger | 5e571d2 | 2020-02-13 07:29:58 -0800 | [diff] [blame] | 156 | load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") |
| 157 | rules_proto_dependencies() |
| 158 | rules_proto_toolchains() |
| 159 | |
| 160 | # @com_google_protobuf//:protoc depends on @io_bazel//third_party/zlib. |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 161 | new_local_repository( |
Yannic Bonenberger | 5e571d2 | 2020-02-13 07:29:58 -0800 | [diff] [blame] | 162 | name = "io_bazel", |
Yun Peng | 9d30849 | 2023-08-08 00:49:00 -0700 | [diff] [blame] | 163 | path = "$(dirname $(dirname $(dirname $(rlocation io_bazel/third_party/zlib/BUILD))))", |
Yannic Bonenberger | 5e571d2 | 2020-02-13 07:29:58 -0800 | [diff] [blame] | 164 | build_file_content = "# Intentionally left empty.", |
| 165 | workspace_file_content = "workspace(name = 'io_bazel')", |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 166 | ) |
| 167 | EOF |
| 168 | fi |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 169 | local pkg="${FUNCNAME[0]}" |
| 170 | mkdir -p "$pkg" || fail "mkdir -p $pkg" |
gregce | 72e559d | 2020-08-12 12:10:14 -0700 | [diff] [blame] | 171 | echo "load('//$pkg:shell.bzl', 'starlark_shell')" > "$pkg/BUILD" |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 172 | cat >> "$pkg/BUILD" <<'EOF' |
Richard Levasseur | 0c617b3 | 2023-09-13 11:37:17 -0700 | [diff] [blame^] | 173 | load("@rules_python//python:py_binary.bzl", "py_binary") |
| 174 | |
gregce | 72e559d | 2020-08-12 12:10:14 -0700 | [diff] [blame] | 175 | starlark_shell( |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 176 | name = "shelly", |
| 177 | output = "ok.txt", |
| 178 | ) |
| 179 | |
| 180 | cc_binary(name="zero", srcs=["zero.cc"]) |
| 181 | |
| 182 | sh_test(name="test_a", srcs=["a.sh"]) |
| 183 | |
| 184 | java_library(name = "javalib", srcs = ["HelloWorld.java"]) |
| 185 | |
| 186 | action_listener( |
| 187 | name = "al", |
| 188 | extra_actions = [":echo-filename"], |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 189 | mnemonics = ["Javac"], |
| 190 | visibility = ["//visibility:public"], |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 191 | ) |
| 192 | |
adonovan | 73402fa | 2020-11-09 10:51:37 -0800 | [diff] [blame] | 193 | extra_action(name = "echo-filename", cmd = "echo Hi \\$(EXTRA_ACTION_FILE)") |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 194 | |
| 195 | py_binary(name = "pybar", srcs=["pybar.py"],) |
| 196 | |
| 197 | proto_library(name = "proto", srcs=["foo.proto"]) |
| 198 | EOF |
| 199 | cat > "$pkg/shell.bzl" <<'EOF' |
| 200 | def _impl(ctx): |
| 201 | ctx.actions.run_shell( |
| 202 | outputs = [ ctx.outputs.output ], |
| 203 | command = "touch %s" % ctx.outputs.output.path, |
| 204 | ) |
| 205 | |
gregce | 72e559d | 2020-08-12 12:10:14 -0700 | [diff] [blame] | 206 | starlark_shell = rule( |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 207 | _impl, |
| 208 | attrs = { |
| 209 | "output": attr.output(mandatory=True), |
| 210 | } |
| 211 | ) |
| 212 | EOF |
| 213 | cat > "$pkg/a.sh" <<'EOF' |
| 214 | #!/bin/sh |
| 215 | exit 0 |
| 216 | EOF |
| 217 | chmod 755 "$pkg/a.sh" |
| 218 | echo "int main(void) {}" > "$pkg/zero.cc" |
| 219 | echo "public class HelloWorld {}" > "$pkg/HelloWorld.java" |
| 220 | echo 'print("Hi")' > "$pkg/pybar.py" |
| 221 | echo 'syntax="proto2"; package foo;' > "$pkg/foo.proto" |
| 222 | |
| 223 | bazel aquery --output=text "//$pkg:all" \ |
| 224 | --experimental_action_listener=$pkg:al \ |
| 225 | --modify_execution_info=\ |
| 226 | echo.*=+requires-extra-action,\ |
| 227 | .*Proto.*=+requires-proto,\ |
| 228 | CppCompile=+requires-cpp-compile,\ |
| 229 | CppLink=+requires-cpp-link,\ |
| 230 | TestRunner=+requires-test-runner,\ |
| 231 | Turbine=+requires-turbine,\ |
| 232 | JavaSourceJar=+requires-java-source-jar,\ |
| 233 | Javac=+requires-javac,\ |
Richard Levasseur | 7a230fd | 2022-06-17 14:19:32 -0700 | [diff] [blame] | 234 | Py.*=+requires-py,\ |
laurentlb | 47e8854 | 2019-07-01 09:29:04 -0700 | [diff] [blame] | 235 | Action=+requires-action \ |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 236 | > output 2> "$TEST_log" || fail "Expected success" |
| 237 | |
| 238 | # There are sometimes other elements in ExecutionInfo, e.g. requires-darwin |
| 239 | # for obj-c, supports-workers for java. Since testing for these combinations |
| 240 | # would be brittle, irrelevant to the operation of the flag, and in some |
| 241 | # cases platform-dependent, we just search for the key itself, not the whole |
| 242 | # ExecutionInfo: {...} line. |
laurentlb | 47e8854 | 2019-07-01 09:29:04 -0700 | [diff] [blame] | 243 | assert_contains "requires-action: ''" output |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 244 | assert_contains "requires-cpp-compile: ''" output |
| 245 | assert_contains "requires-cpp-link: ''" output |
| 246 | assert_contains "requires-extra-action: ''" output |
| 247 | assert_contains "requires-test-runner: ''" output |
| 248 | assert_contains "requires-javac: ''" output |
| 249 | assert_contains "requires-turbine: ''" output |
| 250 | assert_contains "requires-java-source-jar: ''" output |
| 251 | assert_contains "requires-proto: ''" output # GenProtoDescriptorSet should match |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 252 | if [[ "$PRODUCT_NAME" != "bazel" ]]; then |
Richard Levasseur | 7a230fd | 2022-06-17 14:19:32 -0700 | [diff] [blame] | 253 | assert_contains "requires-py: ''" output |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 254 | fi |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Googler | cfe0318 | 2019-03-09 07:42:11 -0800 | [diff] [blame] | 257 | # Regression test for b/127874955. We use --output=textproto since --output=text |
| 258 | # sorts the execution info. |
| 259 | function test_modify_execution_info_deterministic_order() { |
| 260 | local pkg="${FUNCNAME[0]}" |
| 261 | mkdir -p "$pkg/x" "$pkg/y" || fail "mkdir failed" |
| 262 | touch "$pkg/BUILD" |
| 263 | cat > "$pkg/build_defs.bzl" <<'EOF' || fail "Couldn't cat" |
| 264 | def _rule_x_impl(ctx): |
| 265 | output = ctx.outputs.out |
| 266 | ctx.actions.run_shell( |
| 267 | outputs = [output], |
| 268 | command = "touch %s" % output.path, |
| 269 | mnemonic = "RuleX", |
| 270 | execution_requirements = {"requires-x": ""}, |
| 271 | ) |
| 272 | |
| 273 | rule_x = rule(outputs = {"out": "%{name}.out"}, implementation = _rule_x_impl) |
| 274 | |
| 275 | def _rule_y_impl(ctx): |
| 276 | output = ctx.outputs.out |
| 277 | ctx.actions.run_shell( |
| 278 | outputs = [output], |
| 279 | command = "touch %s" % output.path, |
| 280 | mnemonic = "RuleY", |
| 281 | execution_requirements = {"requires-y": ""}, |
| 282 | ) |
| 283 | |
| 284 | rule_y = rule(outputs = {"out": "%{name}.out"}, implementation = _rule_y_impl) |
| 285 | EOF |
| 286 | echo "load('//$pkg:build_defs.bzl', 'rule_x')" > "$pkg/x/BUILD" |
| 287 | echo 'rule_x(name = "x")' >> "$pkg/x/BUILD" |
| 288 | echo "load('//$pkg:build_defs.bzl', 'rule_y')" > "$pkg/y/BUILD" |
| 289 | echo 'rule_y(name = "y")' >> "$pkg/y/BUILD" |
| 290 | |
| 291 | mod='Rule(X|Y)=+requires-x,Rule(X|Y)=+requires-y' |
| 292 | |
| 293 | bazel aquery "//$pkg/x" --output=textproto --modify_execution_info="$mod" \ |
| 294 | > output1 2> "$TEST_log" || fail "Expected success" |
| 295 | |
| 296 | bazel shutdown >& "$TEST_log" || fail "Couldn't shutdown" |
| 297 | |
| 298 | bazel aquery "//$pkg/y" --modify_execution_info="$mod" \ |
| 299 | >& "$TEST_log" || fail "Expected success" |
| 300 | |
| 301 | bazel aquery "//$pkg/x" --output=textproto --modify_execution_info="$mod" \ |
| 302 | > output2 2> "$TEST_log" || fail "Expected success" |
| 303 | |
| 304 | assert_equals "$(cat output1)" "$(cat output2)" |
| 305 | } |
| 306 | |
Googler | ab96caa | 2019-04-22 12:07:07 -0700 | [diff] [blame] | 307 | # Regression test for b/130762259. |
| 308 | function test_modify_execution_info_changes_test_runner_cache_key() { |
| 309 | local pkg="${FUNCNAME[0]}" |
| 310 | mkdir -p "$pkg" |
| 311 | echo "sh_test(name = 'test', srcs = ['test.sh'])" > "$pkg/BUILD" |
| 312 | touch "$pkg/test.sh" |
| 313 | |
| 314 | bazel aquery "mnemonic(TestRunner,//$pkg:test)" --output=text \ |
| 315 | --modify_execution_info= \ |
| 316 | 2> "$TEST_log" | grep ActionKey > key1 || fail "Expected success" |
| 317 | |
| 318 | bazel aquery "mnemonic(TestRunner,//$pkg:test)" --output=text \ |
| 319 | --modify_execution_info=TestRunner=+requires-x \ |
| 320 | 2> "$TEST_log" | grep ActionKey > key2 || fail "Expected success" |
| 321 | |
| 322 | assert_not_equals "$(cat key1)" "$(cat key2)" |
| 323 | } |
| 324 | |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 325 | run_suite "Integration tests of the --modify_execution_info option." |