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 |
| 154 | # proto_library requires this external workspace. |
| 155 | cat >> WORKSPACE << EOF |
| 156 | new_local_repository( |
| 157 | name = "com_google_protobuf", |
| 158 | path = "$(dirname $(rlocation io_bazel/third_party/protobuf/3.6.1/BUILD))", |
| 159 | build_file = "$(rlocation io_bazel/third_party/protobuf/3.6.1/BUILD)", |
| 160 | ) |
| 161 | EOF |
| 162 | fi |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 163 | local pkg="${FUNCNAME[0]}" |
| 164 | mkdir -p "$pkg" || fail "mkdir -p $pkg" |
| 165 | echo "load('//$pkg:shell.bzl', 'skylark_shell')" > "$pkg/BUILD" |
| 166 | cat >> "$pkg/BUILD" <<'EOF' |
| 167 | skylark_shell( |
| 168 | name = "shelly", |
| 169 | output = "ok.txt", |
| 170 | ) |
| 171 | |
| 172 | cc_binary(name="zero", srcs=["zero.cc"]) |
| 173 | |
| 174 | sh_test(name="test_a", srcs=["a.sh"]) |
| 175 | |
| 176 | java_library(name = "javalib", srcs = ["HelloWorld.java"]) |
| 177 | |
| 178 | action_listener( |
| 179 | name = "al", |
| 180 | extra_actions = [":echo-filename"], |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 181 | mnemonics = ["Javac"], |
| 182 | visibility = ["//visibility:public"], |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 183 | ) |
| 184 | |
| 185 | extra_action(name = "echo-filename", cmd = "echo Hi \$(EXTRA_ACTION_FILE)") |
| 186 | |
| 187 | py_binary(name = "pybar", srcs=["pybar.py"],) |
| 188 | |
| 189 | proto_library(name = "proto", srcs=["foo.proto"]) |
| 190 | EOF |
| 191 | cat > "$pkg/shell.bzl" <<'EOF' |
| 192 | def _impl(ctx): |
| 193 | ctx.actions.run_shell( |
| 194 | outputs = [ ctx.outputs.output ], |
| 195 | command = "touch %s" % ctx.outputs.output.path, |
| 196 | ) |
| 197 | |
| 198 | skylark_shell = rule( |
| 199 | _impl, |
| 200 | attrs = { |
| 201 | "output": attr.output(mandatory=True), |
| 202 | } |
| 203 | ) |
| 204 | EOF |
| 205 | cat > "$pkg/a.sh" <<'EOF' |
| 206 | #!/bin/sh |
| 207 | exit 0 |
| 208 | EOF |
| 209 | chmod 755 "$pkg/a.sh" |
| 210 | echo "int main(void) {}" > "$pkg/zero.cc" |
| 211 | echo "public class HelloWorld {}" > "$pkg/HelloWorld.java" |
| 212 | echo 'print("Hi")' > "$pkg/pybar.py" |
| 213 | echo 'syntax="proto2"; package foo;' > "$pkg/foo.proto" |
| 214 | |
| 215 | bazel aquery --output=text "//$pkg:all" \ |
| 216 | --experimental_action_listener=$pkg:al \ |
| 217 | --modify_execution_info=\ |
| 218 | echo.*=+requires-extra-action,\ |
| 219 | .*Proto.*=+requires-proto,\ |
| 220 | CppCompile=+requires-cpp-compile,\ |
| 221 | CppLink=+requires-cpp-link,\ |
| 222 | TestRunner=+requires-test-runner,\ |
| 223 | Turbine=+requires-turbine,\ |
| 224 | JavaSourceJar=+requires-java-source-jar,\ |
| 225 | Javac=+requires-javac,\ |
| 226 | PyTinypar=+requires-py-tinypar,\ |
laurentlb | 47e8854 | 2019-07-01 09:29:04 -0700 | [diff] [blame^] | 227 | Action=+requires-action \ |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 228 | > output 2> "$TEST_log" || fail "Expected success" |
| 229 | |
| 230 | # There are sometimes other elements in ExecutionInfo, e.g. requires-darwin |
| 231 | # for obj-c, supports-workers for java. Since testing for these combinations |
| 232 | # would be brittle, irrelevant to the operation of the flag, and in some |
| 233 | # cases platform-dependent, we just search for the key itself, not the whole |
| 234 | # ExecutionInfo: {...} line. |
laurentlb | 47e8854 | 2019-07-01 09:29:04 -0700 | [diff] [blame^] | 235 | assert_contains "requires-action: ''" output |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 236 | assert_contains "requires-cpp-compile: ''" output |
| 237 | assert_contains "requires-cpp-link: ''" output |
| 238 | assert_contains "requires-extra-action: ''" output |
| 239 | assert_contains "requires-test-runner: ''" output |
| 240 | assert_contains "requires-javac: ''" output |
| 241 | assert_contains "requires-turbine: ''" output |
| 242 | assert_contains "requires-java-source-jar: ''" output |
| 243 | assert_contains "requires-proto: ''" output # GenProtoDescriptorSet should match |
Benjamin Peterson | 804406f | 2018-10-11 08:39:35 -0700 | [diff] [blame] | 244 | if [[ "$PRODUCT_NAME" != "bazel" ]]; then |
| 245 | # Python rules generate some cpp actions and local actions, but py-tinypar |
| 246 | # is the main unique-to-python rule which runs remotely for a py_binary. |
| 247 | assert_contains "requires-py-tinypar: ''" output |
| 248 | fi |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Googler | cfe0318 | 2019-03-09 07:42:11 -0800 | [diff] [blame] | 251 | # Regression test for b/127874955. We use --output=textproto since --output=text |
| 252 | # sorts the execution info. |
| 253 | function test_modify_execution_info_deterministic_order() { |
| 254 | local pkg="${FUNCNAME[0]}" |
| 255 | mkdir -p "$pkg/x" "$pkg/y" || fail "mkdir failed" |
| 256 | touch "$pkg/BUILD" |
| 257 | cat > "$pkg/build_defs.bzl" <<'EOF' || fail "Couldn't cat" |
| 258 | def _rule_x_impl(ctx): |
| 259 | output = ctx.outputs.out |
| 260 | ctx.actions.run_shell( |
| 261 | outputs = [output], |
| 262 | command = "touch %s" % output.path, |
| 263 | mnemonic = "RuleX", |
| 264 | execution_requirements = {"requires-x": ""}, |
| 265 | ) |
| 266 | |
| 267 | rule_x = rule(outputs = {"out": "%{name}.out"}, implementation = _rule_x_impl) |
| 268 | |
| 269 | def _rule_y_impl(ctx): |
| 270 | output = ctx.outputs.out |
| 271 | ctx.actions.run_shell( |
| 272 | outputs = [output], |
| 273 | command = "touch %s" % output.path, |
| 274 | mnemonic = "RuleY", |
| 275 | execution_requirements = {"requires-y": ""}, |
| 276 | ) |
| 277 | |
| 278 | rule_y = rule(outputs = {"out": "%{name}.out"}, implementation = _rule_y_impl) |
| 279 | EOF |
| 280 | echo "load('//$pkg:build_defs.bzl', 'rule_x')" > "$pkg/x/BUILD" |
| 281 | echo 'rule_x(name = "x")' >> "$pkg/x/BUILD" |
| 282 | echo "load('//$pkg:build_defs.bzl', 'rule_y')" > "$pkg/y/BUILD" |
| 283 | echo 'rule_y(name = "y")' >> "$pkg/y/BUILD" |
| 284 | |
| 285 | mod='Rule(X|Y)=+requires-x,Rule(X|Y)=+requires-y' |
| 286 | |
| 287 | bazel aquery "//$pkg/x" --output=textproto --modify_execution_info="$mod" \ |
| 288 | > output1 2> "$TEST_log" || fail "Expected success" |
| 289 | |
| 290 | bazel shutdown >& "$TEST_log" || fail "Couldn't shutdown" |
| 291 | |
| 292 | bazel aquery "//$pkg/y" --modify_execution_info="$mod" \ |
| 293 | >& "$TEST_log" || fail "Expected success" |
| 294 | |
| 295 | bazel aquery "//$pkg/x" --output=textproto --modify_execution_info="$mod" \ |
| 296 | > output2 2> "$TEST_log" || fail "Expected success" |
| 297 | |
| 298 | assert_equals "$(cat output1)" "$(cat output2)" |
| 299 | } |
| 300 | |
Googler | ab96caa | 2019-04-22 12:07:07 -0700 | [diff] [blame] | 301 | # Regression test for b/130762259. |
| 302 | function test_modify_execution_info_changes_test_runner_cache_key() { |
| 303 | local pkg="${FUNCNAME[0]}" |
| 304 | mkdir -p "$pkg" |
| 305 | echo "sh_test(name = 'test', srcs = ['test.sh'])" > "$pkg/BUILD" |
| 306 | touch "$pkg/test.sh" |
| 307 | |
| 308 | bazel aquery "mnemonic(TestRunner,//$pkg:test)" --output=text \ |
| 309 | --modify_execution_info= \ |
| 310 | 2> "$TEST_log" | grep ActionKey > key1 || fail "Expected success" |
| 311 | |
| 312 | bazel aquery "mnemonic(TestRunner,//$pkg:test)" --output=text \ |
| 313 | --modify_execution_info=TestRunner=+requires-x \ |
| 314 | 2> "$TEST_log" | grep ActionKey > key2 || fail "Expected success" |
| 315 | |
| 316 | assert_not_equals "$(cat key1)" "$(cat key2)" |
| 317 | } |
| 318 | |
Googler | fff72a7 | 2018-08-30 17:42:10 -0700 | [diff] [blame] | 319 | run_suite "Integration tests of the --modify_execution_info option." |