diff --git a/docs/changelog/2224.feature.rst b/docs/changelog/2224.feature.rst new file mode 100644 index 000000000..18f57010e --- /dev/null +++ b/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. diff --git a/src/virtualenv/activation/bash/activate.sh b/src/virtualenv/activation/bash/activate.sh index dd7956ef3..fb40db63a 100644 --- a/src/virtualenv/activation/bash/activate.sh +++ b/src/virtualenv/activation/bash/activate.sh @@ -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 diff --git a/src/virtualenv/activation/batch/activate.bat b/src/virtualenv/activation/batch/activate.bat index 184e4f9e1..bf774b282 100644 --- a/src/virtualenv/activation/batch/activate.bat +++ b/src/virtualenv/activation/batch/activate.bat @@ -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%" ) diff --git a/src/virtualenv/activation/cshell/activate.csh b/src/virtualenv/activation/cshell/activate.csh index 72b2cf8ef..837dcda85 100644 --- a/src/virtualenv/activation/cshell/activate.csh +++ b/src/virtualenv/activation/cshell/activate.csh @@ -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 diff --git a/src/virtualenv/activation/fish/activate.fish b/src/virtualenv/activation/fish/activate.fish index faa262270..3cc3c93cb 100644 --- a/src/virtualenv/activation/fish/activate.fish +++ b/src/virtualenv/activation/fish/activate.fish @@ -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 diff --git a/src/virtualenv/activation/nushell/activate.nu b/src/virtualenv/activation/nushell/activate.nu index c1b6c3221..9c1e2c290 100644 --- a/src/virtualenv/activation/nushell/activate.nu +++ b/src/virtualenv/activation/nushell/activate.nu @@ -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) ') ') } diff --git a/src/virtualenv/activation/powershell/activate.ps1 b/src/virtualenv/activation/powershell/activate.ps1 index a370a63f5..d5243475e 100644 --- a/src/virtualenv/activation/powershell/activate.ps1 +++ b/src/virtualenv/activation/powershell/activate.ps1 @@ -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 { diff --git a/tests/conftest.py b/tests/conftest.py index e4766ab99..ad8643b9c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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: diff --git a/tests/unit/activation/conftest.py b/tests/unit/activation/conftest.py index 5aa78bb27..6f2dd431c 100644 --- a/tests/unit/activation/conftest.py +++ b/tests/unit/activation/conftest.py @@ -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, @@ -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 @@ -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))) diff --git a/tests/unit/activation/test_bash.py b/tests/unit/activation/test_bash.py index fccef65c5..612ad378c 100644 --- a/tests/unit/activation/test_bash.py +++ b/tests/unit/activation/test_bash.py @@ -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) diff --git a/tests/unit/activation/test_batch.py b/tests/unit/activation/test_batch.py index e10d902a1..973f0bad8 100644 --- a/tests/unit/activation/test_batch.py +++ b/tests/unit/activation/test_batch.py @@ -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) diff --git a/tests/unit/activation/test_csh.py b/tests/unit/activation/test_csh.py index 69b23b68f..1fa5146de 100644 --- a/tests/unit/activation/test_csh.py +++ b/tests/unit/activation/test_csh.py @@ -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) diff --git a/tests/unit/activation/test_fish.py b/tests/unit/activation/test_fish.py index 8604ff990..7b229e01b 100644 --- a/tests/unit/activation/test_fish.py +++ b/tests/unit/activation/test_fish.py @@ -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) diff --git a/tests/unit/activation/test_nushell.py b/tests/unit/activation/test_nushell.py index 6eb50ba52..6a9c6b96e 100644 --- a/tests/unit/activation/test_nushell.py +++ b/tests/unit/activation/test_nushell.py @@ -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) diff --git a/tests/unit/activation/test_powershell.py b/tests/unit/activation/test_powershell.py index 2d6d9ebb6..f3705cda1 100644 --- a/tests/unit/activation/test_powershell.py +++ b/tests/unit/activation/test_powershell.py @@ -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)