|
18 | 18 |
|
19 | 19 | # File modify/create operation: copy module contents from source_path. |
20 | 20 | UpdateFile = NamedTuple('UpdateFile', [('module', str), |
21 | | - ('source_path', str), |
| 21 | + ('content', str), |
22 | 22 | ('target_path', str)]) |
23 | 23 |
|
24 | 24 | # File delete operation: delete module file. |
@@ -270,11 +270,35 @@ def setup(self) -> None: |
270 | 270 | self.tmpdir = tempfile.TemporaryDirectory(prefix='mypy-test-') |
271 | 271 | os.chdir(self.tmpdir.name) |
272 | 272 | os.mkdir(test_temp_dir) |
| 273 | + |
| 274 | + # Precalculate steps for find_steps() |
| 275 | + steps: Dict[int, List[FileOperation]] = {} |
| 276 | + |
273 | 277 | for path, content in self.files: |
274 | | - dir = os.path.dirname(path) |
275 | | - os.makedirs(dir, exist_ok=True) |
276 | | - with open(path, 'w', encoding='utf8') as f: |
277 | | - f.write(content) |
| 278 | + m = re.match(r'.*\.([0-9]+)$', path) |
| 279 | + if m: |
| 280 | + # Skip writing subsequent incremental steps - rather |
| 281 | + # store them as operations. |
| 282 | + num = int(m.group(1)) |
| 283 | + assert num >= 2 |
| 284 | + target_path = re.sub(r'\.[0-9]+$', '', path) |
| 285 | + module = module_from_path(target_path) |
| 286 | + operation = UpdateFile(module, content, target_path) |
| 287 | + steps.setdefault(num, []).append(operation) |
| 288 | + else: |
| 289 | + # Write the first incremental steps |
| 290 | + dir = os.path.dirname(path) |
| 291 | + os.makedirs(dir, exist_ok=True) |
| 292 | + with open(path, 'w', encoding='utf8') as f: |
| 293 | + f.write(content) |
| 294 | + |
| 295 | + for num, paths in self.deleted_paths.items(): |
| 296 | + assert num >= 2 |
| 297 | + for path in paths: |
| 298 | + module = module_from_path(path) |
| 299 | + steps.setdefault(num, []).append(DeleteFile(module, path)) |
| 300 | + max_step = max(steps) if steps else 2 |
| 301 | + self.steps = [steps.get(num, []) for num in range(2, max_step + 1)] |
278 | 302 |
|
279 | 303 | def teardown(self) -> None: |
280 | 304 | assert self.old_cwd is not None and self.tmpdir is not None, \ |
@@ -312,23 +336,7 @@ def find_steps(self) -> List[List[FileOperation]]: |
312 | 336 |
|
313 | 337 | Defaults to having two steps if there aern't any operations. |
314 | 338 | """ |
315 | | - steps: Dict[int, List[FileOperation]] = {} |
316 | | - for path, _ in self.files: |
317 | | - m = re.match(r'.*\.([0-9]+)$', path) |
318 | | - if m: |
319 | | - num = int(m.group(1)) |
320 | | - assert num >= 2 |
321 | | - target_path = re.sub(r'\.[0-9]+$', '', path) |
322 | | - module = module_from_path(target_path) |
323 | | - operation = UpdateFile(module, path, target_path) |
324 | | - steps.setdefault(num, []).append(operation) |
325 | | - for num, paths in self.deleted_paths.items(): |
326 | | - assert num >= 2 |
327 | | - for path in paths: |
328 | | - module = module_from_path(path) |
329 | | - steps.setdefault(num, []).append(DeleteFile(module, path)) |
330 | | - max_step = max(steps) if steps else 2 |
331 | | - return [steps.get(num, []) for num in range(2, max_step + 1)] |
| 339 | + return self.steps |
332 | 340 |
|
333 | 341 |
|
334 | 342 | def module_from_path(path: str) -> str: |
|
0 commit comments