1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
import os
import shutil
import sys
import tempfile
from pathlib import Path
import pytest
from conftest import assert_bash_exec, assert_complete, bash_env_saved
@pytest.mark.bashcomp(cmd=None, ignore_env=r"^\+COMPREPLY=")
class TestUnitCompgenFiledir:
@pytest.fixture(scope="class")
def functions(self, request, bash):
assert_bash_exec(
bash,
"_f() { local cur;_comp_get_words cur; unset -v COMPREPLY; _comp_compgen_filedir; }; "
"complete -F _f f; "
"complete -F _f -o filenames f2",
)
assert_bash_exec(
bash,
"_g() { local cur;_comp_get_words cur; unset -v COMPREPLY; _comp_compgen_filedir e1; }; "
"complete -F _g g",
)
assert_bash_exec(
bash,
"_fd() { local cur;_comp_get_words cur; unset -v COMPREPLY; _comp_compgen_filedir -d; };"
"complete -F _fd fd",
)
@pytest.fixture(scope="class")
def non_windows_testdir(self, request, bash):
if sys.platform.startswith("win"):
pytest.skip("Filenames not allowed on Windows")
tempdir = Path(tempfile.mkdtemp(prefix="bash-completion_filedir"))
request.addfinalizer(lambda: shutil.rmtree(str(tempdir)))
subdir = tempdir / 'a"b'
subdir.mkdir()
(subdir / "d").touch()
subdir = tempdir / "a*b"
subdir.mkdir()
(subdir / "j").touch()
subdir = tempdir / r"a\b"
subdir.mkdir()
(subdir / "g").touch()
return tempdir
@pytest.fixture(scope="class")
def utf8_ctype(self, bash):
# TODO: this likely is not the right thing to do. Instead we should
# grab the setting from the running shell, possibly eval $(locale)
# in a subshell and grab LC_CTYPE from there. That doesn't seem to work
# either everywhere though.
lc_ctype = os.environ.get("LC_CTYPE", "")
if "UTF-8" not in lc_ctype:
pytest.skip("Applicable only in LC_CTYPE=UTF-8 setups")
return lc_ctype
def test_1(self, bash):
with bash_env_saved(bash) as bash_env:
bash_env.write_variable("cur", "")
assert_bash_exec(bash, 'cur="" _comp_compgen_filedir >/dev/null')
@pytest.mark.parametrize("funcname", "f f2".split())
def test_2(self, bash, functions, funcname):
completion = assert_complete(bash, "%s ab/" % funcname, cwd="_filedir")
assert completion == "e"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_3(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s a\ b/" % funcname, cwd="_filedir"
)
assert completion == "i"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_4(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s a\'b/" % funcname, cwd="_filedir"
)
assert completion == "c"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_5(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s a\&b/" % funcname, cwd="_filedir"
)
assert completion == "f"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_6(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s a\$" % funcname, cwd="_filedir"
)
assert completion == "b/"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_7(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s 'ab/" % funcname, cwd="_filedir"
)
assert completion == "e'"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_8(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s 'a b/" % funcname, cwd="_filedir"
)
assert completion == "i'"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_9(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s 'a$b/" % funcname, cwd="_filedir"
)
assert completion == "h'"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_10(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s 'a&b/" % funcname, cwd="_filedir"
)
assert completion == "f'"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_11(self, bash, functions, funcname):
completion = assert_complete(
bash, r'%s "ab/' % funcname, cwd="_filedir"
)
assert completion == 'e"'
@pytest.mark.parametrize("funcname", "f f2".split())
def test_12(self, bash, functions, funcname):
completion = assert_complete(
bash, r'%s "a b/' % funcname, cwd="_filedir"
)
assert completion == 'i"'
@pytest.mark.parametrize("funcname", "f f2".split())
def test_13(self, bash, functions, funcname):
completion = assert_complete(
bash, "%s \"a'b/" % funcname, cwd="_filedir"
)
assert completion == 'c"'
@pytest.mark.parametrize("funcname", "f f2".split())
def test_14(self, bash, functions, funcname):
completion = assert_complete(
bash, '%s "a&b/' % funcname, cwd="_filedir"
)
assert completion == 'f"'
@pytest.mark.complete(r"fd a\ ", cwd="_filedir")
def test_15(self, functions, completion):
assert completion == "b/"
@pytest.mark.complete("g ", cwd="_filedir/ext")
def test_16(self, functions, completion):
assert completion == sorted("ee.e1 foo/ gg.e1 ii.E1".split())
@pytest.mark.parametrize("funcname", "f f2".split())
def test_17(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s a\$b/" % funcname, cwd="_filedir"
)
assert completion == "h"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_18(self, bash, functions, funcname):
completion = assert_complete(
bash, r"%s \[x" % funcname, cwd="_filedir/brackets"
)
assert completion == r"\]"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_19(self, bash, functions, funcname, non_windows_testdir):
completion = assert_complete(
bash, '%s a\\"b/' % funcname, cwd=non_windows_testdir
)
assert completion == "d"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_20(self, bash, functions, funcname, non_windows_testdir):
completion = assert_complete(
bash, r"%s a\\b/" % funcname, cwd=non_windows_testdir
)
assert completion == "g"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_21(self, bash, functions, funcname, non_windows_testdir):
completion = assert_complete(
bash, "%s 'a\"b/" % funcname, cwd=non_windows_testdir
)
assert completion == "d'"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_22(self, bash, functions, funcname, non_windows_testdir):
completion = assert_complete(
bash, rf"{funcname} '{non_windows_testdir}/a\b/"
)
assert completion == "g'"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_23(self, bash, functions, funcname, non_windows_testdir):
completion = assert_complete(
bash, r'%s "a\"b/' % funcname, cwd=non_windows_testdir
)
assert completion == 'd"'
@pytest.mark.parametrize("funcname", "f f2".split())
def test_24(self, bash, functions, funcname, non_windows_testdir):
completion = assert_complete(
bash, r'%s "a\\b/' % funcname, cwd=non_windows_testdir
)
assert completion == 'g"'
@pytest.mark.parametrize("funcname", "f f2".split())
def test_25(self, bash, functions, funcname):
completion = assert_complete(
bash, r'%s "a\b/' % funcname, cwd="_filedir"
)
assert completion == '\b\b\bb/e"'
@pytest.mark.parametrize("funcname", "f f2".split())
def test_26(self, bash, functions, funcname):
completion = assert_complete(
bash, r'%s "a\$b/' % funcname, cwd="_filedir"
)
assert completion == 'h"'
@pytest.mark.xfail(reason="TODO: non-ASCII issues with test suite?")
@pytest.mark.parametrize("funcname", "f f2".split())
def test_27(self, bash, functions, funcname, utf8_ctype):
completion = assert_complete(bash, "%s aƩ/" % funcname, cwd="_filedir")
assert completion == "g"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_28_dot_1(self, bash, functions, funcname):
"""Exclude . and .. when the completion is attempted for '.[TAB]'"""
completion = assert_complete(bash, r"%s ." % funcname, cwd="_filedir")
assert completion == [".dotfile1", ".dotfile2"]
@pytest.mark.parametrize("funcname", "f f2".split())
def test_28_dot_2(self, bash, functions, funcname):
"""Exclude . and .. when the completion is attempted for 'dir/.[TAB]'"""
completion = assert_complete(bash, r"%s _filedir/." % funcname)
assert completion == [".dotfile1", ".dotfile2"]
@pytest.mark.parametrize("funcname", "f f2".split())
def test_28_dot_3(self, bash, functions, funcname):
"""Include . when the completion is attempted for '..[TAB]'"""
completion = assert_complete(bash, r"%s .." % funcname, cwd="_filedir")
assert completion == "/"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_28_dot_4(self, bash, functions, funcname):
"""Include . when the completion is attempted for '..[TAB]'"""
completion = assert_complete(bash, r"%s _filedir/.." % funcname)
assert completion == "/"
@pytest.mark.parametrize("funcname", "f f2".split())
def test_29_dotdot(self, bash, functions, funcname):
"""Complete files starting with "..".
These types of files are used by the go kubernetes atomic writer [0],
and presumably other types of systems, and we want to make sure they
will be completed correctly.
[0] https://pkg.go.dev/k8s.io/kubernetes/pkg/volume/util#AtomicWriter.Write
"""
completion = assert_complete(
bash, r"%s .." % funcname, cwd="_filedir/dotdot/"
)
assert completion == [
"../",
"..2016_02_01_15_04_05.123456",
"..data",
"..folder/",
]
|