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 da31d21
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
31 changes: 22 additions & 9 deletions kitty_tests/shell_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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

Expand Down Expand Up @@ -48,7 +48,7 @@ def basic_shell_env(home_dir):
return ans


def safe_env_for_running_shell(argv, home_dir, rc='', shell='zsh'):
def safe_env_for_running_shell(argv, home_dir, rc='', shell='zsh', with_kitten=False):
ans = basic_shell_env(home_dir)
if shell == 'zsh':
argv.insert(1, '--noglobalrcs')
Expand All @@ -64,10 +64,14 @@ def safe_env_for_running_shell(argv, home_dir, rc='', shell='zsh'):
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')
if with_kitten:
ans['KITTY_RUNNING_BASH_INTEGRATION_TEST'] = bashrc
else:
setup_bash_env(ans, argv)
ans['KITTY_BASH_INJECT'] += ' posix'
ans['KITTY_BASH_POSIX_ENV'] = 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 @@ -77,13 +81,17 @@ def safe_env_for_running_shell(argv, home_dir, rc='', shell='zsh'):

class ShellIntegration(BaseTest):

with_kitten = False

@contextmanager
def run_shell(self, shell='zsh', rc='', cmd='', setup_env=None):
home_dir = self.home_dir = os.path.realpath(tempfile.mkdtemp())
cmd = cmd or shell
cmd = shlex.split(cmd.format(**locals()))
env = (setup_env or safe_env_for_running_shell)(cmd, home_dir, rc=rc, shell=shell)
env = (setup_env or safe_env_for_running_shell)(cmd, home_dir, rc=rc, shell=shell, with_kitten=self.with_kitten)
try:
if self.with_kitten:
cmd = [kitten_exe(), 'run-shell', '--shell', shlex.join(cmd)]
pty = self.create_pty(cmd, cwd=home_dir, env=env)
i = 10
while i > 0 and not pty.screen_contents().strip():
Expand Down Expand Up @@ -325,9 +333,10 @@ def redrawn():

# test startup file sourcing

def setup_env(excluded, argv, home_dir, rc='', shell='bash'):
def setup_env(excluded, argv, home_dir, rc='', shell='bash', with_kitten=self.with_kitten):
ans = basic_shell_env(home_dir)
setup_bash_env(ans, argv)
if not with_kitten:
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 Expand Up @@ -381,3 +390,7 @@ def run_test(argv, *expected, excluded=(), rc='', wait_string='PROMPT $', assert
}.items():
q = q + "'"
self.ae(decode_ansi_c_quoted_string(q, 0)[0], e, f'Failed to decode: {q!r}')


class ShellIntegrationWithKitten(ShellIntegration):
with_kitten = True
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 da31d21

Please sign in to comment.