Skip to content

Commit 738574f

Browse files
authored
pythongh-108747: Add unit tests for site.{usercustomize,sitecustomize} hooks (python#109470)
1 parent 220bcc9 commit 738574f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Lib/test/test_site.py

+38
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,44 @@ def test_sitecustomize_executed(self):
465465
else:
466466
self.fail("sitecustomize not imported automatically")
467467

468+
@support.requires_subprocess()
469+
def test_customization_modules_on_startup(self):
470+
mod_names = [
471+
'sitecustomize'
472+
]
473+
474+
if site.ENABLE_USER_SITE:
475+
mod_names.append('usercustomize')
476+
477+
temp_dir = tempfile.mkdtemp()
478+
self.addCleanup(os_helper.rmtree, temp_dir)
479+
480+
with EnvironmentVarGuard() as environ:
481+
environ['PYTHONPATH'] = temp_dir
482+
483+
for module_name in mod_names:
484+
os_helper.rmtree(temp_dir)
485+
os.mkdir(temp_dir)
486+
487+
customize_path = os.path.join(temp_dir, f'{module_name}.py')
488+
eyecatcher = f'EXECUTED_{module_name}'
489+
490+
with open(customize_path, 'w') as f:
491+
f.write(f'print("{eyecatcher}")')
492+
493+
output = subprocess.check_output([sys.executable, '-c', '""'])
494+
self.assertIn(eyecatcher, output.decode('utf-8'))
495+
496+
# -S blocks any site-packages
497+
output = subprocess.check_output([sys.executable, '-S', '-c', '""'])
498+
self.assertNotIn(eyecatcher, output.decode('utf-8'))
499+
500+
# -s blocks user site-packages
501+
if 'usercustomize' == module_name:
502+
output = subprocess.check_output([sys.executable, '-s', '-c', '""'])
503+
self.assertNotIn(eyecatcher, output.decode('utf-8'))
504+
505+
468506
@unittest.skipUnless(hasattr(urllib.request, "HTTPSHandler"),
469507
'need SSL support to download license')
470508
@test.support.requires_resource('network')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add unit test for ``usercustomize`` and ``sitecustomize`` hooks from
2+
:class:`site`.

0 commit comments

Comments
 (0)