Skip to content

Commit b328f74

Browse files
committed
Refactor out the mtime fudging
1 parent 8bae5b8 commit b328f74

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

mypy/test/helpers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44
import sys
55
import time
6+
import shutil
67

78
from typing import List, Dict, Tuple, Callable, Any, Optional
89

@@ -356,3 +357,22 @@ def run_command(cmdline: List[str], *, env: Optional[Dict[str, str]] = None,
356357
out = err = b''
357358
process.kill()
358359
return process.returncode, split_lines(out, err)
360+
361+
362+
def copy_and_fudge_mtime(source_path: str, target_path: str) -> None:
363+
# In some systems, mtime has a resolution of 1 second which can
364+
# cause annoying-to-debug issues when a file has the same size
365+
# after a change. We manually set the mtime to circumvent this.
366+
# Note that we increment the old file's mtime, which guarentees a
367+
# different value, rather than incrementing the mtime after the
368+
# copy, which could leave the mtime unchanged if the old file had
369+
# a similarly fudged mtime.
370+
new_time = None
371+
if os.path.isfile(target_path):
372+
new_time = os.stat(target_path).st_mtime + 1
373+
374+
# Use retries to work around potential flakiness on Windows (AppVeyor).
375+
retry_on_error(lambda: shutil.copy(source_path, target_path))
376+
377+
if new_time:
378+
os.utime(target_path, times=(new_time, new_time))

mypy/test/testcheck.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from mypy.test.data import DataDrivenTestCase, DataSuite
1313
from mypy.test.helpers import (
1414
assert_string_arrays_equal, normalize_error_messages,
15-
retry_on_error, update_testcase_output, parse_options
15+
retry_on_error, update_testcase_output, parse_options,
16+
copy_and_fudge_mtime
1617
)
1718
from mypy.errors import CompileError
1819
from mypy.options import Options
@@ -132,14 +133,7 @@ def run_case_once(self, testcase: DataDrivenTestCase, incremental_step: int = 0)
132133
if file.endswith('.' + str(incremental_step)):
133134
full = os.path.join(dn, file)
134135
target = full[:-2]
135-
# Use retries to work around potential flakiness on Windows (AppVeyor).
136-
retry_on_error(lambda: shutil.copy(full, target))
137-
138-
# In some systems, mtime has a resolution of 1 second which can cause
139-
# annoying-to-debug issues when a file has the same size after a
140-
# change. We manually set the mtime to circumvent this.
141-
new_time = os.stat(target).st_mtime + 1
142-
os.utime(target, times=(new_time, new_time))
136+
copy_and_fudge_mtime(full, target)
143137
# Delete files scheduled to be deleted in [delete <path>.num] sections.
144138
for path in testcase.deleted_paths.get(incremental_step, set()):
145139
# Use retries to work around potential flakiness on Windows (AppVeyor).

mypy/test/testdmypy.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from mypy.test.helpers import (
1616
assert_string_arrays_equal, normalize_error_messages,
1717
retry_on_error, testcase_pyversion, update_testcase_output,
18+
copy_and_fudge_mtime,
1819
)
1920
from mypy.options import Options
2021

@@ -91,14 +92,7 @@ def run_case_once(self, testcase: DataDrivenTestCase, incremental_step: int) ->
9192
if file.endswith('.' + str(incremental_step)):
9293
full = os.path.join(dn, file)
9394
target = full[:-2]
94-
# Use retries to work around potential flakiness on Windows (AppVeyor).
95-
retry_on_error(lambda: shutil.copy(full, target))
96-
97-
# In some systems, mtime has a resolution of 1 second which can cause
98-
# annoying-to-debug issues when a file has the same size after a
99-
# change. We manually set the mtime to circumvent this.
100-
new_time = os.stat(target).st_mtime + 1
101-
os.utime(target, times=(new_time, new_time))
95+
copy_and_fudge_mtime(full, target)
10296
# Delete files scheduled to be deleted in [delete <path>.num] sections.
10397
for path in testcase.deleted_paths.get(incremental_step, set()):
10498
# Use retries to work around potential flakiness on Windows (AppVeyor).

mypy/test/testfinegrained.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import os
1111
import re
12-
import shutil
1312

1413
from typing import List, Tuple, Optional, cast
1514

@@ -22,7 +21,7 @@
2221
from mypy.test.data import (
2322
DataDrivenTestCase, DataSuite, UpdateFile, module_from_path
2423
)
25-
from mypy.test.helpers import assert_string_arrays_equal, parse_options
24+
from mypy.test.helpers import assert_string_arrays_equal, parse_options, copy_and_fudge_mtime
2625
from mypy.server.mergecheck import check_consistency
2726
from mypy.dmypy_server import Server
2827
from mypy.main import expand_dir
@@ -102,17 +101,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
102101
for op in operations:
103102
if isinstance(op, UpdateFile):
104103
# Modify/create file
105-
106-
# In some systems, mtime has a resolution of 1 second which can cause
107-
# annoying-to-debug issues when a file has the same size after a
108-
# change. We manually set the mtime to circumvent this.
109-
new_time = None
110-
if os.path.isfile(op.target_path):
111-
new_time = os.stat(op.target_path).st_mtime + 1
112-
113-
shutil.copy(op.source_path, op.target_path)
114-
if new_time:
115-
os.utime(op.target_path, times=(new_time, new_time))
104+
copy_and_fudge_mtime(op.source_path, op.target_path)
116105
else:
117106
# Delete file
118107
os.remove(op.path)

0 commit comments

Comments
 (0)