Skip to content

Commit

Permalink
run fish under pty
Browse files Browse the repository at this point in the history
  • Loading branch information
mjurbanski-reef committed Mar 28, 2024
1 parent 1012312 commit 2c3c80c
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions b2/_internal/_cli/autocomplete_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)})'
Expand All @@ -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,
Expand All @@ -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.
"""
Expand Down

0 comments on commit 2c3c80c

Please sign in to comment.