Debian Codegraph @

tests/units_test.py

raw
1 """Unit tests of the extension bits."""
2 import sys
3
4 import pytest
5
6 from sphinxcontrib.towncrier._compat import shlex_join
7 from sphinxcontrib.towncrier.ext import _get_changelog_draft_entries
8
9
10 NO_OUTPUT_MARKER = r'\[No output\]'
11
12
13 _get_changelog_draft_entries_unwrapped = (
14 _get_changelog_draft_entries.
15 __wrapped__ # So that the non-cached function version is tested
16 )
17
18
19 @pytest.mark.parametrize(
20 ('failing_cmd', 'stdout_msg', 'stderr_msg'),
21 (
22 (
23 (
24 'false',
25 ),
26 NO_OUTPUT_MARKER,
27 NO_OUTPUT_MARKER,
28 ),
29 (
30 (
31 sys.executable,
32 '-c',
33 r'print("test standard output\nsecond line");'
34 'raise SystemExit(1)',
35 ),
36 r'test standard output\nsecond line\n',
37 NO_OUTPUT_MARKER,
38 ),
39 (
40 (
41 sys.executable,
42 '-c',
43 r'raise SystemExit("test standard error\nsecond line")',
44 ),
45 NO_OUTPUT_MARKER,
46 r'test standard error\nsecond line',
47 ),
48 (
49 (
50 sys.executable,
51 '-c',
52 r'print("test standard output\nsecond line out");'
53 r'raise SystemExit("test standard error\nsecond line err")',
54 ),
55 r'test standard output\nsecond line out\n',
56 r'test standard error\nsecond line err',
57 ),
58 ),
59 ids=(
60 'no stdout, no stderr',
61 'stdout, no stderr',
62 'no stdout, stderr',
63 'stdout, stderr',
64 ),
65 )
66 def test_towncrier_draft_generation_failure_msg(
67 failing_cmd, stdout_msg, stderr_msg,
68 monkeypatch,
69 ) -> None:
70 """Test that a failing command produces a :class:`RuntimeError`."""
71 version_string = 'test version'
72
73 monkeypatch.setattr(
74 'sphinxcontrib.towncrier.ext.TOWNCRIER_DRAFT_CMD',
75 failing_cmd, # So that the invoked command would return a failure
76 )
77
78 escaped_failing_cmd = ( # This is necessary because it's used in a regexp
79 shlex_join(failing_cmd).
80 replace('\\', r'\\').
81 replace('(', r'\(').
82 replace(')', r'\)')
83 )
84 expected_return_code = 1
85 expected_error_message = (
86 '^' # noqa: WPS221
87 'Command exited unexpectedly.\n\n'
88 rf"Command: {escaped_failing_cmd} --version '{version_string}'\n"
89 f'Return code: {expected_return_code}\n\n'
90 'Standard output:\n'
91 f'{stdout_msg}\n\n'
92 'Standard error:\n'
93 f'{stderr_msg}'
94 '$'
95 )
96
97 with pytest.raises(RuntimeError, match=expected_error_message):
98 _get_changelog_draft_entries_unwrapped(version_string)