Skip to content

Commit 63f6c0c

Browse files
Add more tests.
1 parent b67acdb commit 63f6c0c

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

Lib/test/test_ntpath.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import unittest
99
import warnings
10+
from ntpath import ALL_BUT_LAST
1011
from test.support import cpython_only, os_helper
1112
from test.support import TestFailed, is_emscripten
1213
from test.support.os_helper import FakePath
@@ -527,40 +528,52 @@ def test_realpath_invalid_paths(self):
527528
# gh-106242: Embedded nulls and non-strict fallback to abspath
528529
self.assertEqual(realpath(path, strict=False), path)
529530
# gh-106242: Embedded nulls should raise OSError (not ValueError)
531+
self.assertRaises(OSError, realpath, path, strict=ALL_BUT_LAST)
530532
self.assertRaises(OSError, realpath, path, strict=True)
531533
path = ABSTFNb + b'\x00'
532534
self.assertEqual(realpath(path, strict=False), path)
535+
self.assertRaises(OSError, realpath, path, strict=ALL_BUT_LAST)
533536
self.assertRaises(OSError, realpath, path, strict=True)
534537
path = ABSTFN + '\\nonexistent\\x\x00'
535538
self.assertEqual(realpath(path, strict=False), path)
539+
self.assertRaises(OSError, realpath, path, strict=ALL_BUT_LAST)
536540
self.assertRaises(OSError, realpath, path, strict=True)
537541
path = ABSTFNb + b'\\nonexistent\\x\x00'
538542
self.assertEqual(realpath(path, strict=False), path)
543+
self.assertRaises(OSError, realpath, path, strict=ALL_BUT_LAST)
539544
self.assertRaises(OSError, realpath, path, strict=True)
540545
path = ABSTFN + '\x00\\..'
541546
self.assertEqual(realpath(path, strict=False), os.getcwd())
547+
self.assertEqual(realpath(path, strict=ALL_BUT_LAST), os.getcwd())
542548
self.assertEqual(realpath(path, strict=True), os.getcwd())
543549
path = ABSTFNb + b'\x00\\..'
544550
self.assertEqual(realpath(path, strict=False), os.getcwdb())
551+
self.assertEqual(realpath(path, strict=ALL_BUT_LAST), os.getcwdb())
545552
self.assertEqual(realpath(path, strict=True), os.getcwdb())
546553
path = ABSTFN + '\\nonexistent\\x\x00\\..'
547554
self.assertEqual(realpath(path, strict=False), ABSTFN + '\\nonexistent')
555+
self.assertRaises(OSError, realpath, path, strict=ALL_BUT_LAST)
548556
self.assertRaises(OSError, realpath, path, strict=True)
549557
path = ABSTFNb + b'\\nonexistent\\x\x00\\..'
550558
self.assertEqual(realpath(path, strict=False), ABSTFNb + b'\\nonexistent')
559+
self.assertRaises(OSError, realpath, path, strict=ALL_BUT_LAST)
551560
self.assertRaises(OSError, realpath, path, strict=True)
552561

553562
path = ABSTFNb + b'\xff'
554563
self.assertRaises(UnicodeDecodeError, realpath, path, strict=False)
564+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=ALL_BUT_LAST)
555565
self.assertRaises(UnicodeDecodeError, realpath, path, strict=True)
556566
path = ABSTFNb + b'\\nonexistent\\\xff'
557567
self.assertRaises(UnicodeDecodeError, realpath, path, strict=False)
568+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=ALL_BUT_LAST)
558569
self.assertRaises(UnicodeDecodeError, realpath, path, strict=True)
559570
path = ABSTFNb + b'\xff\\..'
560571
self.assertRaises(UnicodeDecodeError, realpath, path, strict=False)
572+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=ALL_BUT_LAST)
561573
self.assertRaises(UnicodeDecodeError, realpath, path, strict=True)
562574
path = ABSTFNb + b'\\nonexistent\\\xff\\..'
563575
self.assertRaises(UnicodeDecodeError, realpath, path, strict=False)
576+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=ALL_BUT_LAST)
564577
self.assertRaises(UnicodeDecodeError, realpath, path, strict=True)
565578

