Skip to content

Commit

Permalink
GH-128779: add test for venv --system-site-packages
Browse files Browse the repository at this point in the history
Signed-off-by: Filipe Laíns <[email protected]>
  • Loading branch information
FFY00 committed Jan 30, 2025
1 parent a472244 commit 61f7bfb
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
from test.support import socket_helper
from test.support import captured_stderr
from test.support.os_helper import TESTFN, EnvironmentVarGuard
from test.support.venv import VirtualEnvironmentMixin
import ast
import builtins
import glob
import io
import json
import os
import re
import shutil
Expand All @@ -24,6 +26,7 @@
import sys
import sysconfig
import tempfile
import textwrap
import urllib.error
import urllib.request
from unittest import mock
Expand Down Expand Up @@ -456,7 +459,7 @@ def cleanup(self, prep=False):
if os.path.exists(self.bad_dir_path):
os.rmdir(self.bad_dir_path)

class ImportSideEffectTests(unittest.TestCase):
class ImportSideEffectTests(unittest.TestCase, VirtualEnvironmentMixin):
"""Test side-effects from importing 'site'."""

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

@support.requires_subprocess()
def test_system_site_packages(self):
script = textwrap.dedent("""
import sys, json
print(json.dumps(
sys.path,
indent=2,
))
""")

# Use _get_preferred_schemes to find the system scheme, in case we are in a virtual environment
system_scheme_name = sysconfig._get_preferred_schemes()['prefix']
system_paths = sysconfig.get_paths(system_scheme_name)

with self.venv(system_site_packages=False) as venv:
sys_path = json.loads(venv.run('-c', script).stdout)
assert system_paths['purelib'] not in sys_path, sys_path
assert system_paths['platlib'] not in sys_path, sys_path

with self.venv(system_site_packages=True) as venv:
sys_path = json.loads(venv.run('-c', script).stdout)
assert system_paths['purelib'] in sys_path, sys_path
assert system_paths['platlib'] in sys_path, sys_path


class StartupImportTests(unittest.TestCase):

Expand Down

0 comments on commit 61f7bfb

Please sign in to comment.