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 <[email protected]>
  • Loading branch information
DimitriPapadopoulos and peternewman committed Oct 22, 2022
1 parent 7ec60ca commit 0887584
Show file tree
Hide file tree
Showing 2 changed files with 29 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
22 changes: 21 additions & 1 deletion codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,30 @@ 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')
assert cs.main(g) == 1
# bad glob is invalid
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 0887584

Please sign in to comment.