@@ -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+
3858class 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