Skip to content

Commit 6dd7dec

Browse files
Handle bad globs passed to if --skip/-S
Co-authored-by: Peter Newman <peternewman@users.noreply.github.com>
1 parent a050986 commit 6dd7dec

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

codespell_lib/_codespell.py

+8
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,15 @@ def main(*args):
890890

891891
file_opener = FileOpener(options.hard_encoding_detection,
892892
options.quiet_level)
893+
893894
glob_match = GlobMatch(options.skip)
895+
try:
896+
glob_match.match("/random/path") # does not need a real path
897+
except re.error:
898+
print("ERROR: --skip/-S has been fed an invalid glob, "
899+
"try escaping special characters",
900+
file=sys.stderr)
901+
return EX_USAGE
894902

895903
bad_count = 0
896904
for filename in options.files:

codespell_lib/tests/test_basic.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,30 @@ def test_basic(tmpdir, capsys):
118118
assert cs.main(d) == 0
119119

120120
# empty directory
121-
os.mkdir(op.join(d, 'test'))
121+
os.mkdir(op.join(d, 'empty'))
122122
assert cs.main(d) == 0
123123

124124

125+
def test_bad_glob(tmpdir, capsys):
126+
# disregard invalid globs, properly handle escaped globs
127+
g = op.join(tmpdir, 'glob')
128+
os.mkdir(g)
129+
fname = op.join(g, '[b-a].txt')
130+
with open(fname, 'a') as f:
131+
f.write('abandonned\n')
132+
assert cs.main(g) == 1
133+
# bad glob is invalid
134+
code, _, stderr = cs.main('--skip', '[b-a].txt',
135+
g, std=True)
136+
if sys.hexversion < 0x030A05F0: # Python < 3.10.5 raises re.error
137+
assert code == EX_USAGE, 'invalid glob'
138+
assert 'invalid glob' in stderr
139+
else: # Python >= 3.10.5 does not match
140+
assert code == 1
141+
# properly escaped glob is valid, and matches glob-like file name
142+
assert cs.main('--skip', '[[]b-a[]].txt', g) == 0
143+
144+
125145
@pytest.mark.skipif(
126146
not sys.platform == 'linux', reason='Only supported on Linux')
127147
def test_permission_error(tmp_path, capsys):

0 commit comments

Comments
 (0)