Skip to content

Commit f01b3c2

Browse files
committed
Refine the implementation of site configuration for virtual environments.
1 parent 6afa367 commit f01b3c2

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/pip/_internal/configuration.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ def get_configuration_files():
7575
for path in appdirs.site_config_dirs('pip')
7676
]
7777

78-
site_config_files = [
79-
os.path.join(sys.prefix, CONFIG_BASENAME)
80-
]
78+
site_config_files = []
8179
if getattr(sys, 'base_prefix', sys.prefix) != sys.prefix:
8280
site_config_files.append(
8381
os.path.join(sys.base_prefix, CONFIG_BASENAME),
8482
)
83+
# A virtual environment config takes precedence over a base_prefix config.
84+
site_config_files.append(os.path.join(sys.prefix, CONFIG_BASENAME))
8585

8686
legacy_config_file = os.path.join(
8787
os.path.expanduser('~'),

tests/unit/test_configuration.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,11 @@ def test_site_modification(self):
189189

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

192-
# get the path to site config file
192+
# get the path to site config file, mirroring the behaviour
193+
# in _get_parser_to_modify.
193194
assert mymock.call_count == 1
194195
assert mymock.call_args[0][0] == (
195-
get_configuration_files()[kinds.SITE][0]
196+
get_configuration_files()[kinds.SITE][-1]
196197
)
197198

198199
def test_user_modification(self):

tests/unit/test_options.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from contextlib import contextmanager
3+
import sys
34
from tempfile import NamedTemporaryFile
45

56
import pytest
@@ -433,20 +434,29 @@ def test_client_cert(self):
433434

434435
class TestOptionsConfigFiles:
435436

436-
def test_venv_config_file_found(self, monkeypatch):
437+
@pytest.mark.parametrize("in_venv", (True, False))
438+
def test_venv_config_file_found(self, monkeypatch, in_venv):
437439
# strict limit on the global config files list
438440
monkeypatch.setattr(
439441
pip._internal.utils.appdirs, 'site_config_dirs',
440442
lambda _: ['/a/place']
441443
)
444+
if in_venv:
445+
monkeypatch.setattr(sys, 'base_prefix', '/some/other/place')
446+
else:
447+
monkeypatch.setattr(sys, 'base_prefix', sys.prefix)
442448

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

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

449-
assert len(files) == 4
455+
if in_venv:
456+
# With venvs we get the config from the base_prefix also.
457+
assert len(files) == 5
458+
else:
459+
assert len(files) == 4
450460

451461
@pytest.mark.parametrize(
452462
"args, expect",

0 commit comments

Comments
 (0)