Skip to content

Commit

Permalink
Fix and test global completion in zsh (#463)
Browse files Browse the repository at this point in the history
* Fix continuation chars in zsh

* Fix completion in zsh special contexts

* Remove pre-installed argcomplete on macos

* Re-enable skipped macOS tests
  • Loading branch information
evanunderscore authored Nov 27, 2023
1 parent 59baed5 commit cb5fc38
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
run: |
[[ $(uname) == Linux ]] && sudo apt-get install --yes rpm tcsh fish zsh
[[ $(uname) == Darwin ]] && brew install bash tcsh fish
# Some runners have python-argcomplete preinstalled
# as a dependency of pipx, which interferes with the tests.
[[ $(uname) == Darwin ]] && brew uninstall --ignore-dependencies python-argcomplete || true
python -m pip install --quiet --upgrade codecov
- run: make install
- run: make lint
Expand Down
22 changes: 14 additions & 8 deletions argcomplete/bash_completion.d/_python-argcomplete
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ _python_argcomplete_global() {
req_argv=( "" "${COMP_WORDS[@]:1}" )
__python_argcomplete_expand_tilde_by_ref executable
else
if [[ "$service" != "-default-" ]]; then
# TODO: this may not be sufficient - see https://zsh.sourceforge.io/Doc/Release/Completion-System.html
# May need to call _complete with avoid-completer=_python-argcomplete or something like that
_default
return
fi
executable="${words[1]}"
req_argv=( "${words[@]:1}" )
fi
Expand Down Expand Up @@ -208,7 +202,15 @@ _python_argcomplete_global() {
_ARGCOMPLETE_SHELL="zsh" \
_ARGCOMPLETE_SUPPRESS_SPACE=1 \
__python_argcomplete_run "$executable" "${(@)req_argv[1, ${ARGCOMPLETE}-1]}"))
_describe "$executable" completions
local nosort=()
local nospace=()
if is-at-least 5.8; then
nosort=(-o nosort)
fi
if [[ "${completions-}" =~ ([^\\]): && "${BASH_REMATCH[2]}" =~ [=/:] ]]; then
nospace=(-S '')
fi
_describe "$executable" completions "${nosort[@]}" "${nospace[@]}"
else
COMPREPLY=($(IFS="$IFS" \
COMP_LINE="$COMP_LINE" \
Expand All @@ -234,5 +236,9 @@ _python_argcomplete_global() {
if [[ -z "${ZSH_VERSION-}" ]]; then
complete -o default -o bashdefault -D -F _python_argcomplete_global
else
compdef _python_argcomplete_global -P '*'
autoload is-at-least
# Replace only the default completer (_default).
# There are many other special contexts we don't want to override.
# https://zsh.sourceforge.io/Doc/Release/Completion-System.html
compdef _python_argcomplete_global -default-
fi
11 changes: 10 additions & 1 deletion argcomplete/shell_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@
_ARGCOMPLETE_SHELL="zsh" \
_ARGCOMPLETE_SUPPRESS_SPACE=1 \
__python_argcomplete_run ${script:-${words[1]}}))
_describe "${words[1]}" completions -o nosort
local nosort=()
local nospace=()
if is-at-least 5.8; then
nosort=(-o nosort)
fi
if [[ "${completions-}" =~ ([^\\]): && "${match[1]}" =~ [=/:] ]]; then
nospace=(-S '')
fi
_describe "${words[1]}" completions "${nosort[@]}" "${nospace[@]}"
else
local SUPPRESS_SPACE=0
if compopt +o nospace 2> /dev/null; then
Expand All @@ -67,6 +75,7 @@
if [[ -z "${ZSH_VERSION-}" ]]; then
complete %(complete_opts)s -F _python_argcomplete%(function_suffix)s %(executables)s
else
autoload is-at-least
compdef _python_argcomplete%(function_suffix)s %(executables)s
fi
"""
Expand Down
35 changes: 21 additions & 14 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def bash_repl(command="bash"):
def zsh_repl(command="zsh"):
sh = _repl_sh(command, ["--no-rcs", "-V"], non_printable_insert="%(!..)")
sh.run_command("autoload compinit; compinit -u")
# Require two tabs to print all options (some tests rely on this).
sh.run_command("setopt BASH_AUTO_LIST")
return sh


Expand Down Expand Up @@ -1260,9 +1262,6 @@ def setUp(self):
path = ":".join([os.path.join(BASE_DIR, "scripts"), TEST_DIR, "$PATH"])
sh.run_command("export PATH={0}".format(path))
sh.run_command("export PYTHONPATH={0}".format(BASE_DIR))
if self.repl_provider == bash_repl:
# Disable the "python" module provided by bash-completion
sh.run_command("complete -r python python2 python3")
output = sh.run_command(self.install_cmd)
self.assertEqual(output, "")
# Register a dummy completion with an external argcomplete script
Expand Down Expand Up @@ -1317,18 +1316,24 @@ class TestZsh(TestBashZshBase, unittest.TestCase):
"test_parse_special_characters_dollar",
"test_comp_point", # FIXME
"test_completion_environment", # FIXME
"test_continuation", # FIXME
"test_wordbreak_chars", # FIXME
]

def repl_provider(self):
return zsh_repl()


@unittest.skipIf(BASH_MAJOR_VERSION < 4, "complete -D not supported")
class TestBashGlobal(TestBash):
class TestBashZshGlobalBase(TestBashZshBase):
install_cmd = 'eval "$(activate-global-python-argcomplete --dest=-)"'

def test_redirection_completion(self):
with TempDir(prefix="test_dir_py", dir="."):
self.sh.run_command("cd " + os.getcwd())
self.sh.run_command("echo failure > ./foo.txt")
self.sh.run_command("echo success > ./foo.\t")
with open("foo.txt") as f:
msg = f.read()
self.assertEqual(msg, "success\n")

def test_python_completion(self):
self.sh.run_command("cd " + TEST_DIR)
self.assertEqual(self.sh.run_command("python3 ./prog basic f\t"), "foo\r\n")
Expand Down Expand Up @@ -1368,9 +1373,6 @@ def _test_console_script(self, package=False, wheel=False):
command = "pip install {} --target .".format(test_package)
if not wheel:
command += " --no-binary :all:"
if sys.platform == "darwin":
# Work around https://stackoverflow.com/questions/24257803
command += ' --install-option="--prefix="'
install_output = self.sh.run_command(command)
self.assertEqual(self.sh.run_command("echo $?"), "0\r\n", install_output)
command = "test-module"
Expand All @@ -1379,27 +1381,32 @@ def _test_console_script(self, package=False, wheel=False):
command += " a\t"
self.assertEqual(self.sh.run_command(command), "arg\r\n")

@unittest.skipIf(os.uname()[0] == "Darwin", "Skip test that fails on MacOS")
def test_console_script_module(self):
"""Test completing a console_script for a module."""
self._test_console_script()

@unittest.skipIf(os.uname()[0] == "Darwin", "Skip test that fails on MacOS")
def test_console_script_package(self):
"""Test completing a console_script for a package."""
self._test_console_script(package=True)

@unittest.skipIf(os.uname()[0] == "Darwin", "Skip test that fails on MacOS")
def test_console_script_module_wheel(self):
"""Test completing a console_script for a module from a wheel."""
self._test_console_script(wheel=True)

@unittest.skipIf(os.uname()[0] == "Darwin", "Skip test that fails on MacOS")
def test_console_script_package_wheel(self):
"""Test completing a console_script for a package from a wheel."""
self._test_console_script(package=True, wheel=True)


@unittest.skipIf(BASH_MAJOR_VERSION < 4, "complete -D not supported")
class TestBashGlobal(TestBash, TestBashZshGlobalBase):
pass


class TestZshGlobal(TestZsh, TestBashZshGlobalBase):
pass


class Shell:
def __init__(self, shell):
self.child = pexpect.spawn(shell, encoding="utf-8")
Expand Down

0 comments on commit cb5fc38

Please sign in to comment.