8000 gh-131290: ensure that test files can be executed as standalone scripts by MaximGit1 · Pull Request #131371 · python/cpython · GitHub
[go: up one dir, main page]

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 6 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
8000
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
8000
1 change: 1 addition & 0 deletions Lib/test/test_metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,5 @@ def load_tests(loader, tests, pattern):


if __name__ == "__main__":
__name__ = "test.test_metaclass"
unittest.main()
8 changes: 8 additions & 0 deletions Lib/test/test_pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,12 @@ def test_module_has_no_spec(self):


if __name__ == "__main__":
import importlib.util

# Adding the __spec__ attribute to the __main__ module
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which test is failing because of the lack of __spec__?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_others

user\cpython\Lib\test\test_pyclbr.py", line 226, in test_others
    cm(
    ~~^
        'pdb',
        ^^^^^^
        # pyclbr does not handle elegantly `typing` or properties
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
 File "user\cpython\Lib\test\test_pyclbr.py", line 142, in checkModule
    self.assertHaskey(dict, name, ignore)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "user\cpython\Lib\test\test_pyclbr.py", line 39, in assertHaskey
    self.assertIn(key, obj)
    ~~~~~~~~~~~~~^^^^^^^^^^
AssertionError: 'Pdb' not found in {'contextmanager': <pyclbr.Function object at 0x000001DA2A910690>}

----------------------------------------------------------------------
Ran 6 tests in 11.022s

FAILED (failures=1, errors=1)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make it a local solution for Pdb then? possibly extracting this test into a separate function?

if getattr(sys.modules["__main__"], "__spec__", None) is None:
sys.modules["__main__"].__spec__ = importlib.machinery.ModuleSpec(
name="__main__", loader=None, origin="builtin"
)

unittest_main()
3 changes: 3 additions & 0 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,8 @@ def test_print_warning(self):

def test_unicode_guard_env(self):
guard = os.environ.get(setup.UNICODE_GUARD_ENV)
# if you run the test directly,
# then look at the if __name__ == '__main__' of this file
self.assertIsNotNone(guard, f"{setup.UNICODE_GUARD_ENV} not set")
if guard.isascii():
# Skip to signify that the env var value was changed by the user;
Expand Down Expand Up @@ -2546,4 +2548,5 @@ def test_test_result_get_state(self):


if __name__ == '__main__':
os.environ['PYTHONREGRTEST_UNICODE_GUARD'] = 'some_value' # for test_unicode_guard_env
unittest.main()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
os.environ['PYTHONREGRTEST_UNICODE_GUARD'] = 'some_value' # for test_unicode_guard_env
unittest.main()
with EnvironmentVarGuard() as env:
# for test_unicode_guard_env
PYTHONREGRTEST_UNICODE_GUARD = "some_value"
unittest.main()

and add some from test.support.os_hepler import EnvironmentVarGuard top-level import. However, I don't know if this is correct to test test_unicode_guard_env like this or if we shouldn't just skip if it we're running it from __main__. Namely, decorate the test with @unittest.skipIf(__name__ == '__main__') cc @vstinner

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Several tests are modified when called directly via ./python Lib/test/...
Loading
0