From 7934fd96d39f86f00cd85b772a64954dd081b087 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 11 Dec 2021 14:35:07 +0100 Subject: [PATCH 1/4] Avoid bailing out with uncaught `PermissionError` --- codespell_lib/_codespell.py | 16 ++++++++++++++-- codespell_lib/tests/test_basic.py | 8 ++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 598aa72aa5..b3037a5ff1 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -647,14 +647,26 @@ 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 PermissionError as e: + print("WARNING: %s: %s" % (e.strerror, filename), + file=sys.stderr) + return bad_count + except OSError: return bad_count for i, line in enumerate(lines): diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 8b73584a46..4eaddb3c7d 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -117,6 +117,14 @@ def test_basic(tmpdir, capsys): assert stdout == stderr == '' assert cs.main(d) == 0 + # unreadable file + if sys.platform == 'linux': # cannot create unreadable file on Windows + with open(op.join(d, 'unreadable.txt'), 'w') as f: + f.write('abandonned\n') + os.chmod(f.name, 0o000) + code, _, stderr = cs.main(f.name, std=True) + assert 'WARNING:' in stderr + # empty directory os.mkdir(op.join(d, 'test')) assert cs.main(d) == 0 From 1fe85ab11e95c0b05f14190d5673f243f52217c5 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Wed, 19 Oct 2022 11:26:50 -0400 Subject: [PATCH 2/4] Update codespell_lib/tests/test_basic.py --- codespell_lib/tests/test_basic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 4eaddb3c7d..63d8057bec 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -121,6 +121,8 @@ def test_basic(tmpdir, capsys): if sys.platform == 'linux': # cannot create unreadable file on Windows with open(op.join(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 From 14c66bc3eaf250cec89148565536d5394300e785 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Wed, 19 Oct 2022 11:35:13 -0400 Subject: [PATCH 3/4] FIX: Coverage --- .github/workflows/codespell-private.yml | 4 ++-- .gitignore | 2 ++ codespell_lib/tests/test_basic.py | 24 ++++++++++++++---------- setup.cfg | 2 +- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/.github/workflows/codespell-private.yml b/.github/workflows/codespell-private.yml index 1ad4e3b1a4..44c71aca1d 100644 --- a/.github/workflows/codespell-private.yml +++ b/.github/workflows/codespell-private.yml @@ -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 diff --git a/.gitignore b/.gitignore index 8b5d201fa8..1a443c409b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ codespell.egg-info .cache/ .pytest_cache/ codespell_lib/_version.py +junit-results.xml +*.egg-info/ diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 63d8057bec..ffae8b64e7 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -117,21 +117,25 @@ def test_basic(tmpdir, capsys): assert stdout == stderr == '' assert cs.main(d) == 0 - # unreadable file - if sys.platform == 'linux': # cannot create unreadable file on Windows - with open(op.join(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 - # empty directory os.mkdir(op.join(d, 'test')) 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 diff --git a/setup.cfg b/setup.cfg index 7074b31afd..9776335475 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 From 2c880ccb990dc4cb2c09e2b9246fc7d259702015 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Wed, 19 Oct 2022 11:36:22 -0400 Subject: [PATCH 4/4] FIX: Already short-circuited --- codespell_lib/_codespell.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index b3037a5ff1..df55894980 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -662,10 +662,6 @@ def parse_file(filename, colors, summary, misspellings, exclude_lines, return bad_count try: lines, encoding = file_opener.open(filename) - except PermissionError as e: - print("WARNING: %s: %s" % (e.strerror, filename), - file=sys.stderr) - return bad_count except OSError: return bad_count