Skip to content

Commit b24305d

Browse files
authored
Merge pull request #1554 from akinomyoga/test-xspec
perf(_comp_compgen_filedir_xspec): avoid double negation `!(!(...))`
2 parents 940100c + c53259a commit b24305d

File tree

7 files changed

+41
-4
lines changed

7 files changed

+41
-4
lines changed

bash_completion

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,14 +3264,14 @@ _comp_compgen_filedir_xspec()
32643264
# https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00036.html
32653265
# news://news.gmane.io/4C940E1C.1010304@case.edu
32663266
eval xspec="${xspec}"
3267-
local matchop=!
32683267
if [[ $xspec == !* ]]; then
32693268
xspec=${xspec#!}
3270-
matchop=@
3269+
xspec="@(|!($xspec|${xspec^^}))"
3270+
else
3271+
xspec="@(|$xspec|${xspec^^})"
32713272
fi
3272-
xspec="$matchop($xspec|${xspec^^})"
32733273
3274-
_comp_compgen -av toks -c "$quoted" -- -f -X "@(|!($xspec))"
3274+
_comp_compgen -av toks -c "$quoted" -- -f -X "$xspec"
32753275
32763276
# Try without filter if it failed to produce anything and configured to
32773277
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-} && ${#toks[@]} -lt 1 ]] &&

test/fixtures/_filedir_xspec/a.txt

Whitespace-only changes.

test/fixtures/_filedir_xspec/b.TXT

Whitespace-only changes.

test/fixtures/_filedir_xspec/c.dat

Whitespace-only changes.

test/fixtures/_filedir_xspec/d.bin

Whitespace-only changes.

test/t/unit/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ EXTRA_DIST = \
55
test_unit_compgen_available_interfaces.py \
66
test_unit_compgen_commands.py \
77
test_unit_compgen_filedir.py \
8+
test_unit_compgen_filedir_xspec.py \
89
test_unit_compgen_ip_addresses.py \
910
test_unit_compgen_known_hosts.py \
1011
test_unit_compgen_pgids.py \
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
3+
from conftest import assert_bash_exec, assert_complete
4+
5+
6+
@pytest.mark.bashcomp(cmd=None, ignore_env=r"^\+COMPREPLY=|^[-+]_comp_xspecs=")
7+
class TestUnitFiledirXspec:
8+
@pytest.fixture(scope="class")
9+
def functions(self, request, bash):
10+
assert_bash_exec(
11+
bash,
12+
"_comp_xspecs[xspec1]='!*.txt'; "
13+
"_comp_xspecs[xspec2]=; "
14+
"_comp_xspecs[xspec4]='*.txt'; "
15+
"complete -F _filedir_xspec xspec{1..4}",
16+
)
17+
18+
def test_1(self, bash, functions):
19+
"""Test the pattern for an extension"""
20+
completion = assert_complete(bash, "xspec1 ", cwd="_filedir_xspec")
21+
assert completion == sorted("a.txt b.TXT".split())
22+
23+
def test_2(self, bash, functions):
24+
"""Test an empty _comp_xspecs entry"""
25+
completion = assert_complete(bash, "xspec2 ", cwd="_filedir_xspec")
26+
assert completion == sorted("a.txt b.TXT c.dat d.bin".split())
27+
28+
def test_3(self, bash, functions):
29+
"""Test an unset _comp_xspecs entry"""
30+
completion = assert_complete(bash, "xspec3 ", cwd="_filedir_xspec")
31+
assert completion == sorted("a.txt b.TXT c.dat d.bin".split())
32+
33+
def test_4(self, bash, functions):
34+
"""Test an exclusion pattern"""
35+
completion = assert_complete(bash, "xspec4 ", cwd="_filedir_xspec")
36+
assert completion == sorted("c.dat d.bin".split())

0 commit comments

Comments
 (0)