566579
@os_helper.skip_unless_symlink
@@ -691,34 +704,53 @@ def test_realpath_symlink_loops_strict(self):
691704
self.addCleanup(os_helper.unlink, ABSTFN + "a")
692705

693706
os.symlink(ABSTFN, ABSTFN)
707+
self.assertRaises(OSError, ntpath.realpath, ABSTFN, strict=ALL_BUT_LAST)
694708
self.assertRaises(OSError, ntpath.realpath, ABSTFN, strict=True)
695709

696710
os.symlink(ABSTFN + "1", ABSTFN + "2")
697711
os.symlink(ABSTFN + "2", ABSTFN + "1")
712+
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1", strict=ALL_BUT_LAST)
698713
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1", strict=True)
714+
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "2", strict=ALL_BUT_LAST)
699715
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "2", strict=True)
716+
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\x", strict=ALL_BUT_LAST)
700717
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\x", strict=True)
701718
# Windows eliminates '..' components before resolving links, so the
702719
# following call is not expected to raise.
720+
self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..", strict=ALL_BUT_LAST),
721+
ntpath.dirname(ABSTFN))
703722
self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..", strict=True),
704723
ntpath.dirname(ABSTFN))
724+
self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..\\x", strict=ALL_BUT_LAST),
725+
ntpath.dirname(ABSTFN) + "\\x")
705726
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\..\\x", strict=True)
706727
os.symlink(ABSTFN + "x", ABSTFN + "y")
728+
self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..\\"
729+
+ ntpath.basename(ABSTFN) + "y",
730+
strict=ALL_BUT_LAST),
731+
ABSTFN + "x")
707732
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\..\\"
708733
+ ntpath.basename(ABSTFN) + "y",
709734
strict=True)
735+
self.assertRaises(OSError, ntpath.realpath,
736+
ABSTFN + "1\\..\\" + ntpath.basename(ABSTFN) + "1",
737+
strict=ALL_BUT_LAST)
710738
self.assertRaises(OSError, ntpath.realpath,
711739
ABSTFN + "1\\..\\" + ntpath.basename(ABSTFN) + "1",
712740
strict=True)
713741

714742
os.symlink(ntpath.basename(ABSTFN) + "a\\b", ABSTFN + "a")
743+
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "a", strict=ALL_BUT_LAST)
715744
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "a", strict=True)
716745

717746
os.symlink("..\\" + ntpath.basename(ntpath.dirname(ABSTFN))
718747
+ "\\" + ntpath.basename(ABSTFN) + "c", ABSTFN + "c")
748+
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "c", strict=ALL_BUT_LAST)
719749
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "c", strict=True)
720750

721751
# Test using relative path as well.
752+
self.assertRaises(OSError, ntpath.realpath, ntpath.basename(ABSTFN),
753+
strict=ALL_BUT_LAST)
722754
self.assertRaises(OSError, ntpath.realpath, ntpath.basename(ABSTFN),
723755
strict=True)
724756

Lib/test/test_posixpath.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,27 +487,35 @@ def test_realpath_strict(self):
487487
def test_realpath_invalid_paths(self):
488488
path = '/\x00'
489489
self.assertRaises(ValueError, realpath, path, strict=False)
490+
self.assertRaises(ValueError, realpath, path, strict=ALL_BUT_LAST)
490491
self.assertRaises(ValueError, realpath, path, strict=True)
491492
path = b'/\x00'
492493
self.assertRaises(ValueError, realpath, path, strict=False)
494+
self.assertRaises(ValueError, realpath, path, strict=ALL_BUT_LAST)
493495
self.assertRaises(ValueError, realpath, path, strict=True)
494496
path = '/nonexistent/x\x00'
495497
self.assertRaises(ValueError, realpath, path, strict=False)
498+
self.assertRaises(FileNotFoundError, realpath, path, strict=ALL_BUT_LAST)
496499
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
497500
path = b'/nonexistent/x\x00'
498501
self.assertRaises(ValueError, realpath, path, strict=False)
502+
self.assertRaises(FileNotFoundError, realpath, path, strict=ALL_BUT_LAST)
499503
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
500504
path = '/\x00/..'
501505
self.assertRaises(ValueError, realpath, path, strict=False)
506+
self.assertRaises(ValueError, realpath, path, strict=ALL_BUT_LAST)
502507
self.assertRaises(ValueError, realpath, path, strict=True)
503508
path = b'/\x00/..'
504509
self.assertRaises(ValueError, realpath, path, strict=False)
510+
self.assertRaises(ValueError, realpath, path, strict=ALL_BUT_LAST)
505511
self.assertRaises(ValueError, realpath, path, strict=True)
506512
path = '/nonexistent/x\x00/..'
507513
self.assertRaises(ValueError, realpath, path, strict=False)
514+
self.assertRaises(FileNotFoundError, realpath, path, strict=ALL_BUT_LAST)
508515
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
509516
path = b'/nonexistent/x\x00/..'
510517
self.assertRaises(ValueError, realpath, path, strict=False)
518+
self.assertRaises(FileNotFoundError, realpath, path, strict=ALL_BUT_LAST)
511519
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
512520

