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
3 changes: 3 additions & 0 deletions src/pre_commit_terraform/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def invoke_cli_app(cli_args: list[str]) -> ReturnCodeType:

Includes initializing parsers of all the sub-apps and
choosing what to execute.

Returns:
ReturnCodeType: The return code of the app.
"""
root_cli_parser = initialize_argument_parser()
parsed_cli_args = root_cli_parser.parse_args(cli_args)
Expand Down
6 changes: 5 additions & 1 deletion src/pre_commit_terraform/_cli_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def attach_subcommand_parsers_to(root_cli_parser: ArgumentParser, /) -> None:


def initialize_argument_parser() -> ArgumentParser:
"""Return the root argument parser with sub-commands."""
"""Return the root argument parser with sub-commands.

Returns:
ArgumentParser: The root parser with sub-commands attached.
"""
root_cli_parser = ArgumentParser(prog=f'python -m {__package__ !s}')
attach_subcommand_parsers_to(root_cli_parser)
return root_cli_parser
Expand Down
25 changes: 16 additions & 9 deletions src/pre_commit_terraform/terraform_docs_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


def populate_argument_parser(subcommand_parser: ArgumentParser) -> None:
"""Populate the parser for the subcommand."""
subcommand_parser.description = (
'Run terraform-docs on a set of files. Follows the standard '
'convention of pulling the documentation from main.tf in order to '
Expand Down Expand Up @@ -47,13 +48,19 @@ def populate_argument_parser(subcommand_parser: ArgumentParser) -> None:


def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:
"""Run the entry-point of the CLI app.

Returns:
ReturnCodeType: The return code of the app.
"""
warnings.warn(
'`terraform_docs_replace` hook is DEPRECATED.'
'For migration instructions see '
'https://github.com/antonbabenko/pre-commit-terraform/issues/248'
'#issuecomment-1290829226',
category=UserWarning,
stacklevel=1, # It's should be 2, but tests are failing w/ values >1. As it's deprecated hook, it's safe to leave it as is w/o fixing it later.
stacklevel=1, # It's should be 2, but tests are failing w/ values >1.
# As it's deprecated hook, it's safe to leave it as is w/o fixing it.
)

dirs: list[str] = []
Expand All @@ -65,24 +72,24 @@ def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:

retval = ReturnCode.OK

for dir in dirs:
for directory in dirs:
try:
procArgs = []
procArgs.append('terraform-docs')
proc_args = []
proc_args.append('terraform-docs')
if cast_to('bool', parsed_cli_args.sort):
procArgs.append('--sort-by-required')
procArgs.extend(
proc_args.append('--sort-by-required')
proc_args.extend(
(
'md',
f'./{dir}',
f'./{directory}',
'>',
'./{dir}/{dest}'.format(
dir=dir,
dir=directory,
dest=cast_to('str', parsed_cli_args.dest),
),
),
)
subprocess.check_call(' '.join(procArgs), shell=True)
subprocess.check_call(' '.join(proc_args), shell=True)
except subprocess.CalledProcessError as e:
print(e)
retval = ReturnCode.ERROR
Expand Down
3 changes: 1 addition & 2 deletions tests/pytest/_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
@pytest.mark.parametrize(
('raised_error', 'expected_stderr'),
(
# pytest.param(PreCommitTerraformExit('sentinel'), 'App exiting: sentinel', id='app-exit'),
pytest.param(
PreCommitTerraformRuntimeError('sentinel'),
'App execution took an unexpected turn: sentinel. Exiting...',
Expand Down Expand Up @@ -97,7 +96,7 @@ def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType:
[CustomCmdStub()],
)

with pytest.raises(PreCommitTerraformExit, match='^sentinel$'):
with pytest.raises(PreCommitTerraformExit, match=r'^sentinel$'):
invoke_cli_app(['sentinel'])

captured_outputs = capsys.readouterr()
Expand Down
Loading