Debian Codegraph @

test/units/errors/test_errors.py

raw
1 from __future__ import annotations
2
3 import pathlib
4
5 import pytest
6
7 from ansible.errors import AnsibleError, AnsibleVariableTypeError
8 from ansible._internal._errors._error_utils import SourceContext
9 from ansible._internal._datatag._tags import Origin
10
11 from ..test_utils.controller.display import emits_warnings
12
13
14 @pytest.mark.parametrize("filename, line, col, expected_filename", (
15 ('nonexistent_file.txt', 3, 4, "nonexistent.txt"),
16 ('short_file.txt', None, None, "short_file_no_line.txt"),
17 ('short_file.txt', 324762384, 3, "short_file_overflowed_line.txt"),
18 ('short_file.txt', 3, 324762384, "short_file_overflowed_col.txt"),
19 ('short_file.txt', -1, 3, "short_file_underflowed_line.txt"),
20 ('short_file.txt', 3, -1, "short_file_underflowed_col.txt"),
21 ('short_file.txt', 5, None, "short_file_no_column.txt"),
22 ('short_file.txt', 2, None, "short_file_no_column_overflowed.txt"),
23 ('empty_file.txt', 2, 1, "empty_file_unavailable.txt"),
24 ('short_file.txt', 1, 1, "short_file_no_context_left_marker.txt"),
25 ('short_file.txt', 5, 1, "short_file_left_marker.txt"),
26 ('short_file_missing_trailing_newline.txt', 7, 1, "short_file_missing_trailing_newline_left_marker.txt"),
27 ('short_file.txt', 7, 118, "short_file_last_column_right_marker.txt"),
28 ('short_file.txt', 4, 118, "short_file_truncated_target.txt"),
29 ('short_file.txt', 4, 115, "short_file_truncated_target_last_displayed_char.txt"),
30 ('short_file.txt', 4, 174, "short_file_long_line_truncated_past_target.txt"),
31 ('long_file.txt', 14, 117, "long_file_last_column_right_marker.txt"),
32 ('file_with_tabs.txt', 3, 22, "file_with_tabs_replaced_left_marker.txt"),
33 ))
34 def test_source_context(filename: str, line: int | None, col: int | None, expected_filename: str) -> None:
35 """
36 Validate various SourceContext formatting scenarios.
37 Test case outputs are generated; set `overwrite_expected` to regenerate.
38 """
39 # set this to true to regenerate and overwrite the expected test output files
40 overwrite_expected = False
41
42 fixture_path = pathlib.Path(__file__).parent / 'fixtures'
43
44 origin = Origin(path=str(fixture_path / 'inputs' / filename), line_num=line, col_num=col)
45
46 # DTFIX-FUTURE: assert target_line contents as well?
47 source_context = SourceContext.from_origin(origin)
48 result = '\n'.join(source_context.annotated_source_lines) + '\n'
49
50 expected_path = fixture_path / 'outputs' / expected_filename
51
52 if overwrite_expected: # pragma: nocover
53 expected_path.write_text(result)
54
55 assert result == expected_path.read_text()
56 assert not overwrite_expected
57
58
59 def test_suppress_extended_error_deprecation() -> None:
60 with emits_warnings(deprecation_pattern='suppress_extended_error.*argument'):
61 AnsibleError(suppress_extended_error=True) # pylint: disable=pointless-exception-statement
62
63
64 @pytest.mark.parametrize("obj, expected", (
65 (1, f'Type {int.__name__!r} is unsupported for variable storage.'),
66 (int, f'Type {type.__name__!r} is unsupported for variable storage.'),
67 ))
68 def test_ansible_variable_type_error(obj: object, expected: str) -> None:
69 assert expected in str(AnsibleVariableTypeError.from_value(obj=obj))