513521
path = '/\udfff'
@@ -516,25 +524,29 @@ def test_realpath_invalid_paths(self):
516524
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
517525
else:
518526
self.assertRaises(UnicodeEncodeError, realpath, path, strict=False)
527+
self.assertRaises(UnicodeEncodeError, realpath, path, strict=ALL_BUT_LAST)
519528
self.assertRaises(UnicodeEncodeError, realpath, path, strict=True)
520529
path = '/nonexistent/\udfff'
521530
if sys.platform == 'win32':
522531
self.assertEqual(realpath(path, strict=False), path)
523532
else:
524533
self.assertRaises(UnicodeEncodeError, realpath, path, strict=False)
534+
self.assertRaises(FileNotFoundError, realpath, path, strict=ALL_BUT_LAST)
525535
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
526536
path = '/\udfff/..'
527537
if sys.platform == 'win32':
528538
self.assertEqual(realpath(path, strict=False), '/')
529539
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
530540
else:
531541
self.assertRaises(UnicodeEncodeError, realpath, path, strict=False)
542+
self.assertRaises(UnicodeEncodeError, realpath, path, strict=ALL_BUT_LAST)
532543
self.assertRaises(UnicodeEncodeError, realpath, path, strict=True)
533544
path = '/nonexistent/\udfff/..'
534545
if sys.platform == 'win32':
535546
self.assertEqual(realpath(path, strict=False), '/nonexistent')
536547
else:
537548
self.assertRaises(UnicodeEncodeError, realpath, path, strict=False)
549+
self.assertRaises(FileNotFoundError, realpath, path, strict=ALL_BUT_LAST)
538550
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
539551

540552
path = b'/\xff'
@@ -543,6 +555,7 @@ def test_realpath_invalid_paths(self):
543555
self.assertRaises(UnicodeDecodeError, realpath, path, strict=True)
544556
else:
545557
self.assertEqual(realpath(path, strict=False), path)
558+
self.assertEqual(realpath(path, strict=ALL_BUT_LAST), path)
546559
if support.is_wasi:
547560
self.assertRaises(OSError, realpath, path, strict=True)
548561
else:
@@ -553,8 +566,10 @@ def test_realpath_invalid_paths(self):
553566
else:
554567
self.assertEqual(realpath(path, strict=False), path)
555568
if support.is_wasi:
569+
self.assertRaises(OSError, realpath, path, strict=ALL_BUT_LAST)
556570
self.assertRaises(OSError, realpath, path, strict=True)
557571
else:
572+
self.assertRaises(FileNotFoundError, realpath, path, strict=ALL_BUT_LAST)
558573
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
559574

560575
@os_helper.skip_unless_symlink
@@ -623,30 +638,48 @@ def test_realpath_symlink_loops_strict(self):
623638
# strict mode.
624639
try:
625640
os.symlink(ABSTFN, ABSTFN)
641+
self.assertRaises(OSError, realpath, ABSTFN, strict=ALL_BUT_LAST)
626642
self.assertRaises(OSError, realpath, ABSTFN, strict=True)
627643

