-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_utils.py
More file actions
85 lines (73 loc) · 2.79 KB
/
Copy pathtest_utils.py
File metadata and controls
85 lines (73 loc) · 2.79 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
import unittest.mock
from pathlib import Path, PosixPath
import pytest
from pylint.constants import full_version
from pylint.lint.utils import (
_is_env_set_and_non_empty,
get_fatal_error_message,
prepare_crash_report,
)
from pylint.testutils._run import _Run as Run
def test_prepare_crash_report(tmp_path: PosixPath) -> None:
exception_content = "Exmessage"
python_file = tmp_path / "myfile.py"
python_content = "from shadok import MagicFaucet"
with open(python_file, "w", encoding="utf8") as f:
f.write(python_content)
template_path = None
try:
raise ValueError(exception_content)
except ValueError as ex:
template_path = prepare_crash_report(
ex, str(python_file), str(tmp_path / "pylint-crash-%Y.txt")
)
assert str(tmp_path) in str(template_path)
with open(template_path, encoding="utf8") as f:
template_content = f.read()
assert python_content in template_content
assert exception_content in template_content
assert "in test_prepare_crash_report" in template_content
assert "raise ValueError(exception_content)" in template_content
assert "<details open>" in template_content
assert full_version in template_content
def test_get_fatal_error_message() -> None:
python_path = "mypath.py"
crash_path = "crash.txt"
msg = get_fatal_error_message(python_path, Path(crash_path))
assert python_path in msg
assert crash_path in msg
assert "open an issue" in msg
def test_issue_template_on_fatal_errors(capsys: pytest.CaptureFixture) -> None:
"""Test that we also create an issue template if the offending exception isn't from astroid."""
with pytest.raises(SystemExit):
with unittest.mock.patch(
"astroid.MANAGER.ast_from_file", side_effect=RecursionError()
):
Run([__file__])
captured = capsys.readouterr()
assert "Fatal error while checking" in captured.out
assert "Please open an issue" in captured.out
assert "Traceback" in captured.err
@pytest.mark.parametrize(
"value, expected",
[
(None, False),
("", False),
(0, True),
(1, True),
(False, True),
("on", True),
],
ids=repr,
)
def test_is_env_set_and_non_empty(
monkeypatch: pytest.MonkeyPatch, value: object, expected: bool
) -> None:
"""Test the function returns True if the environment variable is set and non-empty."""
env_var = "TEST_VAR"
if value is not None:
monkeypatch.setenv(env_var, str(value))
assert _is_env_set_and_non_empty(env_var) is expected