Skip to content

Commit df32c81

Browse files
committed
Add test case for --git-only option
1 parent c5de971 commit df32c81

2 files changed

Lines changed: 84 additions & 10 deletions

File tree

codespell_lib/_codespell.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import itertools
2424
import os
2525
import re
26-
import shutil
2726
import subprocess
2827
import sys
2928
import textwrap
@@ -1103,14 +1102,6 @@ def flatten_clean_comma_separated_arguments(
11031102
]
11041103

11051104

1106-
def get_git_executable_path() -> str:
1107-
# Find the full path to the git executable
1108-
git_path = shutil.which("git")
1109-
if git_path is None:
1110-
raise FileNotFoundError("Git executable not found in PATH")
1111-
return git_path
1112-
1113-
11141105
def get_git_tracked_files(
11151106
root: str, files: Iterable[str], glob_match: GlobMatch, check_hidden: bool
11161107
) -> Iterable[str]:
@@ -1131,7 +1122,7 @@ def get_git_tracked_files(
11311122
if not check_hidden:
11321123
exclude_patterns.append(":(exclude)**/.*")
11331124

1134-
git_executable = get_git_executable_path()
1125+
git_executable = 'git'
11351126

11361127
try:
11371128
result = subprocess.run(
@@ -1368,6 +1359,7 @@ def main(*args: str) -> int:
13681359
)
13691360

13701361
bad_count = 0
1362+
13711363
# Build the list of all files based on the git_only option
13721364
if options.git_only:
13731365
all_files = get_git_tracked_files(

codespell_lib/tests/test_basic.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ def test_exclude_file(
499499
bad_name.write_bytes(
500500
(combinations + "5 abandonned 5\n6 abandonned 6").encode("utf-8")
501501
)
502+
502503
assert cs.main(bad_name) == 18
503504
fname = tmp_path / "tmp.txt"
504505
fname.write_bytes(
@@ -519,6 +520,87 @@ def test_exclude_file(
519520
assert cs.main("-x", f"{fname_dummy1},{fname},{fname_dummy2}", bad_name) == 1
520521

521522

523+
def test_git_only_exclude_file(
524+
tmp_path: Path,
525+
capsys: pytest.CaptureFixture[str],
526+
monkeypatch: pytest.MonkeyPatch
527+
) -> None:
528+
monkeypatch.chdir(tmp_path)
529+
"""Test exclude file functionality."""
530+
bad_name = tmp_path / "bad.txt"
531+
# check all possible combinations of lines to ignore and ignores
532+
combinations = "".join(
533+
f"{n} abandonned {n}\n"
534+
f"{n} abandonned {n}\r\n"
535+
f"{n} abandonned {n} \n"
536+
f"{n} abandonned {n} \r\n"
537+
for n in range(1, 5)
538+
)
539+
bad_name.write_bytes(
540+
(combinations + "5 abandonned 5\n6 abandonned 6").encode("utf-8")
541+
)
542+
543+
subprocess.run(
544+
['git', '-C', tmp_path, 'init'],
545+
capture_output=False,
546+
check=True,
547+
text=True,
548+
)
549+
subprocess.run(
550+
['git', '-C', tmp_path, 'add', bad_name],
551+
capture_output=False,
552+
check=True,
553+
text=True,
554+
)
555+
556+
assert cs.main(bad_name) == 18
557+
fname = tmp_path / "tmp.txt"
558+
fname.write_bytes(
559+
b"1 abandonned 1\n"
560+
b"2 abandonned 2\r\n"
561+
b"3 abandonned 3 \n"
562+
b"4 abandonned 4 \r\n"
563+
b"6 abandonned 6\n"
564+
)
565+
566+
result = subprocess.run(
567+
['git', '-C', tmp_path, 'ls-files'],
568+
capture_output=True,
569+
check=True,
570+
text=True,
571+
)
572+
573+
# Should have 23 total errors (bad_name + fname)
574+
assert cs.main(tmp_path) == 23
575+
576+
# Before adding to git, should not report on fname, only 18 error in bad.txt
577+
assert cs.main("--git-only", tmp_path) == 18
578+
subprocess.run(
579+
['git', '-C', tmp_path, 'add', fname],
580+
capture_output=False,
581+
check=True,
582+
text=True,
583+
)
584+
assert cs.main(tmp_path) == 23
585+
# After adding to git, should report on fname
586+
assert cs.main("--git-only", tmp_path) == 23
587+
# After adding to git, should not report on excluded file
588+
assert cs.main("--git-only", "-x", fname, tmp_path) == 1
589+
# comma-separated list of files
590+
fname_dummy1 = tmp_path / "dummy1.txt"
591+
fname_dummy1.touch()
592+
fname_dummy2 = tmp_path / "dummy2.txt"
593+
fname_dummy2.touch()
594+
subprocess.run(
595+
['git', '-C', tmp_path, 'add', fname_dummy1, fname_dummy2],
596+
capture_output=False,
597+
check=True,
598+
text=True,
599+
)
600+
assert cs.main("--git-only", "-x", fname_dummy1, "-x", fname, "-x", fname_dummy2, bad_name) == 1
601+
assert cs.main("--git-only", "-x", f"{fname_dummy1},{fname},{fname_dummy2}", bad_name) == 1
602+
603+
522604
def test_encoding(
523605
tmp_path: Path,
524606
capsys: pytest.CaptureFixture[str],

0 commit comments

Comments
 (0)