Skip to content

Commit

Permalink
Clean up test based off pr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
csm10495 committed Oct 16, 2023
1 parent cbad68d commit cc0e3bb
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,40 +467,26 @@ def test_sitecustomize_executed(self):

@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
('getsitepackages', 'sitecustomize')
mod_names = [
'sitecustomize'
]

if site.ENABLE_USER_SITE:
mod_info.append(('getusersitepackages', 'usercustomize'),)
mod_names.append('usercustomize')

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]
temp_dir = tempfile.mkdtemp()
self.addCleanup(os_helper.rmtree, temp_dir)

try:
os.makedirs(base_path, exist_ok=True)
except PermissionError:
# Can't modify system site packages depending on the system configuration
continue
with EnvironmentVarGuard() as environ:
environ['PYTHONPATH'] = temp_dir

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)
for module_name in mod_names:
os_helper.rmtree(temp_dir)
os.mkdir(temp_dir)

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

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

Expand All @@ -515,8 +501,7 @@ def test_customization_modules_on_startup(self):
if 'usercustomize' == module_name:
output = subprocess.check_output([sys.executable, '-s', '-c', '""'])
self.assertNotIn(eyecatcher, output.decode('utf-8'))
finally:
os.remove(customize_path)


@unittest.skipUnless(hasattr(urllib.request, "HTTPSHandler"),
'need SSL support to download license')
Expand Down

0 comments on commit cc0e3bb

Please sign in to comment.