From 603768f15f1d24e7eabf0079287749d75d20fca0 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Tue, 16 Nov 2021 11:01:07 +0100 Subject: [PATCH] Handle bad globs passed to if --skip/-S --- codespell_lib/_codespell.py | 47 +++++++++++++++++++++++++------ codespell_lib/tests/test_basic.py | 4 +++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 86cc2c07b19..177a0559664 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -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( diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index bbf2ea47ddb..4a8577c89eb 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -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