Skip to content

Commit

Permalink
Using pre-commit built-in text files detection feature (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Mar 31, 2023
1 parent 5802f39 commit d688cf7
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 84 deletions.
23 changes: 10 additions & 13 deletions .pre-commit-config.yaml
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-hooks.yaml
Expand Up @@ -3,27 +3,27 @@
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
description: "Replace tabs by whitespaces before committing"
entry: remove_tabs
language: python
args: [ --whitespaces-count, '4' ]
files: ''
types: [text]
exclude: (Makefile|debian/rules|.gitmodules)(\.in)?$
- id: chmod
name: Set file permissions
Expand All @@ -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]
4 changes: 1 addition & 3 deletions pre_commit_hooks/forbid_crlf.py
@@ -1,5 +1,4 @@
import argparse, sys
from .utils import is_textfile


def contains_crlf(filename):
Expand All @@ -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}")
Expand Down
4 changes: 1 addition & 3 deletions pre_commit_hooks/forbid_tabs.py
@@ -1,5 +1,4 @@
import argparse, sys
from .utils import is_textfile


def contains_tabs(filename):
Expand All @@ -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}")
Expand Down
4 changes: 1 addition & 3 deletions pre_commit_hooks/remove_crlf.py
@@ -1,5 +1,4 @@
import argparse, sys
from .utils import is_textfile


def contains_crlf(filename):
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions pre_commit_hooks/remove_tabs.py
@@ -1,5 +1,4 @@
import argparse, sys
from .utils import is_textfile


def contains_tabs(filename):
Expand All @@ -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"
Expand Down
24 changes: 0 additions & 24 deletions pre_commit_hooks/utils.py

This file was deleted.

1 change: 1 addition & 0 deletions requirements-dev.txt
Expand Up @@ -2,6 +2,7 @@
-e .
# Install development specific dependencies
pre-commit
pylint
pytest
pytest-cov
coverage
14 changes: 0 additions & 14 deletions tests/remove_crlf_test.py
Expand Up @@ -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(
Expand Down
16 changes: 0 additions & 16 deletions tests/text_utils_test.py

This file was deleted.

0 comments on commit d688cf7

Please sign in to comment.