Skip to content

Commit a050986

Browse files
Fix uncaught exception on unreadable files (codespell-project#2196)
* Avoid bailing out with uncaught `PermissionError` * Update codespell_lib/tests/test_basic.py * FIX: Coverage * FIX: Already short-circuited Co-authored-by: Eric Larson <[email protected]>
1 parent 1c081fa commit a050986

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

.github/workflows/codespell-private.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ jobs:
3333
run: |
3434
python --version # just to check
3535
pip install -U pip wheel # upgrade to latest pip find 3.5 wheels; wheel to avoid errors
36-
pip install --upgrade codecov chardet "setuptools!=47.2.0" docutils setuptools_scm[toml]
36+
pip install --upgrade chardet "setuptools!=47.2.0" docutils setuptools_scm[toml]
3737
pip install aspell-python-py3
3838
pip install -e ".[dev]" # install the codespell dev packages
3939
- run: codespell --help
4040
- run: codespell --version
4141
- run: make check
42+
- uses: codecov/codecov-action@v3
4243
- run: codespell --check-filenames --skip="./.git/*,*.pyc,./codespell_lib/tests/test_basic.py,./codespell_lib/data/*,./example/code.c,./build/lib/codespell_lib/tests/test_basic.py,./build/lib/codespell_lib/data/*,README.rst,*.egg-info/*"
4344
# this file has an error
4445
- run: "! codespell codespell_lib/tests/test_basic.py"
45-
- run: codecov
4646

4747
make-check-dictionaries:
4848
runs-on: ubuntu-latest

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ codespell.egg-info
99
.mypy_cache/
1010
.pytest_cache/
1111
codespell_lib/_version.py
12+
junit-results.xml
13+
*.egg-info/

codespell_lib/_codespell.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,22 @@ def parse_file(filename, colors, summary, misspellings, exclude_lines,
647647
if not os.path.isfile(filename):
648648
return bad_count
649649

650-
text = is_text_file(filename)
650+
try:
651+
text = is_text_file(filename)
652+
except PermissionError as e:
653+
print("WARNING: %s: %s" % (e.strerror, filename),
654+
file=sys.stderr)
655+
return bad_count
656+
except OSError:
657+
return bad_count
658+
651659
if not text:
652660
if not options.quiet_level & QuietLevels.BINARY_FILE:
653661
print("WARNING: Binary file: %s" % filename, file=sys.stderr)
654662
return bad_count
655663
try:
656664
lines, encoding = file_opener.open(filename)
657-
except Exception:
665+
except OSError:
658666
return bad_count
659667

660668
for i, line in enumerate(lines):

codespell_lib/tests/test_basic.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ def test_basic(tmpdir, capsys):
122122
assert cs.main(d) == 0
123123

124124

125+
@pytest.mark.skipif(
126+
not sys.platform == 'linux', reason='Only supported on Linux')
127+
def test_permission_error(tmp_path, capsys):
128+
"""Test permission error handling."""
129+
d = tmp_path
130+
with open(d / 'unreadable.txt', 'w') as f:
131+
f.write('abandonned\n')
132+
code, _, stderr = cs.main(f.name, std=True)
133+
assert 'WARNING:' not in stderr
134+
os.chmod(f.name, 0o000)
135+
code, _, stderr = cs.main(f.name, std=True)
136+
assert 'WARNING:' in stderr
137+
138+
125139
def test_interactivity(tmpdir, capsys):
126140
"""Test interaction"""
127141
# Windows can't read a currently-opened file, so here we use

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tool:pytest]
2-
addopts = --cov=codespell_lib -rs --cov-report= --tb=short
2+
addopts = --cov=codespell_lib -rs --cov-report= --tb=short --junit-xml=junit-results.xml
33

44
[flake8]
55
exclude = build, ci-helpers

0 commit comments

Comments
 (0)