Skip to content

Commit

Permalink
Add some handle_pip_version_check() tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerdonek committed Aug 8, 2019
1 parent 505456f commit 9b1ece3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
12 changes: 12 additions & 0 deletions tests/unit/test_base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import time

from mock import patch

from pip._internal.cli.base_command import Command
from pip._internal.utils.logging import BrokenStdoutLoggingError

Expand Down Expand Up @@ -72,6 +74,16 @@ def test_raise_broken_stdout__debug_logging(self, capsys):
assert 'Traceback (most recent call last):' in stderr


@patch('pip._internal.cli.req_command.Command.handle_pip_version_check')
def test_handle_pip_version_check_called(mock_handle_version_check):
"""
Check that Command.handle_pip_version_check() is called.
"""
cmd = FakeCommand()
cmd.main([])
mock_handle_version_check.assert_called_once()


class Test_base_command_logging(object):
"""
Test `pip.base_command.Command` setting up logging consumers based on
Expand Down
44 changes: 40 additions & 4 deletions tests/unit/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from mock import patch

from pip._internal.cli.req_command import (
IndexGroupCommand,
Expand All @@ -7,6 +8,10 @@
)
from pip._internal.commands import commands_dict, create_command

# These are the expected names of the commands whose classes inherit from
# IndexGroupCommand.
EXPECTED_INDEX_GROUP_COMMANDS = ['download', 'install', 'list', 'wheel']


def check_commands(pred, expected):
"""
Expand Down Expand Up @@ -51,19 +56,50 @@ def test_index_group_commands():
"""
Test the commands inheriting from IndexGroupCommand.
"""
expected = ['download', 'install', 'list', 'wheel']

def is_index_group_command(command):
return isinstance(command, IndexGroupCommand)

check_commands(is_index_group_command, expected)
check_commands(is_index_group_command, EXPECTED_INDEX_GROUP_COMMANDS)

# Also check that the commands inheriting from IndexGroupCommand are
# exactly the commands with the --no-index option.
def has_option_no_index(command):
return command.parser.has_option('--no-index')

check_commands(has_option_no_index, expected)
check_commands(has_option_no_index, EXPECTED_INDEX_GROUP_COMMANDS)


@pytest.mark.parametrize('command_name', EXPECTED_INDEX_GROUP_COMMANDS)
@pytest.mark.parametrize(
'disable_pip_version_check, no_index, expected_called',
[
# pip_version_check() is only called when both
# disable_pip_version_check and no_index are False.
(False, False, True),
(False, True, False),
(True, False, False),
(True, True, False),
],
)
@patch('pip._internal.cli.req_command.pip_version_check')
def test_index_group_handle_pip_version_check(
mock_version_check, command_name, disable_pip_version_check, no_index,
expected_called,
):
"""
Test whether pip_version_check() is called when handle_pip_version_check()
is called, for each of the IndexGroupCommand classes.
"""
command = create_command(command_name)
options = command.parser.get_default_values()
options.disable_pip_version_check = disable_pip_version_check
options.no_index = no_index

command.handle_pip_version_check(options)
if expected_called:
mock_version_check.assert_called_once()
else:
mock_version_check.assert_not_called()


def test_requirement_commands():
Expand Down

0 comments on commit 9b1ece3

Please sign in to comment.