Skip to content

GH-128779: add test for venv --system-site-packages #129462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
self.assertNotIn(system_paths['purelib'], sys_path)
self.assertNotIn(system_paths['platlib'], sys_path)

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


class StartupImportTests(unittest.TestCase):

Expand Down
Loading