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