Skip to content

activate.fish: update fish major version check #2891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions src/virtualenv/activation/fish/activate.fish
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function deactivate -d 'Exit virtualenv mode and return to the normal environmen
# reset old environment variables
if test -n "$_OLD_VIRTUAL_PATH"
# https://github.com/fish-shell/fish-shell/issues/436 altered PATH handling
if test (echo $FISH_VERSION | head -c 1) -lt 3
if test (string sub -s 1 -l 1 $FISH_VERSION) -lt 3
set -gx PATH (_fishify_path "$_OLD_VIRTUAL_PATH")
else
set -gx PATH $_OLD_VIRTUAL_PATH
Expand Down Expand Up @@ -61,7 +61,7 @@ deactivate nondestructive
set -gx VIRTUAL_ENV __VIRTUAL_ENV__

# https://github.com/fish-shell/fish-shell/issues/436 altered PATH handling
if test (echo $FISH_VERSION | head -c 1) -lt 3
if test (string sub -s 1 -l 1 $FISH_VERSION) -lt 3
set -gx _OLD_VIRTUAL_PATH (_bashify_path $PATH)
else
set -gx _OLD_VIRTUAL_PATH $PATH
Expand Down
53 changes: 53 additions & 0 deletions tests/unit/activation/test_fish.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

import os
import sys

import pytest

from virtualenv.activation import FishActivator
Expand All @@ -20,4 +23,54 @@ def __init__(self, session) -> None:
def print_prompt(self):
return "fish_prompt"

def _get_test_lines(self, activate_script):
return [
self.print_python_exe(),
self.print_os_env_var("VIRTUAL_ENV"),
self.print_os_env_var("VIRTUAL_ENV_PROMPT"),
self.print_os_env_var("PATH"),
self.activate_call(activate_script),
self.print_python_exe(),
self.print_os_env_var("VIRTUAL_ENV"),
self.print_os_env_var("VIRTUAL_ENV_PROMPT"),
self.print_os_env_var("PATH"),
self.print_prompt(),
# \\ loads documentation from the virtualenv site packages
self.pydoc_call,
self.deactivate,
self.print_python_exe(),
self.print_os_env_var("VIRTUAL_ENV"),
self.print_os_env_var("VIRTUAL_ENV_PROMPT"),
self.print_os_env_var("PATH"),
"", # just finish with an empty new line
]

def assert_output(self, out, raw, _):
# pre-activation
assert out[0], raw
assert out[1] == "None", raw
assert out[2] == "None", raw
# post-activation
expected = self._creator.exe.parent / os.path.basename(sys.executable)
assert self.norm_path(out[4]) == self.norm_path(expected), raw
assert self.norm_path(out[5]) == self.norm_path(self._creator.dest).replace("\\\\", "\\"), raw
assert out[6] == self._creator.env_name
# Some attempts to test the prompt output print more than 1 line.
# So we need to check if the prompt exists on any of them.
prompt_text = f"({self._creator.env_name}) "
assert any(prompt_text in line for line in out[7:-5]), raw

assert out[-5] == "wrote pydoc_test.html", raw
content = tmp_path / "pydoc_test.html"
assert content.exists(), raw
# post deactivation, same as before
assert out[-4] == out[0], raw
assert out[-3] == "None", raw
assert out[-2] == "None", raw

# Check that the PATH is restored
assert out[3] == out[13], raw
# Check that PATH changed after activation
assert out[3] != out[8], raw

activation_tester(Fish)