Skip to content

Commit 99333c1

Browse files
committed
contrib/completions: sync bash completion with current CLI
Sync bash completion with the current CLI surface, including missing and corrected flags, plus a regression test for the observed completion drift. Signed-off-by: immanuwell <pchpr.00@list.ru>
1 parent d0aeb9e commit 99333c1

2 files changed

Lines changed: 173 additions & 16 deletions

File tree

contrib/completions/bash/runc

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,32 @@ _runc_exec() {
157157
local boolean_options="
158158
--help
159159
--no-new-privs
160-
--tty, -t
161-
--detach, -d
160+
--tty
161+
-t
162+
--detach
163+
-d
164+
--ignore-paused
162165
"
163166

164167
local options_with_args="
165168
--console-socket
169+
--pidfd-socket
166170
--cwd
167-
--env, -e
168-
--user, -u
169-
--additional-gids, -g
170-
--process, -p
171+
--env
172+
-e
173+
--user
174+
-u
175+
--additional-gids
176+
-g
177+
--process
178+
-p
171179
--pid-file
172180
--process-label
173181
--apparmor
174-
--cap, -c
182+
--cap
183+
-c
175184
--preserve-fds
176-
--ignore-paused
185+
--cgroup
177186
"
178187

179188
local all_options="$options_with_args $boolean_options"
@@ -184,7 +193,7 @@ _runc_exec() {
184193
return
185194
;;
186195

187-
--console-socket | --cwd | --process | --apparmor)
196+
--console-socket | --pidfd-socket | --cwd | --process | --pid-file | --apparmor)
188197
case "$cur" in
189198
*:*) ;; # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
190199
'')
@@ -278,6 +287,7 @@ _runc_pause() {
278287
--help
279288
-h
280289
"
290+
local options_with_args=""
281291

282292
case "$cur" in
283293
-*)
@@ -295,9 +305,17 @@ _runc_ps() {
295305
-h
296306
"
297307
local options_with_args="
298-
--format, -f
308+
--format
309+
-f
299310
"
300311

312+
case "$prev" in
313+
--format | -f)
314+
COMPREPLY=($(compgen -W 'table json' -- "$cur"))
315+
return
316+
;;
317+
esac
318+
301319
case "$cur" in
302320
-*)
303321
COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
@@ -312,8 +330,10 @@ _runc_delete() {
312330
local boolean_options="
313331
--help
314332
-h
315-
--format, -f
333+
--force
334+
-f
316335
"
336+
local options_with_args=""
317337

318338
case "$cur" in
319339
-*)
@@ -332,6 +352,7 @@ _runc_kill() {
332352
--all
333353
-a
334354
"
355+
local options_with_args=""
335356

336357
case "$prev" in
337358
"kill")
@@ -393,7 +414,7 @@ _runc_list() {
393414

394415
case "$prev" in
395416
--format | -f)
396-
COMPREPLY=($(compgen -W 'text json' -- "$cur"))
417+
COMPREPLY=($(compgen -W 'table json' -- "$cur"))
397418
return
398419
;;
399420

@@ -456,8 +477,9 @@ _runc_spec() {
456477
_runc_run() {
457478
local boolean_options="
458479
--help
459-
--detatch
480+
--detach
460481
-d
482+
--keep
461483
--no-subreaper
462484
--no-pivot
463485
--no-new-keyring
@@ -467,12 +489,13 @@ _runc_run() {
467489
--bundle
468490
-b
469491
--console-socket
492+
--pidfd-socket
470493
--pid-file
471494
--preserve-fds
472495
"
473496

474497
case "$prev" in
475-
--bundle | -b | --console-socket | --pid-file)
498+
--bundle | -b | --console-socket | --pidfd-socket | --pid-file)
476499
case "$cur" in
477500
'')
478501
COMPREPLY=($(compgen -W '/' -- "$cur"))
@@ -575,11 +598,12 @@ _runc_create() {
575598
--bundle
576599
-b
577600
--console-socket
601+
--pidfd-socket
578602
--pid-file
579603
--preserve-fds
580604
"
581605
case "$prev" in
582-
--bundle | -b | --console-socket | --pid-file)
606+
--bundle | -b | --console-socket | --pidfd-socket | --pid-file)
583607
case "$cur" in
584608
'')
585609
COMPREPLY=($(compgen -W '/' -- "$cur"))
@@ -619,6 +643,7 @@ _runc_help() {
619643
_runc_restore() {
620644
local boolean_options="
621645
--help
646+
-h
622647
--tcp-established
623648
--ext-unix-sk
624649
--shell-job
@@ -634,11 +659,14 @@ _runc_restore() {
634659
local options_with_args="
635660
-b
636661
--bundle
662+
--console-socket
637663
--image-path
638664
--work-path
639665
--manage-cgroups-mode
640666
--pid-file
641667
--empty-ns
668+
--lsm-profile
669+
--lsm-mount-context
642670
"
643671

644672
local all_options="$options_with_args $boolean_options"
@@ -649,7 +677,7 @@ _runc_restore() {
649677
return
650678
;;
651679

652-
--pid-file | --image-path | --work-path | --bundle | -b)
680+
--pid-file | --console-socket | --image-path | --work-path | --bundle | -b)
653681
case "$cur" in
654682
*:*) ;; # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
655683
'')
@@ -679,11 +707,25 @@ _runc_restore() {
679707
esac
680708
}
681709

