Repository: codespell-project/codespell

Issue:
Filenames of empty files are not checked
If a file has no content, `codespell --check-filenames` doesn't detect misspelled words in the filename:
```
$ codespell --version
1.18.dev0
$ touch abandonned
$ codespell --check-filenames
$ echo "foo" > abandonned
$ codespell --check-filenames
./abandonned: abandonned ==> abandoned
```

Filenames of empty files are not checked
If a file has no content, `codespell --check-filenames` doesn't detect misspelled words in the filename:
```
$ codespell --version
1.18.dev0
$ touch abandonned
$ codespell --check-filenames
$ echo "foo" > abandonned
$ codespell --check-filenames
./abandonned: abandonned ==> abandoned
```



Discussion:
Good find. I think this is due to `os.path.getsize` here:
https://github.com/codespell-project/codespell/blob/3ed982c/codespell_lib/_codespell.py#L722
Good find. I think this is due to `os.path.getsize` here:
https://github.com/codespell-project/codespell/blob/3ed982c/codespell_lib/_codespell.py#L722

Resolving patch:
diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py
--- a/codespell_lib/_codespell.py
+++ b/codespell_lib/_codespell.py
@@ -457,9 +457,6 @@ def parse_file(filename, colors, summary, misspellings, exclude_lines,
         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 @@ def parse_file(filename, colors, summary, misspellings, exclude_lines,
                          '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 @@ def main(*args):
                     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)


Test patch:
diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py
--- 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_ignore(tmpdir):
 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):