From 2c3c80cff23a78780188dc6cc32133dd3179d085 Mon Sep 17 00:00:00 2001 From: Maciej Urbanski Date: Thu, 28 Mar 2024 11:53:40 +0100 Subject: [PATCH] run fish under pty --- b2/_internal/_cli/autocomplete_install.py | 29 +++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/b2/_internal/_cli/autocomplete_install.py b/b2/_internal/_cli/autocomplete_install.py index 387691a8..175d4945 100644 --- a/b2/_internal/_cli/autocomplete_install.py +++ b/b2/_internal/_cli/autocomplete_install.py @@ -171,19 +171,12 @@ def is_enabled(self) -> bool: if not rc_path.exists(): # if zshrc is missing `zshrc -i` may hang on creation wizard when emulating tty rc_path.touch(mode=0o750) - emulate_tty = os.isatty(0) # is True under GHA or pytest-xdist - if emulate_tty and not find_spec('pexpect'): - emulate_tty = False - logger.warning( - "pexpect is recommended for Zsh shell autocomplete installation check without tty. " - "You can install it via `pip install pexpect`." - ) _silent_success_run_with_pty( [self.shell_exec, '-c', 'autoload -Uz compaudit; echo AUDIT; compaudit'] ) cmd = [self.shell_exec, '-i', '-c', f'[[ -v _comps[{quote(self.prog)}] ]]'] - return _silent_success_run(cmd) if emulate_tty else _silent_success_run_with_pty(cmd) + return _silent_success_run_with_tty(cmd) @SHELL_REGISTRY.register('fish') @@ -208,7 +201,7 @@ def is_enabled(self) -> bool: """ environ = os.environ.copy() environ.setdefault("TERM", "xterm") # TERM has to be set for fish to load completions - return _silent_success_run( + return _silent_success_run_with_tty( [ self.shell_exec, '-i', '-c', f'string length -q -- (complete -C{quote(f"{self.prog} ")} >/dev/null && complete -c {quote(self.prog)})' @@ -217,6 +210,20 @@ def is_enabled(self) -> bool: ) +def _silent_success_run_with_tty( + cmd: list[str], timeout: int = 30, env: dict | None = None +) -> bool: + emulate_tty = not os.isatty(0) # is True under GHA or pytest-xdist + if emulate_tty and not find_spec('pexpect'): + emulate_tty = False + logger.warning( + "pexpect is needed to check autocomplete installation correctness without tty. " + "You can install it via `pip install pexpect`." + ) + run_func = _silent_success_run_with_pty if emulate_tty else _silent_success_run + return run_func(cmd, timeout=timeout, env=env) + + def _silent_success_run(cmd: list[str], timeout: int = 30, env: dict | None = None) -> bool: p = subprocess.Popen( cmd, @@ -241,7 +248,9 @@ def _silent_success_run(cmd: list[str], timeout: int = 30, env: dict | None = No return p.returncode == 0 -def _silent_success_run_with_pty(cmd: list[str], timeout: int = 30, env: dict | None = None) -> bool: +def _silent_success_run_with_pty( + cmd: list[str], timeout: int = 30, env: dict | None = None +) -> bool: """ Run a command with emulated terminal and return whether it succeeded. """