Skip to content

Commit

Permalink
Change the --no-cache-dir error to use raise_option_error().
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerdonek committed Jan 15, 2019
1 parent c0cc004 commit 5fe3157
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,10 @@ def no_cache_dir_callback(option, opt, value, parser):
# environment variable, like PIP_NO_CACHE_DIR=true.
if value is not None:
# Then parse the string value to get argument error-checking.
strtobool(value)
try:
strtobool(value)
except ValueError as exc:
raise_option_error(parser, option=option, msg=str(exc))

# Originally, setting PIP_NO_CACHE_DIR to a value that strtobool()
# converted to 0 (like "false" or "no") caused cache_dir to be disabled
Expand Down
18 changes: 5 additions & 13 deletions tests/unit/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@ def temp_environment_variable(name, value):
os.environ[name] = original


@contextmanager
def assert_raises_message(exc_class, expected):
"""
Assert that an exception with the given type and message is raised.
"""
with pytest.raises(exc_class) as excinfo:
yield

assert str(excinfo.value) == expected


@contextmanager
def assert_option_error(capsys, expected):
"""
Expand Down Expand Up @@ -181,13 +170,16 @@ def test_cache_dir__PIP_NO_CACHE_DIR__with_no_cache_dir(
# value in this case).
assert options.cache_dir is False

def test_cache_dir__PIP_NO_CACHE_DIR_invalid__with_no_cache_dir(self):
def test_cache_dir__PIP_NO_CACHE_DIR_invalid__with_no_cache_dir(
self, capsys,
):
"""
Test setting PIP_NO_CACHE_DIR to an invalid value while also passing
--no-cache-dir.
"""
os.environ['PIP_NO_CACHE_DIR'] = 'maybe'
with assert_raises_message(ValueError, "invalid truth value 'maybe'"):
expected_err = "--no-cache-dir error: invalid truth value 'maybe'"
with assert_option_error(capsys, expected=expected_err):
main(['--no-cache-dir', 'fake'])


Expand Down

0 comments on commit 5fe3157

Please sign in to comment.