Files
commandkit/internal/cobra/testdata/completion_terminal.py

46 lines
1.6 KiB
Python
Raw Normal View History

2026-11-03 03:45:47 +00:00
#!/usr/bin/env python3
"""Exercise one generated completion through an interactive shell."""
import os
import shlex
import shutil
import sys
import pexpect
shell, script, line = sys.argv[1:]
executable = shutil.which(shell)
if executable is None:
raise SystemExit(f"{shell} is not installed")
quoted_script = shlex.quote(script)
if shell == "bash":
args = ["--noprofile", "--norc", "-i"]
setup = 'PS1="PROMPT> "; source /usr/share/bash-completion/bash_completion; source ' + quoted_script
elif shell == "zsh":
args = ["-f"]
setup = 'PROMPT="PROMPT> "; '
if os.environ.get("COMPLETION_ZSH_MODULE_PATH"):
setup += "module_path=(" + shlex.quote(os.environ["COMPLETION_ZSH_MODULE_PATH"]) + "); "
if os.environ.get("COMPLETION_ZSH_FUNCTION_ROOT"):
setup += "fpath=(" + shlex.quote(os.environ["COMPLETION_ZSH_FUNCTION_ROOT"]) + "/**/*(N-/) $fpath); "
setup += "autoload -Uz compinit; compinit -D; source " + quoted_script
elif shell == "fish":
args = ["--no-config", "--interactive"]
setup = "function fish_prompt; printf '\\120\\122\\117\\115\\120\\124\\076\\040'; end; source " + quoted_script
else:
raise SystemExit(f"unsupported shell: {shell}")
child = pexpect.spawn(executable, args, env=os.environ.copy(), cwd=os.environ["PROBE_DIR"],
encoding="utf-8", timeout=15, dimensions=(24, 120))
child.delaybeforesend = 0.05
try:
child.setecho(False)
child.sendline(setup)
child.expect_exact("PROMPT> ")
child.send(line)
child.send("\t")
child.sendline("")
child.expect_exact("PROMPT> ")
finally:
child.close(force=True)