mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 19:46:54 +00:00
Add check for permitted characters in pathnames
Add a new checker that checks for allowed characters in pathnames. Includes arguments for overriding the default allowed list and extending it. Default allow list is alphanumeric plus `.-_`.
This commit is contained in:
parent
69b4df5589
commit
9bb6ebc34d
5 changed files with 109 additions and 0 deletions
50
tests/check_permitted_path_characters_test.py
Normal file
50
tests/check_permitted_path_characters_test.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from pre_commit_hooks.check_permitted_path_characters import main
|
||||
|
||||
|
||||
def test_main_all_pass():
|
||||
ret = main(
|
||||
[
|
||||
'/some/path/foo_test.py',
|
||||
'./relative/path/bar_test.py',
|
||||
'filename-only.py',
|
||||
],
|
||||
)
|
||||
assert ret == 0
|
||||
|
||||
|
||||
def test_main_default_chars():
|
||||
# use '--' for separating pathnames from args, so pathnames with leading
|
||||
# '-' are not interpreted as flags
|
||||
ret = main(
|
||||
[
|
||||
'--',
|
||||
'-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_',
|
||||
'abcdefghijklmnopqrstuvwxyz',
|
||||
],
|
||||
)
|
||||
assert ret == 0
|
||||
|
||||
ret = main(['+'])
|
||||
assert ret == 1
|
||||
|
||||
|
||||
def test_main_invalid_dir():
|
||||
ret = main(['--', '/some+funky%%dir/pathname'])
|
||||
assert ret == 1
|
||||
|
||||
|
||||
def test_main_allowlist():
|
||||
ret = main(['--allowlist', 'abc', 'invalid.py'])
|
||||
assert ret == 1
|
||||
ret = main(['--allowlist', 'abc', 'cba'])
|
||||
assert ret == 0
|
||||
# a pathological case
|
||||
ret = main(['--allowlist', '\b\x01\t/.', '\b.\x01/\t'])
|
||||
assert ret == 0
|
||||
|
||||
|
||||
def test_main_extra_allowlist():
|
||||
ret = main(['--extra-allowlist', '+', 'valid+'])
|
||||
assert ret == 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue