2021-02-10 11:28:55 +01:00
|
|
|
from unittest.mock import Mock
|
2017-06-13 14:17:00 +02:00
|
|
|
|
2021-02-10 11:38:21 +01:00
|
|
|
import pytest
|
|
|
|
|
2020-05-25 18:06:03 +02:00
|
|
|
from pip._internal.cli.status_codes import ERROR, SUCCESS
|
2019-07-10 09:36:33 +02:00
|
|
|
from pip._internal.commands import commands_dict, create_command
|
2017-08-31 17:48:18 +02:00
|
|
|
from pip._internal.exceptions import CommandError
|
2021-09-13 00:31:12 +02:00
|
|
|
from tests.conftest import InMemoryPip
|
|
|
|
from tests.lib import PipTestEnvironment
|
2011-08-02 19:41:31 +02:00
|
|
|
|
|
|
|
|
2021-09-13 00:31:12 +02:00
|
|
|
def test_run_method_should_return_success_when_finds_command_name() -> None:
|
2011-08-02 19:41:31 +02:00
|
|
|
"""
|
|
|
|
Test HelpCommand.run for existing command
|
|
|
|
"""
|
|
|
|
options_mock = Mock()
|
2021-09-13 00:31:12 +02:00
|
|
|
args = ["freeze"]
|
2019-07-10 09:36:33 +02:00
|
|
|
help_cmd = create_command("help")
|
2011-08-02 19:41:31 +02:00
|
|
|
status = help_cmd.run(options_mock, args)
|
|
|
|
assert status == SUCCESS
|
|
|
|
|
2012-01-30 07:26:12 +01:00
|
|
|
|
2021-09-13 00:31:12 +02:00
|
|
|
def test_run_method_should_return_success_when_command_name_not_specified() -> None:
|
2011-08-02 19:41:31 +02:00
|
|
|
"""
|
|
|
|
Test HelpCommand.run when there are no args
|
|
|
|
"""
|
|
|
|
options_mock = Mock()
|
2019-07-10 09:36:33 +02:00
|
|
|
help_cmd = create_command("help")
|
2021-09-13 00:31:12 +02:00
|
|
|
status = help_cmd.run(options_mock, [])
|
2011-08-02 19:41:31 +02:00
|
|
|
assert status == SUCCESS
|
|
|
|
|
2012-01-30 07:26:12 +01:00
|
|
|
|
2021-09-13 00:31:12 +02:00
|
|
|
def test_run_method_should_raise_command_error_when_command_does_not_exist() -> None:
|
2011-08-02 19:41:31 +02:00
|
|
|
"""
|
|
|
|
Test HelpCommand.run for non-existing command
|
|
|
|
"""
|
|
|
|
options_mock = Mock()
|
2021-09-13 00:31:12 +02:00
|
|
|
args = ["mycommand"]
|
2019-07-10 09:36:33 +02:00
|
|
|
help_cmd = create_command("help")
|
2013-08-18 11:59:44 +02:00
|
|
|
|
|
|
|
with pytest.raises(CommandError):
|
|
|
|
help_cmd.run(options_mock, args)
|
2011-08-02 19:41:31 +02:00
|
|
|
|
2012-01-30 07:26:12 +01:00
|
|
|
|
2021-09-13 00:31:12 +02:00
|
|
|
def test_help_command_should_exit_status_ok_when_command_exists(
|
|
|
|
script: PipTestEnvironment,
|
|
|
|
) -> None:
|
2011-08-02 19:41:31 +02:00
|
|
|
"""
|
|
|
|
Test `help` command for existing command
|
|
|
|
"""
|
2013-08-21 11:16:07 +02:00
|
|
|
result = script.pip("help", "freeze")
|
2011-08-02 19:41:31 +02:00
|
|
|
assert result.returncode == SUCCESS
|
|
|
|
|
2012-01-30 07:26:12 +01:00
|
|
|
|
2021-09-13 00:31:12 +02:00
|
|
|
def test_help_command_should_exit_status_ok_when_no_cmd_is_specified(
|
|
|
|
script: PipTestEnvironment,
|
|
|
|
) -> None:
|
2011-08-02 19:41:31 +02:00
|
|
|
"""
|
|
|
|
Test `help` command for no command
|
|
|
|
"""
|
2013-08-21 11:16:07 +02:00
|
|
|
result = script.pip("help")
|
2011-08-02 19:41:31 +02:00
|
|
|
assert result.returncode == SUCCESS
|
|
|
|
|
2012-01-30 07:26:12 +01:00
|
|
|
|
2021-09-13 00:31:12 +02:00
|
|
|
def test_help_command_should_exit_status_error_when_cmd_does_not_exist(
|
|
|
|
script: PipTestEnvironment,
|
|
|
|
) -> None:
|
2011-08-02 19:41:31 +02:00
|
|
|
"""
|
|
|
|
Test `help` command for non-existing command
|
|
|
|
"""
|
2013-08-21 11:16:07 +02:00
|
|
|
result = script.pip("help", "mycommand", expect_error=True)
|
2011-08-02 19:41:31 +02:00
|
|
|
assert result.returncode == ERROR
|
2012-11-10 05:15:45 +01:00
|
|
|
|
2013-08-22 06:40:46 +02:00
|
|
|
|
2021-09-13 00:31:12 +02:00
|
|
|
def test_help_command_redact_auth_from_url(script: PipTestEnvironment) -> None:
|
2020-11-26 14:15:21 +01:00
|
|
|
"""
|
|
|
|
Test `help` on various subcommands redact auth from url
|
|
|
|
"""
|
|
|
|
script.environ["PIP_INDEX_URL"] = "https://user:secret@example.com"
|
|
|
|
result = script.pip("install", "--help")
|
|
|
|
assert result.returncode == SUCCESS
|
|
|
|
assert "secret" not in result.stdout
|
|
|
|
|
|
|
|
|
2021-09-13 00:31:12 +02:00
|
|
|
def test_help_command_redact_auth_from_url_with_extra_index_url(
|
|
|
|
script: PipTestEnvironment,
|
|
|
|
) -> None:
|
2020-12-02 21:53:43 +01:00
|
|
|
"""
|
|
|
|
Test `help` on various subcommands redact auth from url with extra index url
|
|
|
|
"""
|
|
|
|
script.environ["PIP_INDEX_URL"] = "https://user:secret@example.com"
|
|
|
|
script.environ["PIP_EXTRA_INDEX_URL"] = "https://user:secret@example2.com"
|
|
|
|
result = script.pip("install", "--help")
|
|
|
|
assert result.returncode == SUCCESS
|
|
|
|
assert "secret" not in result.stdout
|
|
|
|
|
|
|
|
|
2021-09-13 00:31:12 +02:00
|
|
|
def test_help_commands_equally_functional(in_memory_pip: InMemoryPip) -> None:
|
2012-11-10 05:15:45 +01:00
|
|
|
"""
|
2012-12-14 13:45:46 +01:00
|
|
|
Test if `pip help` and 'pip --help' behave the same way.
|
2012-11-10 05:15:45 +01:00
|
|
|
"""
|
2017-03-26 17:17:02 +02:00
|
|
|
results = list(map(in_memory_pip.pip, ("help", "--help")))
|
|
|
|
results.append(in_memory_pip.pip())
|
2012-11-10 05:15:45 +01:00
|
|
|
|
2023-08-26 10:20:40 +02:00
|
|
|
out = (x.stdout for x in results)
|
|
|
|
ret = (x.returncode for x in results)
|
2012-11-10 05:15:45 +01:00
|
|
|
|
2012-12-14 13:45:46 +01:00
|
|
|
msg = '"pip --help" != "pip help" != "pip"'
|
|
|
|
assert len(set(out)) == 1, "output of: " + msg
|
|
|
|
assert sum(ret) == 0, "exit codes of: " + msg
|
2017-03-26 16:54:32 +02:00
|
|
|
assert all(len(o) > 0 for o in out)
|
2012-12-14 13:45:46 +01:00
|
|
|
|
2019-07-10 09:36:33 +02:00
|
|
|
for name in commands_dict:
|
2014-01-28 15:17:51 +01:00
|
|
|
assert (
|
2017-03-26 17:17:02 +02:00
|
|
|
in_memory_pip.pip("help", name).stdout
|
|
|
|
== in_memory_pip.pip(name, "--help").stdout
|
|
|
|
!= ""
|
2014-01-28 15:17:51 +01:00
|
|
|
)
|