Damien Martin-Guillerez | 5f9c6ba | 2015-04-09 21:10:33 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2015 Google Inc. 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 | : ${DIR:=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)} |
| 20 | source ${DIR}/testenv.sh || { echo "testenv.sh not found!" >&2; exit 1; } |
| 21 | |
| 22 | # List of command to test completion for |
| 23 | : ${COMMAND_ALIASES:=bazel} |
| 24 | |
| 25 | # Completion script |
| 26 | : ${COMPLETION:="$TEST_SRCDIR/scripts/bazel-complete.bash"} |
| 27 | |
| 28 | # Set this to test completion with package path (if enabled) |
| 29 | : ${PACKAGE_PATH_PREFIX:=} |
| 30 | |
| 31 | #### UTILITIES ######################################################### |
| 32 | |
| 33 | # Usage: array_join join_on array |
| 34 | # |
| 35 | # Joins all arguments using the first argument as separator |
| 36 | function array_join { |
| 37 | local joiner="$1" |
| 38 | shift |
| 39 | echo -n "$1" |
| 40 | shift |
| 41 | for i in "$@"; do |
| 42 | echo -n "${joiner}${i}" |
| 43 | done |
| 44 | } |
| 45 | |
| 46 | # Usage: expand <terminal-input> <flags> <stderr-file> |
| 47 | # |
| 48 | # Prints the string resulting from command expansion after the |
| 49 | # specified terminal input is typed at the shell. The argument |
| 50 | # is evaluated using 'echo -e', so \t can be used to invoke |
| 51 | # command expansion. STDERR output from the call to bash is |
| 52 | # sent to stderr-file so it can be inspected, if desired. |
| 53 | # |
| 54 | # This approach is rather gnarly, but guarantees good test fidelity, |
| 55 | # unlike "unit test" approaches based on invoking the completion function |
| 56 | # directly with COMP_WORDS etc defined. |
| 57 | expand() { |
| 58 | local input="$1" flags="$2" stderr_file="$3" |
| 59 | { |
| 60 | # The flags for blaze autocomplete script. |
| 61 | echo "$flags" |
| 62 | # This script is already sourced in a normal bash shell, but we need it |
| 63 | # for the tests, too. |
| 64 | echo "source $COMPLETION" |
| 65 | # Tricky! We turn "bazel" into a self-quoting command |
| 66 | # that echoes its argument string exactly, spaces and all. |
| 67 | # We assume no single-quotes in the input, though. |
| 68 | # |
| 69 | # Alias expansion still inserts an extra space after 'blaze', |
| 70 | # though, hence the following sed. Not sure why. |
| 71 | for i in ${COMMAND_ALIASES[@]}; do |
| 72 | echo "alias $i=\"echo $i'\"" |
| 73 | done |
| 74 | echo -en "$input'" |
| 75 | } | bash --norc -i 2>"$stderr_file" | |
| 76 | sed -e 's/^\('"$(array_join "\|" ${COMMAND_ALIASES[@]})"'\) /\1 /' |
| 77 | } |
| 78 | |
| 79 | # Usage: assert_expansion <prefix> <expected-expansion> <optional-flags> |
| 80 | # |
| 81 | # For multiple flags separate with semicolon. |
| 82 | # e.g. assert_expansion 'foo' 'foo_expand' 'flag1=bar;flag2=baz' |
| 83 | assert_expansion() { |
| 84 | local prefix=$1 expected=$2 flags=${3:-} |
| 85 | for i in ${COMMAND_ALIASES[@]}; do |
| 86 | local nprefix="$i $prefix" |
| 87 | local nexpected="$i $expected" |
| 88 | assert_equals "$nexpected" "$(expand "$nprefix\t" "$flags" "/dev/null")" |
| 89 | done |
| 90 | } |
| 91 | |
| 92 | |
| 93 | # Usage: assert_expansion_error_not_contains <prefix> <unexpected-error> |
| 94 | # <optional-flags> |
| 95 | # |
| 96 | # For multiple flags separate with semicolon. |
| 97 | # |
| 98 | # Asserts that tab-completing after typing the prefix will not result |
| 99 | # in STDERR receiving a string containing regex unexpected-error. |
| 100 | assert_expansion_error_not_contains() { |
| 101 | local prefix=$1 not_expected=$2 flags=${3:-} |
| 102 | local temp_file=$(mktemp -t tmp.stderr.XXXXXX) |
| 103 | for i in ${COMMAND_ALIASES[@]}; do |
| 104 | local nprefix="$i " |
| 105 | expand "$nprefix\t" "$flags" "$temp_file" > /dev/null |
| 106 | assert_not_contains "$not_expected" "$temp_file" |
| 107 | done |
| 108 | } |
| 109 | |
| 110 | #### FIXTURES ########################################################## |
| 111 | |
| 112 | make_empty_packages() { |
| 113 | touch video/streamer2/testing/BUILD |
| 114 | touch ${PACKAGE_PATH_PREFIX:-}video/streamer2/stuff/BUILD |
| 115 | touch video/streamer2/names/BUILD |
| 116 | } |
| 117 | |
| 118 | make_packages() { |
| 119 | mkdir -p video/streamer2/testing || fail "mkdir failed" |
| 120 | cat >video/streamer2/BUILD <<EOF |
| 121 | cc_library(name='task_lib', ...) |
| 122 | cc_library(name='token_bucket', ...) |
| 123 | cc_library(name='with_special+_,=-.@~chars', ...) |
| 124 | #cc_library(name='comment_build_target_1old', ...) |
| 125 | #cc_library(name='comment_build_target_2old', ...) |
| 126 | cc_library(name='comment_build_target_2new', ...) |
| 127 | #cc_test(name='token_bucket_t_1old', ...) |
| 128 | #cc_test(name='token_bucket_t_2old', ...) |
| 129 | cc_test(name='token_bucket_test', ...) |
| 130 | cc_binary(name='token_bucket_binary', ...) |
| 131 | java_binary ( name = 'JavaBinary', ...) |
| 132 | java_binary ( |
| 133 | name = 'AnotherJavaBinary' |
| 134 | ... |
| 135 | ) |
| 136 | cc_binary(other='thing', name='pybin', ...) |
| 137 | genrule(name='checks/thingy', ...) |
| 138 | #cc_binary(name='comment_run_target_1old', ...) |
| 139 | #cc_binary(name='comment_run_target_2old', ...) |
| 140 | cc_binary(name='comment_run_target_2new', ...) |
| 141 | EOF |
| 142 | |
| 143 | mkdir -p ${PACKAGE_PATH_PREFIX:-}video/streamer2/stuff || fail "mkdir failed" |
| 144 | cat >${PACKAGE_PATH_PREFIX:-}video/streamer2/stuff/BUILD <<EOF |
| 145 | cc_library(name='stuff', ...) |
| 146 | EOF |
| 147 | |
| 148 | mkdir -p video/streamer2/names || fail "mkdir failed" |
| 149 | cat >video/streamer2/names/BUILD <<EOF |
| 150 | genrule( |
| 151 | name = 'foo', |
| 152 | cmd = ('name=foo'), |
| 153 | ) |
| 154 | EOF |
| 155 | |
| 156 | mkdir -p dash || fail "mkdir failed" |
| 157 | cat >dash/BUILD <<EOF |
| 158 | cc_library( |
| 159 | name = "mia-bid-multiplier-mixer-module", |
| 160 | ) |
| 161 | EOF |
| 162 | |
| 163 | mkdir -p video/notapackage |
| 164 | } |
| 165 | |
| 166 | #### UNIT TESTS ######################################################## |
| 167 | |
| 168 | source ${COMPLETION} |
| 169 | |
| 170 | assert_expansion_function() { |
| 171 | local ws=${PWD} |
| 172 | local function="$1" displacement="$2" type="$3" expected="$4" current="$5" |
| 173 | assert_equals "$(echo -e "${expected}")" \ |
| 174 | "$(eval "_bazel__${function} \"${ws}\" \"${displacement}\" \"${current}\" \"${type}\"")" |
| 175 | } |
| 176 | |
| 177 | test_expand_rules_in_package() { |
| 178 | make_packages |
| 179 | |
| 180 | assert_expansion_function "expand_rules_in_package" "" label \ |
| 181 | "stuff " "//video/streamer2/stuff:" |
| 182 | assert_expansion_function "expand_rules_in_package" "" label \ |
| 183 | 'task_lib ' 'video/streamer2:ta' |
| 184 | assert_expansion_function "expand_rules_in_package" "" label \ |
| 185 | 'with_special+_,=-.@~chars ' 'video/streamer2:with_s' |
| 186 | |
| 187 | # From a different directory |
| 188 | assert_expansion_function "expand_rules_in_package" "video/" label \ |
| 189 | 'task_lib ' 'streamer2:ta' |
| 190 | assert_expansion_function "expand_rules_in_package" "video/" label \ |
| 191 | '' 'video/streamer2:ta' |
| 192 | assert_expansion_function "expand_rules_in_package" "video/" label \ |
| 193 | 'with_special+_,=-.@~chars ' 'streamer2:with_s' |
| 194 | |
| 195 | # label should match test and non-test rules |
| 196 | assert_expansion_function "expand_rules_in_package" "" label \ |
| 197 | 'token_bucket_test \ntoken_bucket_binary ' \ |
| 198 | 'video/streamer2:token_bucket_' |
| 199 | assert_expansion_function "expand_rules_in_package" "" label \ |
| 200 | 'stuff ' 'video/streamer2/stuff:s' |
| 201 | # Test that label does not match commented-out rules. |
| 202 | assert_expansion_function "expand_rules_in_package" "" label \ |
| 203 | '' 'video/streamer2:comment_build_target_1o' |
| 204 | assert_expansion_function "expand_rules_in_package" "" label \ |
| 205 | 'comment_build_target_2new ' 'video/streamer2:comment_build_target_2' |
| 206 | |
| 207 | # Test that 'label-test' expands only test rules. |
| 208 | assert_expansion_function "expand_rules_in_package" "" label-test \ |
| 209 | 'token_bucket_test ' 'video/streamer2:to' |
| 210 | |
| 211 | # Test that 'label-test' does not match commented-out rules. |
| 212 | assert_expansion_function "expand_rules_in_package" "" label-test \ |
| 213 | '' 'video/streamer2:token_bucket_t_1o' |
| 214 | assert_expansion_function "expand_rules_in_package" "" label-test \ |
| 215 | 'token_bucket_test ' 'video/streamer2:token_bucket_t' |
| 216 | |
| 217 | # Test that :all wildcard is expanded when there is more than one |
| 218 | # match. |
| 219 | # |
| 220 | # One match => no :all. |
| 221 | assert_expansion_function "expand_rules_in_package" "" label-test \ |
| 222 | 'token_bucket_test ' 'video/streamer2:' |
| 223 | # Multiple matches => :all. |
| 224 | assert_expansion_function "expand_rules_in_package" "" label-test \ |
| 225 | 'all ' 'video/streamer2:a' |
| 226 | |
| 227 | # Test that label-bin expands only non-test binary rules. |
| 228 | assert_expansion_function "expand_rules_in_package" "" label-bin \ |
| 229 | 'token_bucket_binary ' 'video/streamer2:to' |
| 230 | |
| 231 | # Test that label-bin expands for binary and test rules, but not library |
| 232 | # with BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set. |
| 233 | BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=true \ |
| 234 | assert_expansion_function "expand_rules_in_package" "" label-bin \ |
| 235 | 'token_bucket_test \ntoken_bucket_binary ' 'video/streamer2:to' |
| 236 | |
| 237 | # Test the label-bin expands for test rules, with |
| 238 | # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set. |
| 239 | BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=1 \ |
| 240 | assert_expansion_function "expand_rules_in_package" "" label-bin \ |
| 241 | 'token_bucket_test ' 'video/streamer2:token_bucket_t' |
| 242 | |
| 243 | # Test that 'label-bin' expands only non-test binary rules when the |
| 244 | # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is false. |
| 245 | BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=false \ |
| 246 | assert_expansion_function "expand_rules_in_package" "" label-bin \ |
| 247 | 'token_bucket_binary ' 'video/streamer2:to' |
| 248 | |
| 249 | # Test that 'label-bin' does not match commented-out rules. |
| 250 | assert_expansion_function "expand_rules_in_package" "" label-bin \ |
| 251 | '' 'video/streamer2:comment_run_target_1o' |
| 252 | assert_expansion_function "expand_rules_in_package" "" label-bin \ |
| 253 | 'comment_run_target_2new ' 'video/streamer2:comment_run_target_2' |
| 254 | |
| 255 | # Test that 'label-bin' expands binaries with spaces in the build rules |
| 256 | assert_expansion_function "expand_rules_in_package" "" label-bin \ |
| 257 | 'JavaBinary ' 'video/streamer2:J' |
| 258 | |
| 259 | # Test that 'label-bin' expands targets when the name attribute is not first |
| 260 | assert_expansion_function "expand_rules_in_package" "" label-bin \ |
| 261 | 'pybin ' 'video/streamer2:py' |
| 262 | |
| 263 | # Test that 'label-bin' expands binaries with newlines in the build rules |
| 264 | assert_expansion_function "expand_rules_in_package" "" label-bin \ |
| 265 | 'AnotherJavaBinary ' 'video/streamer2:A' |
| 266 | |
| 267 | # Test that the expansion of rules with 'name=...' strings isn't messed up. |
| 268 | assert_expansion_function "expand_rules_in_package" "" label \ |
| 269 | 'foo ' 'video/streamer2/names:' |
| 270 | } |
| 271 | |
| 272 | test_expand_package_name() { |
| 273 | make_packages |
| 274 | assert_expansion_function "expand_package_name" "" "" \ |
| 275 | "//video/streamer2/stuff/\n//video/streamer2/stuff:" \ |
| 276 | "//video/streamer2/stu" |
| 277 | assert_expansion_function "expand_package_name" "" "" \ |
| 278 | "//video/notapackage/" \ |
| 279 | "//video/nota" |
| 280 | |
| 281 | assert_expansion_function "expand_package_name" "" "" \ |
| 282 | "video/streamer2/stuff/\nvideo/streamer2/stuff:" \ |
| 283 | "video/streamer2/stu" |
| 284 | assert_expansion_function "expand_package_name" "" "" \ |
| 285 | "video/notapackage/" \ |
| 286 | "video/nota" |
| 287 | |
| 288 | # From another directory |
| 289 | assert_expansion_function "expand_package_name" "video/" "" \ |
| 290 | "" \ |
| 291 | "video/streamer2/stu" |
| 292 | assert_expansion_function "expand_package_name" "video/" "" \ |
| 293 | "" \ |
| 294 | "video/nota" |
| 295 | assert_expansion_function "expand_package_name" "video/" "" \ |
| 296 | "streamer2/stuff/\nstreamer2/stuff:" \ |
| 297 | "streamer2/stu" |
| 298 | assert_expansion_function "expand_package_name" "video/" "" \ |
| 299 | "notapackage/" \ |
| 300 | "nota" |
| 301 | |
| 302 | # label-package |
| 303 | assert_expansion_function "expand_package_name" "" "label-package" \ |
| 304 | "//video/streamer2/stuff/\n//video/streamer2/stuff " \ |
| 305 | "//video/streamer2/stu" |
| 306 | assert_expansion_function "expand_package_name" "" "label-package" \ |
| 307 | "//video/notapackage/" \ |
| 308 | "//video/nota" |
| 309 | } |
| 310 | |
| 311 | test_expand_target_pattern() { |
| 312 | make_packages |
| 313 | assert_expansion_function "expand_target_pattern" "" label \ |
| 314 | "stuff " "//video/streamer2/stuff:" |
| 315 | |
| 316 | assert_expansion_function "expand_target_pattern" "" label \ |
| 317 | "//video/streamer2/stuff/\n//video/streamer2/stuff:" \ |
| 318 | "//video/streamer2/stu" |
| 319 | |
| 320 | assert_expansion_function "expand_target_pattern" "" label \ |
| 321 | "stuff " "video/streamer2/stuff:" |
| 322 | |
| 323 | assert_expansion_function "expand_target_pattern" "" label \ |
| 324 | "video/streamer2/stuff/\nvideo/streamer2/stuff:" \ |
| 325 | "video/streamer2/stu" |
| 326 | |
| 327 | assert_expansion_function "expand_target_pattern" "video/" label \ |
| 328 | "stuff " "streamer2/stuff:" |
| 329 | |
| 330 | assert_expansion_function "expand_target_pattern" "video/" label \ |
| 331 | "streamer2/stuff/\nstreamer2/stuff:" \ |
| 332 | "streamer2/stu" |
| 333 | |
| 334 | assert_expansion_function "expand_target_pattern" "video/" label \ |
| 335 | "stuff " "//video/streamer2/stuff:" |
| 336 | |
| 337 | assert_expansion_function "expand_target_pattern" "video/" label \ |
| 338 | "//video/streamer2/stuff/\n//video/streamer2/stuff:" \ |
| 339 | "//video/streamer2/stu" |
| 340 | |
| 341 | assert_expansion_function "expand_target_pattern" "video/" label \ |
| 342 | "" "video/streamer2/stuff:" |
| 343 | |
| 344 | assert_expansion_function "expand_target_pattern" "video/" label \ |
| 345 | "" "video/streamer2/stu" |
| 346 | } |
| 347 | |
| 348 | test_complete_pattern() { |
| 349 | make_packages |
| 350 | assert_expansion_function "complete_pattern" "" label \ |
| 351 | "stuff " "//video/streamer2/stuff:" |
| 352 | |
| 353 | assert_expansion_function "complete_pattern" "" label \ |
| 354 | "//video/streamer2/stuff/\n//video/streamer2/stuff:" \ |
| 355 | "//video/streamer2/stu" |
| 356 | |
| 357 | assert_expansion_function "complete_pattern" "" label-package \ |
| 358 | "//video/streamer2/stuff/\n//video/streamer2/stuff " \ |
| 359 | "//video/streamer2/stu" |
| 360 | |
| 361 | assert_expansion_function "complete_pattern" "" command \ |
| 362 | "clean " "clea" |
| 363 | |
| 364 | assert_expansion_function "complete_pattern" "" info-key \ |
| 365 | "install_base " "install_b" |
| 366 | |
| 367 | assert_expansion_function "complete_pattern" "" '{clean,add}' \ |
| 368 | "clean " "clea" |
| 369 | |
| 370 | assert_expansion_function "complete_pattern" "" 'command|{abc,def}' \ |
| 371 | "abc " "ab" |
| 372 | |
| 373 | assert_expansion_function "complete_pattern" "" 'command|{abc,def}' \ |
| 374 | "clean " "clea" |
| 375 | |
| 376 | # Assert label expansion |
| 377 | assert_expansion_function "complete_pattern" "" label \ |
| 378 | "stuff " "//video/streamer2/stuff:" |
| 379 | assert_expansion_function "complete_pattern" "" label \ |
| 380 | 'task_lib ' 'video/streamer2:ta' |
| 381 | assert_expansion_function "complete_pattern" "" label \ |
| 382 | 'with_special+_,=-.@~chars ' 'video/streamer2:with_s' |
| 383 | |
| 384 | # From a different directory |
| 385 | assert_expansion_function "complete_pattern" "video/" label \ |
| 386 | "stuff " "//video/streamer2/stuff:" |
| 387 | assert_expansion_function "complete_pattern" "video/" label \ |
| 388 | 'task_lib ' 'streamer2:ta' |
| 389 | assert_expansion_function "complete_pattern" "video/" label \ |
| 390 | '' 'video/streamer2:ta' |
| 391 | assert_expansion_function "complete_pattern" "video/" label \ |
| 392 | 'with_special+_,=-.@~chars ' 'streamer2:with_s' |
| 393 | } |
| 394 | |
| 395 | #### TESTS ############################################################# |
| 396 | |
| 397 | test_basic_subcommand_expansion() { |
| 398 | # 'Test basic subcommand completion' |
| 399 | assert_expansion 'bui' \ |
| 400 | 'build ' |
| 401 | assert_expansion 'hel' \ |
| 402 | 'help ' |
| 403 | assert_expansion 'shut' \ |
| 404 | 'shutdown ' |
| 405 | } |
| 406 | |
| 407 | test_common_options() { |
| 408 | # 'Test common option completion' |
| 409 | assert_expansion '--h' \ |
| 410 | '--host_jvm_' |
| 411 | assert_expansion '--host_jvm_a' \ |
| 412 | '--host_jvm_args=' |
| 413 | } |
| 414 | |
| 415 | test_build_options() { |
| 416 | # 'Test build option completion' |
| 417 | assert_expansion 'build --keep_g' \ |
| 418 | 'build --keep_going ' |
| 419 | assert_expansion 'build --expe' \ |
| 420 | 'build --experimental_' |
| 421 | # ...but 'help' doesn't expand this option: |
| 422 | assert_expansion 'help --cros' \ |
| 423 | 'help --cros' |
| 424 | assert_expansion 'build --test_stra' \ |
| 425 | 'build --test_strategy=' |
| 426 | } |
| 427 | |
| 428 | test_query_options() { |
| 429 | assert_expansion 'query --out' \ |
| 430 | 'query --output=' |
| 431 | |
| 432 | # Basic label expansion works for query, too. |
| 433 | make_packages |
| 434 | assert_expansion 'query video/streamer2:ta' \ |
| 435 | 'query video/streamer2:task_lib ' |
| 436 | assert_expansion 'query //video/streamer2:ta'\ |
| 437 | 'query //video/streamer2:task_lib ' |
| 438 | } |
| 439 | |
| 440 | test_run_options() { |
| 441 | # Should be the same as the build options. |
| 442 | # 'Test run option completion' |
| 443 | assert_expansion 'run --keep_g' \ |
| 444 | 'run --keep_going ' |
| 445 | assert_expansion 'run --expe' \ |
| 446 | 'run --experimental_' |
| 447 | } |
| 448 | |
| 449 | test_tristate_option() { |
| 450 | # 'Test tristate option completion' |
| 451 | assert_expansion 'build --nocache_test_result' \ |
| 452 | 'build --nocache_test_results ' |
| 453 | } |
| 454 | |
| 455 | make_dirs() { |
| 456 | mkdir -p video/streamer2/testing || fail "mkdir failed" |
| 457 | mkdir -p ${PACKAGE_PATH_PREFIX:-}video/streamer2/stuff || fail "mkdir failed" |
| 458 | mkdir -p video/streamer2/names || fail "mkdir failed" |
| 459 | } |
| 460 | |
| 461 | |
| 462 | test_directory_expansion() { |
| 463 | # 'Test expansion of directory names, even across package_path' |
| 464 | |
| 465 | make_dirs |
| 466 | |
| 467 | assert_expansion 'build vide' \ |
| 468 | 'build video/' |
| 469 | assert_expansion 'build video/' \ |
| 470 | 'build video/streamer2/' |
| 471 | assert_expansion 'build video/streamer2/t' \ |
| 472 | 'build video/streamer2/testing/' |
| 473 | assert_expansion 'build video/streamer2/s' \ |
| 474 | 'build video/streamer2/stuff/' |
| 475 | |
| 476 | # Now add BUILD files; it should no longer expand the trailing slashes: |
| 477 | make_empty_packages |
| 478 | |
| 479 | assert_expansion 'build video/streamer2/t' \ |
| 480 | 'build video/streamer2/testing' |
| 481 | assert_expansion 'build video/streamer2/s' \ |
| 482 | 'build video/streamer2/stuff' |
| 483 | |
| 484 | # Use of absolute forms of labels: |
| 485 | assert_expansion 'build //vide' \ |
| 486 | 'build //video/' |
| 487 | assert_expansion 'build //video/' \ |
| 488 | 'build //video/streamer2/' |
| 489 | assert_expansion 'build //video/streamer2/t' \ |
| 490 | 'build //video/streamer2/testing' |
| 491 | assert_expansion 'build //video/streamer2/s' \ |
| 492 | 'build //video/streamer2/stuff' |
| 493 | } |
| 494 | |
| 495 | test_directory_expansion_in_subdir() { |
| 496 | # 'Test expansion of directory names, when in a subdir of the workspace.' |
| 497 | |
| 498 | make_dirs |
| 499 | cd video 2>/dev/null || exit |
| 500 | |
| 501 | # Use of "video" while in "video" => no match: |
| 502 | assert_expansion 'build vide' \ |
| 503 | 'build vide' |
| 504 | assert_expansion 'build video/' \ |
| 505 | 'build video/' |
| 506 | assert_expansion 'build video/streamer2/t' \ |
| 507 | 'build video/streamer2/t' |
| 508 | assert_expansion 'build video/streamer2/s' \ |
| 509 | 'build video/streamer2/s' |
| 510 | |
| 511 | # Use of "//video" while in "video" => matches absolute: |
| 512 | assert_expansion 'build //vide' \ |
| 513 | 'build //video/' |
| 514 | assert_expansion 'build //video/' \ |
| 515 | 'build //video/streamer2/' |
| 516 | assert_expansion 'build //video/streamer2/t' \ |
| 517 | 'build //video/streamer2/testing/' |
| 518 | assert_expansion 'build //video/streamer2/s' \ |
| 519 | 'build //video/streamer2/stuff/' |
| 520 | |
| 521 | # Use of relative paths => matches |
| 522 | assert_expansion 'build streamer2/t' \ |
| 523 | 'build streamer2/testing/' |
| 524 | assert_expansion 'build streamer2/s' \ |
| 525 | 'build streamer2/stuff/' |
| 526 | } |
| 527 | |
| 528 | test_target_expansion() { |
| 529 | # 'Test expansion of target names within packages' |
| 530 | |
| 531 | make_packages |
| 532 | |
| 533 | # TODO(bazel-team): (2009) it would be good to test that "streamer2\t" |
| 534 | # yielded a menu of "streamer2:" and "streamer2/", but testing the |
| 535 | # terminal output (as opposed to the result of expansion) is |
| 536 | # beyond our ability right now. |
| 537 | |
| 538 | assert_expansion 'build video/streamer2:ta' \ |
| 539 | 'build video/streamer2:task_lib ' |
| 540 | |
| 541 | # Special characters |
| 542 | assert_expansion 'build video/streamer2:with_s' \ |
| 543 | 'build video/streamer2:with_special+_,=-.@~chars ' |
| 544 | |
| 545 | # Also, that 'bazel build' matches test and non-test rules (lack |
| 546 | # of trailing space after match => not unique match). |
| 547 | assert_expansion 'build video/streamer2:to' \ |
| 548 | 'build video/streamer2:token_bucket' |
| 549 | |
| 550 | assert_expansion 'build video/streamer2/s' \ |
| 551 | 'build video/streamer2/stuff' |
| 552 | |
| 553 | assert_expansion 'build video/streamer2/stuff:s' \ |
| 554 | 'build video/streamer2/stuff:stuff ' |
| 555 | |
| 556 | # Test that 'bazel build' does not match commented-out rules. |
| 557 | assert_expansion 'build video/streamer2:comment_build_target_1o' \ |
| 558 | 'build video/streamer2:comment_build_target_1o' |
| 559 | |
| 560 | assert_expansion 'build video/streamer2:comment_build_target_2' \ |
| 561 | 'build video/streamer2:comment_build_target_2new ' |
| 562 | |
| 563 | # Test that 'bazel test' expands only test rules. |
| 564 | assert_expansion 'test video/streamer2:to' \ |
| 565 | 'test video/streamer2:token_bucket_test ' |
| 566 | |
| 567 | # Test that 'blaze test' does not match commented-out rules. |
| 568 | assert_expansion 'test video/streamer2:token_bucket_t_1o' \ |
| 569 | 'test video/streamer2:token_bucket_t_1o' |
| 570 | |
| 571 | assert_expansion 'test video/streamer2:token_bucket_t' \ |
| 572 | 'test video/streamer2:token_bucket_test ' |
| 573 | |
| 574 | assert_expansion_error_not_contains 'test video/streamer2:match' \ |
| 575 | 'syntax error' |
| 576 | |
| 577 | # Test that :all wildcard is expanded when there is more than one |
| 578 | # match. |
| 579 | # |
| 580 | # One match => no :all. |
| 581 | assert_expansion 'test video/streamer2:' \ |
| 582 | 'test video/streamer2:token_bucket_test ' |
| 583 | # Multiple matches => :all. |
| 584 | assert_expansion 'build video/streamer2:a' \ |
| 585 | 'build video/streamer2:all ' |
| 586 | |
| 587 | # Test that 'bazel run' expands only non-test binary rules. |
| 588 | assert_expansion 'run video/streamer2:to' \ |
| 589 | 'run video/streamer2:token_bucket_binary ' |
| 590 | |
| 591 | # Test that 'bazel run' expands for binary and test rules, but not library |
| 592 | # with BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set. |
| 593 | assert_expansion 'run video/streamer2:to' \ |
| 594 | 'run video/streamer2:token_bucket_' \ |
| 595 | 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=true' |
| 596 | |
| 597 | # Test the 'bazel run' expands for test rules, with |
| 598 | # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set. |
| 599 | assert_expansion 'run video/streamer2:token_bucket_t' \ |
| 600 | 'run video/streamer2:token_bucket_test ' \ |
| 601 | 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=1' |
| 602 | |
| 603 | # Test that 'bazel run' expands only non-test binary rules when the |
| 604 | # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is false. |
| 605 | assert_expansion 'run video/streamer2:to' \ |
| 606 | 'run video/streamer2:token_bucket_binary ' \ |
| 607 | 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=false' |
| 608 | |
| 609 | # Test that 'bazel run' expands only non-test binary rules when the |
| 610 | # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is false. |
| 611 | assert_expansion 'run video/streamer2:to' \ |
| 612 | 'run video/streamer2:token_bucket_binary ' \ |
| 613 | 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=0' |
| 614 | |
| 615 | # Test that 'bazel run' expands only non-test binary rules when the |
| 616 | # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is invalid. |
| 617 | assert_expansion 'run video/streamer2:to' \ |
| 618 | 'run video/streamer2:token_bucket_binary ' \ |
| 619 | 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=junk' |
| 620 | |
| 621 | # Test that 'bazel run' expands only non-test binary rules when the |
| 622 | # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is empty. |
| 623 | assert_expansion 'run video/streamer2:to' \ |
| 624 | 'run video/streamer2:token_bucket_binary ' \ |
| 625 | 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=' |
| 626 | |
| 627 | # Test that 'bazel run' does not match commented-out rules. |
| 628 | assert_expansion 'run video/streamer2:comment_run_target_1o' \ |
| 629 | 'run video/streamer2:comment_run_target_1o' |
| 630 | |
| 631 | assert_expansion 'run video/streamer2:comment_run_target_2' \ |
| 632 | 'run video/streamer2:comment_run_target_2new ' |
| 633 | |
| 634 | # Test that 'bazel run' expands binaries with spaces in the build rules |
| 635 | assert_expansion 'run video/streamer2:J' \ |
| 636 | 'run video/streamer2:JavaBinary ' |
| 637 | |
| 638 | # Test that 'bazel run' expands targets when the name attribute is not first |
| 639 | assert_expansion 'run video/streamer2:py' \ |
| 640 | 'run video/streamer2:pybin ' |
| 641 | |
| 642 | # Test that 'bazel run' expands binaries with newlines in the build rules |
| 643 | assert_expansion 'run video/streamer2:A' \ |
| 644 | 'run video/streamer2:AnotherJavaBinary ' |
| 645 | |
| 646 | # Test that the expansion of rules with 'name=...' strings isn't messed up. |
| 647 | assert_expansion 'build video/streamer2/names:' \ |
| 648 | 'build video/streamer2/names:foo ' |
| 649 | |
| 650 | # Test that dashes are matched even when locale isn't C. |
| 651 | LC_ALL=en_US.UTF-8 \ |
| 652 | assert_expansion 'build dash:m' \ |
| 653 | 'build dash:mia-bid-multiplier-mixer-module ' |
| 654 | } |
| 655 | |
| 656 | test_target_expansion_in_subdir() { |
| 657 | # 'Test expansion of targets when in a subdir of the workspace.' |
| 658 | |
| 659 | make_packages |
| 660 | cd video 2>/dev/null |
| 661 | |
| 662 | # Relative labels: |
| 663 | assert_expansion 'build streamer2:ta' \ |
| 664 | 'build streamer2:task_lib ' |
| 665 | |
| 666 | assert_expansion 'build streamer2:to' \ |
| 667 | 'build streamer2:token_bucket' |
| 668 | |
| 669 | assert_expansion 'build streamer2/s' \ |
| 670 | 'build streamer2/stuff' |
| 671 | |
| 672 | assert_expansion 'build streamer2/stuff:s' \ |
| 673 | 'build streamer2/stuff:stuff ' |
| 674 | |
| 675 | # (no match) |
| 676 | assert_expansion 'build video/streamer2:ta' \ |
| 677 | 'build video/streamer2:ta' |
| 678 | |
| 679 | # Absolute labels work as usual: |
| 680 | assert_expansion 'build //video/streamer2:ta' \ |
| 681 | 'build //video/streamer2:task_lib ' |
| 682 | } |
| 683 | |
| 684 | test_target_expansion_in_package() { |
| 685 | # 'Test expansion of targets when in a package.' |
| 686 | |
| 687 | make_packages |
| 688 | cd video/streamer2 2>/dev/null |
| 689 | |
| 690 | assert_expansion 'build :ta' \ |
| 691 | 'build :task_lib ' |
| 692 | |
| 693 | assert_expansion 'build :to' \ |
| 694 | 'build :token_bucket' |
| 695 | |
| 696 | # allow slashes in rule names |
| 697 | assert_expansion 'build :checks/th' \ |
| 698 | 'build :checks/thingy ' |
| 699 | |
| 700 | assert_expansion 'build s' \ |
| 701 | 'build stuff' |
| 702 | |
| 703 | # (no expansion) |
| 704 | assert_expansion 'build :s' \ |
| 705 | 'build :s' |
| 706 | } |
| 707 | |
| 708 | test_help() { |
| 709 | # "Test that bazel help expands subcommand names" |
| 710 | assert_expansion 'help qu' \ |
| 711 | 'help query ' |
| 712 | assert_expansion 'help bui' \ |
| 713 | 'help build ' |
| 714 | assert_expansion 'help shut' \ |
| 715 | 'help shutdown ' |
| 716 | assert_expansion 'help start' \ |
| 717 | 'help startup_options ' |
| 718 | } |
| 719 | |
| 720 | test_info() { |
| 721 | # "Test that bazel info keys are expanded" |
| 722 | assert_expansion 'info commi' \ |
| 723 | 'info committed-heap-size ' |
| 724 | assert_expansion 'info i' \ |
| 725 | 'info install_base ' |
| 726 | assert_expansion 'info --show_m' \ |
| 727 | 'info --show_make_env ' |
| 728 | } |
| 729 | |
| 730 | run_suite "Tests of bash completion of 'blaze' command." |