|
4 | 4 | import errno |
5 | 5 | import pathlib |
6 | 6 | import pickle |
7 | | -import shutil |
8 | 7 | import socket |
9 | 8 | import stat |
10 | | -import sys |
11 | 9 | import tempfile |
12 | 10 | import unittest |
13 | | -from contextlib import contextmanager |
14 | 11 |
|
15 | 12 | from test import support |
16 | 13 | TESTFN = support.TESTFN |
@@ -743,7 +740,6 @@ def test_eq(self): |
743 | 740 | self.assertEqual(P('//Some/SHARE/a/B'), P('//somE/share/A/b')) |
744 | 741 |
|
745 | 742 | def test_as_uri(self): |
746 | | - from urllib.parse import quote_from_bytes |
747 | 743 | P = self.cls |
748 | 744 | with self.assertRaises(ValueError): |
749 | 745 | P('/a/b').as_uri() |
@@ -1617,6 +1613,59 @@ def test_mkdir_parents(self): |
1617 | 1613 | # the parent's permissions follow the default process settings |
1618 | 1614 | self.assertEqual(stat.S_IMODE(p.parent.stat().st_mode), mode) |
1619 | 1615 |
|
| 1616 | + def test_mkdir_exist_ok(self): |
| 1617 | + p = self.cls(BASE, 'dirB') |
| 1618 | + st_ctime_first = p.stat().st_ctime |
| 1619 | + self.assertTrue(p.exists()) |
| 1620 | + self.assertTrue(p.is_dir()) |
| 1621 | + with self.assertRaises(FileExistsError) as cm: |
| 1622 | + p.mkdir() |
| 1623 | + self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1624 | + p.mkdir(exist_ok=True) |
| 1625 | + self.assertTrue(p.exists()) |
| 1626 | + self.assertEqual(p.stat().st_ctime, st_ctime_first) |
| 1627 | + |
| 1628 | + def test_mkdir_exist_ok_with_parent(self): |
| 1629 | + p = self.cls(BASE, 'dirC') |
| 1630 | + self.assertTrue(p.exists()) |
| 1631 | + with self.assertRaises(FileExistsError) as cm: |
| 1632 | + p.mkdir() |
| 1633 | + self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1634 | + p = p / 'newdirC' |
| 1635 | + p.mkdir(parents=True) |
| 1636 | + st_ctime_first = p.stat().st_ctime |
| 1637 | + self.assertTrue(p.exists()) |
| 1638 | + with self.assertRaises(FileExistsError) as cm: |
| 1639 | + p.mkdir(parents=True) |
| 1640 | + self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1641 | + p.mkdir(parents=True, exist_ok=True) |
| 1642 | + self.assertTrue(p.exists()) |
| 1643 | + self.assertEqual(p.stat().st_ctime, st_ctime_first) |
| 1644 | + |
| 1645 | + def test_mkdir_with_child_file(self): |
| 1646 | + p = self.cls(BASE, 'dirB', 'fileB') |
| 1647 | + self.assertTrue(p.exists()) |
| 1648 | + # An exception is raised when the last path component is an existing |
| 1649 | + # regular file, regardless of whether exist_ok is true or not. |
| 1650 | + with self.assertRaises(FileExistsError) as cm: |
| 1651 | + p.mkdir(parents=True) |
| 1652 | + self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1653 | + with self.assertRaises(FileExistsError) as cm: |
| 1654 | + p.mkdir(parents=True, exist_ok=True) |
| 1655 | + self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1656 | + |
| 1657 | + def test_mkdir_no_parents_file(self): |
| 1658 | + p = self.cls(BASE, 'fileA') |
| 1659 | + self.assertTrue(p.exists()) |
| 1660 | + # An exception is raised when the last path component is an existing |
| 1661 | + # regular file, regardless of whether exist_ok is true or not. |
| 1662 | + with self.assertRaises(FileExistsError) as cm: |
| 1663 | + p.mkdir() |
| 1664 | + self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1665 | + with self.assertRaises(FileExistsError) as cm: |
| 1666 | + p.mkdir(exist_ok=True) |
| 1667 | + self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 1668 | + |
1620 | 1669 | @with_symlinks |
1621 | 1670 | def test_symlink_to(self): |
1622 | 1671 | P = self.cls(BASE) |
@@ -1852,7 +1901,6 @@ def test_touch_mode(self): |
1852 | 1901 | @with_symlinks |
1853 | 1902 | def test_resolve_loop(self): |
1854 | 1903 | # Loop detection for broken symlinks under POSIX |
1855 | | - P = self.cls |
1856 | 1904 | # Loops with relative symlinks |
1857 | 1905 | os.symlink('linkX/inside', join('linkX')) |
1858 | 1906 | self._check_symlink_loop(BASE, 'linkX') |
|
0 commit comments