|
2 | 2 | import io
|
3 | 3 | import os
|
4 | 4 | import errno
|
5 |
| -import stat |
6 | 5 | import unittest
|
7 | 6 |
|
8 | 7 | from pathlib._abc import PurePathBase, PathBase
|
@@ -1294,11 +1293,6 @@ def close(self):
|
1294 | 1293 | super().close()
|
1295 | 1294 |
|
1296 | 1295 |
|
1297 |
| -DummyPathStatResult = collections.namedtuple( |
1298 |
| - 'DummyPathStatResult', |
1299 |
| - 'st_mode st_ino st_dev st_nlink st_uid st_gid st_size st_atime st_mtime st_ctime') |
1300 |
| - |
1301 |
| - |
1302 | 1296 | class DummyPath(PathBase):
|
1303 | 1297 | """
|
1304 | 1298 | Simple implementation of PathBase that keeps files and directories in
|
@@ -1331,15 +1325,17 @@ def __repr__(self):
|
1331 | 1325 | def with_segments(self, *pathsegments):
|
1332 | 1326 | return type(self)(*pathsegments)
|
1333 | 1327 |
|
1334 |
| - def stat(self, *, follow_symlinks=True): |
1335 |
| - path = str(self).rstrip('/') |
1336 |
| - if path in self._files: |
1337 |
| - st_mode = stat.S_IFREG |
1338 |
| - elif path in self._directories: |
1339 |
| - st_mode = stat.S_IFDIR |
1340 |
| - else: |
1341 |
| - raise FileNotFoundError(errno.ENOENT, "Not found", str(self)) |
1342 |
| - return DummyPathStatResult(st_mode, hash(str(self)), 0, 0, 0, 0, 0, 0, 0, 0) |
| 1328 | + def exists(self, *, follow_symlinks=True): |
| 1329 | + return self.is_dir() or self.is_file() |
| 1330 | + |
| 1331 | + def is_dir(self, *, follow_symlinks=True): |
| 1332 | + return str(self).rstrip('/') in self._directories |
| 1333 | + |
| 1334 | + def is_file(self, *, follow_symlinks=True): |
| 1335 | + return str(self) in self._files |
| 1336 | + |
| 1337 | + def is_symlink(self): |
| 1338 | + return False |
1343 | 1339 |
|
1344 | 1340 | def open(self, mode='r', buffering=-1, encoding=None,
|
1345 | 1341 | errors=None, newline=None):
|
@@ -1958,31 +1954,6 @@ def test_rglob_windows(self):
|
1958 | 1954 | self.assertEqual(set(p.rglob("FILEd")), { P(self.base, "dirC/dirD/fileD") })
|
1959 | 1955 | self.assertEqual(set(p.rglob("*\\")), { P(self.base, "dirC/dirD/") })
|
1960 | 1956 |
|
1961 |
| - def test_stat(self): |
1962 |
| - statA = self.cls(self.base).joinpath('fileA').stat() |
1963 |
| - statB = self.cls(self.base).joinpath('dirB', 'fileB').stat() |
1964 |
| - statC = self.cls(self.base).joinpath('dirC').stat() |
1965 |
| - # st_mode: files are the same, directory differs. |
1966 |
| - self.assertIsInstance(statA.st_mode, int) |
1967 |
| - self.assertEqual(statA.st_mode, statB.st_mode) |
1968 |
| - self.assertNotEqual(statA.st_mode, statC.st_mode) |
1969 |
| - self.assertNotEqual(statB.st_mode, statC.st_mode) |
1970 |
| - # st_ino: all different, |
1971 |
| - self.assertIsInstance(statA.st_ino, int) |
1972 |
| - self.assertNotEqual(statA.st_ino, statB.st_ino) |
1973 |
| - self.assertNotEqual(statA.st_ino, statC.st_ino) |
1974 |
| - self.assertNotEqual(statB.st_ino, statC.st_ino) |
1975 |
| - # st_dev: all the same. |
1976 |
| - self.assertIsInstance(statA.st_dev, int) |
1977 |
| - self.assertEqual(statA.st_dev, statB.st_dev) |
1978 |
| - self.assertEqual(statA.st_dev, statC.st_dev) |
1979 |
| - # other attributes not used by pathlib. |
1980 |
| - |
1981 |
| - def test_stat_no_follow_symlinks_nosymlink(self): |
1982 |
| - p = self.cls(self.base) / 'fileA' |
1983 |
| - st = p.stat() |
1984 |
| - self.assertEqual(st, p.stat(follow_symlinks=False)) |
1985 |
| - |
1986 | 1957 | def test_is_dir(self):
|
1987 | 1958 | P = self.cls(self.base)
|
1988 | 1959 | self.assertTrue((P / 'dirA').is_dir())
|
@@ -2054,26 +2025,26 @@ def test_is_symlink(self):
|
2054 | 2025 | def test_delete_file(self):
|
2055 | 2026 | p = self.cls(self.base) / 'fileA'
|
2056 | 2027 | p._delete()
|
2057 |
| - self.assertFileNotFound(p.stat) |
| 2028 | + self.assertFalse(p.exists()) |
2058 | 2029 | self.assertFileNotFound(p._delete)
|
2059 | 2030 |
|
2060 | 2031 | def test_delete_dir(self):
|
2061 | 2032 | base = self.cls(self.base)
|
2062 | 2033 | base.joinpath('dirA')._delete()
|
2063 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirA').stat) |
2064 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirA', 'linkC').stat, |
2065 |
| - follow_symlinks=False) |
| 2034 | + self.assertFalse(base.joinpath('dirA').exists()) |
| 2035 | + self.assertFalse(base.joinpath('dirA', 'linkC').exists( |
| 2036 | + follow_symlinks=False)) |
2066 | 2037 | base.joinpath('dirB')._delete()
|
2067 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirB').stat) |
2068 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'fileB').stat) |
2069 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirB', 'linkD').stat, |
2070 |
| - follow_symlinks=False) |
| 2038 | + self.assertFalse(base.joinpath('dirB').exists()) |
| 2039 | + self.assertFalse(base.joinpath('dirB', 'fileB').exists()) |
| 2040 | + self.assertFalse(base.joinpath('dirB', 'linkD').exists( |
| 2041 | + follow_symlinks=False)) |
2071 | 2042 | base.joinpath('dirC')._delete()
|
2072 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirC').stat) |
2073 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'dirD').stat) |
2074 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'dirD', 'fileD').stat) |
2075 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'fileC').stat) |
2076 |
| - self.assertRaises(FileNotFoundError, base.joinpath('dirC', 'novel.txt').stat) |
| 2043 | + self.assertFalse(base.joinpath('dirC').exists()) |
| 2044 | + self.assertFalse(base.joinpath('dirC', 'dirD').exists()) |
| 2045 | + self.assertFalse(base.joinpath('dirC', 'dirD', 'fileD').exists()) |
| 2046 | + self.assertFalse(base.joinpath('dirC', 'fileC').exists()) |
| 2047 | + self.assertFalse(base.joinpath('dirC', 'novel.txt').exists()) |
2077 | 2048 |
|
2078 | 2049 | def test_delete_missing(self):
|
2079 | 2050 | tmp = self.cls(self.base, 'delete')
|
|
0 commit comments