Skip to content

Commit 957e1bb

Browse files
committed
pythonGH-110109: Simplify pathlib test setup
Use the class under test to create files, directories and symlinks.
1 parent 89966a6 commit 957e1bb

1 file changed

Lines changed: 34 additions & 71 deletions

File tree

‎Lib/test/test_pathlib.py‎

Lines changed: 34 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,7 @@ class DummyPath(pathlib._PathBase):
16731673
memory.
16741674
"""
16751675
_files = {}
1676-
_directories = {}
1676+
_directories = {'/': set()}
16771677
_symlinks = {}
16781678

16791679
def stat(self, *, follow_symlinks=True):
@@ -1770,33 +1770,38 @@ class DummyPathTest(unittest.TestCase):
17701770
#
17711771

17721772
def setUp(self):
1773-
# note: this must be kept in sync with `PathTest.setUp()`
1773+
pathmod = self.cls.pathmod
1774+
p = self.cls(BASE)
1775+
p.mkdir(parents=True)
1776+
p.joinpath('dirA').mkdir()
1777+
p.joinpath('dirB').mkdir()
1778+
p.joinpath('dirC').mkdir()
1779+
p.joinpath('dirC', 'dirD').mkdir()
1780+
p.joinpath('dirE').mkdir()
1781+
with p.joinpath('fileA').open('wb') as f:
1782+
f.write(b"this is file A\n")
1783+
with p.joinpath('dirB', 'fileB').open('wb') as f:
1784+
f.write(b"this is file B\n")
1785+
with p.joinpath('dirC', 'fileC').open('wb') as f:
1786+
f.write(b"this is file C\n")
1787+
with p.joinpath('dirC', 'novel.txt').open('wb') as f:
1788+
f.write(b"this is a novel\n")
1789+
with p.joinpath('dirC', 'dirD', 'fileD').open('wb') as f:
1790+
f.write(b"this is file D\n")
1791+
if self.can_symlink:
1792+
p.joinpath('linkA').symlink_to('fileA')
1793+
p.joinpath('brokenLink').symlink_to('non-existing')
1794+
p.joinpath('linkB').symlink_to('dirB')
1795+
p.joinpath('dirA', 'linkC').symlink_to(pathmod.join('..', 'dirB'))
1796+
p.joinpath('dirB', 'linkD').symlink_to(pathmod.join('..', 'dirB'))
1797+
p.joinpath('brokenLinkLoop').symlink_to('brokenLinkLoop')
1798+
1799+
def tearDown(self):
17741800
cls = self.cls
17751801
cls._files.clear()
17761802
cls._directories.clear()
17771803
cls._symlinks.clear()
1778-
join = cls.pathmod.join
1779-
cls._files.update({
1780-
join(BASE, 'fileA'): b'this is file A\n',
1781-
join(BASE, 'dirB', 'fileB'): b'this is file B\n',
1782-
join(BASE, 'dirC', 'fileC'): b'this is file C\n',
1783-
join(BASE, 'dirC', 'dirD', 'fileD'): b'this is file D\n',
1784-
join(BASE, 'dirC', 'novel.txt'): b'this is a novel\n',
1785-
})
1786-
cls._directories.update({
1787-
BASE: {'dirA', 'dirB', 'dirC', 'dirE', 'fileA'},
1788-
join(BASE, 'dirA'): set(),
1789-
join(BASE, 'dirB'): {'fileB'},
1790-
join(BASE, 'dirC'): {'dirD', 'fileC', 'novel.txt'},
1791-
join(BASE, 'dirC', 'dirD'): {'fileD'},
1792-
join(BASE, 'dirE'): {},
1793-
})
1794-
dirname = BASE
1795-
while True:
1796-
dirname, basename = cls.pathmod.split(dirname)
1797-
if not basename:
1798-
break
1799-
cls._directories[dirname] = {basename}
1804+
cls._directories['/'] = set()
18001805

18011806
def tempdir(self):
18021807
path = self.cls(BASE).with_name('tmp-dirD')
@@ -2625,22 +2630,6 @@ class DummyPathWithSymlinksTest(DummyPathTest):
26252630
cls = DummyPathWithSymlinks
26262631
can_symlink = True
26272632

2628-
def setUp(self):
2629-
super().setUp()
2630-
cls = self.cls
2631-
join = cls.pathmod.join
2632-
cls._symlinks.update({
2633-
join(BASE, 'linkA'): 'fileA',
2634-
join(BASE, 'linkB'): 'dirB',
2635-
join(BASE, 'dirA', 'linkC'): join('..', 'dirB'),
2636-
join(BASE, 'dirB', 'linkD'): join('..', 'dirB'),
2637-
join(BASE, 'brokenLink'): 'non-existing',
2638-
join(BASE, 'brokenLinkLoop'): 'brokenLinkLoop',
2639-
})
2640-
cls._directories[BASE].update({'linkA', 'linkB', 'brokenLink', 'brokenLinkLoop'})
2641-
cls._directories[join(BASE, 'dirA')].add('linkC')
2642-
cls._directories[join(BASE, 'dirB')].add('linkD')
2643-
26442633

26452634
#
26462635
# Tests for the concrete classes.
@@ -2652,38 +2641,12 @@ class PathTest(DummyPathTest):
26522641
can_symlink = os_helper.can_symlink()
26532642

26542643
def setUp(self):
2655-
# note: this must be kept in sync with `DummyPathTest.setUp()`
2656-
def cleanup():
2657-
os.chmod(join('dirE'), 0o777)
2658-
os_helper.rmtree(BASE)
2659-
self.addCleanup(cleanup)
2660-
os.mkdir(BASE)
2661-
os.mkdir(join('dirA'))
2662-
os.mkdir(join('dirB'))
2663-
os.mkdir(join('dirC'))
2664-
os.mkdir(join('dirC', 'dirD'))
2665-
os.mkdir(join('dirE'))
2666-
with open(join('fileA'), 'wb') as f:
2667-
f.write(b"this is file A\n")
2668-
with open(join('dirB', 'fileB'), 'wb') as f:
2669-
f.write(b"this is file B\n")
2670-
with open(join('dirC', 'fileC'), 'wb') as f:
2671-
f.write(b"this is file C\n")
2672-
with open(join('dirC', 'novel.txt'), 'wb') as f:
2673-
f.write(b"this is a novel\n")
2674-
with open(join('dirC', 'dirD', 'fileD'), 'wb') as f:
2675-
f.write(b"this is file D\n")
2644+
super().setUp()
26762645
os.chmod(join('dirE'), 0)
2677-
if self.can_symlink:
2678-
# Relative symlinks.
2679-
os.symlink('fileA', join('linkA'))
2680-
os.symlink('non-existing', join('brokenLink'))
2681-
os.symlink('dirB', join('linkB'), target_is_directory=True)
2682-
os.symlink(os.path.join('..', 'dirB'), join('dirA', 'linkC'), target_is_directory=True)
2683-
# This one goes upwards, creating a loop.
2684-
os.symlink(os.path.join('..', 'dirB'), join('dirB', 'linkD'), target_is_directory=True)
2685-
# Broken symlink (pointing to itself).
2686-
os.symlink('brokenLinkLoop', join('brokenLinkLoop'))
2646+
2647+
def tearDown(self):
2648+
os.chmod(join('dirE'), 0o777)
2649+
os_helper.rmtree(BASE)
26872650

26882651
def tempdir(self):
26892652
d = os_helper._longpath(tempfile.mkdtemp(suffix='-dirD',

0 commit comments

Comments
 (0)