Skip to content

Commit

Permalink
Refine the implementation of site configuration for virtual environme…
Browse files Browse the repository at this point in the history
…nts.
  • Loading branch information
pelson committed Apr 1, 2021
1 parent 6afa367 commit f01b3c2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/pip/_internal/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ def get_configuration_files():
for path in appdirs.site_config_dirs('pip')
]

site_config_files = [
os.path.join(sys.prefix, CONFIG_BASENAME)
]
site_config_files = []
if getattr(sys, 'base_prefix', sys.prefix) != sys.prefix:
site_config_files.append(
os.path.join(sys.base_prefix, CONFIG_BASENAME),
)
# A virtual environment config takes precedence over a base_prefix config.
site_config_files.append(os.path.join(sys.prefix, CONFIG_BASENAME))

legacy_config_file = os.path.join(
os.path.expanduser('~'),
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ def test_site_modification(self):

self.configuration.set_value("test.hello", "10")

# get the path to site config file
# get the path to site config file, mirroring the behaviour
# in _get_parser_to_modify.
assert mymock.call_count == 1
assert mymock.call_args[0][0] == (
get_configuration_files()[kinds.SITE][0]
get_configuration_files()[kinds.SITE][-1]
)

def test_user_modification(self):
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/test_options.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from contextlib import contextmanager
import sys
from tempfile import NamedTemporaryFile

import pytest
Expand Down Expand Up @@ -433,20 +434,29 @@ def test_client_cert(self):

class TestOptionsConfigFiles:

def test_venv_config_file_found(self, monkeypatch):
@pytest.mark.parametrize("in_venv", (True, False))
def test_venv_config_file_found(self, monkeypatch, in_venv):
# strict limit on the global config files list
monkeypatch.setattr(
pip._internal.utils.appdirs, 'site_config_dirs',
lambda _: ['/a/place']
)
if in_venv:
monkeypatch.setattr(sys, 'base_prefix', '/some/other/place')
else:
monkeypatch.setattr(sys, 'base_prefix', sys.prefix)

cp = pip._internal.configuration.Configuration(isolated=False)

files = []
for _, val in cp.iter_config_files():
files.extend(val)

assert len(files) == 4
if in_venv:
# With venvs we get the config from the base_prefix also.
assert len(files) == 5
else:
assert len(files) == 4

@pytest.mark.parametrize(
"args, expect",
Expand Down

0 comments on commit f01b3c2

Please sign in to comment.