mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-06-29 06:00:45 +00:00
Add remove-em-dash hook
New fixer hook that replaces UTF-8 em-dashes (U+2014) with a plain hyphen (-), modeled on the trailing-whitespace hook. - pre_commit_hooks/remove_em_dash.py: the fixer (binary-safe, UTF-8 only) - tests/remove_em_dash_test.py: full coverage of fix and no-op cases - registered in setup.cfg, .pre-commit-hooks.yaml, and README.md Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
fa6b006f0e
commit
aba8a7597a
5 changed files with 84 additions and 0 deletions
36
tests/remove_em_dash_test.py
Normal file
36
tests/remove_em_dash_test.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.remove_em_dash import main
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('text', 'expected'),
|
||||
(
|
||||
('foo\N{EM DASH}bar\n', b'foo-bar\n'),
|
||||
('foo \N{EM DASH} bar\n', b'foo - bar\n'),
|
||||
('a\N{EM DASH}b\N{EM DASH}c\n', b'a-b-c\n'),
|
||||
('x\N{EM DASH}y\r\nz\r\n', b'x-y\r\nz\r\n'),
|
||||
),
|
||||
)
|
||||
def test_fixes_em_dash(text, expected, tmpdir):
|
||||
path = tmpdir.join('file.txt')
|
||||
path.write_binary(text.encode())
|
||||
assert main((str(path),)) == 1
|
||||
assert path.read_binary() == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'contents',
|
||||
(
|
||||
pytest.param(b'foo-bar\n', id='plain-hyphen'),
|
||||
pytest.param(b'no em dashes here\n', id='no-dash'),
|
||||
pytest.param(b'<a>\x97</a>\n', id='windows-1252-em-dash'),
|
||||
),
|
||||
)
|
||||
def test_noop_without_utf8_em_dash(contents, tmpdir):
|
||||
path = tmpdir.join('file.txt')
|
||||
path.write_binary(contents)
|
||||
assert main((str(path),)) == 0
|
||||
assert path.read_binary() == contents
|
||||
Loading…
Add table
Add a link
Reference in a new issue