diff options
author | Charles Machalow <csm10495@gmail.com> | 2023-10-18 05:05:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-18 05:05:41 (GMT) |
commit | 738574fb21967a1f313f1542dd7b70ae0dcd9705 (patch) | |
tree | 9f215699053c6f89ff4b01fd89e05d07e5e377a9 /Lib/test | |
parent | 220bcc9e27c89bf3b3609b80a31b1398840f195e (diff) | |
download | cpython-738574fb21967a1f313f1542dd7b70ae0dcd9705.zip cpython-738574fb21967a1f313f1542dd7b70ae0dcd9705.tar.gz cpython-738574fb21967a1f313f1542dd7b70ae0dcd9705.tar.bz2 |
gh-108747: Add unit tests for site.{usercustomize,sitecustomize} hooks (#109470)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_site.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index e8ec3b3..33d0975 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -465,6 +465,44 @@ class ImportSideEffectTests(unittest.TestCase): 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') |