2021-08-30 00:43:28 +02:00
|
|
|
from typing import Callable, List
|
|
|
|
from unittest import mock
|
2019-07-10 09:36:33 +02:00
|
|
|
|
2021-02-10 11:38:21 +01:00
|
|
|
import pytest
|
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
from pip._internal.cli.base_command import Command
|
2019-08-07 23:08:12 +02:00
|
|
|
from pip._internal.cli.req_command import (
|
|
|
|
IndexGroupCommand,
|
|
|
|
RequirementCommand,
|
|
|
|
SessionCommandMixin,
|
|
|
|
)
|
2019-07-11 10:27:21 +02:00
|
|
|
from pip._internal.commands import commands_dict, create_command
|
2019-07-10 09:36:33 +02:00
|
|
|
|
2019-08-08 07:03:49 +02:00
|
|
|
# These are the expected names of the commands whose classes inherit from
|
|
|
|
# IndexGroupCommand.
|
2021-06-11 13:01:14 +02:00
|
|
|
EXPECTED_INDEX_GROUP_COMMANDS = ["download", "index", "install", "list", "wheel"]
|
2019-08-08 07:03:49 +02:00
|
|
|
|
2019-07-10 09:36:33 +02:00
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
def check_commands(pred: Callable[[Command], bool], expected: List[str]) -> None:
|
2019-08-07 23:08:12 +02:00
|
|
|
"""
|
|
|
|
Check the commands satisfying a predicate.
|
|
|
|
"""
|
|
|
|
commands = [create_command(name) for name in sorted(commands_dict)]
|
|
|
|
actual = [command.name for command in commands if pred(command)]
|
2020-12-23 20:25:12 +01:00
|
|
|
assert actual == expected, f"actual: {actual}"
|
2019-08-07 23:08:12 +02:00
|
|
|
|
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
def test_commands_dict__order() -> None:
|
2019-07-10 09:36:33 +02:00
|
|
|
"""
|
|
|
|
Check the ordering of commands_dict.
|
|
|
|
"""
|
|
|
|
names = list(commands_dict)
|
|
|
|
# A spot-check is sufficient to check that commands_dict encodes an
|
|
|
|
# ordering.
|
|
|
|
assert names[0] == "install"
|
|
|
|
assert names[-1] == "help"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("name", list(commands_dict))
|
2021-08-30 00:43:28 +02:00
|
|
|
def test_create_command(name: str) -> None:
|
2019-07-10 09:36:33 +02:00
|
|
|
"""Test creating an instance of each available command."""
|
|
|
|
command = create_command(name)
|
|
|
|
assert command.name == name
|
|
|
|
assert command.summary == commands_dict[name].summary
|
2019-08-07 23:08:12 +02:00
|
|
|
|
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
def test_session_commands() -> None:
|
2019-08-07 23:08:12 +02:00
|
|
|
"""
|
|
|
|
Test which commands inherit from SessionCommandMixin.
|
|
|
|
"""
|
2021-08-13 15:23:45 +02:00
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
def is_session_command(command: Command) -> bool:
|
2019-08-07 23:08:12 +02:00
|
|
|
return isinstance(command, SessionCommandMixin)
|
|
|
|
|
2021-06-11 13:01:14 +02:00
|
|
|
expected = ["download", "index", "install", "list", "search", "uninstall", "wheel"]
|
2019-08-07 23:08:12 +02:00
|
|
|
check_commands(is_session_command, expected)
|
|
|
|
|
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
def test_index_group_commands() -> None:
|
2019-08-07 23:08:12 +02:00
|
|
|
"""
|
|
|
|
Test the commands inheriting from IndexGroupCommand.
|
|
|
|
"""
|
2021-08-13 15:23:45 +02:00
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
def is_index_group_command(command: Command) -> bool:
|
2019-08-07 23:08:12 +02:00
|
|
|
return isinstance(command, IndexGroupCommand)
|
|
|
|
|
2019-08-08 07:03:49 +02:00
|
|
|
check_commands(is_index_group_command, EXPECTED_INDEX_GROUP_COMMANDS)
|
2019-08-07 23:08:12 +02:00
|
|
|
|
|
|
|
# Also check that the commands inheriting from IndexGroupCommand are
|
|
|
|
# exactly the commands with the --no-index option.
|
2021-08-30 00:43:28 +02:00
|
|
|
def has_option_no_index(command: Command) -> bool:
|
2019-08-07 23:08:12 +02:00
|
|
|
return command.parser.has_option("--no-index")
|
|
|
|
|
2019-08-08 07:03:49 +02:00
|
|
|
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",
|
|
|
|
[
|
2019-09-29 16:23:38 +02:00
|
|
|
# pip_self_version_check() is only called when both
|
2019-08-08 07:03:49 +02:00
|
|
|
# disable_pip_version_check and no_index are False.
|
|
|
|
(False, False, True),
|
|
|
|
(False, True, False),
|
|
|
|
(True, False, False),
|
|
|
|
(True, True, False),
|
|
|
|
],
|
|
|
|
)
|
2021-08-30 00:43:28 +02:00
|
|
|
@mock.patch("pip._internal.cli.req_command.pip_self_version_check")
|
2019-08-08 07:03:49 +02:00
|
|
|
def test_index_group_handle_pip_version_check(
|
2021-08-30 00:43:28 +02:00
|
|
|
mock_version_check: mock.Mock,
|
|
|
|
command_name: str,
|
|
|
|
disable_pip_version_check: bool,
|
|
|
|
no_index: bool,
|
|
|
|
expected_called: bool,
|
|
|
|
) -> None:
|
2019-08-08 07:03:49 +02:00
|
|
|
"""
|
2019-09-29 16:23:38 +02:00
|
|
|
Test whether pip_self_version_check() is called when
|
|
|
|
handle_pip_version_check() is called, for each of the
|
|
|
|
IndexGroupCommand classes.
|
2019-08-08 07:03:49 +02:00
|
|
|
"""
|
|
|
|
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()
|
2019-08-07 23:08:12 +02:00
|
|
|
|
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
def test_requirement_commands() -> None:
|
2019-08-07 23:08:12 +02:00
|
|
|
"""
|
|
|
|
Test which commands inherit from RequirementCommand.
|
|
|
|
"""
|
2021-08-13 15:23:45 +02:00
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
def is_requirement_command(command: Command) -> bool:
|
2019-08-07 23:08:12 +02:00
|
|
|
return isinstance(command, RequirementCommand)
|
|
|
|
|
|
|
|
check_commands(is_requirement_command, ["download", "install", "wheel"])
|