Skip to content

Commit 5269422

Browse files
authored
Get rid of all of the clean_up tracking in the test suite (#7896)
It has caused me grief several times and helped me zero times. We nuke the temporary directory anyways, so why bother carefully trying to deconstruct it first.
1 parent 8010414 commit 5269422

File tree

1 file changed

+1
-81
lines changed

1 file changed

+1
-81
lines changed

mypy/test/data.py

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import tempfile
66
import posixpath
77
import re
8-
from os import remove, rmdir
98
import shutil
109
from abc import abstractmethod
1110
import sys
@@ -178,9 +177,6 @@ class DataDrivenTestCase(pytest.Item): # type: ignore # inheriting from Any
178177
expected_rechecked_modules = None # type: Dict[int, Set[str]]
179178
expected_fine_grained_targets = None # type: Dict[int, List[str]]
180179

181-
# Files/directories to clean up after test case; (is directory, path) tuples
182-
clean_up = None # type: List[Tuple[bool, str]]
183-
184180
# Whether or not we should normalize the output to standardize things like
185181
# forward vs backward slashes in file paths for Windows vs Linux.
186182
normalize_output = True
@@ -209,7 +205,6 @@ def __init__(self,
209205
self.line = line
210206
self.old_cwd = None # type: Optional[str]
211207
self.tmpdir = None # type: Optional[tempfile.TemporaryDirectory[str]]
212-
self.clean_up = []
213208

214209
def runtest(self) -> None:
215210
if self.skip:
@@ -237,88 +232,13 @@ def setup(self) -> None:
237232
self.tmpdir = tempfile.TemporaryDirectory(prefix='mypy-test-')
238233
os.chdir(self.tmpdir.name)
239234
os.mkdir(test_temp_dir)
240-
encountered_files = set()
241-
self.clean_up = []
242-
for paths in self.deleted_paths.values():
243-
for path in paths:
244-
self.clean_up.append((False, path))
245-
encountered_files.add(path)
246235
for path, content in self.files:
247236
dir = os.path.dirname(path)
248-
for d in self.add_dirs(dir):
249-
self.clean_up.append((True, d))
237+
os.makedirs(dir, exist_ok=True)
250238
with open(path, 'w', encoding='utf8') as f:
251239
f.write(content)
252-
if path not in encountered_files:
253-
self.clean_up.append((False, path))
254-
encountered_files.add(path)
255-
if re.search(r'\.[2-9]$', path):
256-
# Make sure new files introduced in the second and later runs are accounted for
257-
renamed_path = path[:-2]
258-
if renamed_path not in encountered_files:
259-
encountered_files.add(renamed_path)
260-
self.clean_up.append((False, renamed_path))
261-
for path, _ in self.output_files:
262-
# Create directories for expected output and mark them to be cleaned up at the end
263-
# of the test case.
264-
dir = os.path.dirname(path)
265-
for d in self.add_dirs(dir):
266-
self.clean_up.append((True, d))
267-
self.clean_up.append((False, path))
268-
269-
def add_dirs(self, dir: str) -> List[str]:
270-
"""Add all subdirectories required to create dir.
271-
272-
Return an array of the created directories in the order of creation.
273-
"""
274-
if dir == '' or os.path.isdir(dir):
275-
return []
276-
else:
277-
dirs = self.add_dirs(os.path.dirname(dir)) + [dir]
278-
os.mkdir(dir)
279-
return dirs
280240

281241
def teardown(self) -> None:
282-
# First remove files.
283-
for is_dir, path in reversed(self.clean_up):
284-
if not is_dir:
285-
try:
286-
remove(path)
287-
except FileNotFoundError:
288-
# Breaking early using Ctrl+C may happen before file creation. Also, some
289-
# files may be deleted by a test case.
290-
pass
291-
# Then remove directories.
292-
for is_dir, path in reversed(self.clean_up):
293-
if is_dir:
294-
pycache = os.path.join(path, '__pycache__')
295-
if os.path.isdir(pycache):
296-
shutil.rmtree(pycache)
297-
# As a somewhat nasty hack, ignore any dirs with .mypy_cache in the path,
298-
# to allow test cases to intentionally corrupt the cache without provoking
299-
# the test suite when there are still files left over.
300-
# (Looking at / should be fine on windows because these are paths specified
301-
# in the test cases.)
302-
if '/.mypy_cache' in path:
303-
continue
304-
try:
305-
rmdir(path)
306-
except OSError as error:
307-
print(' ** Error removing directory %s -- contents:' % path)
308-
for item in os.listdir(path):
309-
print(' ', item)
310-
# Most likely, there are some files in the
311-
# directory. Use rmtree to nuke the directory, but
312-
# fail the test case anyway, since this seems like
313-
# a bug in a test case -- we shouldn't leave
314-
# garbage lying around. By nuking the directory,
315-
# the next test run hopefully passes.
316-
path = error.filename
317-
# Be defensive -- only call rmtree if we're sure we aren't removing anything
318-
# valuable.
319-
if path.startswith(test_temp_dir + '/') and os.path.isdir(path):
320-
shutil.rmtree(path)
321-
raise
322242
assert self.old_cwd is not None and self.tmpdir is not None, \
323243
"test was not properly set up"
324244
os.chdir(self.old_cwd)

0 commit comments

Comments
 (0)