blob: ca66c42e8899c58ebce2f0bf5638e65de8201d4b [file] [log] [blame]
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +00001#!/bin/bash
2#
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2015 The Bazel Authors. All rights reserved.
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +00004#
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)}
20source ${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
Kristina Chodorow93fbc3e2016-04-27 17:03:28 +000026: ${COMPLETION:="$TEST_SRCDIR/io_bazel/scripts/bazel-complete.bash"}
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +000027
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
36function 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.
57expand() {
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.
Androbin9c78a792017-11-29 01:31:47 -080071 for i in "${COMMAND_ALIASES[@]}"; do
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +000072 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'
83assert_expansion() {
84 local prefix=$1 expected=$2 flags=${3:-}
Androbin9c78a792017-11-29 01:31:47 -080085 for i in "${COMMAND_ALIASES[@]}"; do
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +000086 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.
100assert_expansion_error_not_contains() {
101 local prefix=$1 not_expected=$2 flags=${3:-}
Julio Merino672cb582016-09-01 16:39:17 +0000102 local temp_file="$(mktemp "${TEST_TMPDIR}/tmp.stderr.XXXXXX")"
Androbin9c78a792017-11-29 01:31:47 -0800103 for i in "${COMMAND_ALIASES[@]}"; do
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000104 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
112make_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
118make_packages() {
119 mkdir -p video/streamer2/testing || fail "mkdir failed"
120 cat >video/streamer2/BUILD <<EOF
121cc_library(name='task_lib', ...)
122cc_library(name='token_bucket', ...)
123cc_library(name='with_special+_,=-.@~chars', ...)
124#cc_library(name='comment_build_target_1old', ...)
125#cc_library(name='comment_build_target_2old', ...)
126cc_library(name='comment_build_target_2new', ...)
127#cc_test(name='token_bucket_t_1old', ...)
128#cc_test(name='token_bucket_t_2old', ...)
129cc_test(name='token_bucket_test', ...)
130cc_binary(name='token_bucket_binary', ...)
131java_binary ( name = 'JavaBinary', ...)
132java_binary (
133 name = 'AnotherJavaBinary'
134 ...
135)
136cc_binary(other='thing', name='pybin', ...)
137genrule(name='checks/thingy', ...)
138#cc_binary(name='comment_run_target_1old', ...)
139#cc_binary(name='comment_run_target_2old', ...)
140cc_binary(name='comment_run_target_2new', ...)
141EOF
142
143 mkdir -p ${PACKAGE_PATH_PREFIX:-}video/streamer2/stuff || fail "mkdir failed"
144 cat >${PACKAGE_PATH_PREFIX:-}video/streamer2/stuff/BUILD <<EOF
145cc_library(name='stuff', ...)
146EOF
147
148 mkdir -p video/streamer2/names || fail "mkdir failed"
149 cat >video/streamer2/names/BUILD <<EOF
150genrule(
151 name = 'foo',
152 cmd = ('name=foo'),
153)
154EOF
155
156 mkdir -p dash || fail "mkdir failed"
157 cat >dash/BUILD <<EOF
158cc_library(
159 name = "mia-bid-multiplier-mixer-module",
160)
161EOF
162
163 mkdir -p video/notapackage
164}
165
166#### UNIT TESTS ########################################################
167
168source ${COMPLETION}
169
170assert_expansion_function() {
171 local ws=${PWD}
172 local function="$1" displacement="$2" type="$3" expected="$4" current="$5"
Philipp Wollermann713a78c2015-12-14 12:40:08 +0000173 disable_errexit
Yuval055c93d2021-03-16 02:18:53 -0700174 local actual_result=$(eval "_bazel__${function} \"${ws}\" \"${displacement}\" \"${current}\" \"${type}\"" | sort)
Philipp Wollermann713a78c2015-12-14 12:40:08 +0000175 enable_errexit
176 assert_equals "$(echo -ne "${expected}")" "${actual_result}"
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000177}
178
179test_expand_rules_in_package() {
180 make_packages
181
182 assert_expansion_function "expand_rules_in_package" "" label \
183 "stuff " "//video/streamer2/stuff:"
184 assert_expansion_function "expand_rules_in_package" "" label \
185 'task_lib ' 'video/streamer2:ta'
186 assert_expansion_function "expand_rules_in_package" "" label \
187 'with_special+_,=-.@~chars ' 'video/streamer2:with_s'
188
189 # From a different directory
190 assert_expansion_function "expand_rules_in_package" "video/" label \
191 'task_lib ' 'streamer2:ta'
192 assert_expansion_function "expand_rules_in_package" "video/" label \
193 '' 'video/streamer2:ta'
194 assert_expansion_function "expand_rules_in_package" "video/" label \
195 'with_special+_,=-.@~chars ' 'streamer2:with_s'
196
197 # label should match test and non-test rules
198 assert_expansion_function "expand_rules_in_package" "" label \
Yuval055c93d2021-03-16 02:18:53 -0700199 'token_bucket_binary \ntoken_bucket_test ' \
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000200 'video/streamer2:token_bucket_'
201 assert_expansion_function "expand_rules_in_package" "" label \
202 'stuff ' 'video/streamer2/stuff:s'
203 # Test that label does not match commented-out rules.
204 assert_expansion_function "expand_rules_in_package" "" label \
205 '' 'video/streamer2:comment_build_target_1o'
206 assert_expansion_function "expand_rules_in_package" "" label \
207 'comment_build_target_2new ' 'video/streamer2:comment_build_target_2'
208
209 # Test that 'label-test' expands only test rules.
210 assert_expansion_function "expand_rules_in_package" "" label-test \
211 'token_bucket_test ' 'video/streamer2:to'
212
213 # Test that 'label-test' does not match commented-out rules.
214 assert_expansion_function "expand_rules_in_package" "" label-test \
215 '' 'video/streamer2:token_bucket_t_1o'
216 assert_expansion_function "expand_rules_in_package" "" label-test \
217 'token_bucket_test ' 'video/streamer2:token_bucket_t'
218
219 # Test that :all wildcard is expanded when there is more than one
220 # match.
221 #
222 # One match => no :all.
223 assert_expansion_function "expand_rules_in_package" "" label-test \
224 'token_bucket_test ' 'video/streamer2:'
225 # Multiple matches => :all.
226 assert_expansion_function "expand_rules_in_package" "" label-test \
227 'all ' 'video/streamer2:a'
228
229 # Test that label-bin expands only non-test binary rules.
230 assert_expansion_function "expand_rules_in_package" "" label-bin \
231 'token_bucket_binary ' 'video/streamer2:to'
232
233 # Test that label-bin expands for binary and test rules, but not library
234 # with BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set.
235 BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=true \
236 assert_expansion_function "expand_rules_in_package" "" label-bin \
Yuval055c93d2021-03-16 02:18:53 -0700237 'token_bucket_binary \ntoken_bucket_test ' 'video/streamer2:to'
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000238
239 # Test the label-bin expands for test rules, with
240 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set.
241 BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=1 \
242 assert_expansion_function "expand_rules_in_package" "" label-bin \
243 'token_bucket_test ' 'video/streamer2:token_bucket_t'
244
245 # Test that 'label-bin' expands only non-test binary rules when the
246 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is false.
247 BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=false \
248 assert_expansion_function "expand_rules_in_package" "" label-bin \
249 'token_bucket_binary ' 'video/streamer2:to'
250
251 # Test that 'label-bin' does not match commented-out rules.
252 assert_expansion_function "expand_rules_in_package" "" label-bin \
253 '' 'video/streamer2:comment_run_target_1o'
254 assert_expansion_function "expand_rules_in_package" "" label-bin \
255 'comment_run_target_2new ' 'video/streamer2:comment_run_target_2'
256
257 # Test that 'label-bin' expands binaries with spaces in the build rules
258 assert_expansion_function "expand_rules_in_package" "" label-bin \
259 'JavaBinary ' 'video/streamer2:J'
260
261 # Test that 'label-bin' expands targets when the name attribute is not first
262 assert_expansion_function "expand_rules_in_package" "" label-bin \
263 'pybin ' 'video/streamer2:py'
264
265 # Test that 'label-bin' expands binaries with newlines in the build rules
266 assert_expansion_function "expand_rules_in_package" "" label-bin \
267 'AnotherJavaBinary ' 'video/streamer2:A'
268
269 # Test that the expansion of rules with 'name=...' strings isn't messed up.
270 assert_expansion_function "expand_rules_in_package" "" label \
271 'foo ' 'video/streamer2/names:'
272}
273
274test_expand_package_name() {
275 make_packages
276 assert_expansion_function "expand_package_name" "" "" \
277 "//video/streamer2/stuff/\n//video/streamer2/stuff:" \
278 "//video/streamer2/stu"
279 assert_expansion_function "expand_package_name" "" "" \
280 "//video/notapackage/" \
281 "//video/nota"
282
283 assert_expansion_function "expand_package_name" "" "" \
284 "video/streamer2/stuff/\nvideo/streamer2/stuff:" \
285 "video/streamer2/stu"
286 assert_expansion_function "expand_package_name" "" "" \
287 "video/notapackage/" \
288 "video/nota"
289
290 # From another directory
291 assert_expansion_function "expand_package_name" "video/" "" \
292 "" \
293 "video/streamer2/stu"
294 assert_expansion_function "expand_package_name" "video/" "" \
295 "" \
296 "video/nota"
297 assert_expansion_function "expand_package_name" "video/" "" \
298 "streamer2/stuff/\nstreamer2/stuff:" \
299 "streamer2/stu"
300 assert_expansion_function "expand_package_name" "video/" "" \
301 "notapackage/" \
302 "nota"
303
304 # label-package
305 assert_expansion_function "expand_package_name" "" "label-package" \
Yuval055c93d2021-03-16 02:18:53 -0700306 "//video/streamer2/stuff \n//video/streamer2/stuff/" \
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000307 "//video/streamer2/stu"
308 assert_expansion_function "expand_package_name" "" "label-package" \
309 "//video/notapackage/" \
310 "//video/nota"
311}
312
313test_expand_target_pattern() {
314 make_packages
315 assert_expansion_function "expand_target_pattern" "" label \
316 "stuff " "//video/streamer2/stuff:"
317
318 assert_expansion_function "expand_target_pattern" "" label \
319 "//video/streamer2/stuff/\n//video/streamer2/stuff:" \
320 "//video/streamer2/stu"
321
322 assert_expansion_function "expand_target_pattern" "" label \
323 "stuff " "video/streamer2/stuff:"
324
325 assert_expansion_function "expand_target_pattern" "" label \
326 "video/streamer2/stuff/\nvideo/streamer2/stuff:" \
327 "video/streamer2/stu"
328
329 assert_expansion_function "expand_target_pattern" "video/" label \
330 "stuff " "streamer2/stuff:"
331
332 assert_expansion_function "expand_target_pattern" "video/" label \
333 "streamer2/stuff/\nstreamer2/stuff:" \
334 "streamer2/stu"
335
336 assert_expansion_function "expand_target_pattern" "video/" label \
337 "stuff " "//video/streamer2/stuff:"
338
339 assert_expansion_function "expand_target_pattern" "video/" label \
340 "//video/streamer2/stuff/\n//video/streamer2/stuff:" \
341 "//video/streamer2/stu"
342
343 assert_expansion_function "expand_target_pattern" "video/" label \
344 "" "video/streamer2/stuff:"
345
346 assert_expansion_function "expand_target_pattern" "video/" label \
347 "" "video/streamer2/stu"
348}
349
350test_complete_pattern() {
351 make_packages
352 assert_expansion_function "complete_pattern" "" label \
353 "stuff " "//video/streamer2/stuff:"
354
355 assert_expansion_function "complete_pattern" "" label \
356 "//video/streamer2/stuff/\n//video/streamer2/stuff:" \
357 "//video/streamer2/stu"
358
359 assert_expansion_function "complete_pattern" "" label-package \
Yuval055c93d2021-03-16 02:18:53 -0700360 "//video/streamer2/stuff \n//video/streamer2/stuff/" \
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000361 "//video/streamer2/stu"
362
363 assert_expansion_function "complete_pattern" "" command \
364 "clean " "clea"
365
366 assert_expansion_function "complete_pattern" "" info-key \
367 "install_base " "install_b"
368
369 assert_expansion_function "complete_pattern" "" '{clean,add}' \
370 "clean " "clea"
371
372 assert_expansion_function "complete_pattern" "" 'command|{abc,def}' \
373 "abc " "ab"
374
375 assert_expansion_function "complete_pattern" "" 'command|{abc,def}' \
376 "clean " "clea"
377
378 # Assert label expansion
379 assert_expansion_function "complete_pattern" "" label \
380 "stuff " "//video/streamer2/stuff:"
381 assert_expansion_function "complete_pattern" "" label \
382 'task_lib ' 'video/streamer2:ta'
383 assert_expansion_function "complete_pattern" "" label \
384 'with_special+_,=-.@~chars ' 'video/streamer2:with_s'
385
386 # From a different directory
387 assert_expansion_function "complete_pattern" "video/" label \
388 "stuff " "//video/streamer2/stuff:"
389 assert_expansion_function "complete_pattern" "video/" label \
390 'task_lib ' 'streamer2:ta'
391 assert_expansion_function "complete_pattern" "video/" label \
392 '' 'video/streamer2:ta'
393 assert_expansion_function "complete_pattern" "video/" label \
394 'with_special+_,=-.@~chars ' 'streamer2:with_s'
Yuval055c93d2021-03-16 02:18:53 -0700395
396 # Path expansion
397 if [[ -z $PACKAGE_PATH_PREFIX ]]; then
398 assert_expansion_function "complete_pattern" "" path \
399 "video/streamer2/BUILD \nvideo/streamer2/names/\nvideo/streamer2/stuff/\nvideo/streamer2/testing/" \
400 "video/streamer2/"
401 else
402 # When $PACKAGE_PATH_PREFIX is set, the "stuff" directory will not be in
403 # the same directory as the others, so we have to omit it.
404 assert_expansion_function "complete_pattern" "" path \
405 "video/streamer2/BUILD \nvideo/streamer2/names/\nvideo/streamer2/testing/" \
406 "video/streamer2/"
407 fi
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000408}
409
410#### TESTS #############################################################
411
412test_basic_subcommand_expansion() {
413 # 'Test basic subcommand completion'
414 assert_expansion 'bui' \
415 'build '
416 assert_expansion 'hel' \
417 'help '
418 assert_expansion 'shut' \
419 'shutdown '
420}
421
ccalvarin5e5ee0d2018-08-23 08:56:01 -0700422test_common_startup_options() {
423 # 'Test common startup option completion'
424 assert_expansion '--hos' \
cushon2a8b6572018-07-25 10:33:40 -0700425 '--host_jvm_'
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000426 assert_expansion '--host_jvm_a' \
427 '--host_jvm_args='
428}
429
430test_build_options() {
431 # 'Test build option completion'
432 assert_expansion 'build --keep_g' \
433 'build --keep_going '
434 assert_expansion 'build --expe' \
435 'build --experimental_'
436 # ...but 'help' doesn't expand this option:
437 assert_expansion 'help --cros' \
438 'help --cros'
439 assert_expansion 'build --test_stra' \
440 'build --test_strategy='
441}
442
443test_query_options() {
444 assert_expansion 'query --out' \
445 'query --output='
446
447 # Basic label expansion works for query, too.
448 make_packages
449 assert_expansion 'query video/streamer2:ta' \
450 'query video/streamer2:task_lib '
451 assert_expansion 'query //video/streamer2:ta'\
452 'query //video/streamer2:task_lib '
453}
454
455test_run_options() {
456 # Should be the same as the build options.
457 # 'Test run option completion'
458 assert_expansion 'run --keep_g' \
459 'run --keep_going '
460 assert_expansion 'run --expe' \
461 'run --experimental_'
462}
463
464test_tristate_option() {
465 # 'Test tristate option completion'
466 assert_expansion 'build --nocache_test_result' \
467 'build --nocache_test_results '
468}
469
470make_dirs() {
471 mkdir -p video/streamer2/testing || fail "mkdir failed"
472 mkdir -p ${PACKAGE_PATH_PREFIX:-}video/streamer2/stuff || fail "mkdir failed"
473 mkdir -p video/streamer2/names || fail "mkdir failed"
474}
475
476
477test_directory_expansion() {
478 # 'Test expansion of directory names, even across package_path'
479
480 make_dirs
481
482 assert_expansion 'build vide' \
483 'build video/'
484 assert_expansion 'build video/' \
485 'build video/streamer2/'
486 assert_expansion 'build video/streamer2/t' \
487 'build video/streamer2/testing/'
488 assert_expansion 'build video/streamer2/s' \
489 'build video/streamer2/stuff/'
490
491 # Now add BUILD files; it should no longer expand the trailing slashes:
492 make_empty_packages
493
494 assert_expansion 'build video/streamer2/t' \
495 'build video/streamer2/testing'
496 assert_expansion 'build video/streamer2/s' \
497 'build video/streamer2/stuff'
498
499 # Use of absolute forms of labels:
500 assert_expansion 'build //vide' \
501 'build //video/'
502 assert_expansion 'build //video/' \
503 'build //video/streamer2/'
504 assert_expansion 'build //video/streamer2/t' \
505 'build //video/streamer2/testing'
506 assert_expansion 'build //video/streamer2/s' \
507 'build //video/streamer2/stuff'
508}
509
510test_directory_expansion_in_subdir() {
511 # 'Test expansion of directory names, when in a subdir of the workspace.'
512
513 make_dirs
514 cd video 2>/dev/null || exit
515
516 # Use of "video" while in "video" => no match:
517 assert_expansion 'build vide' \
518 'build vide'
519 assert_expansion 'build video/' \
520 'build video/'
521 assert_expansion 'build video/streamer2/t' \
522 'build video/streamer2/t'
523 assert_expansion 'build video/streamer2/s' \
524 'build video/streamer2/s'
525
526 # Use of "//video" while in "video" => matches absolute:
527 assert_expansion 'build //vide' \
528 'build //video/'
529 assert_expansion 'build //video/' \
530 'build //video/streamer2/'
531 assert_expansion 'build //video/streamer2/t' \
532 'build //video/streamer2/testing/'
533 assert_expansion 'build //video/streamer2/s' \
534 'build //video/streamer2/stuff/'
535
536 # Use of relative paths => matches
537 assert_expansion 'build streamer2/t' \
538 'build streamer2/testing/'
539 assert_expansion 'build streamer2/s' \
540 'build streamer2/stuff/'
541}
542
Cal Peyser351ffaa2016-08-31 14:14:41 +0000543test_target_expansion() {
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000544 # 'Test expansion of target names within packages'
545
546 make_packages
547
548 # TODO(bazel-team): (2009) it would be good to test that "streamer2\t"
549 # yielded a menu of "streamer2:" and "streamer2/", but testing the
550 # terminal output (as opposed to the result of expansion) is
551 # beyond our ability right now.
552
553 assert_expansion 'build video/streamer2:ta' \
554 'build video/streamer2:task_lib '
555
556 # Special characters
557 assert_expansion 'build video/streamer2:with_s' \
558 'build video/streamer2:with_special+_,=-.@~chars '
559
560 # Also, that 'bazel build' matches test and non-test rules (lack
561 # of trailing space after match => not unique match).
562 assert_expansion 'build video/streamer2:to' \
563 'build video/streamer2:token_bucket'
564
565 assert_expansion 'build video/streamer2/s' \
566 'build video/streamer2/stuff'
567
568 assert_expansion 'build video/streamer2/stuff:s' \
569 'build video/streamer2/stuff:stuff '
570
571 # Test that 'bazel build' does not match commented-out rules.
572 assert_expansion 'build video/streamer2:comment_build_target_1o' \
573 'build video/streamer2:comment_build_target_1o'
574
575 assert_expansion 'build video/streamer2:comment_build_target_2' \
576 'build video/streamer2:comment_build_target_2new '
577
578 # Test that 'bazel test' expands only test rules.
579 assert_expansion 'test video/streamer2:to' \
580 'test video/streamer2:token_bucket_test '
581
582 # Test that 'blaze test' does not match commented-out rules.
583 assert_expansion 'test video/streamer2:token_bucket_t_1o' \
584 'test video/streamer2:token_bucket_t_1o'
585
586 assert_expansion 'test video/streamer2:token_bucket_t' \
587 'test video/streamer2:token_bucket_test '
588
589 assert_expansion_error_not_contains 'test video/streamer2:match' \
590 'syntax error'
591
592 # Test that :all wildcard is expanded when there is more than one
593 # match.
594 #
595 # One match => no :all.
596 assert_expansion 'test video/streamer2:' \
597 'test video/streamer2:token_bucket_test '
598 # Multiple matches => :all.
599 assert_expansion 'build video/streamer2:a' \
600 'build video/streamer2:all '
601
602 # Test that 'bazel run' expands only non-test binary rules.
603 assert_expansion 'run video/streamer2:to' \
604 'run video/streamer2:token_bucket_binary '
605
606 # Test that 'bazel run' expands for binary and test rules, but not library
607 # with BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set.
608 assert_expansion 'run video/streamer2:to' \
609 'run video/streamer2:token_bucket_' \
610 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=true'
611
612 # Test the 'bazel run' expands for test rules, with
613 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set.
614 assert_expansion 'run video/streamer2:token_bucket_t' \
615 'run video/streamer2:token_bucket_test ' \
616 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=1'
617
618 # Test that 'bazel run' expands only non-test binary rules when the
619 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is false.
620 assert_expansion 'run video/streamer2:to' \
621 'run video/streamer2:token_bucket_binary ' \
622 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=false'
623
624 # Test that 'bazel run' expands only non-test binary rules when the
625 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is false.
626 assert_expansion 'run video/streamer2:to' \
627 'run video/streamer2:token_bucket_binary ' \
628 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=0'
629
630 # Test that 'bazel run' expands only non-test binary rules when the
631 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is invalid.
632 assert_expansion 'run video/streamer2:to' \
633 'run video/streamer2:token_bucket_binary ' \
634 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=junk'
635
636 # Test that 'bazel run' expands only non-test binary rules when the
637 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is empty.
638 assert_expansion 'run video/streamer2:to' \
639 'run video/streamer2:token_bucket_binary ' \
640 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN='
641
642 # Test that 'bazel run' does not match commented-out rules.
643 assert_expansion 'run video/streamer2:comment_run_target_1o' \
644 'run video/streamer2:comment_run_target_1o'
645
646 assert_expansion 'run video/streamer2:comment_run_target_2' \
647 'run video/streamer2:comment_run_target_2new '
648
649 # Test that 'bazel run' expands binaries with spaces in the build rules
650 assert_expansion 'run video/streamer2:J' \
651 'run video/streamer2:JavaBinary '
652
653 # Test that 'bazel run' expands targets when the name attribute is not first
654 assert_expansion 'run video/streamer2:py' \
655 'run video/streamer2:pybin '
656
657 # Test that 'bazel run' expands binaries with newlines in the build rules
658 assert_expansion 'run video/streamer2:A' \
659 'run video/streamer2:AnotherJavaBinary '
660
661 # Test that the expansion of rules with 'name=...' strings isn't messed up.
662 assert_expansion 'build video/streamer2/names:' \
663 'build video/streamer2/names:foo '
664
665 # Test that dashes are matched even when locale isn't C.
666 LC_ALL=en_US.UTF-8 \
667 assert_expansion 'build dash:m' \
668 'build dash:mia-bid-multiplier-mixer-module '
669}
670
671test_target_expansion_in_subdir() {
672 # 'Test expansion of targets when in a subdir of the workspace.'
673
674 make_packages
675 cd video 2>/dev/null
676
677 # Relative labels:
678 assert_expansion 'build streamer2:ta' \
679 'build streamer2:task_lib '
680
681 assert_expansion 'build streamer2:to' \
682 'build streamer2:token_bucket'
683
684 assert_expansion 'build streamer2/s' \
685 'build streamer2/stuff'
686
687 assert_expansion 'build streamer2/stuff:s' \
688 'build streamer2/stuff:stuff '
689
690 # (no match)
691 assert_expansion 'build video/streamer2:ta' \
692 'build video/streamer2:ta'
693
694 # Absolute labels work as usual:
695 assert_expansion 'build //video/streamer2:ta' \
696 'build //video/streamer2:task_lib '
697}
698
699test_target_expansion_in_package() {
700 # 'Test expansion of targets when in a package.'
701
702 make_packages
703 cd video/streamer2 2>/dev/null
704
705 assert_expansion 'build :ta' \
706 'build :task_lib '
707
708 assert_expansion 'build :to' \
709 'build :token_bucket'
710
711 # allow slashes in rule names
712 assert_expansion 'build :checks/th' \
713 'build :checks/thingy '
714
715 assert_expansion 'build s' \
716 'build stuff'
717
718 # (no expansion)
719 assert_expansion 'build :s' \
720 'build :s'
721}
722
Yuval055c93d2021-03-16 02:18:53 -0700723test_filename_expansion_after_double_dash() {
724 make_packages
725 assert_expansion 'run :target -- vid' \
726 'run :target -- video/'
727 assert_expansion 'run :target -- video/st' \
728 'run :target -- video/streamer2/'
729 assert_expansion 'run :target -- video/streamer2/B' \
730 'run :target -- video/streamer2/BUILD '
731 assert_expansion 'run :target -- video/streamer2/n' \
732 'run :target -- video/streamer2/names/'
733
734 # Autocomplete arguments as well.
735 assert_expansion 'run :target -- --arg=video/streamer2/n' \
736 'run :target -- --arg=video/streamer2/names/'
737}
738
Damien Martin-Guillerez5f9c6ba2015-04-09 21:10:33 +0000739test_help() {
740 # "Test that bazel help expands subcommand names"
741 assert_expansion 'help qu' \
742 'help query '
743 assert_expansion 'help bui' \
744 'help build '
745 assert_expansion 'help shut' \
746 'help shutdown '
747 assert_expansion 'help start' \
748 'help startup_options '
749}
750
751test_info() {
752 # "Test that bazel info keys are expanded"
753 assert_expansion 'info commi' \
754 'info committed-heap-size '
755 assert_expansion 'info i' \
756 'info install_base '
757 assert_expansion 'info --show_m' \
758 'info --show_make_env '
759}
760
761run_suite "Tests of bash completion of 'blaze' command."