Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bad globs passed to if --skip/-S #2159

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,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 @@ -117,10 +117,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