|
| 1 | +"""Acceptance tests for issue #241: a warning Python raises while parsing an analysed |
| 2 | +source is about that source, not about the analyzer, and never reaches its stderr. |
| 3 | +
|
| 4 | +An invalid escape sequence is a ``SyntaxWarning`` from Python 3.12 on and a |
| 5 | +``DeprecationWarning`` on 3.11. With ``-j N`` the parent and a worker both parse every |
| 6 | +module, so an unfiltered warning was printed twice. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import warnings |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from coretrace_python.frontend.parser import parse_source |
| 19 | + |
| 20 | +INVALID_ESCAPE = 'import re\n\nPATTERN = re.compile("\\d+")\n' |
| 21 | + |
| 22 | + |
| 23 | +def test_parsing_raises_no_warning_about_the_analysed_source() -> None: |
| 24 | + with warnings.catch_warnings(record=True) as caught: |
| 25 | + warnings.simplefilter("always") |
| 26 | + tree = parse_source(INVALID_ESCAPE, "escapes.py") |
| 27 | + |
| 28 | + assert caught == [] |
| 29 | + assert len(tree.body) == 2 |
| 30 | + |
| 31 | + |
| 32 | +def test_parsing_leaves_the_warning_filters_of_the_process_alone() -> None: |
| 33 | + with warnings.catch_warnings(record=True) as caught: |
| 34 | + warnings.simplefilter("always") |
| 35 | + parse_source(INVALID_ESCAPE, "escapes.py") |
| 36 | + warnings.warn("raised by the analyzer itself", SyntaxWarning, stacklevel=1) |
| 37 | + |
| 38 | + assert [str(warning.message) for warning in caught] == ["raised by the analyzer itself"] |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("jobs", ["1", "2"]) |
| 42 | +def test_check_prints_nothing_on_stderr_about_the_analysed_sources( |
| 43 | + tmp_path: Path, jobs: str |
| 44 | +) -> None: |
| 45 | + root = tmp_path / "project" |
| 46 | + root.mkdir() |
| 47 | + (root / "escapes.py").write_text(INVALID_ESCAPE, encoding="utf-8") |
| 48 | + (root / "other.py").write_text("def run():\n return 1\n", encoding="utf-8") |
| 49 | + |
| 50 | + checked = subprocess.run( |
| 51 | + [sys.executable, "-m", "coretrace_python", "--check", str(root), "-j", jobs], |
| 52 | + capture_output=True, |
| 53 | + text=True, |
| 54 | + check=False, |
| 55 | + ) |
| 56 | + |
| 57 | + assert checked.stderr == "" |
| 58 | + assert checked.stdout == "no findings\ncoverage: 2/2 files, 2/2 functions\n" |
| 59 | + assert checked.returncode == 0 |
0 commit comments