|
| 1 | +import errno |
1 | 2 | import os |
2 | 3 | import posixpath |
3 | 4 | import sys |
4 | 5 | import unittest |
5 | | -from posixpath import realpath, abspath, dirname, basename |
| 6 | +from posixpath import realpath, abspath, dirname, basename, ALL_BUT_LAST |
6 | 7 | from test import test_genericpath |
7 | 8 | from test.support import import_helper |
8 | 9 | from test.support import os_helper |
@@ -475,7 +476,7 @@ def test_realpath_symlink_loops(self): |
475 | 476 | self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x") |
476 | 477 | os.symlink(ABSTFN+"x", ABSTFN+"y") |
477 | 478 | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"), |
478 | | - ABSTFN + "y") |
| 479 | + ABSTFN + "x") |
479 | 480 | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"), |
480 | 481 | ABSTFN + "1") |
481 | 482 |
|
@@ -637,6 +638,129 @@ def test_realpath_resolve_first(self): |
637 | 638 | safe_rmdir(ABSTFN + "/k") |
638 | 639 | safe_rmdir(ABSTFN) |
639 | 640 |
|
| 641 | + @os_helper.skip_unless_symlink |
| 642 | + @skip_if_ABSTFN_contains_backslash |
| 643 | + def test_realpath_mode(self): |
| 644 | + try: |
| 645 | + os.mkdir(ABSTFN) |
| 646 | + os.mkdir(ABSTFN + "/dir") |
| 647 | + open(ABSTFN + "/file", "wb").close() |
| 648 | + open(ABSTFN + "/dir/file2", "wb").close() |
| 649 | + os.symlink("file", ABSTFN + "/link") |
| 650 | + os.symlink("dir", ABSTFN + "/link2") |
| 651 | + os.symlink("nonexisting", ABSTFN + "/broken") |
| 652 | + os.symlink("cycle", ABSTFN + "/cycle") |
| 653 | + def check(path, mode, expected, errno=None): |
| 654 | + if isinstance(expected, str): |
| 655 | + assert errno is None |
| 656 | + self.assertEqual(realpath(path, strict=mode), ABSTFN + expected) |
| 657 | + else: |
| 658 | + with self.assertRaises(expected) as cm: |
| 659 | + realpath(path, strict=mode) |
| 660 | + if errno is not None: |
| 661 | + self.assertEqual(cm.exception.errno, errno) |
| 662 | + |
| 663 | + with os_helper.change_cwd(ABSTFN): |
| 664 | + check("file", False, "/file") |
| 665 | + check("file", ALL_BUT_LAST, "/file") |
| 666 | + check("file", True, "/file") |
| 667 | + check("file/", False, "/file") |
| 668 | + check("file/", ALL_BUT_LAST, NotADirectoryError) |
| 669 | + check("file/", True, NotADirectoryError) |
| 670 | + check("file/file2", False, "/file/file2") |
| 671 | + check("file/file2", ALL_BUT_LAST, NotADirectoryError) |
| 672 | + check("file/file2", True, NotADirectoryError) |
| 673 | + check("file/.", False, "/file") |
| 674 | + check("file/.", ALL_BUT_LAST, NotADirectoryError) |
| 675 | + check("file/.", True, NotADirectoryError) |
| 676 | + check("file/../link", False, "/file") |
| 677 | + check("file/../link", ALL_BUT_LAST, NotADirectoryError) |
| 678 | + check("file/../link", True, NotADirectoryError) |
| 679 | + |
| 680 | + check("dir", False, "/dir") |
| 681 | + check("dir", ALL_BUT_LAST, "/dir") |
| 682 | + check("dir", True, "/dir") |
| 683 | + check("dir/", False, "/dir") |
| 684 | + check("dir/", ALL_BUT_LAST, "/dir") |
| 685 | + check("dir/", True, "/dir") |
| 686 | + check("dir/file2", False, "/dir/file2") |
| 687 | + check("dir/file2", ALL_BUT_LAST, "/dir/file2") |
| 688 | + check("dir/file2", True, "/dir/file2") |
| 689 | + |
| 690 | + check("link", False, "/file") |
| 691 | + check("link", ALL_BUT_LAST, "/file") |
| 692 | + check("link", True, "/file") |
| 693 | + check("link/", False, "/file") |
| 694 | + check("link/", ALL_BUT_LAST, NotADirectoryError) |
| 695 | + check("link/", True, NotADirectoryError) |
| 696 | + check("link/file2", False, "/file/file2") |
| 697 | + check("link/file2", ALL_BUT_LAST, NotADirectoryError) |
| 698 | + check("link/file2", True, NotADirectoryError) |
| 699 | + check("link/.", False, "/file") |
| 700 | + check("link/.", ALL_BUT_LAST, NotADirectoryError) |
| 701 | + check("link/.", True, NotADirectoryError) |
| 702 | + check("link/../link", False, "/file") |
| 703 | + check("link/../link", ALL_BUT_LAST, NotADirectoryError) |
| 704 | + check("link/../link", True, NotADirectoryError) |
| 705 | + |
| 706 | + check("link2", False, "/dir") |
| 707 | + check("link2", ALL_BUT_LAST, "/dir") |
| 708 | + check("link2", True, "/dir") |
| 709 | + check("link2/", False, "/dir") |
| 710 | + check("link2/", ALL_BUT_LAST, "/dir") |
| 711 | + check("link2/", True, "/dir") |
| 712 | + check("link2/file2", False, "/dir/file2") |
| 713 | + check("link2/file2", ALL_BUT_LAST, "/dir/file2") |
| 714 | + check("link2/file2", True, "/dir/file2") |
| 715 | + |
| 716 | + check("nonexisting", False, "/nonexisting") |
| 717 | + check("nonexisting", ALL_BUT_LAST, "/nonexisting") |
| 718 | + check("nonexisting", True, FileNotFoundError) |
| 719 | + check("nonexisting/", False, "/nonexisting") |
| 720 | + check("nonexisting/", ALL_BUT_LAST, "/nonexisting") |
| 721 | + check("nonexisting/", True, FileNotFoundError) |
| 722 | + check("nonexisting/file", False, "/nonexisting/file") |
| 723 | + check("nonexisting/file", ALL_BUT_LAST, FileNotFoundError) |
| 724 | + check("nonexisting/file", True, FileNotFoundError) |
| 725 | + check("nonexisting/../link", False, "/file") |
| 726 | + check("nonexisting/../link", ALL_BUT_LAST, FileNotFoundError) |
| 727 | + check("nonexisting/../link", True, FileNotFoundError) |
| 728 | + |
| 729 | + check("broken", False, "/nonexisting") |
| 730 | + check("broken", ALL_BUT_LAST, "/nonexisting") |
| 731 | + check("broken", True, FileNotFoundError) |
| 732 | + check("broken/", False, "/nonexisting") |
| 733 | + check("broken/", ALL_BUT_LAST, "/nonexisting") |
| 734 | + check("broken/", True, FileNotFoundError) |
| 735 | + check("broken/file", False, "/nonexisting/file") |
| 736 | + check("broken/file", ALL_BUT_LAST, FileNotFoundError) |
| 737 | + check("broken/file", True, FileNotFoundError) |
| 738 | + check("broken/../link", False, "/file") |
| 739 | + check("broken/../link", ALL_BUT_LAST, FileNotFoundError) |
| 740 | + check("broken/../link", True, FileNotFoundError) |
| 741 | + |
| 742 | + check("cycle", False, "/cycle") |
| 743 | + check("cycle", ALL_BUT_LAST, OSError, errno.ELOOP) |
| 744 | + check("cycle", True, OSError, errno.ELOOP) |
| 745 | + check("cycle/", False, "/cycle") |
| 746 | + check("cycle/", ALL_BUT_LAST, OSError, errno.ELOOP) |
| 747 | + check("cycle/", True, OSError, errno.ELOOP) |
| 748 | + check("cycle/file", False, "/cycle/file") |
| 749 | + check("cycle/file", ALL_BUT_LAST, OSError, errno.ELOOP) |
| 750 | + check("cycle/file", True, OSError, errno.ELOOP) |
| 751 | + check("cycle/../link", False, "/file") |
| 752 | + check("cycle/../link", ALL_BUT_LAST, OSError, errno.ELOOP) |
| 753 | + check("cycle/../link", True, OSError, errno.ELOOP) |
| 754 | + finally: |
| 755 | + os_helper.unlink(ABSTFN + "/file") |
| 756 | + os_helper.unlink(ABSTFN + "/dir/file2") |
| 757 | + os_helper.unlink(ABSTFN + "/link") |
| 758 | + os_helper.unlink(ABSTFN + "/link2") |
| 759 | + os_helper.unlink(ABSTFN + "/broken") |
| 760 | + os_helper.unlink(ABSTFN + "/cycle") |
| 761 | + os_helper.rmdir(ABSTFN + "/dir") |
| 762 | + os_helper.rmdir(ABSTFN) |
| 763 | + |
640 | 764 | def test_relpath(self): |
641 | 765 | (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") |
642 | 766 | try: |
|
0 commit comments