File: catch_syntax_error.py

package info (click to toggle)
python-friendly-traceback 0.7.62%2Bgit20240811.d7dbff6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,264 kB
  • sloc: python: 21,500; makefile: 4
file content (57 lines) | stat: -rw-r--r-- 2,068 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sys

import pytest

import friendly_traceback
from syntax_errors_descriptions import descriptions

friendly_traceback.set_lang("en")


@pytest.mark.parametrize("filename", descriptions.keys())
def test_syntax_errors(filename):
    cause = descriptions[filename]["in cause"]

    try:
        # __import__(filename)
        exec("from . import %s" % filename)
    except Exception:
        friendly_traceback.explain_traceback(redirect="capture")
    result = friendly_traceback.get_output()

    if "tab_error" in filename:
        assert "TabError" in result, "TabError identified incorrectly; %s" % filename
    elif "indentation" in filename or "indented" in cause:
        assert "IndentationError" in result, (
            "IndentationError identified incorrectly; %s" % filename
        )
    else:
        assert "SyntaxError" in result or "IndentationError" in result, (
            "SyntaxError identified incorrectly; %s" % filename
        )

    unwrapped_result = " ".join(result.split())

    if "version dependent" in descriptions[filename]:
        causes = descriptions[filename]["version dependent"]
        for cause in causes:
            if cause in unwrapped_result:
                break
        else:
            assert False, "\nExpected to see a version dependent cause in\n%s" % result
    else:
        assert cause in unwrapped_result, "\nExpected to see: %s\nIn: %s" % (cause, result)
        if "also in cause" in descriptions[filename]:
            other_causes = descriptions[filename]["also in cause"]
            for cause in other_causes:
                assert cause in unwrapped_result, "\nExpected to see: %s\nIn: %s" % (
                    cause,
                    result,
                )
        if "not in cause" in descriptions[filename]:
            not_in_cause = descriptions[filename]["not in cause"]
            for cause in not_in_cause:
                assert cause not in unwrapped_result, "\nDid not expect to see: %s\nIn: %s" % (
                    cause,
                    result,
                )