special-case --prompt . to the cwd (#2220)

Co-authored-by: Bernát Gábor <gaborjbernat@gmail.com>
This commit is contained in:
Ruairidh MacLeod 2021-10-23 11:16:41 +01:00 committed by GitHub
parent 4188ac6e72
commit 6bfc29e1a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 3 deletions

View file

@ -28,7 +28,7 @@ repos:
rev: v1.11.0
hooks:
- id: blacken-docs
additional_dependencies: [black==20.8b1]
additional_dependencies: [black==21.9b0]
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
hooks:

View file

@ -0,0 +1 @@
Special-case ``--prompt .`` to the name of the current directory - by :user:`rkm`.

View file

@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals
import os
from abc import ABCMeta, abstractmethod
from six import add_metaclass
@ -14,7 +15,7 @@ class Activator(object):
:param options: the parsed options as defined within :meth:`add_parser_arguments`
"""
self.flag_prompt = options.prompt
self.flag_prompt = os.path.basename(os.getcwd()) if options.prompt == "." else options.prompt
@classmethod
def supports(cls, interpreter):

View file

@ -43,7 +43,10 @@ class ActivationSelector(ComponentBuilder):
"--prompt",
dest="prompt",
metavar="prompt",
help="provides an alternative prompt prefix for this environment",
help=(
"provides an alternative prompt prefix for this environment "
"(value of . means name of the current working directory)"
),
default=None,
)
for activator in self.active.values():

View file

@ -0,0 +1,18 @@
from __future__ import absolute_import, unicode_literals
from argparse import Namespace
from virtualenv.activation.activator import Activator
def test_activator_prompt_cwd(monkeypatch, tmp_path):
class FakeActivator(Activator):
def generate(self, creator):
raise NotImplementedError
cwd = tmp_path / "magic"
cwd.mkdir()
monkeypatch.chdir(cwd)
activator = FakeActivator(Namespace(prompt="."))
assert activator.flag_prompt == "magic"