Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/86.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Respect the process-reported shell name when a login shell differs from ``$SHELL``.
21 changes: 13 additions & 8 deletions src/shellingham/posix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ def _iter_process_parents(pid, max_depth=10):


def _get_login_shell(proc_cmd):
"""Form shell information from SHELL environ if possible."""
"""Use SHELL's full path when it matches the process-reported name."""
login_shell = os.environ.get("SHELL", "")
if login_shell:
proc_cmd = login_shell
else:
proc_cmd = proc_cmd[1:]
return (os.path.basename(proc_cmd).lower(), proc_cmd)
if (
login_shell
and os.path.basename(login_shell).lower()
== os.path.basename(proc_cmd[1:]).lower()
):
return (os.path.basename(login_shell).lower(), login_shell)
return None


_INTERPRETER_SHELL_NAMES = [
Expand Down Expand Up @@ -85,8 +87,11 @@ def _get_interpreter_shell(proc_name, proc_args):


def _get_shell(cmd, *args):
if cmd.startswith("-"): # Login shell! Let's use this.
return _get_login_shell(cmd)
if cmd.startswith("-"): # Login-style command.
shell = _get_login_shell(cmd)
if shell:
return shell
cmd = cmd[1:]
name = os.path.basename(cmd).lower()
if name == "rosetta" or QEMU_BIN_REGEX.fullmatch(name):
# If the current process is Rosetta or QEMU, this likely is a
Expand Down
50 changes: 50 additions & 0 deletions tests/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,53 @@ def test_get_shell(mocker, environ, mapping, result):
environ.patch(SHELL="==MOCKED=LOGIN=SHELL==/bash")
mocker.patch.object(posix, "_iter_process_parents", return_value=mapping)
assert posix.get_shell(pid=77061) == result


@pytest.mark.parametrize('command, login_shell, result', [
('-fish', '/bin/zsh', ('fish', 'fish')),
('-/opt/homebrew/bin/fish', '/bin/zsh',
('fish', '/opt/homebrew/bin/fish')),
('-zsh', '/opt/homebrew/bin/fish', ('zsh', 'zsh')),
('-bash', '/usr/local/bin/bash', ('bash', '/usr/local/bin/bash')),
('-/bin/bash', '/usr/local/bin/bash', ('bash', '/usr/local/bin/bash')),
('-BASH', '/usr/local/bin/bash', ('bash', '/usr/local/bin/bash')),
('-bash', '/usr/local/bin/BASH', ('bash', '/usr/local/bin/BASH')),
('-fish', None, ('fish', 'fish')),
('-fish', '', ('fish', 'fish')),
('/opt/homebrew/bin/fish', '/bin/zsh',
('fish', '/opt/homebrew/bin/fish')),
('-not-a-shell', '/bin/zsh', None),
])
def test_login_shell_matches_process(monkeypatch, command, login_shell, result):
if login_shell is None:
monkeypatch.delenv('SHELL', raising=False)
else:
monkeypatch.setenv('SHELL', login_shell)
mapping = [
Process(args=('python',), pid='2', ppid='1'),
Process(args=(command,), pid='1', ppid='0'),
]
monkeypatch.setattr(posix, '_iter_process_parents', lambda *args: mapping)
assert posix.get_shell(pid=2) == result


def test_login_shell_mismatch_continues_search(monkeypatch):
monkeypatch.setenv('SHELL', '/bin/zsh')
mapping = [
Process(args=('python',), pid='3', ppid='2'),
Process(args=('-not-a-shell',), pid='2', ppid='1'),
Process(args=('/bin/bash',), pid='1', ppid='0'),
]
monkeypatch.setattr(posix, '_iter_process_parents', lambda *args: mapping)
assert posix.get_shell(pid=3) == ('bash', '/bin/bash')


def test_login_shell_mismatch_checks_interpreter(monkeypatch):
monkeypatch.setenv('SHELL', '/bin/zsh')
mapping = [
Process(args=('python',), pid='2', ppid='1'),
Process(args=('-python3', '/usr/local/bin/xonsh'), pid='1', ppid='0'),
]
monkeypatch.setattr(posix, '_iter_process_parents', lambda *args: mapping)
monkeypatch.setattr(posix.os.path, 'isfile', lambda path: True)
assert posix.get_shell(pid=2) == ('xonsh', '/usr/local/bin/xonsh')