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 multiline options #3400

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,10 @@ class QuietLevels:


class GlobMatch:
def __init__(self, pattern: Optional[str]) -> None:
self.pattern_list: Optional[List[str]]
if pattern:
# Pattern might be a list of comma-delimited strings
self.pattern_list = ",".join(pattern).split(",")
else:
self.pattern_list = None
def __init__(self, pattern: List[str]) -> None:
self.pattern_list: List[str] = pattern

def match(self, filename: str) -> bool:
if self.pattern_list is None:
return False

return any(fnmatch.fnmatch(filename, p) for p in self.pattern_list)


Expand Down Expand Up @@ -1109,6 +1101,20 @@ def parse_file(
return bad_count


def flatten_clean_comma_separated_arguments(
arguments: Iterable[str],
) -> List[str]:
"""
>>> flatten_clean_comma_separated_arguments(["a, b ,\n c, d,", "e"])
['a', 'b', 'c', 'd', 'e']
>>> flatten_clean_comma_separated_arguments([])
[]
"""
return [
item.strip() for argument in arguments for item in argument.split(",") if item
]


def _script_main() -> int:
"""Wrap to main() for setuptools."""
return main(*sys.argv[1:])
Expand Down Expand Up @@ -1256,7 +1262,9 @@ def main(*args: str) -> int:

file_opener = FileOpener(options.hard_encoding_detection, options.quiet_level)

glob_match = GlobMatch(options.skip)
glob_match = GlobMatch(
flatten_clean_comma_separated_arguments(options.skip) if options.skip else []
)
try:
glob_match.match("/random/path") # does not need a real path
except re.error:
Expand Down
43 changes: 24 additions & 19 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ def test_ignore(
(subdir / "bad.txt").write_text("abandonned")
assert cs.main(tmp_path) == 2
assert cs.main("--skip=bad*", tmp_path) == 0
assert cs.main("--skip=whatever.txt,bad*,whatelse.txt", tmp_path) == 0
assert cs.main("--skip=whatever.txt,\n bad* ,", tmp_path) == 0
assert cs.main("--skip=*ignoredir*", tmp_path) == 1
assert cs.main("--skip=ignoredir", tmp_path) == 1
assert cs.main("--skip=*ignoredir/bad*", tmp_path) == 1
Expand Down Expand Up @@ -1213,7 +1215,7 @@ def test_ill_formed_ini_config_file(
assert "ill-formed config file" in stderr


@pytest.mark.parametrize("kind", ["cfg", "toml", "toml_list"])
@pytest.mark.parametrize("kind", ["cfg", "cfg_multiline", "toml", "toml_list"])
def test_config_toml(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
Expand All @@ -1235,44 +1237,47 @@ def test_config_toml(
assert "bad.txt" in stdout
assert "abandonned.txt" in stdout

if kind == "cfg":
if kind.startswith("cfg"):
conffile = tmp_path / "setup.cfg"
args = ("--config", conffile)
conffile.write_text(
"""\
if kind == "cfg":
text = """\
[codespell]
skip = bad.txt, whatever.txt
count =
"""
)
elif kind == "toml":
assert kind == "toml"
else:
assert kind == "cfg_multiline"
text = """\
[codespell]
skip = whatever.txt,
bad.txt ,
,

count =
"""
conffile.write_text(text)
else:
if sys.version_info < (3, 11):
pytest.importorskip("tomli")
tomlfile = tmp_path / "pyproject.toml"
args = ("--toml", tomlfile)
tomlfile.write_text(
"""\
if kind == "toml":
text = """\
[tool.codespell]
skip = 'bad.txt,whatever.txt'
check-filenames = false
count = true
"""
)
else:
assert kind == "toml_list"
if sys.version_info < (3, 11):
pytest.importorskip("tomli")
tomlfile = tmp_path / "pyproject.toml"
args = ("--toml", tomlfile)
tomlfile.write_text(
"""\
else:
assert kind == "toml_list"
text = """\
[tool.codespell]
skip = ['bad.txt', 'whatever.txt']
check-filenames = false
count = true
"""
)
tomlfile.write_text(text)

# Should pass when skipping bad.txt or abandonned.txt
result = cs.main(d, *args, std=True)
Expand Down
Loading