1414 # but for those that require it we import here.
1515 nt = None
1616
17+
18+ def _norm (path ):
19+ if isinstance (path , (bytes , str , os .PathLike )):
20+ return ntpath .normcase (os .fsdecode (path ))
21+ elif hasattr (path , "__iter__" ):
22+ return tuple (ntpath .normcase (os .fsdecode (p )) for p in path )
23+ return path
24+
25+
1726def tester (fn , wantResult ):
1827 fn = fn .replace ("\\ " , "\\ \\ " )
1928 gotResult = eval (fn )
20- if wantResult != gotResult :
29+ if wantResult != gotResult and _norm ( wantResult ) != _norm ( gotResult ) :
2130 raise TestFailed ("%s should return: %s but returned: %s" \
2231 % (str (fn ), str (wantResult ), str (gotResult )))
2332
@@ -33,16 +42,22 @@ def tester(fn, wantResult):
3342 with warnings .catch_warnings ():
3443 warnings .simplefilter ("ignore" , DeprecationWarning )
3544 gotResult = eval (fn )
36- if isinstance (wantResult , str ):
37- wantResult = os .fsencode (wantResult )
38- elif isinstance (wantResult , tuple ):
39- wantResult = tuple (os .fsencode (r ) for r in wantResult )
40- if wantResult != gotResult :
45+ if _norm (wantResult ) != _norm (gotResult ):
4146 raise TestFailed ("%s should return: %s but returned: %s" \
4247 % (str (fn ), str (wantResult ), repr (gotResult )))
4348
4449
45- class TestNtpath (unittest .TestCase ):
50+ class NtpathTestCase (unittest .TestCase ):
51+ def assertPathEqual (self , path1 , path2 ):
52+ if path1 == path2 or _norm (path1 ) == _norm (path2 ):
53+ return
54+ self .assertEqual (path1 , path2 )
55+
56+ def assertPathIn (self , path , pathset ):
57+ self .assertIn (_norm (path ), _norm (pathset ))
58+
59+
60+ class TestNtpath (NtpathTestCase ):
4661 def test_splitext (self ):
4762 tester ('ntpath.splitext("foo.ext")' , ('foo' , '.ext' ))
4863 tester ('ntpath.splitext("/foo/foo.ext")' , ('/foo/foo' , '.ext' ))
@@ -459,7 +474,7 @@ class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
459474 attributes = ['relpath' ]
460475
461476
462- class PathLikeTests (unittest . TestCase ):
477+ class PathLikeTests (NtpathTestCase ):
463478
464479 path = ntpath
465480
@@ -470,67 +485,67 @@ def setUp(self):
470485 with open (self .file_name , 'xb' , 0 ) as file :
471486 file .write (b"test_ntpath.PathLikeTests" )
472487
473- def assertPathEqual (self , func ):
474- self .assertEqual (func (self .file_path ), func (self .file_name ))
488+ def _check_function (self , func ):
489+ self .assertPathEqual (func (self .file_path ), func (self .file_name ))
475490
476491 def test_path_normcase (self ):
477- self .assertPathEqual (self .path .normcase )
492+ self ._check_function (self .path .normcase )
478493
479494 def test_path_isabs (self ):
480- self .assertPathEqual (self .path .isabs )
495+ self ._check_function (self .path .isabs )
481496
482497 def test_path_join (self ):
483498 self .assertEqual (self .path .join ('a' , FakePath ('b' ), 'c' ),
484499 self .path .join ('a' , 'b' , 'c' ))
485500
486501 def test_path_split (self ):
487- self .assertPathEqual (self .path .split )
502+ self ._check_function (self .path .split )
488503
489504 def test_path_splitext (self ):
490- self .assertPathEqual (self .path .splitext )
505+ self ._check_function (self .path .splitext )
491506
492507 def test_path_splitdrive (self ):
493- self .assertPathEqual (self .path .splitdrive )
508+ self ._check_function (self .path .splitdrive )
494509
495510 def test_path_basename (self ):
496- self .assertPathEqual (self .path .basename )
511+ self ._check_function (self .path .basename )
497512
498513 def test_path_dirname (self ):
499- self .assertPathEqual (self .path .dirname )
514+ self ._check_function (self .path .dirname )
500515
501516 def test_path_islink (self ):
502- self .assertPathEqual (self .path .islink )
517+ self ._check_function (self .path .islink )
503518
504519 def test_path_lexists (self ):
505- self .assertPathEqual (self .path .lexists )
520+ self ._check_function (self .path .lexists )
506521
507522 def test_path_ismount (self ):
508- self .assertPathEqual (self .path .ismount )
523+ self ._check_function (self .path .ismount )
509524
510525 def test_path_expanduser (self ):
511- self .assertPathEqual (self .path .expanduser )
526+ self ._check_function (self .path .expanduser )
512527
513528 def test_path_expandvars (self ):
514- self .assertPathEqual (self .path .expandvars )
529+ self ._check_function (self .path .expandvars )
515530
516531 def test_path_normpath (self ):
517- self .assertPathEqual (self .path .normpath )
532+ self ._check_function (self .path .normpath )
518533
519534 def test_path_abspath (self ):
520- self .assertPathEqual (self .path .abspath )
535+ self ._check_function (self .path .abspath )
521536
522537 def test_path_realpath (self ):
523- self .assertPathEqual (self .path .realpath )
538+ self ._check_function (self .path .realpath )
524539
525540 def test_path_relpath (self ):
526- self .assertPathEqual (self .path .relpath )
541+ self ._check_function (self .path .relpath )
527542
528543 def test_path_commonpath (self ):
529544 common_path = self .path .commonpath ([self .file_path , self .file_name ])
530- self .assertEqual (common_path , self .file_name )
545+ self .assertPathEqual (common_path , self .file_name )
531546
532547 def test_path_isdir (self ):
533- self .assertPathEqual (self .path .isdir )
548+ self ._check_function (self .path .isdir )
534549
535550
536551if __name__ == "__main__" :
0 commit comments