Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/pre_commit_terraform/_cli_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def attach_subcommand_parsers_to(root_cli_parser: ArgumentParser, /) -> None:
required=True,
)
for subcommand_module in SUBCOMMAND_MODULES:
subcommand_parser = subcommand_parsers.add_parser(subcommand_module.CLI_SUBCOMMAND_NAME)
subcommand_parser = subcommand_parsers.add_parser(
subcommand_module.CLI_SUBCOMMAND_NAME
)
subcommand_parser.set_defaults(
invoke_cli_app=subcommand_module.invoke_cli_app,
)
Expand Down
4 changes: 2 additions & 2 deletions src/pre_commit_terraform/_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class PreCommitTerraformBaseError(Exception):


class PreCommitTerraformRuntimeError(
PreCommitTerraformBaseError,
RuntimeError,
PreCommitTerraformBaseError,
RuntimeError,
):
"""An exception representing a runtime error condition."""

Expand Down
3 changes: 2 additions & 1 deletion src/pre_commit_terraform/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class CLISubcommandModuleProtocol(Protocol):
"""This constant contains a CLI."""

def populate_argument_parser(
self, subcommand_parser: ArgumentParser,
self,
subcommand_parser: ArgumentParser,
) -> None:
"""Run a module hook for populating the subcommand parser."""

Expand Down
26 changes: 17 additions & 9 deletions src/pre_commit_terraform/terraform_docs_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ def populate_argument_parser(subcommand_parser: ArgumentParser) -> None:
'replace the entire README.md file each time.'
)
subcommand_parser.add_argument(
'--dest', dest='dest', default='README.md',
'--dest',
dest='dest',
default='README.md',
)
subcommand_parser.add_argument(
'--sort-inputs-by-required', dest='sort', action='store_true',
'--sort-inputs-by-required',
dest='sort',
action='store_true',
help='[deprecated] use --sort-by-required instead',
)
subcommand_parser.add_argument(
'--sort-by-required', dest='sort', action='store_true',
'--sort-by-required',
dest='sort',
action='store_true',
)
subcommand_parser.add_argument(
'--with-aggregate-type-defaults',
Expand All @@ -51,8 +57,9 @@ def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:

dirs: list[str] = []
for filename in cast_to(list[str], parsed_cli_args.filenames):
if (os.path.realpath(filename) not in dirs and
(filename.endswith(".tf") or filename.endswith(".tfvars"))):
if os.path.realpath(filename) not in dirs and (
filename.endswith('.tf') or filename.endswith('.tfvars')
):
dirs.append(os.path.dirname(filename))

retval = ReturnCode.OK
Expand All @@ -64,13 +71,14 @@ def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:
if cast_to(bool, parsed_cli_args.sort):
procArgs.append('--sort-by-required')
procArgs.append('md')
procArgs.append("./{dir}".format(dir=dir))
procArgs.append('./{dir}'.format(dir=dir))
procArgs.append('>')
procArgs.append(
'./{dir}/{dest}'.
format(dir=dir, dest=cast_to(bool, parsed_cli_args.dest)),
'./{dir}/{dest}'.format(
dir=dir, dest=cast_to(bool, parsed_cli_args.dest)
),
)
subprocess.check_call(" ".join(procArgs), shell=True)
subprocess.check_call(' '.join(procArgs), shell=True)
except subprocess.CalledProcessError as e:
print(e)
retval = ReturnCode.ERROR
Expand Down
20 changes: 12 additions & 8 deletions tests/pytest/_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@
),
)
def test_known_interrupts(
capsys: pytest.CaptureFixture[str],
expected_stderr: str,
monkeypatch: pytest.MonkeyPatch,
raised_error: BaseException,
capsys: pytest.CaptureFixture[str],
expected_stderr: str,
monkeypatch: pytest.MonkeyPatch,
raised_error: BaseException,
) -> None:
"""Check that known interrupts are turned into return code 1."""

class CustomCmdStub:
CLI_SUBCOMMAND_NAME = 'sentinel'

def populate_argument_parser(
self, subcommand_parser: ArgumentParser,
self,
subcommand_parser: ArgumentParser,
) -> None:
return None

Expand All @@ -72,15 +74,17 @@ def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType:


def test_app_exit(
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Check that an exit exception is re-raised."""

class CustomCmdStub:
CLI_SUBCOMMAND_NAME = 'sentinel'

def populate_argument_parser(
self, subcommand_parser: ArgumentParser,
self,
subcommand_parser: ArgumentParser,
) -> None:
return None

Expand Down
17 changes: 8 additions & 9 deletions tests/pytest/terraform_docs_replace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def test_arg_parser_populated() -> None:
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.*$'
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
Expand Down Expand Up @@ -70,10 +69,10 @@ def test_check_is_deprecated() -> None:
'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,
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()
Expand All @@ -86,7 +85,7 @@ def test_control_flow_positive(
assert ReturnCode.OK == invoke_cli_app(parsed_cli_args)

executed_commands = [
cmd for ((cmd, ), _shell) in check_call_mock.call_args_list
cmd for ((cmd,), _shell) in check_call_mock.call_args_list
]

assert len(expected_cmds) == check_call_mock.call_count
Expand All @@ -98,8 +97,8 @@ def test_control_flow_positive(
'pre_commit_terraform.terraform_docs_replace',
)
def test_control_flow_negative(
mocker: pytest_mock.MockerFixture,
monkeypatch: pytest.MonkeyPatch,
mocker: pytest_mock.MockerFixture,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Check that the subcommand's error processing works."""
parsed_cli_args = Namespace(
Expand Down
Loading