710+
_runc_features() {
711+
local boolean_options="
712+
--help
713+
-h
714+
"
715+
716+
case "$cur" in
717+
-*)
718+
COMPREPLY=($(compgen -W "$boolean_options" -- "$cur"))
719+
;;
720+
esac
721+
}
722+
682723
_runc_resume() {
683724
local boolean_options="
684725
--help
685726
-h
686727
"
728+
local options_with_args=""
687729

688730
case "$cur" in
689731
-*)
@@ -700,6 +742,7 @@ _runc_state() {
700742
--help
701743
-h
702744
"
745+
local options_with_args=""
703746

704747
case "$cur" in
705748
-*)
@@ -715,6 +758,7 @@ _runc_start() {
715758
--help
716759
-h
717760
"
761+
local options_with_args=""
718762

719763
case "$cur" in
720764
-*)
@@ -775,6 +819,7 @@ _runc() {
775819
delete
776820
events
777821
exec
822+
features
778823
kill
779824
list
780825
pause

tests/integration/completion.bats

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env bats
2+
3+
load helpers
4+
5+
function setup() {
6+
requires root
7+
}
8+
9+
function _get_comp_words_by_ref() {
10+
local OPTIND _opt
11+
while getopts "n:" _opt; do
12+
:
13+
done
14+
shift $((OPTIND - 1))
15+
16+
local cur_var=$1
17+
local prev_var=$2
18+
local words_var=$3
19+
local cword_var=$4
20+
21+
printf -v "$cur_var" '%s' "${COMP_WORDS[COMP_CWORD]}"
22+
if ((COMP_CWORD > 0)); then
23+
printf -v "$prev_var" '%s' "${COMP_WORDS[COMP_CWORD - 1]}"
24+
else
25+
printf -v "$prev_var" ''
26+
fi
27+
eval "$words_var=()"
28+
local i
29+
for ((i = 0; i < ${#COMP_WORDS[@]}; i++)); do
30+
eval "$words_var+=(\"\${COMP_WORDS[$i]}\")"
31+
done
32+
printf -v "$cword_var" '%s' "$COMP_CWORD"
33+
}
34+
35+
function _filedir() {
36+
:
37+
}
38+
39+
function compopt() {
40+
return 0
41+
}
42+
43+
function complete_runc() {
44+
# shellcheck disable=SC1091
45+
source "${INTEGRATION_ROOT}/../../contrib/completions/bash/runc"
46+
47+
COMP_WORDS=("$@")
48+
COMP_CWORD=$((${#COMP_WORDS[@]} - 1))
49+
COMPREPLY=()
50+
_runc
51+
52+
printf '%s\n' "${COMPREPLY[@]}"
53+
}
54+
55+
function contains_reply() {
56+
local needle=$1
57+
shift
58+
59+
local reply
60+
for reply in "$@"; do
61+
if [ "$reply" = "$needle" ]; then
62+
return 0
63+
fi
64+
done
65+
66+
return 1
67+
}
68+
69+
@test "top-level completion includes features" {
70+
mapfile -t replies < <(complete_runc runc fe)
71+
72+
contains_reply "features" "${replies[@]}"
73+
}
74+
75+
@test "delete completion suggests --force without a trailing comma" {
76+
mapfile -t replies < <(complete_runc runc delete --f)
77+
78+
contains_reply "--force" "${replies[@]}"
79+
run ! contains_reply "--force," "${replies[@]}"
80+
}
81+
82+
@test "run completion matches supported flags" {
83+
mapfile -t replies < <(complete_runc runc run --k)
84+
contains_reply "--keep" "${replies[@]}"
85+
86+
mapfile -t replies < <(complete_runc runc run --d)
87+
contains_reply "--detach" "${replies[@]}"
88+
run ! contains_reply "--detatch" "${replies[@]}"
89+
90+
mapfile -t replies < <(complete_runc runc run --pidf)
91+
contains_reply "--pidfd-socket" "${replies[@]}"
92+
}
93+
94+
@test "exec completion includes pidfd socket and plain long flags" {
95+
mapfile -t replies < <(complete_runc runc exec --pidf)
96+
contains_reply "--pidfd-socket" "${replies[@]}"
97+
98+
mapfile -t replies < <(complete_runc runc exec --t)
99+
contains_reply "--tty" "${replies[@]}"
100+
run ! contains_reply "--tty," "${replies[@]}"
101+
}
102+
103+
@test "format completions use table or json" {
104+
mapfile -t replies < <(complete_runc runc list --format "")
105+
contains_reply "table" "${replies[@]}"
106+
contains_reply "json" "${replies[@]}"
107+
run ! contains_reply "text" "${replies[@]}"
108+
109+
mapfile -t replies < <(complete_runc runc ps --format "")
110+
contains_reply "table" "${replies[@]}"
111+
contains_reply "json" "${replies[@]}"
112+
}

0 commit comments

Comments
 (0)