Skip to content

gh-131290: ensure that test files can be executed as standalone scripts #131371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a9c7bc0
fixed test_regrtest
MaximGit1 Mar 17, 2025
b875bee
added spec in test.test_pyclbr.py
MaximGit1 Mar 17, 2025
678893f
changed module name when calling test_metaclass.py directly
MaximGit1 Mar 17, 2025
4670696
Merge branch 'main' into fix-for-running-tests-issue-131290
MaximGit1 Mar 17, 2025
3241aeb
added Misc/NEWS
MaximGit1 Mar 17, 2025
5ffd268
Merge branch 'main' into fix-for-running-tests-issue-131290
MaximGit1 Mar 17, 2025
d3cdd6b
Apply review suggestions for test_regrtest
MaximGit1 Mar 19, 2025
682613f
added comment for test_metaclass.py
MaximGit1 Apr 6, 2025
22c996d
refactor(tests): improve structure and naming in test_pyclbr.py
MaximGit1 Apr 6, 2025
6744e91
created news file
MaximGit1 Apr 6, 2025
2509478
Update Misc/NEWS.d/next/Tests/2025-03-17-19-47-27.gh-issue-131290.NyC…
MaximGit1 Apr 6, 2025
fd29fd1
Removed new entry file NEWS
MaximGit1 Apr 6, 2025
a6868a2
Merge remote-tracking branch 'origin/fix-for-running-tests-issue-1312…
MaximGit1 Apr 6, 2025
bef8cc5
local import removed in test_pyclbr.py
MaximGit1 Apr 6, 2025
584e07b
Created contextmanager for temporarily setting __spec__ on __main__ m…
MaximGit1 Apr 6, 2025
9ec71f2
imports order changed
MaximGit1 Apr 6, 2025
2134259
refactored contextmanager
MaximGit1 Apr 7, 2025
cd3f0bb
removed test_pdb_module
MaximGit1 Apr 7, 2025
19e1a3a
changed the order of checks in the test_others function
MaximGit1 Apr 7, 2025
d12e5c6
added comment and ignores
MaximGit1 Apr 7, 2025
9e28fa4
Update Lib/test/test_pyclbr.py
MaximGit1 Apr 7, 2025
ca81976
Merge branch 'main' into fix-for-running-tests-issue-131290
picnixz Apr 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/test/test_metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,6 @@ def load_tests(loader, tests, pattern):


if __name__ == "__main__":
# set __name__ to match doctest expectations
__name__ = "test.test_metaclass"
unittest.main()
43 changes: 35 additions & 8 deletions Lib/test/test_pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
Nick Mathewson
'''

import importlib.machinery
import sys
from contextlib import contextmanager
from textwrap import dedent
from types import FunctionType, MethodType, BuiltinFunctionType
import pyclbr
Expand All @@ -22,6 +24,29 @@
# is imperfect (as designed), testModule is called with a set of
# members to ignore.


@contextmanager
def temporary_main_spec():
"""
A context manager that temporarily sets the `__spec__` attribute
of the `__main__` module if it's missing.
"""
main_mod = sys.modules.get("__main__")
if main_mod is None:
yield # Do nothing if __main__ is not present
return

original_spec = getattr(main_mod, "__spec__", None)
if original_spec is None:
main_mod.__spec__ = importlib.machinery.ModuleSpec(
name="__main__", loader=None, origin="built-in"
)
try:
yield
finally:
main_mod.__spec__ = original_spec


class PyclbrTest(TestCase):

def assertListEq(self, l1, l2, ignore):
Expand Down Expand Up @@ -145,8 +170,9 @@ def test_easy(self):
self.checkModule('pyclbr')
# XXX: Metaclasses are not supported
# self.checkModule('ast')
self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
"DocTestCase", '_DocTestSuite'))
with temporary_main_spec():
self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
"DocTestCase", '_DocTestSuite'))
self.checkModule('difflib', ignore=("Match",))

def test_cases(self):
Expand Down Expand Up @@ -223,12 +249,13 @@ def test_others(self):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
cm('sre_parse', ignore=('dump', 'groups', 'pos')) # from sre_constants import *; property
cm(
'pdb',
# pyclbr does not handle elegantly `typing` or properties
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'),
)
cm('pydoc', ignore=('input', 'output',)) # properties
with temporary_main_spec():
cm(
'pdb',
# pyclbr does not handle elegantly `typing` or properties
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals', 'Pdb'),
)
cm('pydoc', ignore=('input', 'output',)) # properties

# Tests for modules inside packages
cm('email.parser')
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2546,4 +2546,5 @@ def test_test_result_get_state(self):


if __name__ == '__main__':
setup.setup_process()
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests in :file:`Lib/test` can now be correctly executed as standalone scripts.
Loading