From 0a979062b18390679d01d16314197dead579af1d Mon Sep 17 00:00:00 2001 From: Ruairidh MacLeod Date: Tue, 19 Oct 2021 21:55:25 +0000 Subject: [PATCH 1/3] special-case a '.' prompt to the cwd --- docs/changelog/2220.feature.rst | 1 + src/virtualenv/activation/activator.py | 8 +++++++- src/virtualenv/run/plugin/activators.py | 5 ++++- tests/unit/activation/test_activator.py | 26 +++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 docs/changelog/2220.feature.rst create mode 100644 tests/unit/activation/test_activator.py diff --git a/docs/changelog/2220.feature.rst b/docs/changelog/2220.feature.rst new file mode 100644 index 000000000..2bdc76875 --- /dev/null +++ b/docs/changelog/2220.feature.rst @@ -0,0 +1 @@ +Special-case ``--prompt .`` to the name of the current directory - by :user:`rkm`. diff --git a/src/virtualenv/activation/activator.py b/src/virtualenv/activation/activator.py index 587ac105b..cf73d0564 100644 --- a/src/virtualenv/activation/activator.py +++ b/src/virtualenv/activation/activator.py @@ -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,12 @@ def __init__(self, options): :param options: the parsed options as defined within :meth:`add_parser_arguments` """ - self.flag_prompt = options.prompt + + prompt = options.prompt + if prompt == ".": + prompt = "({}) ".format(os.path.basename(os.getcwd())) + + self.flag_prompt = prompt @classmethod def supports(cls, interpreter): diff --git a/src/virtualenv/run/plugin/activators.py b/src/virtualenv/run/plugin/activators.py index dea28277f..1b09d8d12 100644 --- a/src/virtualenv/run/plugin/activators.py +++ b/src/virtualenv/run/plugin/activators.py @@ -43,7 +43,10 @@ def handle_selected_arg_parse(self, options): "--prompt", dest="prompt", metavar="prompt", - help="provides an alternative prompt prefix for this environment", + help=( + "provides an alternative prompt prefix for this environment. " + "A value of '.' will be replaced with the name of the current directory" + ), default=None, ) for activator in self.active.values(): diff --git a/tests/unit/activation/test_activator.py b/tests/unit/activation/test_activator.py new file mode 100644 index 000000000..b0cd72f3d --- /dev/null +++ b/tests/unit/activation/test_activator.py @@ -0,0 +1,26 @@ +from __future__ import absolute_import, unicode_literals + +from argparse import Namespace + +import pytest + +from virtualenv.activation.activator import Activator + + +class FakeActivator(Activator): + def generate(self, creator): + raise NotImplementedError + + +@pytest.mark.parametrize( + ("prompt", "expected"), + ( + (None, None), + ("foo", "foo"), + # Special case for the current directory + (".", "(virtualenv) "), + ), +) +def test_activator_prompt_normal(prompt, expected): + activator = FakeActivator(Namespace(prompt=prompt)) + assert activator.flag_prompt == expected From 81c5086f39e06945bf83c070f75aeef3824448ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sat, 23 Oct 2021 11:06:13 +0100 Subject: [PATCH 2/3] PR Feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bernát Gábor --- .pre-commit-config.yaml | 2 +- src/virtualenv/activation/activator.py | 7 +------ tests/unit/activation/test_activator.py | 26 +++++++++---------------- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b14317ea4..a9ba6790d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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: diff --git a/src/virtualenv/activation/activator.py b/src/virtualenv/activation/activator.py index cf73d0564..80d7e47fd 100644 --- a/src/virtualenv/activation/activator.py +++ b/src/virtualenv/activation/activator.py @@ -15,12 +15,7 @@ def __init__(self, options): :param options: the parsed options as defined within :meth:`add_parser_arguments` """ - - prompt = options.prompt - if prompt == ".": - prompt = "({}) ".format(os.path.basename(os.getcwd())) - - self.flag_prompt = prompt + self.flag_prompt = os.path.basename(os.getcwd()) if options.prompt == "." else options.prompt @classmethod def supports(cls, interpreter): diff --git a/tests/unit/activation/test_activator.py b/tests/unit/activation/test_activator.py index b0cd72f3d..4a8a51c66 100644 --- a/tests/unit/activation/test_activator.py +++ b/tests/unit/activation/test_activator.py @@ -2,25 +2,17 @@ from argparse import Namespace -import pytest - from virtualenv.activation.activator import Activator -class FakeActivator(Activator): - def generate(self, creator): - raise NotImplementedError +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) -@pytest.mark.parametrize( - ("prompt", "expected"), - ( - (None, None), - ("foo", "foo"), - # Special case for the current directory - (".", "(virtualenv) "), - ), -) -def test_activator_prompt_normal(prompt, expected): - activator = FakeActivator(Namespace(prompt=prompt)) - assert activator.flag_prompt == expected + activator = FakeActivator(Namespace(prompt=".")) + assert activator.flag_prompt == "magic" From 712ed9d666fde5473e2461632b281b71c0b7c8ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sat, 23 Oct 2021 11:08:49 +0100 Subject: [PATCH 3/3] Fix help message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bernát Gábor --- src/virtualenv/run/plugin/activators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/virtualenv/run/plugin/activators.py b/src/virtualenv/run/plugin/activators.py index 1b09d8d12..8180981b1 100644 --- a/src/virtualenv/run/plugin/activators.py +++ b/src/virtualenv/run/plugin/activators.py @@ -44,8 +44,8 @@ def handle_selected_arg_parse(self, options): dest="prompt", metavar="prompt", help=( - "provides an alternative prompt prefix for this environment. " - "A value of '.' will be replaced with the name of the current directory" + "provides an alternative prompt prefix for this environment " + "(value of . means name of the current working directory)" ), default=None, )