]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-108747: Add unit tests for site.{usercustomize,sitecustomize} hooks (#109470)
authorCharles Machalow <csm10495@gmail.com>
Wed, 18 Oct 2023 05:05:41 +0000 (22:05 -0700)
committerGitHub <noreply@github.com>
Wed, 18 Oct 2023 05:05:41 +0000 (22:05 -0700)
Lib/test/test_site.py
Misc/NEWS.d/next/Tests/2023-09-15-15-00-14.gh-issue-108747.ql0owS.rst [new file with mode: 0644]

index e8ec3b35881fec2bc2cc3b0b76536628d9031f75..33d0975bda8eaabd9f51adb069d3b3e1e09e9efe 100644 (file)
@@ -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')
diff --git a/Misc/NEWS.d/next/Tests/2023-09-15-15-00-14.gh-issue-108747.ql0owS.rst b/Misc/NEWS.d/next/Tests/2023-09-15-15-00-14.gh-issue-108747.ql0owS.rst
new file mode 100644 (file)
index 0000000..ba1fe97
--- /dev/null
@@ -0,0 +1,2 @@
+Add unit test for ``usercustomize`` and ``sitecustomize`` hooks from\r
+:class:`site`.\r