Skip to content
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

?? prompt should be wrapped in paranthesis ?? #2224

Merged
merged 24 commits into from Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog/2224.feature.rst
@@ -0,0 +1,3 @@
The activated virtualenv prompt is now always wrapped in parentheses. This
affects venvs created with the ``--prompt`` attribute, and matches virtualenv's
behaviour on par with venv.
2 changes: 1 addition & 1 deletion src/virtualenv/activation/bash/activate.sh
Expand Up @@ -63,7 +63,7 @@ fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1-}"
if [ "x__VIRTUAL_PROMPT__" != x ] ; then
PS1="__VIRTUAL_PROMPT__${PS1-}"
PS1="(__VIRTUAL_PROMPT__) ${PS1-}"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
fi
Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/batch/activate.bat
Expand Up @@ -14,7 +14,7 @@ if defined _OLD_VIRTUAL_PROMPT (
)
if not defined VIRTUAL_ENV_DISABLE_PROMPT (
if "__VIRTUAL_PROMPT__" NEQ "" (
set "PROMPT=__VIRTUAL_PROMPT__%PROMPT%"
set "PROMPT=(__VIRTUAL_PROMPT__) %PROMPT%"
) else (
for %%d in ("%VIRTUAL_ENV%") do set "PROMPT=(%%~nxd) %PROMPT%"
)
Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/cshell/activate.csh
Expand Up @@ -18,7 +18,7 @@ setenv PATH "$VIRTUAL_ENV:q/__BIN_NAME__:$PATH:q"


if ('__VIRTUAL_PROMPT__' != "") then
set env_name = '__VIRTUAL_PROMPT__'
set env_name = '(__VIRTUAL_PROMPT__) '
else
set env_name = '('"$VIRTUAL_ENV:t:q"') '
endif
Expand Down
4 changes: 2 additions & 2 deletions src/virtualenv/activation/fish/activate.fish
Expand Up @@ -88,9 +88,9 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
# Prompt override provided?
# If not, just prepend the environment name.
if test -n '__VIRTUAL_PROMPT__'
printf '%s%s' '__VIRTUAL_PROMPT__' (set_color normal)
printf '(%s) ' '__VIRTUAL_PROMPT__'
else
printf '%s(%s) ' (set_color normal) (basename "$VIRTUAL_ENV")
printf '(%s) ' (basename "$VIRTUAL_ENV")
end

string join -- \n $prompt # handle multi-line prompts
Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/nushell/activate.nu
Expand Up @@ -21,7 +21,7 @@ load-env $new-env

# Creating the new prompt for the session
let virtual_prompt = (if ("__VIRTUAL_PROMPT__" != "") {
"__VIRTUAL_PROMPT__"
"(__VIRTUAL_PROMPT__) "
} {
(build-string '(' ($virtual-env | path basename) ') ')
}
Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/powershell/activate.ps1
Expand Up @@ -46,7 +46,7 @@ if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
function global:prompt {
# Add the custom prefix to the existing prompt
$previous_prompt_value = & $function:_old_virtual_prompt
("__VIRTUAL_PROMPT__" + $previous_prompt_value)
("(__VIRTUAL_PROMPT__) " + $previous_prompt_value)
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Expand Up @@ -301,7 +301,7 @@ def is_inside_ci():
def special_char_name():
base = "e-$ èрт🚒♞中片-j"
# workaround for pypy3 https://bitbucket.org/pypy/pypy/issues/3147/venv-non-ascii-support-windows
encoding = "ascii" if IS_PYPY and IS_WIN else sys.getfilesystemencoding()
encoding = "ascii" if IS_WIN else sys.getfilesystemencoding()
# let's not include characters that the file system cannot encode)
result = ""
for char in base:
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/activation/conftest.py
Expand Up @@ -126,6 +126,7 @@ def _get_test_lines(self, activate_script):
self.activate_call(activate_script),
self.print_python_exe(),
self.print_os_env_var("VIRTUAL_ENV"),
self.print_prompt(),
# \\ loads documentation from the virtualenv site packages
self.pydoc_call,
self.deactivate,
Expand All @@ -143,7 +144,12 @@ def assert_output(self, out, raw, tmp_path):
expected = self._creator.exe.parent / os.path.basename(sys.executable)
assert self.norm_path(out[2]) == self.norm_path(expected), raw
assert self.norm_path(out[3]) == self.norm_path(self._creator.dest).replace("\\\\", "\\"), raw
assert out[4] == "wrote pydoc_test.html", raw
# 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 = "({}) ".format(self._creator.env_name)
assert any(prompt_text in line for line in out[4:-3]), raw

assert out[-3] == "wrote pydoc_test.html", raw
content = tmp_path / "pydoc_test.html"
assert content.exists(), raw
# post deactivation, same as before
Expand Down Expand Up @@ -172,6 +178,9 @@ def print_os_env_var(self, var):
),
)

def print_prompt(self):
return NotImplemented

def activate_call(self, script):
cmd = self.quote(ensure_text(str(self.activate_cmd)))
scr = self.quote(ensure_text(str(script)))
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/activation/test_bash.py
Expand Up @@ -19,4 +19,7 @@ def __init__(self, session):
"You must source this script: $ source ",
)

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

activation_tester(Bash)
3 changes: 3 additions & 0 deletions tests/unit/activation/test_batch.py
Expand Up @@ -27,4 +27,7 @@ def quote(self, s):
"""double quotes needs to be single, and single need to be double"""
return "".join(("'" if c == '"' else ('"' if c == "'" else c)) for c in pipes.quote(s))

def print_prompt(self):
return "echo %PROMPT%"

activation_tester(Batch)
3 changes: 3 additions & 0 deletions tests/unit/activation/test_csh.py
Expand Up @@ -8,4 +8,7 @@ class Csh(activation_tester_class):
def __init__(self, session):
super(Csh, self).__init__(CShellActivator, session, "csh", "activate.csh", "csh")

def print_prompt(self):
return "echo 'source \"$VIRTUAL_ENV/bin/activate.csh\"; echo $prompt' | csh -i"

activation_tester(Csh)
3 changes: 3 additions & 0 deletions tests/unit/activation/test_fish.py
Expand Up @@ -17,4 +17,7 @@ class Fish(activation_tester_class):
def __init__(self, session):
super(Fish, self).__init__(FishActivator, session, "fish", "activate.fish", "fish")

def print_prompt(self):
return "fish_prompt"

activation_tester(Fish)
3 changes: 3 additions & 0 deletions tests/unit/activation/test_nushell.py
Expand Up @@ -23,4 +23,7 @@ def __init__(self, session):

self.unix_line_ending = not IS_WIN

def print_prompt(self):
return r"echo $virtual_prompt; printf '\n'"

activation_tester(Nushell)
3 changes: 3 additions & 0 deletions tests/unit/activation/test_powershell.py
Expand Up @@ -32,4 +32,7 @@ def _get_test_lines(self, activate_script):
def invoke_script(self):
return [self.cmd, "-File"]

def print_prompt(self):
return "prompt"

activation_tester(PowerShell)