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 2 commits
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
32 changes: 29 additions & 3 deletions mypy/test/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sys
import re
import os
import re
import sys
import time

from typing import List, Dict, Tuple
from typing import List, Dict, Tuple, TypeVar, Callable

from mypy import defaults
from mypy.myunit import AssertionFailure
Expand Down Expand Up @@ -283,3 +284,28 @@ def normalize_error_messages(messages: List[str]) -> List[str]:
for m in messages:
a.append(m.replace(os.sep, '/'))
return a


_T = TypeVar('_T')


def retry_on_error(func: Callable[[], _T], max_wait: float = 1.0) -> _T:
"""Retry callback with exponential backoff when it raises OSError.

If the function still generates an error after max_wait seconds, propagate
the exception.

This can be effective against random file system operation failures on
Windows.
"""
t0 = time.time()
wait_time = 0.01
while True:
try:
return func()
except OSError:
wait_time = min(wait_time * 2, t0 + max_wait - time.time())
if wait_time <= 0.01:
# Done enough waiting, the error seems persistent.
raise
time.sleep(wait_time)
8 changes: 5 additions & 3 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
from mypy.test.helpers import (
assert_string_arrays_equal, normalize_error_messages,
testcase_pyversion, update_testcase_output,
retry_on_error, testcase_pyversion, update_testcase_output,
)
from mypy.errors import CompileError
from mypy.options import Options
Expand Down Expand Up @@ -147,15 +147,17 @@ def run_case_once(self, testcase: DataDrivenTestCase, incremental_step: int = 0)
if file.endswith('.' + str(incremental_step)):
full = os.path.join(dn, file)
target = full[:-2]
shutil.copy(full, target)
# Use retries to work around potential flakiness on Windows (AppVeyor).
retry_on_error(lambda: shutil.copy(full, target))
Copy link
Member

Choose a reason for hiding this comment

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

I was surprised to see that you need to retry the copy too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A test case might first delete a file and the reintroduce it here, and I suspect that this could then require a retry. Not 100% sure though, but it seems better to be defensive since test flakes are very annoying.


# In some systems, mtime has a resolution of 1 second which can cause
# annoying-to-debug issues when a file has the same size after a
# 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)
# Use retries to work around potential flakiness on Windows (AppVeyor).
retry_on_error(lambda: 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.

The test failures seem to be due to the lack of a return type context. Maybe the retry_on_error() function should just take a Callable[[], None]?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed the return type to Any. The type check might be worth filing as a mypy bug, since it looks like mypy should be able to infer the call -- I'll think about it.


# Parse options after moving files (in case mypy.ini is being moved).
options = self.parse_options(original_program_text, testcase, incremental_step)
Expand Down