From a389ab8c23b3ec556d5b1c4588f20b99d3b7dfa0 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 1 Feb 2026 04:18:17 +0000 Subject: [PATCH] fix: resolve CI test failures --- errorfix/tests/test_formatters.py | 88 +++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 errorfix/tests/test_formatters.py diff --git a/errorfix/tests/test_formatters.py b/errorfix/tests/test_formatters.py new file mode 100644 index 0000000..6a0d547 --- /dev/null +++ b/errorfix/tests/test_formatters.py @@ -0,0 +1,88 @@ +import pytest +from errorfix.formatters import TextFormatter, JSONFormatter, StructuredFormatter +from errorfix.patterns.matcher import MatchResult +from errorfix.rules.rule import Rule + + +def create_match_result(rule_id="test001", rule_name="Test Rule"): + rule = Rule( + id=rule_id, + name=rule_name, + pattern=r"TestError", + fix="Apply this fix", + description="Test error description", + severity="error", + language="python" + ) + return MatchResult( + rule=rule, + matched_text="TestError: something went wrong", + groups={"1": "something"}, + start_pos=0, + end_pos=30, + ) + + +class TestTextFormatter: + def test_format_single_match(self): + formatter = TextFormatter(use_colors=False) + match = create_match_result() + result = formatter.format([match], "TestError: something went wrong") + assert "Test Rule" in result + assert "Apply this fix" in result + assert "Test error description" in result + + def test_format_no_matches(self): + formatter = TextFormatter(use_colors=False) + result = formatter.format([], "Some unknown error") + assert "No matching rules found" in result + + def test_format_multiple_matches(self): + formatter = TextFormatter(use_colors=False) + matches = [create_match_result("r1", "Rule 1"), create_match_result("r2", "Rule 2")] + result = formatter.format(matches, "Test error") + assert "Fix #1" in result + assert "Fix #2" in result + assert "Rule 1" in result + assert "Rule 2" in result + + def test_colored_output(self): + formatter = TextFormatter(use_colors=True) + match = create_match_result() + result = formatter.format([match], "Test error") + assert "\033[" in result + + +class TestJSONFormatter: + def test_format_single_match(self): + formatter = JSONFormatter(pretty=True) + match = create_match_result() + result = formatter.format([match], "Test error") + import json + data = json.loads(result) + assert data["match_count"] == 1 + assert data["matches"][0]["rule"]["id"] == "test001" + + def test_format_empty(self): + formatter = JSONFormatter(pretty=True) + result = formatter.format([], "Test error") + import json + data = json.loads(result) + assert data["match_count"] == 0 + assert len(data["matches"]) == 0 + + def test_pretty_false(self): + formatter = JSONFormatter(pretty=False) + match = create_match_result() + result = formatter.format([match], "Test error") + import json + data = json.loads(result) + assert data["match_count"] == 1 + + +class TestStructuredFormatter: + def test_format_returns_dict_string(self): + formatter = StructuredFormatter() + match = create_match_result() + result = formatter.format([match], "Test error") + assert "rule_id" in result or "dict" in result.lower()