|
5 | 5 | import tempfile
|
6 | 6 | import posixpath
|
7 | 7 | import re
|
8 |
| -from os import remove, rmdir |
9 | 8 | import shutil
|
10 | 9 | from abc import abstractmethod
|
11 | 10 | import sys
|
@@ -178,9 +177,6 @@ class DataDrivenTestCase(pytest.Item): # type: ignore # inheriting from Any
|
178 | 177 | expected_rechecked_modules = None # type: Dict[int, Set[str]]
|
179 | 178 | expected_fine_grained_targets = None # type: Dict[int, List[str]]
|
180 | 179 |
|
181 |
| - # Files/directories to clean up after test case; (is directory, path) tuples |
182 |
| - clean_up = None # type: List[Tuple[bool, str]] |
183 |
| - |
184 | 180 | # Whether or not we should normalize the output to standardize things like
|
185 | 181 | # forward vs backward slashes in file paths for Windows vs Linux.
|
186 | 182 | normalize_output = True
|
@@ -209,7 +205,6 @@ def __init__(self,
|
209 | 205 | self.line = line
|
210 | 206 | self.old_cwd = None # type: Optional[str]
|
211 | 207 | self.tmpdir = None # type: Optional[tempfile.TemporaryDirectory[str]]
|
212 |
| - self.clean_up = [] |
213 | 208 |
|
214 | 209 | def runtest(self) -> None:
|
215 | 210 | if self.skip:
|
@@ -237,88 +232,13 @@ def setup(self) -> None:
|
237 | 232 | self.tmpdir = tempfile.TemporaryDirectory(prefix='mypy-test-')
|
238 | 233 | os.chdir(self.tmpdir.name)
|
239 | 234 | 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) |
246 | 235 | for path, content in self.files:
|
247 | 236 | 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) |
250 | 238 | with open(path, 'w', encoding='utf8') as f:
|
251 | 239 | 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 |
280 | 240 |
|
281 | 241 | 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 |
322 | 242 | assert self.old_cwd is not None and self.tmpdir is not None, \
|
323 | 243 | "test was not properly set up"
|
324 | 244 | os.chdir(self.old_cwd)
|
|
0 commit comments