Fix the filename typo detection edge cases. Closes #1532 and some other bugs (#1539)
* Add a test for checking the filename of an empty file
To test #1532
* Test an irregular file with a typo
* Fix the filename typo detection edge cases
* Split the irregular file test into a seperate test
Probably easier to skip and certainly makes the maths easier
* Skip the test if mkfifo is missing
* Add the missing import
* Fix the test skip logic
* Write in the right language!
* Test filename checking on a binary file
diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py
index e90c3f8..b71f4a2 100755
--- a/codespell_lib/_codespell.py
+++ b/codespell_lib/_codespell.py
@@ -457,9 +457,6 @@
f = sys.stdin
lines = f.readlines()
else:
- # ignore binary files
- if not os.path.isfile(filename):
- return 0
if options.check_filenames:
for word in word_regex.findall(filename):
lword = word.lower()
@@ -494,15 +491,19 @@
'WRONGWORD': cwrongword,
'RIGHTWORD': crightword, 'REASON': creason})
+ # ignore irregular files
+ if not os.path.isfile(filename):
+ return bad_count
+
text = is_text_file(filename)
if not text:
if not options.quiet_level & QuietLevels.BINARY_FILE:
print("WARNING: Binary file: %s" % filename, file=sys.stderr)
- return 0
+ return bad_count
try:
lines, encoding = file_opener.open(filename)
except Exception:
- return 0
+ return bad_count
for i, line in enumerate(lines):
if line in exclude_lines:
@@ -722,8 +723,6 @@
fname = os.path.join(root, file_)
if glob_match.match(fname): # skip paths
continue
- if not os.path.isfile(fname) or not os.path.getsize(fname):
- continue
bad_count += parse_file(
fname, colors, summary, misspellings, exclude_lines,
file_opener, word_regex, context, options)
diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py
index 5a3f3d0..47cf94a 100644
--- a/codespell_lib/tests/test_basic.py
+++ b/codespell_lib/tests/test_basic.py
@@ -8,6 +8,8 @@
import subprocess
import sys
+import pytest
+
import codespell_lib as cs
@@ -271,9 +273,29 @@
def test_check_filename(tmpdir):
"""Test filename check."""
d = str(tmpdir)
+ # Empty file
+ with open(op.join(d, 'abandonned.txt'), 'w') as f:
+ f.write('')
+ assert cs.main('-f', d) == 1
+ # Normal file with contents
with open(op.join(d, 'abandonned.txt'), 'w') as f:
f.write('.')
assert cs.main('-f', d) == 1
+ # Normal file with binary contents
+ with open(op.join(d, 'abandonned.txt'), 'wb') as f:
+ f.write(b'\x00\x00naiive\x00\x00')
+ assert cs.main('-f', d) == 1
+
+
+@pytest.mark.skipif((not hasattr(os, "mkfifo") or not callable(os.mkfifo)),
+ reason='requires os.mkfifo')
+def test_check_filename_irregular_file(tmpdir):
+ """Test irregular file filename check."""
+ # Irregular file (!isfile())
+ d = str(tmpdir)
+ os.mkfifo(op.join(d, 'abandonned'))
+ assert cs.main('-f', d) == 1
+ d = str(tmpdir)
def test_check_hidden(tmpdir):