Skip to content

Commit

Permalink
Handle bad globs passed to if --skip/-S
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos committed Dec 3, 2021
1 parent e3f5aa9 commit 603768f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
47 changes: 38 additions & 9 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,27 +872,56 @@ def main(*args):

if os.path.isdir(filename):
for root, dirs, files in os.walk(filename):
if glob_match.match(root): # skip (absolute) directories
del dirs[:]
continue
if is_hidden(root, options.check_hidden): # dir itself hidden
# skip (absolute) directories
try:
if glob_match.match(root):
del dirs[:]
continue
except re.error:
print("ERROR: --skip/-S has been fed an invalid glob",
file=sys.stderr)
return EX_USAGE
# ignore hidden directories
if is_hidden(root, options.check_hidden):
continue
for file_ in files:
# ignore hidden files in directories
if is_hidden(file_, options.check_hidden):
continue
if glob_match.match(file_): # skip files
continue
# skip files
try:
if glob_match.match(file_):
continue
except re.error:
print("ERROR: --skip/-S has been fed an invalid glob",
file=sys.stderr)
return EX_USAGE
fname = os.path.join(root, file_)
if glob_match.match(fname): # skip paths
continue
# skip paths
try:
if glob_match.match(fname):
continue
except re.error:
print("ERROR: --skip/-S has been fed an invalid glob",
file=sys.stderr)
return EX_USAGE

bad_count += parse_file(
fname, colors, summary, misspellings, exclude_lines,
file_opener, word_regex, ignore_word_regex, uri_regex,
uri_ignore_words, context, options)

# skip (relative) directories
dirs[:] = [dir_ for dir_ in dirs if not glob_match.match(dir_)]
try:
dirs[:] = [
dir_
for dir_ in dirs
if not glob_match.match(dir_)
]
except re.error:
print("ERROR: --skip/-S has been fed an invalid glob",
file=sys.stderr)
return EX_USAGE

elif not glob_match.match(filename): # skip files
bad_count += parse_file(
Expand Down
4 changes: 4 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def test_basic(tmpdir, capsys):
assert 'cannot find dictionary' in stderr
os.remove(fname)

code, _, stderr = cs.main('--skip', 'file with [weird signs b-a].txt',
f.name, std=True)
assert code == EX_USAGE, 'invalid glob'

with open(op.join(d, 'bad.txt'), 'w') as f:
f.write('abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd')
assert cs.main(d) == 4
Expand Down

0 comments on commit 603768f

Please sign in to comment.