628644
os.symlink(ABSTFN+"1", ABSTFN+"2")
629645
os.symlink(ABSTFN+"2", ABSTFN+"1")
646+
self.assertRaises(OSError, realpath, ABSTFN+"1", strict=ALL_BUT_LAST)
630647
self.assertRaises(OSError, realpath, ABSTFN+"1", strict=True)
648+
self.assertRaises(OSError, realpath, ABSTFN+"2", strict=ALL_BUT_LAST)
631649
self.assertRaises(OSError, realpath, ABSTFN+"2", strict=True)
632650

651+
self.assertRaises(OSError, realpath, ABSTFN+"1/x", strict=ALL_BUT_LAST)
633652
self.assertRaises(OSError, realpath, ABSTFN+"1/x", strict=True)
653+
self.assertRaises(OSError, realpath, ABSTFN+"1/..", strict=ALL_BUT_LAST)
634654
self.assertRaises(OSError, realpath, ABSTFN+"1/..", strict=True)
655+
self.assertRaises(OSError, realpath, ABSTFN+"1/../x", strict=ALL_BUT_LAST)
635656
self.assertRaises(OSError, realpath, ABSTFN+"1/../x", strict=True)
636657
os.symlink(ABSTFN+"x", ABSTFN+"y")
637658
self.assertRaises(OSError, realpath,
638-
ABSTFN+"1/../" + basename(ABSTFN) + "y", strict=True)
659+
ABSTFN+"1/../" + basename(ABSTFN) + "y",
660+
strict=ALL_BUT_LAST)
639661
self.assertRaises(OSError, realpath,
640-
ABSTFN+"1/../" + basename(ABSTFN) + "1", strict=True)
662+
ABSTFN+"1/../" + basename(ABSTFN) + "y",
663+
strict=True)
664+
self.assertRaises(OSError, realpath,
665+
ABSTFN+"1/../" + basename(ABSTFN) + "1",
666+
strict=ALL_BUT_LAST)
667+
self.assertRaises(OSError, realpath,
668+
ABSTFN+"1/../" + basename(ABSTFN) + "1",
669+
strict=True)
641670

642671
os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a")
672+
self.assertRaises(OSError, realpath, ABSTFN+"a", strict=ALL_BUT_LAST)
643673
self.assertRaises(OSError, realpath, ABSTFN+"a", strict=True)
644674

645675
os.symlink("../" + basename(dirname(ABSTFN)) + "/" +
646676
basename(ABSTFN) + "c", ABSTFN+"c")
677+
self.assertRaises(OSError, realpath, ABSTFN+"c", strict=ALL_BUT_LAST)
647678
self.assertRaises(OSError, realpath, ABSTFN+"c", strict=True)
648679

