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