-
-
Notifications
You must be signed in to change notification settings - Fork 581
Expand file tree
/
Copy pathterraform_docs_replace_test.py
More file actions
125 lines (109 loc) · 3.84 KB
/
terraform_docs_replace_test.py
File metadata and controls
125 lines (109 loc) · 3.84 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""Tests for the `replace-docs` subcommand."""
from argparse import ArgumentParser, Namespace
from subprocess import CalledProcessError
import pytest_mock
import pytest
from pre_commit_terraform._structs import ReturnCode
from pre_commit_terraform.terraform_docs_replace import (
invoke_cli_app,
populate_argument_parser,
)
from pre_commit_terraform.terraform_docs_replace import (
subprocess as replace_docs_subprocess_mod,
)
def test_arg_parser_populated() -> None:
"""Check that `replace-docs` populates its parser."""
test_arg_parser = ArgumentParser()
populate_argument_parser(test_arg_parser)
assert test_arg_parser.get_default('dest') == 'README.md'
def test_check_is_deprecated() -> None:
"""Verify that `replace-docs` shows a deprecation warning."""
deprecation_msg_regex = (
r'^`terraform_docs_replace` hook is DEPRECATED\.For migration.*$'
)
with pytest.warns(UserWarning, match=deprecation_msg_regex):
# not `pytest.deprecated_call()` due to this being a user warning
invoke_cli_app(Namespace(filenames=[]))
@pytest.mark.parametrize(
('parsed_cli_args', 'expected_cmds'),
(
pytest.param(Namespace(filenames=[]), [], id='no-files'),
pytest.param(
Namespace(
dest='SENTINEL.md',
filenames=['some.tf'],
sort=False,
),
['terraform-docs md ./ > .//SENTINEL.md'],
id='one-file',
),
pytest.param(
Namespace(
dest='SENTINEL.md',
filenames=['some.tf', 'thing/weird.tfvars'],
sort=True,
),
[
'terraform-docs --sort-by-required md ./ > .//SENTINEL.md',
'terraform-docs --sort-by-required md ./thing '
'> ./thing/SENTINEL.md',
],
id='two-sorted-files',
),
pytest.param(
Namespace(filenames=['some.thing', 'un.supported']),
[],
id='invalid-files',
),
),
)
@pytest.mark.filterwarnings(
'ignore:`terraform_docs_replace` hook is DEPRECATED.:UserWarning:'
'pre_commit_terraform.terraform_docs_replace',
)
def test_control_flow_positive(
expected_cmds: list[str],
mocker: pytest_mock.MockerFixture,
monkeypatch: pytest.MonkeyPatch,
parsed_cli_args: Namespace,
) -> None:
"""Check that the subcommand's happy path works."""
check_call_mock = mocker.Mock()
monkeypatch.setattr(
replace_docs_subprocess_mod,
'check_call',
check_call_mock,
)
assert invoke_cli_app(parsed_cli_args) == ReturnCode.OK
executed_commands = [
cmd for ((cmd,), _shell) in check_call_mock.call_args_list
]
assert len(expected_cmds) == check_call_mock.call_count
assert expected_cmds == executed_commands
@pytest.mark.filterwarnings(
'ignore:`terraform_docs_replace` hook is DEPRECATED.:UserWarning:'
'pre_commit_terraform.terraform_docs_replace',
)
def test_control_flow_negative(
mocker: pytest_mock.MockerFixture,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Check that the subcommand's error processing works."""
parsed_cli_args = Namespace(
dest='SENTINEL.md',
filenames=['some.tf'],
sort=True,
)
expected_cmd = 'terraform-docs --sort-by-required md ./ > .//SENTINEL.md'
check_call_mock = mocker.Mock(
side_effect=CalledProcessError(ReturnCode.ERROR, expected_cmd),
)
monkeypatch.setattr(
replace_docs_subprocess_mod,
'check_call',
check_call_mock,
)
assert invoke_cli_app(parsed_cli_args) == ReturnCode.ERROR
# S604 - 'shell=True' is insecure, but this hook is deprecated and we don't
# want to spent time on testing fixes for it
check_call_mock.assert_called_once_with(expected_cmd, shell=True) # noqa: S604