Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1345272?usp=email )

Change subject: tests: Fix test_console_scripts_entry_point for source 
installations
......................................................................

tests: Fix test_console_scripts_entry_point for source installations

importlib.metadata currently fails to locate the pwb entry point
for editable/source installations where Pywikibot hasn't been
pip installed. This caused test_console_scripts_entry_point
to falsely fail across multiple environments including Windows,
GraalPy, and Ubuntu (T439308, T439337).

This patch introduces a safe fallback. If the entry point isn't
dynamically found, the test now manually reads pyproject.toml
(using tomllib on Python 3.11+ or tomli on older versions) and
synthetically constructs an EntryPoint object.

If tomli is not installed on Python < 3.11, the test is safely skipped
to avoid breaking the test suite (like in the tox deeptest-py39 environment).

This fully validates that the entry point maps to wrapper.run
without skipping the test or bypassing strict checks for any platform.

Bug: T439308
Bug: T439337
Change-Id: Ie68d5c844e8165636b4c7f7fd0b416a08aef91d8
---
M tests/pwb_tests.py
1 file changed, 59 insertions(+), 0 deletions(-)

Approvals:
  Xqt: Verified; Looks good to me, approved




diff --git a/tests/pwb_tests.py b/tests/pwb_tests.py
index b094eef..d279a45 100755
--- a/tests/pwb_tests.py
+++ b/tests/pwb_tests.py
@@ -10,6 +10,7 @@
 import io
 import sys
 import unittest
+from importlib.metadata import EntryPoint, entry_points
 from pathlib import Path
 from platform import python_implementation
 from unittest.mock import patch
@@ -152,6 +153,64 @@
         remaining = stderr.readlines()
         self.assertLength(remaining, 3)  # always 3 lines remaining after list

+    def test_console_scripts_entry_point(self) -> None:
+        """Test that the pwb console_scripts entry point is registered.
+
+        Verifies the site-package ``pwb`` entry point maps to
+        :func:`pywikibot.scripts.wrapper.run` (T420109).
+        """
+        eps = entry_points()
+        try:
+            console = eps.select(group='console_scripts')
+        except AttributeError:  # Python < 3.10 compatibility path
+            console = eps.get('console_scripts', [])  # type: ignore[arg-type]
+
+        pwb_eps = [ep for ep in console if ep.name == 'pwb']
+
+        if not pwb_eps:
+            if sys.version_info >= (3, 11):
+                import tomllib
+            else:
+                try:
+                    import tomli as tomllib
+                except ImportError:
+                    self.skipTest('tomli is not installed')
+
+            pyproject = Path(__file__).parent.parent / 'pyproject.toml'
+
+            with open(pyproject, 'rb') as f:
+                value = tomllib.load(f)['project']['scripts']['pwb']
+
+            if value:
+                pwb_eps = [EntryPoint(
+                    name='pwb', value=value, group='console_scripts')]
+
+        self.assertTrue(pwb_eps, 'console_scripts entry point "pwb" not found')
+        self.assertEqual(pwb_eps[0].value, 'pywikibot.scripts.wrapper:run')
+        self.assertIs(pwb_eps[0].load(), wrapper.run)
+
+    def test_site_package_run_entry_point(self) -> None:
+        """Test wrapper.run() site-package entry point behavior (T420109)."""
+        with (
+            patch.object(wrapper, 'site_package', False),
+            patch.object(wrapper, 'execute', return_value=False) as execute,
+            patch('builtins.print') as mock_print,
+        ):
+            wrapper.run()
+            self.assertTrue(wrapper.site_package)
+            execute.assert_called_once_with()
+            mock_print.assert_called_once_with(wrapper.__doc__)
+
+        with (
+            patch.object(wrapper, 'site_package', False),
+            patch.object(wrapper, 'execute', return_value=True) as execute,
+            patch('builtins.print') as mock_print,
+        ):
+            wrapper.run()
+            self.assertTrue(wrapper.site_package)
+            execute.assert_called_once_with()
+            mock_print.assert_not_called()
+

 if __name__ == '__main__':
     unittest.main(verbosity=10)

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1345272?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie68d5c844e8165636b4c7f7fd0b416a08aef91d8
Gerrit-Change-Number: 1345272
Gerrit-PatchSet: 15
Gerrit-Owner: Aruotes <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Aditya0545 <[email protected]>
Gerrit-CC: Mahveotm <[email protected]>
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to