Skip to content

Commit

Permalink
Ignore line endings in exclude-file (codespell-project#1889)
Browse files Browse the repository at this point in the history
Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
Co-authored-by: Peter Newman <peternewman@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 23, 2023
1 parent 8c703d1 commit 86b30db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
4 changes: 2 additions & 2 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ def parse_ignore_words_option(ignore_words_option: List[str]) -> Set[str]:
def build_exclude_hashes(filename: str, exclude_lines: Set[str]) -> None:
with open(filename, encoding="utf-8") as f:
for line in f:
exclude_lines.add(line)
exclude_lines.add(line.rstrip())


def build_ignore_words(filename: str, ignore_words: Set[str]) -> None:
Expand Down Expand Up @@ -896,7 +896,7 @@ def parse_file(
return bad_count

for i, line in enumerate(lines):
if line in exclude_lines:
if line.rstrip() in exclude_lines:
continue

fixed_words = set()
Expand Down
41 changes: 34 additions & 7 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,21 @@ def test_ignore_dictionary(
) -> None:
"""Test ignore dictionary functionality."""
bad_name = tmp_path / "bad.txt"
bad_name.write_text("1 abandonned 1\n2 abandonned 2\nabondon\n")
assert cs.main(bad_name) == 3
bad_name.write_text(
"1 abandonned 1\n"
"2 abandonned 2\n"
"3 abandonned 3\r\n"
"4 abilty 4\n"
"5 abilty 5\n"
"6 abilty 6\r\n"
"7 ackward 7\n"
"8 ackward 8\n"
"9 ackward 9\r\n"
"abondon\n"
)
assert cs.main(bad_name) == 10
fname = tmp_path / "ignore.txt"
fname.write_text("abandonned\n")
fname.write_text("abandonned\nabilty\r\nackward")
assert cs.main("-I", fname, bad_name) == 1


Expand Down Expand Up @@ -363,11 +374,27 @@ def test_exclude_file(
) -> None:
"""Test exclude file functionality."""
bad_name = tmp_path / "bad.txt"
bad_name.write_bytes(b"1 abandonned 1\n2 abandonned 2\n")
assert cs.main(bad_name) == 2
# check all possible combinations of lines to ignore and ignores
combinations = "".join(
f"{n} abandonned {n}\n"
f"{n} abandonned {n}\r\n"
f"{n} abandonned {n} \n"
f"{n} abandonned {n} \r\n"
for n in range(1, 5)
)
bad_name.write_bytes(
(combinations + "5 abandonned 5\n6 abandonned 6").encode("utf-8")
)
assert cs.main(bad_name) == 18
fname = tmp_path / "tmp.txt"
fname.write_bytes(b"1 abandonned 1\n")
assert cs.main(bad_name) == 2
fname.write_bytes(
b"1 abandonned 1\n"
b"2 abandonned 2\r\n"
b"3 abandonned 3 \n"
b"4 abandonned 4 \r\n"
b"6 abandonned 6\n"
)
assert cs.main(bad_name) == 18
assert cs.main("-x", fname, bad_name) == 1


Expand Down

0 comments on commit 86b30db

Please sign in to comment.