Skip to content

Commit 16eeaf5

Browse files
Address review comments.
1 parent fe168e6 commit 16eeaf5

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

Lib/test/test_ntpath.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import unittest
88
import warnings
99
from ntpath import ALLOW_MISSING
10-
from test import support
1110
from test.support import TestFailed, cpython_only, os_helper
1211
from test.support.os_helper import FakePath
1312
from test import test_genericpath
@@ -78,6 +77,27 @@ def tester(fn, wantResult):
7877
%(str(fn), str(wantResult), repr(gotResult)))
7978

8079

80+
def _parameterize(*parameters):
81+
"""Simplistic decorator to parametrize a test
82+
83+
Runs the decorated test multiple times in subTest, with a value from
84+
'parameters' passed as an extra positional argument.
85+
Calls doCleanups() after each run.
86+
87+
Not for general use. Intended to avoid indenting for easier backports.
88+
89+
See https://discuss.python.org/t/91827 for discussing generalizations.
90+
"""
91+
def _parametrize_decorator(func):
92+
def _parameterized(self, *args, **kwargs):
93+
for parameter in parameters:
94+
with self.subTest(parameter):
95+
func(self, *args, parameter, **kwargs)
96+
self.doCleanups()
97+
return _parameterized
98+
return _parametrize_decorator
99+
100+
81101
class NtpathTestCase(unittest.TestCase):
82102
def assertPathEqual(self, path1, path2):
83103
if path1 == path2 or _norm(path1) == _norm(path2):
@@ -536,7 +556,7 @@ def test_realpath_pardir_missing_ok(self):
536556

537557
@os_helper.skip_unless_symlink
538558
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
539-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}), _do_cleanups=True)
559+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
540560
def test_realpath_basic(self, kwargs):
541561
ABSTFN = ntpath.abspath(os_helper.TESTFN)
542562
open(ABSTFN, "wb").close()
@@ -615,7 +635,7 @@ def test_realpath_invalid_paths(self):
615635
self.assertEqual(realpath(path, strict=ALLOW_MISSING), ABSTFNb + b'\\nonexistent')
616636

617637
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
618-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}))
638+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
619639
def test_realpath_invalid_unicode_paths(self, kwargs):
620640
realpath = ntpath.realpath
621641
ABSTFN = ntpath.abspath(os_helper.TESTFN)
@@ -635,7 +655,7 @@ def test_realpath_invalid_unicode_paths(self, kwargs):
635655

636656
@os_helper.skip_unless_symlink
637657
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
638-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}), _do_cleanups=True)
658+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
639659
def test_realpath_relative(self, kwargs):
640660
ABSTFN = ntpath.abspath(os_helper.TESTFN)
641661
open(ABSTFN, "wb").close()
@@ -849,7 +869,7 @@ def test_realpath_symlink_loops_raise(self):
849869

850870
@os_helper.skip_unless_symlink
851871
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
852-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}), _do_cleanups=True)
872+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
853873
def test_realpath_symlink_prefix(self, kwargs):
854874
ABSTFN = ntpath.abspath(os_helper.TESTFN)
855875
self.addCleanup(os_helper.unlink, ABSTFN + "3")

Lib/test/test_posixpath.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ def skip_if_ABSTFN_contains_backslash(test):
3535
return [test, unittest.skip(msg)(test)][found_backslash]
3636

3737

38+
def _parameterize(*parameters):
39+
"""Simplistic decorator to parametrize a test
40+
41+
Runs the decorated test multiple times in subTest, with a value from
42+
'parameters' passed as an extra positional argument.
43+
Does *not* call doCleanups() after each run.
44+
45+
Not for general use. Intended to avoid indenting for easier backports.
46+
47+
See https://discuss.python.org/t/91827 for discussing generalizations.
48+
"""
49+
def _parametrize_decorator(func):
50+
def _parameterized(self, *args, **kwargs):
51+
for parameter in parameters:
52+
with self.subTest(parameter):
53+
func(self, *args, parameter, **kwargs)
54+
return _parameterized
55+
return _parametrize_decorator
56+
57+
3858
class PosixPathTest(unittest.TestCase):
3959

4060
def setUp(self):
@@ -444,7 +464,7 @@ def test_normpath(self):
444464
self.assertEqual(result, expected)
445465

446466
@skip_if_ABSTFN_contains_backslash
447-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}))
467+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
448468
def test_realpath_curdir(self, kwargs):
449469
self.assertEqual(realpath('.', **kwargs), os.getcwd())
450470
self.assertEqual(realpath('./.', **kwargs), os.getcwd())
@@ -455,7 +475,7 @@ def test_realpath_curdir(self, kwargs):
455475
self.assertEqual(realpath(b'/'.join([b'.'] * 100), **kwargs), os.getcwdb())
456476

457477
@skip_if_ABSTFN_contains_backslash
458-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}))
478+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
459479
def test_realpath_pardir(self, kwargs):
460480
self.assertEqual(realpath('..', **kwargs), dirname(os.getcwd()))
461481
self.assertEqual(realpath('../..', **kwargs), dirname(dirname(os.getcwd())))
@@ -467,7 +487,7 @@ def test_realpath_pardir(self, kwargs):
467487

