Skip to content

Commit 5fe3157

Browse files
committed
Change the --no-cache-dir error to use raise_option_error().
1 parent c0cc004 commit 5fe3157

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

src/pip/_internal/cli/cmdoptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,10 @@ def no_cache_dir_callback(option, opt, value, parser):
555555
# environment variable, like PIP_NO_CACHE_DIR=true.
556556
if value is not None:
557557
# Then parse the string value to get argument error-checking.
558-
strtobool(value)
558+
try:
559+
strtobool(value)
560+
except ValueError as exc:
561+
raise_option_error(parser, option=option, msg=str(exc))
559562

560563
# Originally, setting PIP_NO_CACHE_DIR to a value that strtobool()
561564
# converted to 0 (like "false" or "no") caused cache_dir to be disabled

tests/unit/test_options.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ def temp_environment_variable(name, value):
2626
os.environ[name] = original
2727

2828

29-
@contextmanager
30-
def assert_raises_message(exc_class, expected):
31-
"""
32-
Assert that an exception with the given type and message is raised.
33-
"""
34-
with pytest.raises(exc_class) as excinfo:
35-
yield
36-
37-
assert str(excinfo.value) == expected
38-
39-
4029
@contextmanager
4130
def assert_option_error(capsys, expected):
4231
"""
@@ -181,13 +170,16 @@ def test_cache_dir__PIP_NO_CACHE_DIR__with_no_cache_dir(
181170
# value in this case).
182171
assert options.cache_dir is False
183172

184-
def test_cache_dir__PIP_NO_CACHE_DIR_invalid__with_no_cache_dir(self):
173+
def test_cache_dir__PIP_NO_CACHE_DIR_invalid__with_no_cache_dir(
174+
self, capsys,
175+
):
185176
"""
186177
Test setting PIP_NO_CACHE_DIR to an invalid value while also passing
187178
--no-cache-dir.
188179
"""
189180
os.environ['PIP_NO_CACHE_DIR'] = 'maybe'
190-
with assert_raises_message(ValueError, "invalid truth value 'maybe'"):
181+
expected_err = "--no-cache-dir error: invalid truth value 'maybe'"
182+
with assert_option_error(capsys, expected=expected_err):
191183
main(['--no-cache-dir', 'fake'])
192184

193185

0 commit comments

Comments
 (0)