mirror of
https://github.com/codespell-project/codespell.git
synced 2025-05-17 23:46:43 +08:00
Fix uncaught exception on unreadable files (#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 <larson.eric.d@gmail.com>
This commit is contained in:

committed by
GitHub

parent
1c081fa945
commit
a050986bf8
4
.github/workflows/codespell-private.yml
vendored
4
.github/workflows/codespell-private.yml
vendored
@ -33,16 +33,16 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
python --version # just to check
|
python --version # just to check
|
||||||
pip install -U pip wheel # upgrade to latest pip find 3.5 wheels; wheel to avoid errors
|
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 aspell-python-py3
|
||||||
pip install -e ".[dev]" # install the codespell dev packages
|
pip install -e ".[dev]" # install the codespell dev packages
|
||||||
- run: codespell --help
|
- run: codespell --help
|
||||||
- run: codespell --version
|
- run: codespell --version
|
||||||
- run: make check
|
- 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/*"
|
- 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
|
# this file has an error
|
||||||
- run: "! codespell codespell_lib/tests/test_basic.py"
|
- run: "! codespell codespell_lib/tests/test_basic.py"
|
||||||
- run: codecov
|
|
||||||
|
|
||||||
make-check-dictionaries:
|
make-check-dictionaries:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -9,3 +9,5 @@ codespell.egg-info
|
|||||||
.mypy_cache/
|
.mypy_cache/
|
||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
codespell_lib/_version.py
|
codespell_lib/_version.py
|
||||||
|
junit-results.xml
|
||||||
|
*.egg-info/
|
||||||
|
@ -647,14 +647,22 @@ def parse_file(filename, colors, summary, misspellings, exclude_lines,
|
|||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
return bad_count
|
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 text:
|
||||||
if not options.quiet_level & QuietLevels.BINARY_FILE:
|
if not options.quiet_level & QuietLevels.BINARY_FILE:
|
||||||
print("WARNING: Binary file: %s" % filename, file=sys.stderr)
|
print("WARNING: Binary file: %s" % filename, file=sys.stderr)
|
||||||
return bad_count
|
return bad_count
|
||||||
try:
|
try:
|
||||||
lines, encoding = file_opener.open(filename)
|
lines, encoding = file_opener.open(filename)
|
||||||
except Exception:
|
except OSError:
|
||||||
return bad_count
|
return bad_count
|
||||||
|
|
||||||
for i, line in enumerate(lines):
|
for i, line in enumerate(lines):
|
||||||
|
@ -122,6 +122,20 @@ def test_basic(tmpdir, capsys):
|
|||||||
assert cs.main(d) == 0
|
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):
|
def test_interactivity(tmpdir, capsys):
|
||||||
"""Test interaction"""
|
"""Test interaction"""
|
||||||
# Windows can't read a currently-opened file, so here we use
|
# Windows can't read a currently-opened file, so here we use
|
||||||
|
Reference in New Issue
Block a user