diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c86ceaf..e6e31a3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,21 +43,10 @@ repos: args: - --py37-plus exclude: ^tests/resources/.* - - repo: https://github.com/pre-commit/mirrors-pylint - rev: v3.0.0a5 - hooks: - - id: pylint - args: - - --rcfile=.pylintrc - - --reports=no - - --py-version=3.7 - additional_dependencies: [pytest, rapidfuzz] - exclude: ^tests/resources/.*(init_with_license\.py|todo).*$ - repo: https://github.com/psf/black rev: 23.3.0 hooks: - id: black - args: [--quiet] exclude: ^tests/resources/ - repo: https://github.com/Lucas-C/pre-commit-hooks-bandit rev: v1.0.6 @@ -77,8 +66,16 @@ repos: - --show-error-context - repo: local hooks: - - id: py.test - name: py.test + - id: pylint + name: pylint + # 3x faster than the official pylint hook, and has no issue with imports + # (tested with: time pre-commit run pylint --all-files) + language: system + entry: pylint + files: \.py$ + exclude: ^tests/resources/.*(init_with_license|todo) + - id: pytest + name: pytest language: python additional_dependencies: [pytest, pytest-cov, coverage, rapidfuzz] entry: pytest -sv diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index fa64480..7aad14f 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -3,19 +3,19 @@ description: "Forbid files containing CRLF end-lines to be committed" entry: forbid_crlf language: python - files: '' + types: [text] - id: remove-crlf name: CRLF end-lines remover description: "Replace CRLF end-lines by LF ones before committing" entry: remove_crlf language: python - files: '' + types: [text] - id: forbid-tabs name: No-tabs checker description: "Forbid files containing tabs to be committed" entry: forbid_tabs language: python - files: '' + types: [text] exclude: (Makefile|debian/rules|.gitmodules)(\.in)?$ - id: remove-tabs name: Tabs remover @@ -23,7 +23,7 @@ entry: remove_tabs language: python args: [ --whitespaces-count, '4' ] - files: '' + types: [text] exclude: (Makefile|debian/rules|.gitmodules)(\.in)?$ - id: chmod name: Set file permissions @@ -34,4 +34,4 @@ description: "Insert a short license disclaimer as a header comment in source files" entry: insert_license language: python - files: '.*/.*' + types: [text] diff --git a/pre_commit_hooks/forbid_crlf.py b/pre_commit_hooks/forbid_crlf.py index b3bc99b..eb48969 100644 --- a/pre_commit_hooks/forbid_crlf.py +++ b/pre_commit_hooks/forbid_crlf.py @@ -1,5 +1,4 @@ import argparse, sys -from .utils import is_textfile def contains_crlf(filename): @@ -14,8 +13,7 @@ def main(argv=None): parser = argparse.ArgumentParser() parser.add_argument("filenames", nargs="*", help="filenames to check") args = parser.parse_args(argv) - text_files = [f for f in args.filenames if is_textfile(f)] - files_with_crlf = [f for f in text_files if contains_crlf(f)] + files_with_crlf = [f for f in args.filenames if contains_crlf(f)] return_code = 0 for file_with_crlf in files_with_crlf: print(f"CRLF end-lines detected in file: {file_with_crlf}") diff --git a/pre_commit_hooks/forbid_tabs.py b/pre_commit_hooks/forbid_tabs.py index 9d2e1f4..2fd7fbe 100644 --- a/pre_commit_hooks/forbid_tabs.py +++ b/pre_commit_hooks/forbid_tabs.py @@ -1,5 +1,4 @@ import argparse, sys -from .utils import is_textfile def contains_tabs(filename): @@ -11,8 +10,7 @@ def main(argv=None): parser = argparse.ArgumentParser() parser.add_argument("filenames", nargs="*", help="filenames to check") args = parser.parse_args(argv) - text_files = [f for f in args.filenames if is_textfile(f)] - files_with_tabs = [f for f in text_files if contains_tabs(f)] + files_with_tabs = [f for f in args.filenames if contains_tabs(f)] return_code = 0 for file_with_tabs in files_with_tabs: print(f"Tabs detected in file: {file_with_tabs}") diff --git a/pre_commit_hooks/remove_crlf.py b/pre_commit_hooks/remove_crlf.py index 769722c..dd508df 100644 --- a/pre_commit_hooks/remove_crlf.py +++ b/pre_commit_hooks/remove_crlf.py @@ -1,5 +1,4 @@ import argparse, sys -from .utils import is_textfile def contains_crlf(filename): @@ -23,8 +22,7 @@ def main(argv=None): parser = argparse.ArgumentParser() parser.add_argument("filenames", nargs="*", help="filenames to check") args = parser.parse_args(argv) - text_files = [f for f in args.filenames if is_textfile(f)] - files_with_crlf = [f for f in text_files if contains_crlf(f)] + files_with_crlf = [f for f in args.filenames if contains_crlf(f)] for file_with_crlf in files_with_crlf: print(f"Removing CRLF end-lines in: {file_with_crlf}") removes_crlf_in_file(file_with_crlf) diff --git a/pre_commit_hooks/remove_tabs.py b/pre_commit_hooks/remove_tabs.py index 597765c..35a676f 100644 --- a/pre_commit_hooks/remove_tabs.py +++ b/pre_commit_hooks/remove_tabs.py @@ -1,5 +1,4 @@ import argparse, sys -from .utils import is_textfile def contains_tabs(filename): @@ -26,8 +25,7 @@ def main(argv=None): ) parser.add_argument("filenames", nargs="*", help="filenames to check") args = parser.parse_args(argv) - text_files = [f for f in args.filenames if is_textfile(f)] - files_with_tabs = [f for f in text_files if contains_tabs(f)] + files_with_tabs = [f for f in args.filenames if contains_tabs(f)] for file_with_tabs in files_with_tabs: print( f"Substituting tabs in: {file_with_tabs} by {args.whitespaces_count} whitespaces" diff --git a/pre_commit_hooks/utils.py b/pre_commit_hooks/utils.py deleted file mode 100644 index 04837c0..0000000 --- a/pre_commit_hooks/utils.py +++ /dev/null @@ -1,24 +0,0 @@ -# Taken from: http://code.activestate.com/recipes/173220-test-if-a-file-or-string-is-text-or-binary/ - -KNOWN_BINARY_FILE_EXTS = (".pdf",) - - -def is_textfile(filename, blocksize=512): - if any(filename.endswith(ext) for ext in KNOWN_BINARY_FILE_EXTS): - return False - with open(filename, "rb") as text_file: - return is_text(text_file.read(blocksize)) - - -def is_text(stuff): - if b"\0" in stuff: - return False - if not stuff: # Empty files are considered text - return True - # Try to decode as UTF-8 - try: - stuff.decode("utf8") - except UnicodeDecodeError: - return False - else: - return True diff --git a/requirements-dev.txt b/requirements-dev.txt index 36965cc..e92e337 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,6 +2,7 @@ -e . # Install development specific dependencies pre-commit +pylint pytest pytest-cov coverage diff --git a/tests/remove_crlf_test.py b/tests/remove_crlf_test.py index 5a640d1..edd51a0 100644 --- a/tests/remove_crlf_test.py +++ b/tests/remove_crlf_test.py @@ -19,20 +19,6 @@ def test_remove_crlf(input_s, expected, tmpdir): assert input_file.read_bytes() == bytes(expected, "UTF-8") -@pytest.mark.parametrize( - ("input_s", "expected"), - ( - ("foo\r\nbar", "foo\r\nbar"), - ("bar\nbaz\r\n", "bar\nbaz\r\n"), - ), -) -def test_noremove_crlf(input_s, expected, tmpdir): - input_file = Path(tmpdir.join("file.pdf")) - input_file.write_bytes(bytes(input_s, "UTF-8")) - assert remove_crlf([str(input_file)]) == 0 - assert input_file.read_bytes() == bytes(expected, "UTF-8") - - @pytest.mark.parametrize(("arg"), ("", "a.b", "a/b")) def test_badopt(arg): with pytest.raises( diff --git a/tests/text_utils_test.py b/tests/text_utils_test.py deleted file mode 100644 index 9dc4b2b..0000000 --- a/tests/text_utils_test.py +++ /dev/null @@ -1,16 +0,0 @@ -import pytest - -from pre_commit_hooks.utils import is_text - - -@pytest.mark.parametrize( - ("input_s", "expected"), - ( - (b"foo \r\nbar", True), - (b"\xe9 \n\n", False), - (b"foo \x00bar", False), # NUL character is not considered text - (b"", True), # Empty file is considered text - ), -) -def test_is_text(input_s, expected): - assert is_text(input_s) == expected