From 268398741898a01498b7cab46e34513c26b7238c Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 15 Jun 2026 19:13:36 -0400 Subject: [PATCH 1/2] test: cover CLI paths and string-length boundary; clarify cls comment - Add tests for run_on_file (reporting, max_string_length, SyntaxError branch) and main(), which were previously untested. - Add a max_string_length boundary test (len == max is flagged). - Use a fixture to restore the class-level option these tests mutate. - Fix the misleading "# unused" comment on Flake8ASTErrorInfo.cls; it is the 4th tuple element flake8 expects. Refs #86 Assisted-by: ClaudeCode:claude-opus-4.8 --- src/flake8_errmsg/__init__.py | 2 +- tests/test_package.py | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/flake8_errmsg/__init__.py b/src/flake8_errmsg/__init__.py index d07090a..478e9c6 100644 --- a/src/flake8_errmsg/__init__.py +++ b/src/flake8_errmsg/__init__.py @@ -32,7 +32,7 @@ class Flake8ASTErrorInfo(NamedTuple): line_number: int offset: int msg: str - cls: type # unused + cls: type # the checker class; the 4th tuple element flake8 expects class Visitor(ast.NodeVisitor): diff --git a/tests/test_package.py b/tests/test_package.py index aad6b81..347c788 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -1,6 +1,9 @@ from __future__ import annotations import ast +import sys + +import pytest import flake8_errmsg as m @@ -184,3 +187,71 @@ def test_err10_namedexpr_in_lambda_ok(): results = list(m.ErrMsgASTPlugin(node).run()) # The walrus binds in the lambda's own scope, not the raise's, so no EM106. assert results == [] + + +# --- max_string_length boundary --- + + +@pytest.fixture +def _reset_max_string_length(): + """Restore the class-level option, which several tests mutate in place.""" + original = m.ErrMsgASTPlugin.max_string_length + yield + m.ErrMsgASTPlugin.max_string_length = original + + +@pytest.mark.usefixtures("_reset_max_string_length") +def test_string_length_boundary(): + node = ast.parse('raise RuntimeError("12345")\n') # length 5 + + m.ErrMsgASTPlugin.max_string_length = 5 # len == max is still flagged (>=) + assert len(list(m.ErrMsgASTPlugin(node).run())) == 1 + + m.ErrMsgASTPlugin.max_string_length = 6 # len < max is ignored + assert list(m.ErrMsgASTPlugin(node).run()) == [] + + +# --- CLI / run_on_file --- + + +@pytest.mark.usefixtures("_reset_max_string_length") +def test_run_on_file_reports_errors(tmp_path, capsys): + path = tmp_path / "example.py" + path.write_text('raise RuntimeError("a long message")\n', encoding="utf-8") + + m.run_on_file(str(path)) + + out = capsys.readouterr().out + assert f"{path}:1:0 EM101" in out + + +@pytest.mark.usefixtures("_reset_max_string_length") +def test_run_on_file_respects_max_string_length(tmp_path, capsys): + path = tmp_path / "example.py" + path.write_text('raise RuntimeError("short")\n', encoding="utf-8") + + m.run_on_file(str(path), max_string_length=100) + + assert capsys.readouterr().out == "" + + +def test_run_on_file_syntax_error(tmp_path, capsys): + path = tmp_path / "broken.py" + path.write_text("raise RuntimeError(\n", encoding="utf-8") + + with pytest.raises(SystemExit) as exc: + m.run_on_file(str(path)) + + assert exc.value.code == 1 + assert "Traceback:" in capsys.readouterr().out + + +@pytest.mark.usefixtures("_reset_max_string_length") +def test_main(tmp_path, capsys, monkeypatch): + path = tmp_path / "example.py" + path.write_text('raise RuntimeError("a long message")\n', encoding="utf-8") + + monkeypatch.setattr(sys, "argv", ["flake8-errmsg", str(path)]) + m.main() + + assert "EM101" in capsys.readouterr().out From 1815be00ba9d7a64b1477b57761f0abeeeab5749 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 15 Jun 2026 19:14:50 -0400 Subject: [PATCH 2/2] test: restore max_string_length in the existing test_string_length too Fold the pre-existing test that mutated the class-level option into the restoring fixture, so it no longer leaks state to later tests. Assisted-by: ClaudeCode:claude-opus-4.8 --- tests/test_package.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_package.py b/tests/test_package.py index 347c788..fdf303e 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -36,6 +36,7 @@ def test_err1(): ) +@pytest.mark.usefixtures("_reset_max_string_length") def test_string_length(): node = ast.parse(ERR1) plugin = m.ErrMsgASTPlugin(node)