mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 12:34:17 +00:00
Add check-yaml-filename-extension hook
This commit is contained in:
parent
5420c705a4
commit
1ea1ff2061
5 changed files with 66 additions and 0 deletions
|
|
@ -95,6 +95,12 @@
|
||||||
entry: check-yaml
|
entry: check-yaml
|
||||||
language: python
|
language: python
|
||||||
types: [yaml]
|
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
|
- id: debug-statements
|
||||||
name: debug statements (python)
|
name: debug statements (python)
|
||||||
description: checks for debugger imports and py37+ `breakpoint()` calls in python source.
|
description: checks for debugger imports and py37+ `breakpoint()` calls in python source.
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,10 @@ Attempts to load all yaml files to verify syntax.
|
||||||
portability to other yaml implementations.
|
portability to other yaml implementations.
|
||||||
Implies `--allow-multiple-documents`.
|
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`
|
#### `debug-statements`
|
||||||
Check for debugger imports and py37+ `breakpoint()` calls in python source.
|
Check for debugger imports and py37+ `breakpoint()` calls in python source.
|
||||||
|
|
||||||
|
|
|
||||||
29
pre_commit_hooks/check_yaml_filename_extension.py
Normal file
29
pre_commit_hooks/check_yaml_filename_extension.py
Normal file
|
|
@ -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())
|
||||||
|
|
@ -45,6 +45,7 @@ console_scripts =
|
||||||
check-vcs-permalinks = pre_commit_hooks.check_vcs_permalinks:main
|
check-vcs-permalinks = pre_commit_hooks.check_vcs_permalinks:main
|
||||||
check-xml = pre_commit_hooks.check_xml:main
|
check-xml = pre_commit_hooks.check_xml:main
|
||||||
check-yaml = pre_commit_hooks.check_yaml: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
|
debug-statement-hook = pre_commit_hooks.debug_statement_hook:main
|
||||||
destroyed-symlinks = pre_commit_hooks.destroyed_symlinks:main
|
destroyed-symlinks = pre_commit_hooks.destroyed_symlinks:main
|
||||||
detect-aws-credentials = pre_commit_hooks.detect_aws_credentials:main
|
detect-aws-credentials = pre_commit_hooks.detect_aws_credentials:main
|
||||||
|
|
|
||||||
26
tests/check_yaml_filename_extension_test.py
Normal file
26
tests/check_yaml_filename_extension_test.py
Normal file
|
|
@ -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()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue