Skip to content

Add machine-readable output format, and test #3473

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ def parse_options(
default=False,
help="print summary of fixes",
)
parser.add_argument(
"-m",
"--machine-readable",
action="store_true",
default=False,
help="Use alternate output format intended for parsing",
)

parser.add_argument(
"--count",
Expand Down Expand Up @@ -1018,6 +1025,15 @@ def parse_file(

if (not context_shown) and (context is not None):
print_context(lines, i, context)

if options.machine_readable:
mrreason = f" ({reason})" if reason else ""
print(
f"@{filename}: line {i + 1}, col {match.start() + 1}, "
f"{word} ==> {fixword}{mrreason}"
)
continue

if filename != "-":
print(
f"{cfilename}:{cline}: {cwrongword} "
Expand Down
24 changes: 24 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import subprocess
import sys
import textwrap
from io import StringIO
from pathlib import Path
from shutil import copyfile
Expand Down Expand Up @@ -325,6 +326,29 @@ def test_summary(
assert "abandonned" in stdout.split()[-2]


def test_machine_readable(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test machine-readable output format."""
fname = tmp_path / "mrfile.txt"
fname.write_text(
textwrap.dedent("""\
abandonned
the word is abandonned
the word abandonned is wrong
""")
)
result = cs.main(fname, "--machine-readable", std=True)
assert isinstance(result, tuple)
code, stdout, stderr = result
output_lines = [line for line in stdout.split("\n") if line]
assert all([outline.startswith("@") for outline in output_lines])
assert "line 1, col 1, abandonned ==> abandoned" in output_lines[0]
assert "line 2, col 13" in output_lines[1]
assert "line 3, col 10" in output_lines[2]


def test_ignore_dictionary(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,6 @@ max-complexity = 45
[tool.ruff.lint.pylint]
allow-magic-value-types = ["bytes", "int", "str",]
max-args = 13
max-branches = 46
max-branches = 48
max-returns = 11
max-statements = 119
max-statements = 125