Skip to content

Commit

Permalink
Fix uncaught exception on unreadable files (#2196)
Browse files Browse the repository at this point in the history
* Avoid bailing out with uncaught `PermissionError`

* Update codespell_lib/tests/test_basic.py

* FIX: Coverage

* FIX: Already short-circuited

Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
  • Loading branch information
DimitriPapadopoulos and larsoner authored Oct 19, 2022
1 parent 1c081fa commit a050986
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codespell-private.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ jobs:
run: |
python --version # just to check
pip install -U pip wheel # upgrade to latest pip find 3.5 wheels; wheel to avoid errors
pip install --upgrade codecov chardet "setuptools!=47.2.0" docutils setuptools_scm[toml]
pip install --upgrade chardet "setuptools!=47.2.0" docutils setuptools_scm[toml]
pip install aspell-python-py3
pip install -e ".[dev]" # install the codespell dev packages
- run: codespell --help
- run: codespell --version
- run: make check
- uses: codecov/codecov-action@v3
- 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/*"
# this file has an error
- run: "! codespell codespell_lib/tests/test_basic.py"
- run: codecov

make-check-dictionaries:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ codespell.egg-info
.mypy_cache/
.pytest_cache/
codespell_lib/_version.py
junit-results.xml
*.egg-info/
12 changes: 10 additions & 2 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,14 +647,22 @@ def parse_file(filename, colors, summary, misspellings, exclude_lines,
if not os.path.isfile(filename):
return bad_count

text = is_text_file(filename)
try:
text = is_text_file(filename)
except PermissionError as e:
print("WARNING: %s: %s" % (e.strerror, filename),
file=sys.stderr)
return bad_count
except OSError:
return bad_count

if not text:
if not options.quiet_level & QuietLevels.BINARY_FILE:
print("WARNING: Binary file: %s" % filename, file=sys.stderr)
return bad_count
try:
lines, encoding = file_opener.open(filename)
except Exception:
except OSError:
return bad_count

for i, line in enumerate(lines):
Expand Down
14 changes: 14 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ def test_basic(tmpdir, capsys):
assert cs.main(d) == 0


@pytest.mark.skipif(
not sys.platform == 'linux', reason='Only supported on Linux')
def test_permission_error(tmp_path, capsys):
"""Test permission error handling."""
d = tmp_path
with open(d / 'unreadable.txt', 'w') as f:
f.write('abandonned\n')
code, _, stderr = cs.main(f.name, std=True)
assert 'WARNING:' not in stderr
os.chmod(f.name, 0o000)
code, _, stderr = cs.main(f.name, std=True)
assert 'WARNING:' in stderr


def test_interactivity(tmpdir, capsys):
"""Test interaction"""
# Windows can't read a currently-opened file, so here we use
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool:pytest]
addopts = --cov=codespell_lib -rs --cov-report= --tb=short
addopts = --cov=codespell_lib -rs --cov-report= --tb=short --junit-xml=junit-results.xml

[flake8]
exclude = build, ci-helpers
Expand Down

0 comments on commit a050986

Please sign in to comment.