Skip to content

Commit

Permalink
Handle bad globs passed to if --skip/-S
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Newman <peternewman@users.noreply.github.com>
  • Loading branch information
DimitriPapadopoulos and peternewman committed Oct 20, 2022
1 parent a050986 commit 614a382
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 8 additions & 0 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,15 @@ def main(*args):

file_opener = FileOpener(options.hard_encoding_detection,
options.quiet_level)

glob_match = GlobMatch(options.skip)
try:
glob_match.match("/random/path") # does not need a real path
except re.error:
print("ERROR: --skip/-S has been fed an invalid glob, "
"try escaping special characters",
file=sys.stderr)
return EX_USAGE

bad_count = 0
for filename in options.files:
Expand Down
23 changes: 22 additions & 1 deletion codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,31 @@ def test_basic(tmpdir, capsys):
assert cs.main(d) == 0

# empty directory
os.mkdir(op.join(d, 'test'))
os.mkdir(op.join(d, 'empty'))
assert cs.main(d) == 0


def test_bad_glob(tmpdir, capsys):
# disregard invalid globs, properly handle escaped globs
g = op.join(tmpdir, 'glob')
os.mkdir(g)
fname = op.join(g, '[b-a].txt')
with open(fname, 'a') as f:
f.write('abandonned\n')
# do not skip
assert cs.main(g) == 1
# bad glob is invalid, and does not match
code, _, stderr = cs.main('--skip', '[b-a].txt',
g, std=True)
if sys.hexversion < 0x030A05F0: # Python < 3.10.5 raises re.error
assert code == EX_USAGE, 'invalid glob'
assert 'invalid glob' in stderr
else: # Python >= 3.10.5 does not match
assert code == 1
# properly escaped glob is valid, and matches glob-like file name
assert cs.main('--skip', '[[]b-a[]].txt', g) == 0


@pytest.mark.skipif(
not sys.platform == 'linux', reason='Only supported on Linux')
def test_permission_error(tmp_path, capsys):
Expand Down

0 comments on commit 614a382

Please sign in to comment.