@@ -35,6 +35,26 @@ def skip_if_ABSTFN_contains_backslash(test):
35
35
return [test , unittest .skip (msg )(test )][found_backslash ]
36
36
37
37
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
+
38
58
class PosixPathTest (unittest .TestCase ):
39
59
40
60
def setUp (self ):
@@ -444,7 +464,7 @@ def test_normpath(self):
444
464
self .assertEqual (result , expected )
445
465
446
466
@skip_if_ABSTFN_contains_backslash
447
- @support . subTests ( 'kwargs' , ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING }) )
467
+ @_parameterize ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING })
448
468
def test_realpath_curdir (self , kwargs ):
449
469
self .assertEqual (realpath ('.' , ** kwargs ), os .getcwd ())
450
470
self .assertEqual (realpath ('./.' , ** kwargs ), os .getcwd ())
@@ -455,7 +475,7 @@ def test_realpath_curdir(self, kwargs):
455
475
self .assertEqual (realpath (b'/' .join ([b'.' ] * 100 ), ** kwargs ), os .getcwdb ())
456
476
457
477
@skip_if_ABSTFN_contains_backslash
458
- @support . subTests ( 'kwargs' , ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING }) )
478
+ @_parameterize ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING })
459
479
def test_realpath_pardir (self , kwargs ):
460
480
self .assertEqual (realpath ('..' , ** kwargs ), dirname (os .getcwd ()))
461
481
self .assertEqual (realpath ('../..' , ** kwargs ), dirname (dirname (os .getcwd ())))
@@ -467,7 +487,7 @@ def test_realpath_pardir(self, kwargs):
467
487
468
488
@os_helper .skip_unless_symlink
469
489
@skip_if_ABSTFN_contains_backslash
470
- @support . subTests ( 'kwargs' , ( {}, {'strict' : ALLOW_MISSING }) )
490
+ @_parameterize ( {}, {'strict' : ALLOW_MISSING })
471
491
def test_realpath_basic (self , kwargs ):
472
492
# Basic operation.
473
493
try :
@@ -585,7 +605,7 @@ def test_realpath_invalid_paths(self):
585
605
586
606
@os_helper .skip_unless_symlink
587
607
@skip_if_ABSTFN_contains_backslash
588
- @support . subTests ( 'kwargs' , ( {}, {'strict' : ALLOW_MISSING }) )
608
+ @_parameterize ( {}, {'strict' : ALLOW_MISSING })
589
609
def test_realpath_relative (self , kwargs ):
590
610
try :
591
611
os .symlink (posixpath .relpath (ABSTFN + "1" ), ABSTFN )
@@ -595,7 +615,7 @@ def test_realpath_relative(self, kwargs):
595
615
596
616
@os_helper .skip_unless_symlink
597
617
@skip_if_ABSTFN_contains_backslash
598
- @support . subTests ( 'kwargs' , ( {}, {'strict' : ALLOW_MISSING }) )
618
+ @_parameterize ( {}, {'strict' : ALLOW_MISSING })
599
619
def test_realpath_missing_pardir (self , kwargs ):
600
620
try :
601
621
os .symlink (TESTFN + "1" , TESTFN )
@@ -647,7 +667,7 @@ def test_realpath_symlink_loops(self):
647
667
648
668
@os_helper .skip_unless_symlink
649
669
@skip_if_ABSTFN_contains_backslash
650
- @support . subTests ( 'kwargs' , ( {'strict' : True }, {'strict' : ALLOW_MISSING }) )
670
+ @_parameterize ( {'strict' : True }, {'strict' : ALLOW_MISSING })
651
671
def test_realpath_symlink_loops_strict (self , kwargs ):
652
672
# Bug #43757, raise OSError if we get into an infinite symlink loop in
653
673
# the strict modes.
@@ -689,7 +709,7 @@ def test_realpath_symlink_loops_strict(self, kwargs):
689
709
690
710
@os_helper .skip_unless_symlink
691
711
@skip_if_ABSTFN_contains_backslash
692
- @support . subTests ( 'kwargs' , ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING }) )
712
+ @_parameterize ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING })
693
713
def test_realpath_repeated_indirect_symlinks (self , kwargs ):
694
714
# Issue #6975.
695
715
try :
@@ -704,7 +724,7 @@ def test_realpath_repeated_indirect_symlinks(self, kwargs):
704
724
705
725
@os_helper .skip_unless_symlink
706
726
@skip_if_ABSTFN_contains_backslash
707
- @support . subTests ( 'kwargs' , ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING }) )
727
+ @_parameterize ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING })
708
728
def test_realpath_deep_recursion (self , kwargs ):
709
729
depth = 10
710
730
try :
@@ -724,7 +744,7 @@ def test_realpath_deep_recursion(self, kwargs):
724
744
725
745
@os_helper .skip_unless_symlink
726
746
@skip_if_ABSTFN_contains_backslash
727
- @support . subTests ( 'kwargs' , ( {}, {'strict' : ALLOW_MISSING }) )
747
+ @_parameterize ( {}, {'strict' : ALLOW_MISSING })
728
748
def test_realpath_resolve_parents (self , kwargs ):
729
749
# We also need to resolve any symlinks in the parents of a relative
730
750
# path passed to realpath. E.g.: current working directory is
@@ -745,7 +765,7 @@ def test_realpath_resolve_parents(self, kwargs):
745
765
746
766
@os_helper .skip_unless_symlink
747
767
@skip_if_ABSTFN_contains_backslash
748
- @support . subTests ( 'kwargs' , ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING }) )
768
+ @_parameterize ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING })
749
769
def test_realpath_resolve_before_normalizing (self , kwargs ):
750
770
# Bug #990669: Symbolic links should be resolved before we
751
771
# 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):
774
794
775
795
@os_helper .skip_unless_symlink
776
796
@skip_if_ABSTFN_contains_backslash
777
- @support . subTests ( 'kwargs' , ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING }) )
797
+ @_parameterize ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING })
778
798
def test_realpath_resolve_first (self , kwargs ):
779
799
# Bug #1213894: The first component of the path, if not absolute,
780
800
# must be resolved too.
@@ -812,7 +832,7 @@ def test_realpath_unreadable_symlink(self):
812
832
@skip_if_ABSTFN_contains_backslash
813
833
@unittest .skipIf (os .chmod not in os .supports_follow_symlinks , "Can't set symlink permissions" )
814
834
@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 })
816
836
def test_realpath_unreadable_symlink_strict (self , kwargs ):
817
837
try :
818
838
os .symlink (ABSTFN + "1" , ABSTFN )
@@ -1148,7 +1168,7 @@ def test_path_normpath(self):
1148
1168
def test_path_abspath (self ):
1149
1169
self .assertPathEqual (self .path .abspath )
1150
1170
1151
- @support . subTests ( 'kwargs' , ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING }) )
1171
+ @_parameterize ( {}, {'strict' : True }, {'strict' : ALLOW_MISSING })
1152
1172
def test_path_realpath (self , kwargs ):
1153
1173
self .assertPathEqual (self .path .realpath )
1154
1174
0 commit comments