mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
pathlib tests: create walk()
test hierarchy without using class under test (#128338)
In the tests for `pathlib.Path.walk()`, avoid using the path class under test (`self.cls`) in test setup. Instead we use `os` functions in `test_pathlib`, and direct manipulation of `DummyPath` internal data in `test_pathlib_abc`.
This commit is contained in:
parent
a0088b40bb
commit
fd94c6a803
2 changed files with 58 additions and 39 deletions
|
@ -3029,6 +3029,42 @@ class PathWalkTest(test_pathlib_abc.DummyPathWalkTest):
|
||||||
if name in _tests_needing_symlinks and not self.can_symlink:
|
if name in _tests_needing_symlinks and not self.can_symlink:
|
||||||
self.skipTest('requires symlinks')
|
self.skipTest('requires symlinks')
|
||||||
super().setUp()
|
super().setUp()
|
||||||
|
|
||||||
|
def createTestHierarchy(self):
|
||||||
|
# Build:
|
||||||
|
# TESTFN/
|
||||||
|
# TEST1/ a file kid and two directory kids
|
||||||
|
# tmp1
|
||||||
|
# SUB1/ a file kid and a directory kid
|
||||||
|
# tmp2
|
||||||
|
# SUB11/ no kids
|
||||||
|
# SUB2/ a file kid and a dirsymlink kid
|
||||||
|
# tmp3
|
||||||
|
# link/ a symlink to TEST2
|
||||||
|
# broken_link
|
||||||
|
# broken_link2
|
||||||
|
# TEST2/
|
||||||
|
# tmp4 a lone file
|
||||||
|
t2_path = self.cls(self.base, "TEST2")
|
||||||
|
os.makedirs(self.sub11_path)
|
||||||
|
os.makedirs(self.sub2_path)
|
||||||
|
os.makedirs(t2_path)
|
||||||
|
|
||||||
|
tmp1_path = self.walk_path / "tmp1"
|
||||||
|
tmp2_path = self.sub1_path / "tmp2"
|
||||||
|
tmp3_path = self.sub2_path / "tmp3"
|
||||||
|
tmp4_path = self.cls(self.base, "TEST2", "tmp4")
|
||||||
|
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
|
||||||
|
with open(path, "w", encoding='utf-8') as f:
|
||||||
|
f.write(f"I'm {path} and proud of it. Blame test_pathlib.\n")
|
||||||
|
|
||||||
|
if self.can_symlink:
|
||||||
|
broken_link_path = self.sub2_path / "broken_link"
|
||||||
|
broken_link2_path = self.sub2_path / "broken_link2"
|
||||||
|
os.symlink(t2_path, self.link_path, target_is_directory=True)
|
||||||
|
os.symlink('broken', broken_link_path)
|
||||||
|
os.symlink(os.path.join('tmp3', 'broken'), broken_link2_path)
|
||||||
|
self.sub2_tree = (self.sub2_path, [], ["broken_link", "broken_link2", "link", "tmp3"])
|
||||||
sub21_path= self.sub2_path / "SUB21"
|
sub21_path= self.sub2_path / "SUB21"
|
||||||
tmp5_path = sub21_path / "tmp3"
|
tmp5_path = sub21_path / "tmp3"
|
||||||
broken_link3_path = self.sub2_path / "broken_link3"
|
broken_link3_path = self.sub2_path / "broken_link3"
|
||||||
|
@ -3052,7 +3088,7 @@ class PathWalkTest(test_pathlib_abc.DummyPathWalkTest):
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
if 'SUB21' in self.sub2_tree[1]:
|
if 'SUB21' in self.sub2_tree[1]:
|
||||||
os.chmod(self.sub2_path / "SUB21", stat.S_IRWXU)
|
os.chmod(self.sub2_path / "SUB21", stat.S_IRWXU)
|
||||||
super().tearDown()
|
os_helper.rmtree(self.base)
|
||||||
|
|
||||||
def test_walk_bad_dir(self):
|
def test_walk_bad_dir(self):
|
||||||
errors = []
|
errors = []
|
||||||
|
|
|
@ -1580,52 +1580,35 @@ class DummyPathWalkTest(unittest.TestCase):
|
||||||
can_symlink = False
|
can_symlink = False
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# Build:
|
|
||||||
# TESTFN/
|
|
||||||
# TEST1/ a file kid and two directory kids
|
|
||||||
# tmp1
|
|
||||||
# SUB1/ a file kid and a directory kid
|
|
||||||
# tmp2
|
|
||||||
# SUB11/ no kids
|
|
||||||
# SUB2/ a file kid and a dirsymlink kid
|
|
||||||
# tmp3
|
|
||||||
# link/ a symlink to TEST2
|
|
||||||
# broken_link
|
|
||||||
# broken_link2
|
|
||||||
# TEST2/
|
|
||||||
# tmp4 a lone file
|
|
||||||
self.walk_path = self.cls(self.base, "TEST1")
|
self.walk_path = self.cls(self.base, "TEST1")
|
||||||
self.sub1_path = self.walk_path / "SUB1"
|
self.sub1_path = self.walk_path / "SUB1"
|
||||||
self.sub11_path = self.sub1_path / "SUB11"
|
self.sub11_path = self.sub1_path / "SUB11"
|
||||||
self.sub2_path = self.walk_path / "SUB2"
|
self.sub2_path = self.walk_path / "SUB2"
|
||||||
tmp1_path = self.walk_path / "tmp1"
|
|
||||||
tmp2_path = self.sub1_path / "tmp2"
|
|
||||||
tmp3_path = self.sub2_path / "tmp3"
|
|
||||||
self.link_path = self.sub2_path / "link"
|
self.link_path = self.sub2_path / "link"
|
||||||
t2_path = self.cls(self.base, "TEST2")
|
self.sub2_tree = (self.sub2_path, [], ["tmp3"])
|
||||||
tmp4_path = self.cls(self.base, "TEST2", "tmp4")
|
self.createTestHierarchy()
|
||||||
broken_link_path = self.sub2_path / "broken_link"
|
|
||||||
broken_link2_path = self.sub2_path / "broken_link2"
|
|
||||||
|
|
||||||
self.sub11_path.mkdir(parents=True)
|
def createTestHierarchy(self):
|
||||||
self.sub2_path.mkdir(parents=True)
|
cls = self.cls
|
||||||
t2_path.mkdir(parents=True)
|
cls._files = {
|
||||||
|
f'{self.base}/TEST1/tmp1': b'this is tmp1\n',
|
||||||
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
|
f'{self.base}/TEST1/SUB1/tmp2': b'this is tmp2\n',
|
||||||
with path.open("w", encoding='utf-8') as f:
|
f'{self.base}/TEST1/SUB2/tmp3': b'this is tmp3\n',
|
||||||
f.write(f"I'm {path} and proud of it. Blame test_pathlib.\n")
|
f'{self.base}/TEST2/tmp4': b'this is tmp4\n',
|
||||||
|
}
|
||||||
if self.can_symlink:
|
cls._directories = {
|
||||||
self.link_path.symlink_to(t2_path, target_is_directory=True)
|
f'{self.base}': {'TEST1', 'TEST2'},
|
||||||
broken_link_path.symlink_to('broken')
|
f'{self.base}/TEST1': {'SUB1', 'SUB2', 'tmp1'},
|
||||||
broken_link2_path.symlink_to(self.cls('tmp3', 'broken'))
|
f'{self.base}/TEST1/SUB1': {'SUB11', 'tmp2'},
|
||||||
self.sub2_tree = (self.sub2_path, [], ["broken_link", "broken_link2", "link", "tmp3"])
|
f'{self.base}/TEST1/SUB1/SUB11': set(),
|
||||||
else:
|
f'{self.base}/TEST1/SUB2': {'tmp3'},
|
||||||
self.sub2_tree = (self.sub2_path, [], ["tmp3"])
|
f'{self.base}/TEST2': {'tmp4'},
|
||||||
|
}
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
base = self.cls(self.base)
|
cls = self.cls
|
||||||
base._delete()
|
cls._files.clear()
|
||||||
|
cls._directories.clear()
|
||||||
|
|
||||||
def test_walk_topdown(self):
|
def test_walk_topdown(self):
|
||||||
walker = self.walk_path.walk()
|
walker = self.walk_path.walk()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue