blob: 3eed6fbc86edf95c5d664c4f4c07f6f9ca651e2e [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
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
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.
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'
83assert_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.
100assert_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
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
174 local actual_result=$(eval "_bazel__${function} \"${ws}\" \"${displacement}\" \"${current}\" \"${type}\"")
175 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 \
199 'token_bucket_test \ntoken_bucket_binary ' \
200 '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 \
237 'token_bucket_test \ntoken_bucket_binary ' 'video/streamer2:to'
238
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" \
306 "//video/streamer2/stuff/\n//video/streamer2/stuff " \
307 "//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 \
360 "//video/streamer2/stuff/\n//video/streamer2/stuff " \
361 "//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'
395}
396
397#### TESTS #############################################################
398
399test_basic_subcommand_expansion() {
400 # 'Test basic subcommand completion'
401 assert_expansion 'bui' \
402 'build '
403 assert_expansion 'hel' \
404 'help '
405 assert_expansion 'shut' \
406 'shutdown '
407}
408
409test_common_options() {
410 # 'Test common option completion'
411 assert_expansion '--h' \
412 '--host_jvm_'
413 assert_expansion '--host_jvm_a' \
414 '--host_jvm_args='
415}
416
417test_build_options() {
418 # 'Test build option completion'
419 assert_expansion 'build --keep_g' \
420 'build --keep_going '
421 assert_expansion 'build --expe' \
422 'build --experimental_'
423 # ...but 'help' doesn't expand this option:
424 assert_expansion 'help --cros' \
425 'help --cros'
426 assert_expansion 'build --test_stra' \
427 'build --test_strategy='
428}
429
430test_query_options() {
431 assert_expansion 'query --out' \
432 'query --output='
433
434 # Basic label expansion works for query, too.
435 make_packages
436 assert_expansion 'query video/streamer2:ta' \
437 'query video/streamer2:task_lib '
438 assert_expansion 'query //video/streamer2:ta'\
439 'query //video/streamer2:task_lib '
440}
441
442test_run_options() {
443 # Should be the same as the build options.
444 # 'Test run option completion'
445 assert_expansion 'run --keep_g' \
446 'run --keep_going '
447 assert_expansion 'run --expe' \
448 'run --experimental_'
449}
450
451test_tristate_option() {
452 # 'Test tristate option completion'
453 assert_expansion 'build --nocache_test_result' \
454 'build --nocache_test_results '
455}
456
457make_dirs() {
458 mkdir -p video/streamer2/testing || fail "mkdir failed"
459 mkdir -p ${PACKAGE_PATH_PREFIX:-}video/streamer2/stuff || fail "mkdir failed"
460 mkdir -p video/streamer2/names || fail "mkdir failed"
461}
462
463
464test_directory_expansion() {
465 # 'Test expansion of directory names, even across package_path'
466
467 make_dirs
468
469 assert_expansion 'build vide' \
470 'build video/'
471 assert_expansion 'build video/' \
472 'build video/streamer2/'
473 assert_expansion 'build video/streamer2/t' \
474 'build video/streamer2/testing/'
475 assert_expansion 'build video/streamer2/s' \
476 'build video/streamer2/stuff/'
477
478 # Now add BUILD files; it should no longer expand the trailing slashes:
479 make_empty_packages
480
481 assert_expansion 'build video/streamer2/t' \
482 'build video/streamer2/testing'
483 assert_expansion 'build video/streamer2/s' \
484 'build video/streamer2/stuff'
485
486 # Use of absolute forms of labels:
487 assert_expansion 'build //vide' \
488 'build //video/'
489 assert_expansion 'build //video/' \
490 'build //video/streamer2/'
491 assert_expansion 'build //video/streamer2/t' \
492 'build //video/streamer2/testing'
493 assert_expansion 'build //video/streamer2/s' \
494 'build //video/streamer2/stuff'
495}
496
497test_directory_expansion_in_subdir() {
498 # 'Test expansion of directory names, when in a subdir of the workspace.'
499
500 make_dirs
501 cd video 2>/dev/null || exit
502
503 # Use of "video" while in "video" => no match:
504 assert_expansion 'build vide' \
505 'build vide'
506 assert_expansion 'build video/' \
507 'build video/'
508 assert_expansion 'build video/streamer2/t' \
509 'build video/streamer2/t'
510 assert_expansion 'build video/streamer2/s' \
511 'build video/streamer2/s'
512
513 # Use of "//video" while in "video" => matches absolute:
514 assert_expansion 'build //vide' \
515 'build //video/'
516 assert_expansion 'build //video/' \
517 'build //video/streamer2/'
518 assert_expansion 'build //video/streamer2/t' \
519 'build //video/streamer2/testing/'
520 assert_expansion 'build //video/streamer2/s' \
521 'build //video/streamer2/stuff/'
522
523 # Use of relative paths => matches
524 assert_expansion 'build streamer2/t' \
525 'build streamer2/testing/'
526 assert_expansion 'build streamer2/s' \
527 'build streamer2/stuff/'
528}
529
530test_target_expansion() {
531 # 'Test expansion of target names within packages'
532
533 make_packages
534
535 # TODO(bazel-team): (2009) it would be good to test that "streamer2\t"
536 # yielded a menu of "streamer2:" and "streamer2/", but testing the
537 # terminal output (as opposed to the result of expansion) is
538 # beyond our ability right now.
539
540 assert_expansion 'build video/streamer2:ta' \
541 'build video/streamer2:task_lib '
542
543 # Special characters
544 assert_expansion 'build video/streamer2:with_s' \
545 'build video/streamer2:with_special+_,=-.@~chars '
546
547 # Also, that 'bazel build' matches test and non-test rules (lack
548 # of trailing space after match => not unique match).
549 assert_expansion 'build video/streamer2:to' \
550 'build video/streamer2:token_bucket'
551
552 assert_expansion 'build video/streamer2/s' \
553 'build video/streamer2/stuff'
554
555 assert_expansion 'build video/streamer2/stuff:s' \
556 'build video/streamer2/stuff:stuff '
557
558 # Test that 'bazel build' does not match commented-out rules.
559 assert_expansion 'build video/streamer2:comment_build_target_1o' \
560 'build video/streamer2:comment_build_target_1o'
561
562 assert_expansion 'build video/streamer2:comment_build_target_2' \
563 'build video/streamer2:comment_build_target_2new '
564
565 # Test that 'bazel test' expands only test rules.
566 assert_expansion 'test video/streamer2:to' \
567 'test video/streamer2:token_bucket_test '
568
569 # Test that 'blaze test' does not match commented-out rules.
570 assert_expansion 'test video/streamer2:token_bucket_t_1o' \
571 'test video/streamer2:token_bucket_t_1o'
572
573 assert_expansion 'test video/streamer2:token_bucket_t' \
574 'test video/streamer2:token_bucket_test '
575
576 assert_expansion_error_not_contains 'test video/streamer2:match' \
577 'syntax error'
578
579 # Test that :all wildcard is expanded when there is more than one
580 # match.
581 #
582 # One match => no :all.
583 assert_expansion 'test video/streamer2:' \
584 'test video/streamer2:token_bucket_test '
585 # Multiple matches => :all.
586 assert_expansion 'build video/streamer2:a' \
587 'build video/streamer2:all '
588
589 # Test that 'bazel run' expands only non-test binary rules.
590 assert_expansion 'run video/streamer2:to' \
591 'run video/streamer2:token_bucket_binary '
592
593 # Test that 'bazel run' expands for binary and test rules, but not library
594 # with BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set.
595 assert_expansion 'run video/streamer2:to' \
596 'run video/streamer2:token_bucket_' \
597 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=true'
598
599 # Test the 'bazel run' expands for test rules, with
600 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN set.
601 assert_expansion 'run video/streamer2:token_bucket_t' \
602 'run video/streamer2:token_bucket_test ' \
603 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=1'
604
605 # Test that 'bazel run' expands only non-test binary rules when the
606 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is false.
607 assert_expansion 'run video/streamer2:to' \
608 'run video/streamer2:token_bucket_binary ' \
609 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=false'
610
611 # Test that 'bazel run' expands only non-test binary rules when the
612 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is false.
613 assert_expansion 'run video/streamer2:to' \
614 'run video/streamer2:token_bucket_binary ' \
615 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=0'
616
617 # Test that 'bazel run' expands only non-test binary rules when the
618 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is invalid.
619 assert_expansion 'run video/streamer2:to' \
620 'run video/streamer2:token_bucket_binary ' \
621 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN=junk'
622
623 # Test that 'bazel run' expands only non-test binary rules when the
624 # BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN is empty.
625 assert_expansion 'run video/streamer2:to' \
626 'run video/streamer2:token_bucket_binary ' \
627 'BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN='
628
629 # Test that 'bazel run' does not match commented-out rules.
630 assert_expansion 'run video/streamer2:comment_run_target_1o' \
631 'run video/streamer2:comment_run_target_1o'
632
633 assert_expansion 'run video/streamer2:comment_run_target_2' \
634 'run video/streamer2:comment_run_target_2new '
635
636 # Test that 'bazel run' expands binaries with spaces in the build rules
637 assert_expansion 'run video/streamer2:J' \
638 'run video/streamer2:JavaBinary '
639
640 # Test that 'bazel run' expands targets when the name attribute is not first
641 assert_expansion 'run video/streamer2:py' \
642 'run video/streamer2:pybin '
643
644 # Test that 'bazel run' expands binaries with newlines in the build rules
645 assert_expansion 'run video/streamer2:A' \
646 'run video/streamer2:AnotherJavaBinary '
647
648 # Test that the expansion of rules with 'name=...' strings isn't messed up.
649 assert_expansion 'build video/streamer2/names:' \
650 'build video/streamer2/names:foo '
651
652 # Test that dashes are matched even when locale isn't C.
653 LC_ALL=en_US.UTF-8 \
654 assert_expansion 'build dash:m' \
655 'build dash:mia-bid-multiplier-mixer-module '
656}
657
658test_target_expansion_in_subdir() {
659 # 'Test expansion of targets when in a subdir of the workspace.'
660
661 make_packages
662 cd video 2>/dev/null
663
664 # Relative labels:
665 assert_expansion 'build streamer2:ta' \
666 'build streamer2:task_lib '
667
668 assert_expansion 'build streamer2:to' \
669 'build streamer2:token_bucket'
670
671 assert_expansion 'build streamer2/s' \
672 'build streamer2/stuff'
673
674 assert_expansion 'build streamer2/stuff:s' \
675 'build streamer2/stuff:stuff '
676
677 # (no match)
678 assert_expansion 'build video/streamer2:ta' \
679 'build video/streamer2:ta'
680
681 # Absolute labels work as usual:
682 assert_expansion 'build //video/streamer2:ta' \
683 'build //video/streamer2:task_lib '
684}
685
686test_target_expansion_in_package() {
687 # 'Test expansion of targets when in a package.'
688
689 make_packages
690 cd video/streamer2 2>/dev/null
691
692 assert_expansion 'build :ta' \
693 'build :task_lib '
694
695 assert_expansion 'build :to' \
696 'build :token_bucket'
697
698 # allow slashes in rule names
699 assert_expansion 'build :checks/th' \
700 'build :checks/thingy '
701
702 assert_expansion 'build s' \
703 'build stuff'
704
705 # (no expansion)
706 assert_expansion 'build :s' \
707 'build :s'
708}
709
710test_help() {
711 # "Test that bazel help expands subcommand names"
712 assert_expansion 'help qu' \
713 'help query '
714 assert_expansion 'help bui' \
715 'help build '
716 assert_expansion 'help shut' \
717 'help shutdown '
718 assert_expansion 'help start' \
719 'help startup_options '
720}
721
722test_info() {
723 # "Test that bazel info keys are expanded"
724 assert_expansion 'info commi' \
725 'info committed-heap-size '
726 assert_expansion 'info i' \
727 'info install_base '
728 assert_expansion 'info --show_m' \
729 'info --show_make_env '
730}
731
732run_suite "Tests of bash completion of 'blaze' command."