Skip to content

Commit

Permalink
Fix bad return code in bash activation if hashing is disabled
Browse files Browse the repository at this point in the history
If hashing is disabled in bash, the `hash` command will return a nonzero
return code. Since it is the last command in the script that return code
will also be the "return code" of the source command, so if anyone uses

source activate.sh || die horribly

having hashing disabled will ruin their day.

Fix this by overriding the return code of `hash` if it's bad.
Add tests to verify a good return code and try with and without hashing.
  • Loading branch information
fenkes-ibm committed May 13, 2024
1 parent 0a3816e commit ada5bd7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog/2717.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bad return code from activate.sh if hashing is disabled - by :user:'fenkes-ibm'.
2 changes: 1 addition & 1 deletion src/virtualenv/activation/bash/activate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ pydoc () {
# The hash command must be called to get it to forget past
# commands. Without forgetting past commands the $PATH changes
# we made may not be respected
hash -r 2>/dev/null
hash -r 2>/dev/null || true
1 change: 1 addition & 0 deletions tests/unit/activation/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __call__(self, monkeypatch, tmp_path):
process = Popen(invoke, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
raw_, _ = process.communicate()
raw = raw_.decode()
assert process.returncode == 0, raw
except subprocess.CalledProcessError as exception:
output = exception.output + exception.stderr
assert not exception.returncode, output # noqa: PT017
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/activation/test_bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash")
def test_bash(raise_on_non_source_class, activation_tester):
@pytest.mark.parametrize("hashing_enabled", [True, False])
def test_bash(raise_on_non_source_class, hashing_enabled, activation_tester):
class Bash(raise_on_non_source_class):
def __init__(self, session) -> None:
super().__init__(
Expand All @@ -18,6 +19,11 @@ def __init__(self, session) -> None:
"sh",
"You must source this script: $ source ",
)
self.deactivate += " || exit 1"
self._invoke_script.append("-h" if hashing_enabled else "+h")

def activate_call(self, script):
return super().activate_call(script) + " || exit 1"

def print_prompt(self):
return self.print_os_env_var("PS1")
Expand Down

0 comments on commit ada5bd7

Please sign in to comment.