468488
@os_helper.skip_unless_symlink
469489
@skip_if_ABSTFN_contains_backslash
470-
@support.subTests('kwargs', ({}, {'strict': ALLOW_MISSING}))
490+
@_parameterize({}, {'strict': ALLOW_MISSING})
471491
def test_realpath_basic(self, kwargs):
472492
# Basic operation.
473493
try:
@@ -585,7 +605,7 @@ def test_realpath_invalid_paths(self):
585605

586606
@os_helper.skip_unless_symlink
587607
@skip_if_ABSTFN_contains_backslash
588-
@support.subTests('kwargs', ({}, {'strict': ALLOW_MISSING}))
608+
@_parameterize({}, {'strict': ALLOW_MISSING})
589609
def test_realpath_relative(self, kwargs):
590610
try:
591611
os.symlink(posixpath.relpath(ABSTFN+"1"), ABSTFN)
@@ -595,7 +615,7 @@ def test_realpath_relative(self, kwargs):
595615

596616
@os_helper.skip_unless_symlink
597617
@skip_if_ABSTFN_contains_backslash
598-
@support.subTests('kwargs', ({}, {'strict': ALLOW_MISSING}))
618+
@_parameterize({}, {'strict': ALLOW_MISSING})
599619
def test_realpath_missing_pardir(self, kwargs):
600620
try:
601621
os.symlink(TESTFN + "1", TESTFN)
@@ -647,7 +667,7 @@ def test_realpath_symlink_loops(self):
647667

648668
@os_helper.skip_unless_symlink
649669
@skip_if_ABSTFN_contains_backslash
650-
@support.subTests('kwargs', ({'strict': True}, {'strict': ALLOW_MISSING}))
670+
@_parameterize({'strict': True}, {'strict': ALLOW_MISSING})
651671
def test_realpath_symlink_loops_strict(self, kwargs):
652672
# Bug #43757, raise OSError if we get into an infinite symlink loop in
653673
# the strict modes.
@@ -689,7 +709,7 @@ def test_realpath_symlink_loops_strict(self, kwargs):
689709

690710
@os_helper.skip_unless_symlink
691711
@skip_if_ABSTFN_contains_backslash
692-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}))
712+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
693713
def test_realpath_repeated_indirect_symlinks(self, kwargs):
694714
# Issue #6975.
695715
try:
@@ -704,7 +724,7 @@ def test_realpath_repeated_indirect_symlinks(self, kwargs):
704724

705725
@os_helper.skip_unless_symlink
706726
@skip_if_ABSTFN_contains_backslash
707-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}))
727+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
708728
def test_realpath_deep_recursion(self, kwargs):
709729
depth = 10
710730
try:
@@ -724,7 +744,7 @@ def test_realpath_deep_recursion(self, kwargs):
724744

725745
@os_helper.skip_unless_symlink
726746
@skip_if_ABSTFN_contains_backslash
727-
@support.subTests('kwargs', ({}, {'strict': ALLOW_MISSING}))
747+
@_parameterize({}, {'strict': ALLOW_MISSING})
728748
def test_realpath_resolve_parents(self, kwargs):
729749
# We also need to resolve any symlinks in the parents of a relative
730750
# path passed to realpath. E.g.: current working directory is
@@ -745,7 +765,7 @@ def test_realpath_resolve_parents(self, kwargs):
745765

746766
@os_helper.skip_unless_symlink
747767
@skip_if_ABSTFN_contains_backslash
748-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}))
768+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
749769
def test_realpath_resolve_before_normalizing(self, kwargs):
750770
# Bug #990669: Symbolic links should be resolved before we
751771
# normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
@@ -774,7 +794,7 @@ def test_realpath_resolve_before_normalizing(self, kwargs):
774794

775795
@os_helper.skip_unless_symlink
776796
@skip_if_ABSTFN_contains_backslash
777-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}))
797+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
778798
def test_realpath_resolve_first(self, kwargs):
779799
# Bug #1213894: The first component of the path, if not absolute,
780800
# must be resolved too.
@@ -812,7 +832,7 @@ def test_realpath_unreadable_symlink(self):
812832
@skip_if_ABSTFN_contains_backslash
813833
@unittest.skipIf(os.chmod not in os.supports_follow_symlinks, "Can't set symlink permissions")
814834
@unittest.skipIf(sys.platform != "darwin", "only macOS requires read permission to readlink()")
815-
@support.subTests('kwargs', ({'strict': True}, {'strict': ALLOW_MISSING}))
835+
@_parameterize({'strict': True}, {'strict': ALLOW_MISSING})
816836
def test_realpath_unreadable_symlink_strict(self, kwargs):
817837
try:
818838
os.symlink(ABSTFN+"1", ABSTFN)
@@ -1148,7 +1168,7 @@ def test_path_normpath(self):
11481168
def test_path_abspath(self):
11491169
self.assertPathEqual(self.path.abspath)
11501170

1151-
@support.subTests('kwargs', ({}, {'strict': True}, {'strict': ALLOW_MISSING}))
1171+
@_parameterize({}, {'strict': True}, {'strict': ALLOW_MISSING})
11521172
def test_path_realpath(self, kwargs):
11531173
self.assertPathEqual(self.path.realpath)
11541174

0 commit comments

Comments
 (0)