From 957e1bb117047f3eded7b2951e3ce6c999568f27 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 30 Sep 2023 18:11:19 +0100 Subject: [PATCH] GH-110109: Simplify pathlib test setup Use the class under test to create files, directories and symlinks. --- Lib/test/test_pathlib.py | 105 +++++++++++++-------------------------- 1 file changed, 34 insertions(+), 71 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 319148e9065a65..2d115b4cd09378 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1673,7 +1673,7 @@ class DummyPath(pathlib._PathBase): memory. """ _files = {} - _directories = {} + _directories = {'/': set()} _symlinks = {} def stat(self, *, follow_symlinks=True): @@ -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') @@ -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. @@ -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',