|
| 1 | +import signal |
| 2 | +import textwrap |
| 3 | + |
1 | 4 | import pytest
|
2 | 5 |
|
3 | 6 |
|
@@ -269,3 +272,87 @@ def pyfunc(x):
|
269 | 272 | '1: error: Function is missing a type annotation',
|
270 | 273 | ])
|
271 | 274 | assert result.ret != 0
|
| 275 | + |
| 276 | + |
| 277 | +def test_looponfail(testdir): |
| 278 | + """Ensure that the plugin works with --looponfail.""" |
| 279 | + |
| 280 | + pass_source = textwrap.dedent( |
| 281 | + """\ |
| 282 | + def pyfunc(x: int) -> int: |
| 283 | + return x * 2 |
| 284 | + """, |
| 285 | + ) |
| 286 | + fail_source = textwrap.dedent( |
| 287 | + """\ |
| 288 | + def pyfunc(x: int) -> str: |
| 289 | + return x * 2 |
| 290 | + """, |
| 291 | + ) |
| 292 | + pyfile = testdir.makepyfile(fail_source) |
| 293 | + looponfailroot = testdir.mkdir("looponfailroot") |
| 294 | + looponfailroot_pyfile = looponfailroot.join(pyfile.basename) |
| 295 | + pyfile.move(looponfailroot_pyfile) |
| 296 | + pyfile = looponfailroot_pyfile |
| 297 | + testdir.makeini( |
| 298 | + textwrap.dedent( |
| 299 | + """\ |
| 300 | + [pytest] |
| 301 | + looponfailroots = {looponfailroots} |
| 302 | + """.format( |
| 303 | + looponfailroots=looponfailroot, |
| 304 | + ), |
| 305 | + ), |
| 306 | + ) |
| 307 | + |
| 308 | + child = testdir.spawn_pytest( |
| 309 | + "--mypy --looponfail " + str(pyfile), |
| 310 | + expect_timeout=30.0, |
| 311 | + ) |
| 312 | + |
| 313 | + def _expect_session(): |
| 314 | + child.expect("==== test session starts ====") |
| 315 | + |
| 316 | + def _expect_failure(): |
| 317 | + _expect_session() |
| 318 | + child.expect("==== FAILURES ====") |
| 319 | + child.expect(pyfile.basename + " ____") |
| 320 | + child.expect("2: error: Incompatible return value") |
| 321 | + # These only show with mypy>=0.730: |
| 322 | + # child.expect("==== mypy ====") |
| 323 | + # child.expect("Found 1 error in 1 file (checked 1 source file)") |
| 324 | + child.expect("2 failed") |
| 325 | + child.expect("#### LOOPONFAILING ####") |
| 326 | + _expect_waiting() |
| 327 | + |
| 328 | + def _expect_waiting(): |
| 329 | + child.expect("#### waiting for changes ####") |
| 330 | + child.expect("Watching") |
| 331 | + |
| 332 | + def _fix(): |
| 333 | + pyfile.write(pass_source) |
| 334 | + _expect_changed() |
| 335 | + _expect_success() |
| 336 | + |
| 337 | + def _expect_changed(): |
| 338 | + child.expect("MODIFIED " + str(pyfile)) |
| 339 | + |
| 340 | + def _expect_success(): |
| 341 | + for _ in range(2): |
| 342 | + _expect_session() |
| 343 | + # These only show with mypy>=0.730: |
| 344 | + # child.expect("==== mypy ====") |
| 345 | + # child.expect("Success: no issues found in 1 source file") |
| 346 | + child.expect("2 passed") |
| 347 | + _expect_waiting() |
| 348 | + |
| 349 | + def _break(): |
| 350 | + pyfile.write(fail_source) |
| 351 | + _expect_changed() |
| 352 | + _expect_failure() |
| 353 | + |
| 354 | + _expect_failure() |
| 355 | + _fix() |
| 356 | + _break() |
| 357 | + _fix() |
| 358 | + child.kill(signal.SIGTERM) |
0 commit comments