Skip to content

Add test cases that delete a file during incremental checking #3461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions mypy/test/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def parse_test_cases(
output_files = [] # type: List[Tuple[str, str]] # path and contents for output files
tcout = [] # type: List[str] # Regular output errors
tcout2 = {} # type: Dict[int, List[str]] # Output errors for incremental, runs 2+
deleted_paths = {} # type: Dict[int, Set[str]] # from run number of paths
stale_modules = {} # type: Dict[int, Set[str]] # from run number to module names
rechecked_modules = {} # type: Dict[ int, Set[str]] # from run number module names
while i < len(p) and p[i].id != 'case':
Expand Down Expand Up @@ -99,6 +100,16 @@ def parse_test_cases(
rechecked_modules[passnum] = set()
else:
rechecked_modules[passnum] = {item.strip() for item in arg.split(',')}
elif p[i].id == 'delete':
# File to delete during a multi-step test case
arg = p[i].arg
assert arg is not None
m = re.match(r'(.*)\.([0-9]+)$', arg)
assert m, 'Invalid delete section: {}'.format(arg)
num = int(m.group(2))
assert num >= 2, "Can't delete during step {}".format(num)
full = os.path.join(base_path, m.group(1))
deleted_paths.setdefault(num, set()).add(full)
elif p[i].id == 'out' or p[i].id == 'out1':
tcout = p[i].data
if native_sep and os.path.sep == '\\':
Expand Down Expand Up @@ -142,7 +153,7 @@ def parse_test_cases(
tc = DataDrivenTestCase(p[i0].arg, input, tcout, tcout2, path,
p[i0].line, lastline, perform,
files, output_files, stale_modules,
rechecked_modules, native_sep)
rechecked_modules, deleted_paths, native_sep)
out.append(tc)
if not ok:
raise ValueError(
Expand Down Expand Up @@ -180,6 +191,7 @@ def __init__(self,
output_files: List[Tuple[str, str]],
expected_stale_modules: Dict[int, Set[str]],
expected_rechecked_modules: Dict[int, Set[str]],
deleted_paths: Dict[int, Set[str]],
native_sep: bool = False,
) -> None:
super().__init__(name)
Expand All @@ -194,24 +206,30 @@ def __init__(self,
self.output_files = output_files
self.expected_stale_modules = expected_stale_modules
self.expected_rechecked_modules = expected_rechecked_modules
self.deleted_paths = deleted_paths
self.native_sep = native_sep

def set_up(self) -> None:
super().set_up()
encountered_files = set()
self.clean_up = []
all_deleted = [] # type: List[str]
for paths in self.deleted_paths.values():
all_deleted += paths
for path, content in self.files:
dir = os.path.dirname(path)
for d in self.add_dirs(dir):
self.clean_up.append((True, d))
with open(path, 'w') as f:
f.write(content)
self.clean_up.append((False, path))
if path not in all_deleted:
# TODO: Don't assume that deleted files don't get reintroduced.
self.clean_up.append((False, path))
encountered_files.add(path)
if re.search(r'\.[2-9]$', path):
# Make sure new files introduced in the second and later runs are accounted for
renamed_path = path[:-2]
if renamed_path not in encountered_files:
if renamed_path not in encountered_files and renamed_path not in all_deleted:
encountered_files.add(renamed_path)
self.clean_up.append((False, renamed_path))
for path, _ in self.output_files:
Expand Down
2 changes: 2 additions & 0 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ def run_case_once(self, testcase: DataDrivenTestCase, incremental_step: int = 0)
# change. We manually set the mtime to circumvent this.
new_time = os.stat(target).st_mtime + 1
os.utime(target, times=(new_time, new_time))
for path in testcase.deleted_paths.get(incremental_step, set()):
os.remove(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll need a try/except around this similar to other places (see tear_down() in same file). That should prevent the AppVeyor flakes (in general on Windows remove() sometimes fails due to e.g. virus checkers having the file open).


# Parse options after moving files (in case mypy.ini is being moved).
options = self.parse_options(original_program_text, testcase, incremental_step)
Expand Down
31 changes: 31 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
-- Before the tests are run again, in step N any *.py.N files are copied to
-- *.py.
--
-- You can add an empty section like `[delete mod.py.2]` to delete `mod.py`
-- before the second run.
--
-- Errors expected in the first run should be in the `[out1]` section, and
-- errors expected in the second run should be in the `[out2]` section, and so on.
-- If a section is omitted, it is expected there are no errors on that run.
Expand Down Expand Up @@ -1947,6 +1950,34 @@ main:5: error: Revealed type is 'builtins.int'
-- TODO: Add another test for metaclass in import cycle (reversed from the above test).
-- This currently doesn't work.

[case testDeleteFile]
import n
[file n.py]
import m
[file m.py]
x = 1
[delete m.py.2]
[rechecked n]
[stale]
[out2]
tmp/n.py:1: error: Cannot find module named 'm'
tmp/n.py:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)

[case testDeleteFileWithinCycle]
import a
[file a.py]
import b
[file b.py]
import c
[file c.py]
import a
[file a.py.2]
import c
[delete b.py.2]
[rechecked a, c]
[stale a]
[out2]

[case testThreePassesBasic]
import m
[file m.py]
Expand Down