Skip to content

Commit

Permalink
pythongh-108747: Add unit tests for site.{usercustomize,sitecustomize…
Browse files Browse the repository at this point in the history
…} hooks (python#109470)
  • Loading branch information
csm10495 authored Oct 18, 2023
1 parent 220bcc9 commit 738574f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,44 @@ def test_sitecustomize_executed(self):
else:
self.fail("sitecustomize not imported automatically")

@support.requires_subprocess()
def test_customization_modules_on_startup(self):
mod_names = [
'sitecustomize'
]

if site.ENABLE_USER_SITE:
mod_names.append('usercustomize')

temp_dir = tempfile.mkdtemp()
self.addCleanup(os_helper.rmtree, temp_dir)

with EnvironmentVarGuard() as environ:
environ['PYTHONPATH'] = temp_dir

for module_name in mod_names:
os_helper.rmtree(temp_dir)
os.mkdir(temp_dir)

customize_path = os.path.join(temp_dir, f'{module_name}.py')
eyecatcher = f'EXECUTED_{module_name}'

with open(customize_path, 'w') as f:
f.write(f'print("{eyecatcher}")')

output = subprocess.check_output([sys.executable, '-c', '""'])
self.assertIn(eyecatcher, output.decode('utf-8'))

# -S blocks any site-packages
output = subprocess.check_output([sys.executable, '-S', '-c', '""'])
self.assertNotIn(eyecatcher, output.decode('utf-8'))

# -s blocks user site-packages
if 'usercustomize' == module_name:
output = subprocess.check_output([sys.executable, '-s', '-c', '""'])
self.assertNotIn(eyecatcher, output.decode('utf-8'))


@unittest.skipUnless(hasattr(urllib.request, "HTTPSHandler"),
'need SSL support to download license')
@test.support.requires_resource('network')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add unit test for ``usercustomize`` and ``sitecustomize`` hooks from
:class:`site`.

0 comments on commit 738574f

Please sign in to comment.