pre-commit-hooks/tests/check_permitted_path_characters_test.py
Noah Pendleton 9bb6ebc34d 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 `.-_`.
2022-03-01 18:13:56 -05:00

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