649680
# Test using relative path as well.
681+
with os_helper.change_cwd(dirname(ABSTFN)):
682+
self.assertRaises(OSError, realpath, basename(ABSTFN), strict=ALL_BUT_LAST)
650683
with os_helper.change_cwd(dirname(ABSTFN)):
651684
self.assertRaises(OSError, realpath, basename(ABSTFN), strict=True)
652685
finally:
@@ -768,6 +801,8 @@ def test_realpath_unreadable_symlink(self):
768801
self.assertEqual(realpath(ABSTFN + '/foo'), ABSTFN + '/foo')
769802
self.assertEqual(realpath(ABSTFN + '/../foo'), dirname(ABSTFN) + '/foo')
770803
self.assertEqual(realpath(ABSTFN + '/foo/..'), ABSTFN)
804+
with self.assertRaises(PermissionError):
805+
realpath(ABSTFN, strict=ALL_BUT_LAST)
771806
with self.assertRaises(PermissionError):
772807
realpath(ABSTFN, strict=True)
773808
finally:
@@ -780,14 +815,19 @@ def test_realpath_nonterminal_file(self):
780815
with open(ABSTFN, 'w') as f:
781816
f.write('test_posixpath wuz ere')
782817
self.assertEqual(realpath(ABSTFN, strict=False), ABSTFN)
818+
self.assertEqual(realpath(ABSTFN, strict=ALL_BUT_LAST), ABSTFN)
783819
self.assertEqual(realpath(ABSTFN, strict=True), ABSTFN)
784820
self.assertEqual(realpath(ABSTFN + "/", strict=False), ABSTFN)
821+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/", strict=ALL_BUT_LAST)
785822
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/", strict=True)
786823
self.assertEqual(realpath(ABSTFN + "/.", strict=False), ABSTFN)
824+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/.", strict=ALL_BUT_LAST)
787825
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/.", strict=True)
788826
self.assertEqual(realpath(ABSTFN + "/..", strict=False), dirname(ABSTFN))
827+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/..", strict=ALL_BUT_LAST)
789828
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/..", strict=True)
790829
self.assertEqual(realpath(ABSTFN + "/subdir", strict=False), ABSTFN + "/subdir")
830+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=ALL_BUT_LAST)
791831
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=True)
792832
finally:
793833
os_helper.unlink(ABSTFN)
@@ -800,14 +840,19 @@ def test_realpath_nonterminal_symlink_to_file(self):
800840
f.write('test_posixpath wuz ere')
801841
os.symlink(ABSTFN + "1", ABSTFN)
802842
self.assertEqual(realpath(ABSTFN, strict=False), ABSTFN + "1")
843+
self.assertEqual(realpath(ABSTFN, strict=ALL_BUT_LAST), ABSTFN + "1")
803844
self.assertEqual(realpath(ABSTFN, strict=True), ABSTFN + "1")
804845
self.assertEqual(realpath(ABSTFN + "/", strict=False), ABSTFN + "1")
846+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/", strict=ALL_BUT_LAST)
805847
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/", strict=True)
806848
self.assertEqual(realpath(ABSTFN + "/.", strict=False), ABSTFN + "1")
849+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/.", strict=ALL_BUT_LAST)
807850
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/.", strict=True)
808851
self.assertEqual(realpath(ABSTFN + "/..", strict=False), dirname(ABSTFN))
852+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/..", strict=ALL_BUT_LAST)
809853
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/..", strict=True)
810854
self.assertEqual(realpath(ABSTFN + "/subdir", strict=False), ABSTFN + "1/subdir")
855+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=ALL_BUT_LAST)
811856
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=True)
812857
finally:
813858
os_helper.unlink(ABSTFN)
@@ -822,22 +867,26 @@ def test_realpath_nonterminal_symlink_to_symlinks_to_file(self):
822867
os.symlink(ABSTFN + "2", ABSTFN + "1")
823868
os.symlink(ABSTFN + "1", ABSTFN)
824869
self.assertEqual(realpath(ABSTFN, strict=False), ABSTFN + "2")
870+
self.assertEqual(realpath(ABSTFN, strict=ALL_BUT_LAST), ABSTFN + "2")
825871
self.assertEqual(realpath(ABSTFN, strict=True), ABSTFN + "2")
826872
self.assertEqual(realpath(ABSTFN + "/", strict=False), ABSTFN + "2")
873+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/", strict=ALL_BUT_LAST)
827874
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/", strict=True)
828875
self.assertEqual(realpath(ABSTFN + "/.", strict=False), ABSTFN + "2")
876+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/.", strict=ALL_BUT_LAST)
829877
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/.", strict=True)
830878
self.assertEqual(realpath(ABSTFN + "/..", strict=False), dirname(ABSTFN))
879+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/..", strict=ALL_BUT_LAST)
831880
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/..", strict=True)
832881
self.assertEqual(realpath(ABSTFN + "/subdir", strict=False), ABSTFN + "2/subdir")
882+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=ALL_BUT_LAST)
833883
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=True)
834884
finally:
835885
os_helper.unlink(ABSTFN)
836886
os_helper.unlink(ABSTFN + "1")
837887
os_helper.unlink(ABSTFN + "2")
838888

839889
@os_helper.skip_unless_symlink
840-
# @skip_if_ABSTFN_contains_backslash
841890
def test_realpath_mode(self):
842891
self.addCleanup(os_helper.rmdir, ABSTFN)
843892
self.addCleanup(os_helper.rmdir, ABSTFN + "/dir")

0 commit comments

Comments
 (0)