Skip to content

Commit

Permalink
trailing-whitespace: Add option to only lint files without fixing them
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Rubo committed Feb 1, 2024
1 parent 77c7147 commit 1d17bf8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,14 @@ very specific format. You must opt in to this by setting [`files`](https://pre-
#### `trailing-whitespace`
Trims trailing whitespace.
Detects and optionally trims trailing whitespace.
- By default, this hook trims all whitespace from the ends of lines.
To specify a custom set of characters to trim instead, use `args: [--chars,"<chars to trim>"]`.
To only lint files without fixing them, specify `--no-fix`.
- To preserve Markdown [hard linebreaks](https://github.github.com/gfm/#hard-line-break)
use `args: [--markdown-linebreak-ext=md]` (or other extensions used
by your markdownfiles). If for some reason you want to treat all files
as markdown, use `--markdown-linebreak-ext=*`.
- By default, this hook trims all whitespace from the ends of lines.
To specify a custom set of characters to trim instead, use `args: [--chars,"<chars to trim>"]`.

### Deprecated / replaced hooks

Expand Down
35 changes: 28 additions & 7 deletions pre_commit_hooks/trailing_whitespace_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
from typing import Sequence


def _fix_file(
def _process_file(
filename: str,
is_markdown: bool,
chars: bytes | None,
fix: bool,
) -> bool:
with open(filename, mode='rb') as file_processed:
lines = file_processed.readlines()
newlines = [_process_line(line, is_markdown, chars) for line in lines]
if newlines != lines:
with open(filename, mode='wb') as file_processed:
for line in newlines:
file_processed.write(line)
if fix:
with open(filename, mode='wb') as file_processed:
for line in newlines:
file_processed.write(line)
return True
else:
return False
Expand Down Expand Up @@ -61,10 +63,29 @@ def main(argv: Sequence[str] | None = None) -> int:
parser.add_argument(
'--chars',
help=(
'The set of characters to strip from the end of lines. '
'The set of characters to consider as whitespace at the end of '
'lines. '
'Defaults to all whitespace characters.'
),
)
parser.add_argument(
'--fix',
action='store_true',
help=(
'Fix files by stripping detected trailing whitespace. '
'Default: On'
),
)
parser.add_argument(
'--no-fix',
dest='fix',
action='store_false',
help=(
'Do not fix files by stripping detected trailing whitespace. '
'Default: Off, meaning files do get fixed.'
),
)
parser.set_defaults(fix=True)
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
args = parser.parse_args(argv)

Expand Down Expand Up @@ -93,8 +114,8 @@ def main(argv: Sequence[str] | None = None) -> int:
for filename in args.filenames:
_, extension = os.path.splitext(filename.lower())
md = all_markdown or extension in md_exts
if _fix_file(filename, md, chars):
print(f'Fixing {filename}')
if _process_file(filename, md, chars, args.fix):
print(('Fixing ' if args.fix else 'Not fixing ') + filename)
return_code = 1
return return_code

Expand Down
15 changes: 15 additions & 0 deletions tests/trailing_whitespace_fixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ def test_fixes_trailing_whitespace(input_s, expected, tmpdir):
assert path.read() == expected


@pytest.mark.parametrize(
('arg', 'expected'),
(
('--fix', 'foo\nbar\n'),
('--no-fix', 'foo \nbar \n'),
),
)
def test_fix_no_fix(tmpdir, arg, expected):
input_s = 'foo \nbar \n'
path = tmpdir.join('file.txt')
path.write(input_s)
assert main((str(path), arg)) == 1
assert path.read() == expected


def test_ok_no_newline_end_of_file(tmpdir):
filename = tmpdir.join('f')
filename.write_binary(b'foo\nbar')
Expand Down

0 comments on commit 1d17bf8

Please sign in to comment.