Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip subdirectories of hidden directories #2541

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,12 @@ def main(*args: str) -> int:
)

# skip (relative) directories
dirs[:] = [dir_ for dir_ in dirs if not glob_match.match(dir_)]
dirs[:] = [
dir_
for dir_ in dirs
if not glob_match.match(dir_)
and not is_hidden(dir_, options.check_hidden)
]

elif not glob_match.match(filename): # skip files
bad_count += parse_file(
Expand Down
68 changes: 51 additions & 17 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,33 @@ def test_check_hidden(
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test ignoring of hidden files."""
fname = tmp_path / "test.txt"
# visible file
fname.write_text("abandonned\n")
#
# tmp_path
# └── test.txt
#
fname = tmp_path / "test.txt"
fname.write_text("erorr\n")
assert cs.main(fname) == 1
assert cs.main(tmp_path) == 1

# hidden file
#
# tmp_path
# └── .test.txt
#
hidden_file = tmp_path / ".test.txt"
fname.rename(hidden_file)
assert cs.main(hidden_file) == 0
assert cs.main(tmp_path) == 0
assert cs.main("--check-hidden", hidden_file) == 1
assert cs.main("--check-hidden", tmp_path) == 1

# hidden file with typo in name
#
# tmp_path
# └── .abandonned.txt
#
typo_file = tmp_path / ".abandonned.txt"
hidden_file.rename(typo_file)
assert cs.main(typo_file) == 0
Expand All @@ -476,16 +490,28 @@ def test_check_hidden(
assert cs.main("--check-hidden", tmp_path) == 1
assert cs.main("--check-hidden", "--check-filenames", typo_file) == 2
assert cs.main("--check-hidden", "--check-filenames", tmp_path) == 2

# hidden directory
#
# tmp_path
# ├── .abandonned
# │   ├── .abandonned.txt
# │   └── subdir
# │   └── .abandonned.txt
# └── .abandonned.txt
#
assert cs.main(tmp_path) == 0
assert cs.main("--check-hidden", tmp_path) == 1
assert cs.main("--check-hidden", "--check-filenames", tmp_path) == 2
hidden_dir = tmp_path / ".abandonned"
hidden_dir.mkdir()
copyfile(typo_file, hidden_dir / typo_file.name)
hidden = tmp_path / ".abandonned"
hidden.mkdir()
copyfile(typo_file, hidden / typo_file.name)
subdir = hidden / "subdir"
subdir.mkdir()
copyfile(typo_file, subdir / typo_file.name)
assert cs.main(tmp_path) == 0
assert cs.main("--check-hidden", tmp_path) == 2
assert cs.main("--check-hidden", "--check-filenames", tmp_path) == 5
assert cs.main("--check-hidden", tmp_path) == 3
assert cs.main("--check-hidden", "--check-filenames", tmp_path) == 8
# check again with a relative path
try:
rel = op.relpath(tmp_path)
Expand All @@ -494,20 +520,28 @@ def test_check_hidden(
pass
else:
assert cs.main(rel) == 0
assert cs.main("--check-hidden", rel) == 2
assert cs.main("--check-hidden", "--check-filenames", rel) == 5
assert cs.main("--check-hidden", rel) == 3
assert cs.main("--check-hidden", "--check-filenames", rel) == 8

# hidden subdirectory
assert cs.main(tmp_path) == 0
assert cs.main("--check-hidden", tmp_path) == 2
assert cs.main("--check-hidden", "--check-filenames", tmp_path) == 5
#
# tmp_path
# ├── .abandonned
# │   ├── .abandonned.txt
# │   └── subdir
# │   └── .abandonned.txt
# ├── .abandonned.txt
# └── subdir
# └── .abandonned
# └── .abandonned.txt
subdir = tmp_path / "subdir"
subdir.mkdir()
hidden_subdir = subdir / ".abandonned"
hidden_subdir.mkdir()
copyfile(typo_file, hidden_subdir / typo_file.name)
hidden = subdir / ".abandonned"
hidden.mkdir()
copyfile(typo_file, hidden / typo_file.name)
assert cs.main(tmp_path) == 0
assert cs.main("--check-hidden", tmp_path) == 3
assert cs.main("--check-hidden", "--check-filenames", tmp_path) == 8
assert cs.main("--check-hidden", tmp_path) == 4
assert cs.main("--check-hidden", "--check-filenames", tmp_path) == 11


def test_case_handling(
Expand Down