1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/tests/functional/test_configuration.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

150 lines
5.9 KiB
Python
Raw Permalink Normal View History

"""Tests for the config command
"""
2020-06-01 01:42:14 +02:00
import re
2017-05-14 09:15:22 +02:00
import textwrap
2017-06-13 20:11:31 +02:00
2018-07-30 06:10:59 +02:00
from pip._internal.cli.status_codes import ERROR
2020-09-23 16:27:09 +02:00
from pip._internal.configuration import CONFIG_BASENAME, get_configuration_files
from tests.lib import PipTestEnvironment
2017-06-13 20:11:31 +02:00
from tests.lib.configuration_helpers import ConfigurationMixin, kinds
from tests.lib.venv import VirtualEnvironment
def test_no_options_passed_should_error(script: PipTestEnvironment) -> None:
result = script.pip("config", expect_error=True)
assert result.returncode == ERROR
2017-05-15 18:38:02 +02:00
class TestBasicLoading(ConfigurationMixin):
def test_basic_modification_pipeline(self, script: PipTestEnvironment) -> None:
2017-05-14 09:27:39 +02:00
script.pip("config", "get", "test.blah", expect_error=True)
2017-05-14 09:15:22 +02:00
script.pip("config", "set", "test.blah", "1")
2017-05-14 09:27:39 +02:00
result = script.pip("config", "get", "test.blah")
assert result.stdout.strip() == "1"
script.pip("config", "unset", "test.blah")
script.pip("config", "get", "test.blah", expect_error=True)
2017-05-14 09:15:22 +02:00
def test_listing_is_correct(self, script: PipTestEnvironment) -> None:
2017-05-14 09:15:22 +02:00
script.pip("config", "set", "test.listing-beta", "2")
script.pip("config", "set", "test.listing-alpha", "1")
script.pip("config", "set", "test.listing-gamma", "3")
result = script.pip("config", "list")
lines = list(
filter(lambda x: x.startswith("test.listing-"), result.stdout.splitlines())
)
expected = """
test.listing-alpha='1'
test.listing-beta='2'
test.listing-gamma='3'
"""
2017-05-14 09:15:22 +02:00
assert lines == textwrap.dedent(expected).strip().splitlines()
def test_forget_section(self, script: PipTestEnvironment) -> None:
2019-05-12 21:28:45 +02:00
result = script.pip("config", "set", "isolated", "true", expect_error=True)
assert "global.isolated" in result.stderr
2020-06-01 01:42:14 +02:00
def test_env_var_values(self, script: PipTestEnvironment) -> None:
2020-06-01 01:42:14 +02:00
"""Test that pip configuration set with environment variables
is correctly displayed under "env_var".
"""
env_vars = {
"PIP_DEFAULT_TIMEOUT": "60",
"PIP_FIND_LINKS": "http://mirror.example.com",
}
script.environ.update(env_vars)
result = script.pip("config", "debug")
assert "PIP_DEFAULT_TIMEOUT='60'" in result.stdout
assert "PIP_FIND_LINKS='http://mirror.example.com'" in result.stdout
assert re.search(r"env_var:\n( .+\n)+", result.stdout)
def test_env_values(self, script: PipTestEnvironment) -> None:
2020-06-01 01:42:14 +02:00
"""Test that custom pip configuration using the environment variable
PIP_CONFIG_FILE is correctly displayed under "env". This configuration
takes place of per-user configuration file displayed under "user".
"""
config_file = script.scratch_path / "test-pip.cfg"
script.environ["PIP_CONFIG_FILE"] = str(config_file)
config_file.write_text(
textwrap.dedent(
"""\
[global]
timeout = 60
[freeze]
timeout = 10
"""
)
2021-08-13 15:23:45 +02:00
)
2020-06-01 01:42:14 +02:00
result = script.pip("config", "debug")
assert f"{config_file}, exists: True" in result.stdout
2020-06-01 01:42:14 +02:00
assert "global.timeout: 60" in result.stdout
assert "freeze.timeout: 10" in result.stdout
assert re.search(r"env:\n( .+\n)+", result.stdout)
def test_user_values(self, script: PipTestEnvironment) -> None:
2020-06-01 01:42:14 +02:00
"""Test that the user pip configuration set using --user
is correctly displayed under "user". This configuration takes place
of custom path location using the environment variable PIP_CONFIG_FILE
displayed under "env".
"""
# Use new config file
new_config_file = get_configuration_files()[kinds.USER][1]
script.pip("config", "--user", "set", "global.timeout", "60")
script.pip("config", "--user", "set", "freeze.timeout", "10")
result = script.pip("config", "debug")
assert f"{new_config_file}, exists: True" in result.stdout
2020-06-01 01:42:14 +02:00
assert "global.timeout: 60" in result.stdout
assert "freeze.timeout: 10" in result.stdout
assert re.search(r"user:\n( .+\n)+", result.stdout)
def test_site_values(
self, script: PipTestEnvironment, virtualenv: VirtualEnvironment
) -> None:
2020-06-01 01:42:14 +02:00
"""Test that the current environment configuration set using --site
is correctly displayed under "site".
"""
# Site config file will be inside the virtualenv
site_config_file = virtualenv.location / CONFIG_BASENAME
script.pip("config", "--site", "set", "global.timeout", "60")
script.pip("config", "--site", "set", "freeze.timeout", "10")
result = script.pip("config", "debug")
assert f"{site_config_file}, exists: True" in result.stdout
2020-06-01 01:42:14 +02:00
assert "global.timeout: 60" in result.stdout
assert "freeze.timeout: 10" in result.stdout
assert re.search(r"site:\n( .+\n)+", result.stdout)
def test_global_config_file(self, script: PipTestEnvironment) -> None:
"""Test that the system-wide configuration can be identified"""
# We cannot write to system-wide files which might have permissions
# defined in a way that the tox virtualenvcannot write to those
# locations. Additionally we cannot patch those paths since pip config
# commands runs inside a subprocess.
# So we just check if the file can be identified
global_config_file = get_configuration_files()[kinds.GLOBAL][0]
result = script.pip("config", "debug")
assert f"{global_config_file}, exists:" in result.stdout
2022-02-03 12:57:21 +01:00
def test_editor_does_not_exist(self, script: PipTestEnvironment) -> None:
"""Ensure that FileNotFoundError sets filename correctly"""
result = script.pip(
"config", "edit", "--editor", "notrealeditor", expect_error=True
)
assert "notrealeditor" in result.stderr