Skip to content

Commit d9c0294

Browse files
committed
pythonGH-130614: pathlib ABCs: revise test suite for readable paths
Test `pathlib.types._ReadablePath` in a dedicated test module. These tests cover `ReadableZipPath`, `ReadableLocalPath` and `Path`, where the former two classes are implementations of `_ReadablePath` for use in tests.
1 parent 475f933 commit d9c0294

File tree

5 files changed

+768
-280
lines changed

5 files changed

+768
-280
lines changed
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
"""
2+
Implementation of ReadablePath for local paths, for use in pathlib tests.
3+
4+
LocalPathGround is also defined here. It helps establish the "ground truth"
5+
about local paths in tests.
6+
"""
7+
8+
import os
9+
import pathlib.types
10+
11+
from test.support import os_helper
12+
from test.test_pathlib.support.lexical_path import LexicalPath
13+
14+
15+
class LocalPathGround:
16+
can_symlink = os_helper.can_symlink()
17+
18+
def __init__(self, path_cls):
19+
self.path_cls = path_cls
20+
21+
def setup(self, local_suffix=""):
22+
root = self.path_cls(os_helper.TESTFN + local_suffix)
23+
os.mkdir(root)
24+
return root
25+
26+
def teardown(self, root):
27+
os_helper.rmtree(root)
28+
29+
def create_file(self, p, data=b''):
30+
with open(p, 'wb') as f:
31+
f.write(data)
32+
33+
def create_dir(self, p):
34+
os.mkdir(p)
35+
36+
def create_symlink(self, p, target):
37+
os.symlink(target, p)
38+
39+
def create_hierarchy(self, p):
40+
os.mkdir(os.path.join(p, 'dirA'))
41+
os.mkdir(os.path.join(p, 'dirB'))
42+
os.mkdir(os.path.join(p, 'dirC'))
43+
os.mkdir(os.path.join(p, 'dirC', 'dirD'))
44+
with open(os.path.join(p, 'fileA'), 'wb') as f:
45+
f.write(b"this is file A\n")
46+
with open(os.path.join(p, 'dirB', 'fileB'), 'wb') as f:
47+
f.write(b"this is file B\n")
48+
with open(os.path.join(p, 'dirC', 'fileC'), 'wb') as f:
49+
f.write(b"this is file C\n")
50+
with open(os.path.join(p, 'dirC', 'novel.txt'), 'wb') as f:
51+
f.write(b"this is a novel\n")
52+
with open(os.path.join(p, 'dirC', 'dirD', 'fileD'), 'wb') as f:
53+
f.write(b"this is file D\n")
54+
if self.can_symlink:
55+
# Relative symlinks.
56+
os.symlink('fileA', os.path.join(p, 'linkA'))
57+
os.symlink('non-existing', os.path.join(p, 'brokenLink'))
58+
os.symlink('dirB',
59+
os.path.join(p, 'linkB'),
60+
target_is_directory=True)
61+
os.symlink(os.path.join('..', 'dirB'),
62+
os.path.join(p, 'dirA', 'linkC'),
63+
target_is_directory=True)
64+
# Broken symlink (pointing to itself).
65+
os.symlink('brokenLinkLoop', os.path.join(p, 'brokenLinkLoop'))
66+
67+
isdir = staticmethod(os.path.isdir)
68+
isfile = staticmethod(os.path.isfile)
69+
islink = staticmethod(os.path.islink)
70+
readlink = staticmethod(os.readlink)
71+
72+
def readtext(self, p):
73+
with open(p, 'r') as f:
74+
return f.read()
75+
76+
def readbytes(self, p):
77+
with open(p, 'rb') as f:
78+
return f.read()
79+
80+
81+
class LocalPathInfo(pathlib.types.PathInfo):
82+
"""
83+
Simple implementation of PathInfo for a local path
84+
"""
85+
def __init__(self, path):
86+
self._path = str(path)
87+
self._exists = None
88+
self._is_dir = None
89+
self._is_file = None
90+
self._is_symlink = None
91+
92+
def exists(self, *, follow_symlinks=True):
93+
"""Whether this path exists."""
94+
if not follow_symlinks and self.is_symlink():
95+
return True
96+
if self._exists is None:
97+
self._exists = os.path.exists(self._path)
98+
return self._exists
99+
100+
def is_dir(self, *, follow_symlinks=True):
101+
"""Whether this path is a directory."""
102+
if not follow_symlinks and self.is_symlink():
103+
return False
104+
if self._is_dir is None:
105+
self._is_dir = os.path.isdir(self._path)
106+
return self._is_dir
107+
108+
def is_file(self, *, follow_symlinks=True):
109+
"""Whether this path is a regular file."""
110+
if not follow_symlinks and self.is_symlink():
111+
return False
112+
if self._is_file is None:
113+
self._is_file = os.path.isfile(self._path)
114+
return self._is_file
115+
116+
def is_symlink(self):
117+
"""Whether this path is a symbolic link."""
118+
if self._is_symlink is None:
119+
self._is_symlink = os.path.islink(self._path)
120+
return self._is_symlink
121+
122+
123+
class ReadableLocalPath(pathlib.types._ReadablePath, LexicalPath):
124+
"""
125+
Simple implementation of a ReadablePath class for local filesystem paths.
126+
"""
127+
__slots__ = ('_info')
128+
129+
def __init__(self, *pathsegments):
130+
super().__init__(*pathsegments)
131+
self._info = None
132+
133+
def __fspath__(self):
134+
return str(self)
135+
136+
def __open_rb__(self, buffering=-1):
137+
return open(self, 'rb')
138+
139+
def iterdir(self):
140+
return (self / name for name in os.listdir(self))
141+
142+
def readlink(self):
143+
return self.with_segments(os.readlink(self))
144+
145+
@property
146+
def info(self):
147+
if self._info is None:
148+
self._info = LocalPathInfo(self)
149+
return self._info

0 commit comments

Comments
 (0)