Skip to content

Commit 0e542f2

Browse files
Handle bad globs passed to if --skip/-S
Co-authored-by: Peter Newman <[email protected]>
1 parent 3bd72d2 commit 0e542f2

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

codespell_lib/_codespell.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,15 @@ def main(*args):
898898

899899
file_opener = FileOpener(options.hard_encoding_detection,
900900
options.quiet_level)
901+
901902
glob_match = GlobMatch(options.skip)
903+
try:
904+
glob_match.match("/random/path") # does not need a real path
905+
except re.error:
906+
print("ERROR: --skip/-S has been fed an invalid glob, "
907+
"try escaping special characters",
908+
file=sys.stderr)
909+
return EX_USAGE
902910

903911
bad_count = 0
904912
for filename in options.files:

codespell_lib/tests/test_basic.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,30 @@ def test_basic(tmpdir, capsys):
117117
assert cs.main(d) == 0
118118

119119
# empty directory
120-
os.mkdir(op.join(d, 'test'))
120+
os.mkdir(op.join(d, 'empty'))
121121
assert cs.main(d) == 0
122122

123123

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

0 commit comments

Comments
 (0)