Skip to content

Commit

Permalink
Port the shell integration tests to use the run-shell kitten
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Jun 26, 2023
1 parent 8ffbfa1 commit 6c327f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
15 changes: 5 additions & 10 deletions kitty_tests/shell_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
from functools import lru_cache, partial

from kitty.bash import decode_ansi_c_quoted_string
from kitty.constants import kitty_base_dir, shell_integration_dir, terminfo_dir
from kitty.constants import kitten_exe, kitty_base_dir, shell_integration_dir, terminfo_dir
from kitty.fast_data_types import CURSOR_BEAM, CURSOR_BLOCK, CURSOR_UNDERLINE
from kitty.shell_integration import setup_bash_env, setup_fish_env, setup_zsh_env

from . import BaseTest

Expand Down Expand Up @@ -54,20 +53,17 @@ def safe_env_for_running_shell(argv, home_dir, rc='', shell='zsh'):
argv.insert(1, '--noglobalrcs')
with open(os.path.join(home_dir, '.zshrc'), 'w') as f:
print(rc + '\nZLE_RPROMPT_INDENT=0', file=f)
setup_zsh_env(ans, argv)
elif shell == 'fish':
conf_dir = os.path.join(home_dir, '.config', 'fish')
os.makedirs(conf_dir, exist_ok=True)
# Avoid generating unneeded completion scripts
os.makedirs(os.path.join(home_dir, '.local', 'share', 'fish', 'generated_completions'), exist_ok=True)
with open(os.path.join(conf_dir, 'config.fish'), 'w') as f:
print(rc + '\n', file=f)
setup_fish_env(ans, argv)
elif shell == 'bash':
setup_bash_env(ans, argv)
ans['KITTY_BASH_INJECT'] += ' posix'
ans['KITTY_BASH_POSIX_ENV'] = os.path.join(home_dir, '.bashrc')
with open(ans['KITTY_BASH_POSIX_ENV'], 'w') as f:
bashrc = os.path.join(home_dir, '.bashrc')
ans['KITTY_RUNNING_BASH_INTEGRATION_TEST'] = bashrc
with open(bashrc, 'w') as f:
# ensure LINES and COLUMNS are kept up to date
print('shopt -s checkwinsize', file=f)
if rc:
Expand All @@ -84,7 +80,7 @@ def run_shell(self, shell='zsh', rc='', cmd='', setup_env=None):
cmd = shlex.split(cmd.format(**locals()))
env = (setup_env or safe_env_for_running_shell)(cmd, home_dir, rc=rc, shell=shell)
try:
pty = self.create_pty(cmd, cwd=home_dir, env=env)
pty = self.create_pty([kitten_exe(), 'run-shell', '--shell', shlex.join(cmd)], cwd=home_dir, env=env)
i = 10
while i > 0 and not pty.screen_contents().strip():
pty.process_input_from_child()
Expand Down Expand Up @@ -327,7 +323,6 @@ def redrawn():

def setup_env(excluded, argv, home_dir, rc='', shell='bash'):
ans = basic_shell_env(home_dir)
setup_bash_env(ans, argv)
for x in {'profile', 'bash.bashrc', '.bash_profile', '.bash_login', '.profile', '.bashrc', 'rcfile'} - excluded:
with open(os.path.join(home_dir, x), 'w') as f:
if x == '.bashrc' and rc:
Expand Down
10 changes: 9 additions & 1 deletion tools/tui/shell_integration/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,22 @@ func bash_setup_func(shell_integration_dir string, argv []string, env map[string
sorted := remove_args.AsSlice()
slices.Sort(sorted)
for _, i := range utils.Reverse(sorted) {
slices.Delete(argv, i, i+1)
argv = slices.Delete(argv, i, i+1)
}
if env[`HISTFILE`] == "" && !inject.Has(`posix`) {
// In POSIX mode the default history file is ~/.sh_history instead of ~/.bash_history
env[`HISTFILE`] = utils.Expanduser(`~/.bash_history`)
env[`KITTY_BASH_UNEXPORT_HISTFILE`] = `1`
}
argv = slices.Insert(argv, 1, `--posix`)

if bashrc := os.Getenv(`KITTY_RUNNING_BASH_INTEGRATION_TEST`); bashrc != `` {
// prevent bash from source /etc/profile which is not under our control
os.Unsetenv(`KITTY_RUNNING_BASH_INTEGRATION_TEST`)
env[`KITTY_BASH_INJECT`] += ` posix`
env[`KITTY_BASH_POSIX_ENV`] = bashrc
}

return argv, env, nil
}

Expand Down

0 comments on commit 6c327f5

Please sign in to comment.