Skip to content

Commit 61f7bfb

Browse files
committed
pythonGH-128779: add test for venv --system-site-packages
Signed-off-by: Filipe Laíns <[email protected]>
1 parent a472244 commit 61f7bfb

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

Lib/test/test_site.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
from test.support import socket_helper
1313
from test.support import captured_stderr
1414
from test.support.os_helper import TESTFN, EnvironmentVarGuard
15+
from test.support.venv import VirtualEnvironmentMixin
1516
import ast
1617
import builtins
1718
import glob
1819
import io
20+
import json
1921
import os
2022
import re
2123
import shutil
@@ -24,6 +26,7 @@
2426
import sys
2527
import sysconfig
2628
import tempfile
29+
import textwrap
2730
import urllib.error
2831
import urllib.request
2932
from unittest import mock
@@ -456,7 +459,7 @@ def cleanup(self, prep=False):
456459
if os.path.exists(self.bad_dir_path):
457460
os.rmdir(self.bad_dir_path)
458461

459-
class ImportSideEffectTests(unittest.TestCase):
462+
class ImportSideEffectTests(unittest.TestCase, VirtualEnvironmentMixin):
460463
"""Test side-effects from importing 'site'."""
461464

462465
def setUp(self):
@@ -576,6 +579,31 @@ def test_license_exists_at_url(self):
576579
code = e.code
577580
self.assertEqual(code, 200, msg="Can't find " + url)
578581

582+
@support.requires_subprocess()
583+
def test_system_site_packages(self):
584+
script = textwrap.dedent("""
585+
import sys, json
586+
587+
print(json.dumps(
588+
sys.path,
589+
indent=2,
590+
))
591+
""")
592+
593+
# Use _get_preferred_schemes to find the system scheme, in case we are in a virtual environment
594+
system_scheme_name = sysconfig._get_preferred_schemes()['prefix']
595+
system_paths = sysconfig.get_paths(system_scheme_name)
596+
597+
with self.venv(system_site_packages=False) as venv:
598+
sys_path = json.loads(venv.run('-c', script).stdout)
599+
assert system_paths['purelib'] not in sys_path, sys_path
600+
assert system_paths['platlib'] not in sys_path, sys_path
601+
602+
with self.venv(system_site_packages=True) as venv:
603+
sys_path = json.loads(venv.run('-c', script).stdout)
604+
assert system_paths['purelib'] in sys_path, sys_path
605+
assert system_paths['platlib'] in sys_path, sys_path
606+
579607

580608
class StartupImportTests(unittest.TestCase):
581609

0 commit comments

Comments
 (0)