mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 19:46:54 +00:00
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 `.-_`.
50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
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
|