Skip to content

Commit

Permalink
pythonGH-110109: Simplify pathlib test setup
Browse files Browse the repository at this point in the history
Use the class under test to create files, directories and symlinks.
  • Loading branch information
barneygale committed Oct 3, 2023
1 parent 89966a6 commit 957e1bb
Showing 1 changed file with 34 additions and 71 deletions.
105 changes: 34 additions & 71 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,7 +1673,7 @@ class DummyPath(pathlib._PathBase):
memory.
"""
_files = {}
_directories = {}
_directories = {'/': set()}
_symlinks = {}

def stat(self, *, follow_symlinks=True):
Expand Down Expand Up @@ -1770,33 +1770,38 @@ class DummyPathTest(unittest.TestCase):
#

def setUp(self):
# note: this must be kept in sync with `PathTest.setUp()`
pathmod = self.cls.pathmod
p = self.cls(BASE)
p.mkdir(parents=True)
p.joinpath('dirA').mkdir()
p.joinpath('dirB').mkdir()
p.joinpath('dirC').mkdir()
p.joinpath('dirC', 'dirD').mkdir()
p.joinpath('dirE').mkdir()
with p.joinpath('fileA').open('wb') as f:
f.write(b"this is file A\n")
with p.joinpath('dirB', 'fileB').open('wb') as f:
f.write(b"this is file B\n")
with p.joinpath('dirC', 'fileC').open('wb') as f:
f.write(b"this is file C\n")
with p.joinpath('dirC', 'novel.txt').open('wb') as f:
f.write(b"this is a novel\n")
with p.joinpath('dirC', 'dirD', 'fileD').open('wb') as f:
f.write(b"this is file D\n")
if self.can_symlink:
p.joinpath('linkA').symlink_to('fileA')
p.joinpath('brokenLink').symlink_to('non-existing')
p.joinpath('linkB').symlink_to('dirB')
p.joinpath('dirA', 'linkC').symlink_to(pathmod.join('..', 'dirB'))
p.joinpath('dirB', 'linkD').symlink_to(pathmod.join('..', 'dirB'))
p.joinpath('brokenLinkLoop').symlink_to('brokenLinkLoop')

def tearDown(self):
cls = self.cls
cls._files.clear()
cls._directories.clear()
cls._symlinks.clear()
join = cls.pathmod.join
cls._files.update({
join(BASE, 'fileA'): b'this is file A\n',
join(BASE, 'dirB', 'fileB'): b'this is file B\n',
join(BASE, 'dirC', 'fileC'): b'this is file C\n',
join(BASE, 'dirC', 'dirD', 'fileD'): b'this is file D\n',
join(BASE, 'dirC', 'novel.txt'): b'this is a novel\n',
})
cls._directories.update({
BASE: {'dirA', 'dirB', 'dirC', 'dirE', 'fileA'},
join(BASE, 'dirA'): set(),
join(BASE, 'dirB'): {'fileB'},
join(BASE, 'dirC'): {'dirD', 'fileC', 'novel.txt'},
join(BASE, 'dirC', 'dirD'): {'fileD'},
join(BASE, 'dirE'): {},
})
dirname = BASE
while True:
dirname, basename = cls.pathmod.split(dirname)
if not basename:
break
cls._directories[dirname] = {basename}
cls._directories['/'] = set()

def tempdir(self):
path = self.cls(BASE).with_name('tmp-dirD')
Expand Down Expand Up @@ -2625,22 +2630,6 @@ class DummyPathWithSymlinksTest(DummyPathTest):
cls = DummyPathWithSymlinks
can_symlink = True

def setUp(self):
super().setUp()
cls = self.cls
join = cls.pathmod.join
cls._symlinks.update({
join(BASE, 'linkA'): 'fileA',
join(BASE, 'linkB'): 'dirB',
join(BASE, 'dirA', 'linkC'): join('..', 'dirB'),
join(BASE, 'dirB', 'linkD'): join('..', 'dirB'),
join(BASE, 'brokenLink'): 'non-existing',
join(BASE, 'brokenLinkLoop'): 'brokenLinkLoop',
})
cls._directories[BASE].update({'linkA', 'linkB', 'brokenLink', 'brokenLinkLoop'})
cls._directories[join(BASE, 'dirA')].add('linkC')
cls._directories[join(BASE, 'dirB')].add('linkD')


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

def setUp(self):
# note: this must be kept in sync with `DummyPathTest.setUp()`
def cleanup():
os.chmod(join('dirE'), 0o777)
os_helper.rmtree(BASE)
self.addCleanup(cleanup)
os.mkdir(BASE)
os.mkdir(join('dirA'))
os.mkdir(join('dirB'))
os.mkdir(join('dirC'))
os.mkdir(join('dirC', 'dirD'))
os.mkdir(join('dirE'))
with open(join('fileA'), 'wb') as f:
f.write(b"this is file A\n")
with open(join('dirB', 'fileB'), 'wb') as f:
f.write(b"this is file B\n")
with open(join('dirC', 'fileC'), 'wb') as f:
f.write(b"this is file C\n")
with open(join('dirC', 'novel.txt'), 'wb') as f:
f.write(b"this is a novel\n")
with open(join('dirC', 'dirD', 'fileD'), 'wb') as f:
f.write(b"this is file D\n")
super().setUp()
os.chmod(join('dirE'), 0)
if self.can_symlink:
# Relative symlinks.
os.symlink('fileA', join('linkA'))
os.symlink('non-existing', join('brokenLink'))
os.symlink('dirB', join('linkB'), target_is_directory=True)
os.symlink(os.path.join('..', 'dirB'), join('dirA', 'linkC'), target_is_directory=True)
# This one goes upwards, creating a loop.
os.symlink(os.path.join('..', 'dirB'), join('dirB', 'linkD'), target_is_directory=True)
# Broken symlink (pointing to itself).
os.symlink('brokenLinkLoop', join('brokenLinkLoop'))

def tearDown(self):
os.chmod(join('dirE'), 0o777)
os_helper.rmtree(BASE)

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

0 comments on commit 957e1bb

Please sign in to comment.