Skip to content

Commit 396ec3b

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

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/pip/_internal/configuration.py

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from contextlib import contextmanager
34
from tempfile import NamedTemporaryFile
45

@@ -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)