diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index f8523d4..0caa87e 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -95,6 +95,12 @@ entry: check-yaml language: python types: [yaml] +- id: check-yaml-filename-extension + name: check yaml filename extension + description: checks that yaml file names ends with specified extension. + entry: check-yaml-filename-extension + language: python + types: [yaml] - id: debug-statements name: debug statements (python) description: checks for debugger imports and py37+ `breakpoint()` calls in python source. diff --git a/README.md b/README.md index 6dcbf0d..cd37ea2 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,10 @@ Attempts to load all yaml files to verify syntax. portability to other yaml implementations. Implies `--allow-multiple-documents`. +#### `check-yaml-filename-extension` +Ensures that yaml file names ends with a specified extension. + - `--extension` - Specifies which extension to use `yaml` or `yml`. Defaults to `yaml`. + #### `debug-statements` Check for debugger imports and py37+ `breakpoint()` calls in python source. diff --git a/pre_commit_hooks/check_yaml_filename_extension.py b/pre_commit_hooks/check_yaml_filename_extension.py new file mode 100644 index 0000000..5e0bd9e --- /dev/null +++ b/pre_commit_hooks/check_yaml_filename_extension.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +import argparse +import os +from typing import Sequence + +from pre_commit_hooks.util import cmd_output + + +def main(argv: Sequence[str] | None = None) -> int: + parser = argparse.ArgumentParser() + parser.add_argument('--extension', choices=['yaml', 'yml'], default='yaml') + parser.add_argument('filenames', nargs='*', help='Filenames to check.') + + args = parser.parse_args(argv) + extension = f'.{args.extension}' + + retval = 0 + for filename in args.filenames: + if not filename.endswith(extension): + new_filename = f'{os.path.splitext(filename)[0]}{extension}' + cmd_output('git', 'mv', filename, new_filename) + retval = 1 + + return retval + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/setup.cfg b/setup.cfg index d4658e2..b3f8751 100644 --- a/setup.cfg +++ b/setup.cfg @@ -45,6 +45,7 @@ console_scripts = check-vcs-permalinks = pre_commit_hooks.check_vcs_permalinks:main check-xml = pre_commit_hooks.check_xml:main check-yaml = pre_commit_hooks.check_yaml:main + check-yaml-filename-extension = pre_commit_hooks.check_yaml_filename_extension:main debug-statement-hook = pre_commit_hooks.debug_statement_hook:main destroyed-symlinks = pre_commit_hooks.destroyed_symlinks:main detect-aws-credentials = pre_commit_hooks.detect_aws_credentials:main diff --git a/tests/check_yaml_filename_extension_test.py b/tests/check_yaml_filename_extension_test.py new file mode 100644 index 0000000..afbcbd9 --- /dev/null +++ b/tests/check_yaml_filename_extension_test.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +import pytest + +from pre_commit_hooks.check_yaml_filename_extension import main +from pre_commit_hooks.util import cmd_output + + +@pytest.mark.parametrize( + ('filename', 'new_filename', 'expected_retval'), ( + ('file_1.yml', 'file_1.yaml', 1), + ('.file_2.yml', '.file_2.yaml', 1), + ('file_3.yaml', 'file_3.yaml', 0), + ), +) +def test_main(temp_git_dir, filename, new_filename, expected_retval): + with temp_git_dir.as_cwd(): + temp_git_dir.join(filename).write('---') + cmd_output('git', 'add', filename) + + retv = main([filename]) + + assert retv == expected_retval + assert temp_git_dir.join(new_filename).exists() + + assert retv == 0 or not temp_git_dir.join(filename).exists()