Skip to content

Commit 6b9a33e

Browse files
authored
Merge pull request #1027 from scop/feat/dict-nounset
fix(dict): nounset error when host/port not set
2 parents 397a49d + 81affbd commit 6b9a33e

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

bash_completion

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,9 @@ _comp_get_words()
925925
upargs+=(-v "$vprev" "${words[cword - 1]}")
926926
}
927927
[[ $vwords ]] && {
928+
# Note: bash < 4.4 has a bug that all the elements are connected with
929+
# ${v+"$@"} when IFS does not contain whitespace.
930+
local IFS=$' \t\n'
928931
upvars+=("$vwords")
929932
upargs+=(-a"${#words[@]}" "$vwords" ${words+"${words[@]}"})
930933
}
@@ -1073,7 +1076,7 @@ _comp_compgen_filedir()
10731076
fi
10741077

10751078
# Note: bash < 4.4 has a bug that all the elements are connected with
1076-
# ${v-"${a[@]}"} when IFS does not contain whitespace.
1079+
# ${v+"${a[@]}"} when IFS does not contain whitespace.
10771080
local IFS=$' \t\n'
10781081
local -a _tmp=(${toks[@]+"${toks[@]}"})
10791082
_comp_unlocal toks

completions/dict

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# TODO:API: rename per conventions, rework to use vars rather than outputting
44
_dictdata()
55
{
6-
# shellcheck disable=SC2086
7-
dict $host $port $1 2>/dev/null | command sed -ne \
6+
dict "$@" 2>/dev/null | command sed -ne \
87
's/^[[:blank:]]\{1,\}\([^[:blank:]]*\).*$/\1/p'
98
}
109

@@ -13,23 +12,24 @@ _comp_cmd_dict()
1312
local cur prev words cword comp_args
1413
_comp_initialize -- "$@" || return
1514

16-
local host port db i
15+
local -a dict_options=()
16+
local host="" port="" db i
1717

1818
local noargopts='!(-*|*[hpdis]*)'
1919
for ((i = 1; i < cword; i++)); do
2020
# shellcheck disable=SC2254
2121
case ${words[i]} in
2222
--host | -${noargopts}h)
2323
host=${words[++i]}
24-
[[ $host ]] && host="-h $host"
24+
[[ $host ]] && dict_options+=(-h "$host")
2525
;;
2626
--port | -${noargopts}p)
2727
port=${words[++i]}
28-
[[ $port ]] && port="-p $port"
28+
[[ $port ]] && dict_options+=(-p "$port")
2929
;;
3030
--database | -${noargopts}d)
3131
db=${words[++i]}
32-
[[ $db ]] && host="-d $db"
32+
[[ $db ]] && dict_options+=(-d "$db")
3333
;;
3434
esac
3535
done
@@ -42,11 +42,11 @@ _comp_cmd_dict()
4242
# shellcheck disable=SC2254
4343
case $prev in
4444
--database | -info | -${noargopts}[di])
45-
_comp_compgen_split -- "$(_dictdata -D)"
45+
_comp_compgen_split -- "$(_dictdata ${dict_options[@]+"${dict_options[@]}"} -D)"
4646
return
4747
;;
4848
--strategy | -${noargopts}s)
49-
_comp_compgen_split -- "$(_dictdata -S)"
49+
_comp_compgen_split -- "$(_dictdata ${dict_options[@]+"${dict_options[@]}"} -S)"
5050
return
5151
;;
5252
esac

test/t/test_dict.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1+
import os
2+
13
import pytest
24

35

46
class TestDict:
57
@pytest.mark.complete("dict -", require_cmd=True)
68
def test_1(self, completion):
79
assert completion
10+
11+
@pytest.mark.xfail(
12+
os.environ.get("NETWORK") == "none",
13+
reason="The database list is unavailable without network",
14+
)
15+
@pytest.mark.complete("dict --database ", require_cmd=True)
16+
def test_database(self, completion):
17+
# Ensure the directory name "__load_completion/" not generated because
18+
# filenames in the current dictory (i.e., test/fixtures) are generated
19+
# by "-o default" when "_comp_cmd_dict" fails to generate any
20+
# completions.
21+
assert completion and "__load_completion/" not in completion
22+
23+
@pytest.mark.xfail(
24+
os.environ.get("NETWORK") == "none",
25+
reason="The database list is unavailable without network",
26+
)
27+
@pytest.mark.complete(
28+
"dict -h dict.org --database ", require_cmd=True, env=dict(IFS="")
29+
)
30+
def test_database_IFS(self, completion):
31+
assert completion and "__load_completion/" not in completion

0 commit comments

Comments
 (0)