Skip to content

Commit

Permalink
Add unit tests for site.{usercustomize,sitecustomize} hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
csm10495 committed Sep 15, 2023
1 parent 3d88145 commit c9145ea
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):
# Check that sitecustomize and or usercustomize are executed on startup
mod_info = [
# func to get directory, file base name
('getusersitepackages', 'usercustomize'),
('getsitepackages', 'sitecustomize')
]

for func_name, module_name in mod_info:
# getusersitepackages returns a string.. getsitepackages returns a list..
# handle either way.
base_path = getattr(site, func_name)()
if not isinstance(base_path, str):
base_path = base_path[0]

customize_path = os.path.join(base_path, f'{module_name}.py')
if os.path.exists(customize_path):
# backup old sitecustomize.py
oldcustomize_path = customize_path + '.old'
if os.path.exists(oldcustomize_path):
os.remove(oldcustomize_path)
os.rename(customize_path, oldcustomize_path)
self.addCleanup(os.rename, oldcustomize_path, customize_path)

self.addCleanup(os.remove, customize_path)

eyecatcher = 'EXECUTED'

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

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

output = subprocess.check_output(f'{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 c9145ea

Please sign in to comment.