Skip to content

Commit

Permalink
conn: use /usr/bin/env to run the shell
Browse files Browse the repository at this point in the history
To make the code more portable to distributions that don't have
bash in /usr/bin.

Resolves: next-actions#95

Link: <shadow-maint/shadow#1131>
Reported-by: Iker Pedrosa <ipedrosa@redhat.com>
Reported-by: Alejandro Colomar <alx@kernel.org>
Suggested-by: Alejandro Colomar <alx@kernel.org>
  • Loading branch information
pbrezina committed Nov 27, 2024
1 parent 33e6aaa commit 3e520f5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pytest_mh/conn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ class Bash(Shell):
"""

def __init__(self) -> None:
super().__init__("bash", "/usr/bin/bash -c")
super().__init__("bash", "/usr/bin/env bash -c")

def build_command_line(self, script: str, *, cwd: str | None, env: dict[str, Any]) -> str:
full_script = self._add_cwd_and_env(script, cwd=cwd, env=env)
Expand Down Expand Up @@ -1065,7 +1065,7 @@ def _add_cwd_and_env(self, script: str, *, cwd: str | None, env: dict[str, Any])

def _escape_single_quotes(self, command: str) -> str:
"""
We call the command as `bash -c '$command'`.
We call the command as `/usr/bin/env bash -c '$command'`.
We need to escape ' inside the script to make it work correctly.
"""
Expand Down
8 changes: 4 additions & 4 deletions tests/test_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_conn__shell_bash__script(input: str, expected: str):
shell = Bash()

assert shell.name == "bash"
assert shell.shell_command == "/usr/bin/bash -c"
assert shell.shell_command == "/usr/bin/env bash -c"

cmd = shell.build_command_line(input, cwd=None, env={})
assert cmd == f"{shell.shell_command} '{expected}'"
Expand All @@ -34,7 +34,7 @@ def test_conn__shell_bash__cwd():
shell = Bash()

assert shell.name == "bash"
assert shell.shell_command == "/usr/bin/bash -c"
assert shell.shell_command == "/usr/bin/env bash -c"

cmd = shell.build_command_line("echo hello world", cwd="/home/test", env={})
expected = textwrap.dedent(
Expand All @@ -51,7 +51,7 @@ def test_conn__shell_bash__env():
shell = Bash()

assert shell.name == "bash"
assert shell.shell_command == "/usr/bin/bash -c"
assert shell.shell_command == "/usr/bin/env bash -c"

cmd = shell.build_command_line("echo hello world", cwd=None, env={"HELLO": "WORLD", "JOHN": "DOE"})
expected = textwrap.dedent(
Expand All @@ -69,7 +69,7 @@ def test_conn__shell_bash__cwd_env():
shell = Bash()

assert shell.name == "bash"
assert shell.shell_command == "/usr/bin/bash -c"
assert shell.shell_command == "/usr/bin/env bash -c"

cmd = shell.build_command_line("echo hello world", cwd="/home/test", env={"HELLO": "WORLD", "JOHN": "DOE"})
expected = textwrap.dedent(
Expand Down

0 comments on commit 3e520f5

Please sign in to comment.