Coverage for tests / unit / tools / ruff / fix / test_successful_fix.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Tests for execute_ruff_fix - Successful fix scenarios."""
3from __future__ import annotations
5from unittest.mock import MagicMock, patch
7from assertpy import assert_that
9from lintro.tools.implementations.ruff.fix import execute_ruff_fix
12def test_execute_ruff_fix_with_fixable_issues(
13 mock_ruff_tool: MagicMock,
14 sample_ruff_json_output: str,
15 sample_ruff_json_empty_output: str,
16) -> None:
17 """Fix issues and report correct counts when fixable issues exist.
19 Args:
20 mock_ruff_tool: Mock RuffTool instance for testing.
21 sample_ruff_json_output: Sample JSON output from ruff with issues.
22 sample_ruff_json_empty_output: Sample empty JSON output from ruff.
23 """
24 with patch(
25 "lintro.tools.implementations.ruff.fix.walk_files_with_excludes",
26 ) as mock_walk:
27 mock_walk.return_value = ["test.py"]
29 # First call: check (finds 2 issues)
30 # Second call: fix (returns empty - all fixed)
31 mock_ruff_tool._run_subprocess.side_effect = [
32 (False, sample_ruff_json_output), # Initial check finds issues
33 (True, sample_ruff_json_empty_output), # After fix, no remaining
34 ]
36 result = execute_ruff_fix(mock_ruff_tool, ["test.py"])
38 assert_that(result.success).is_true()
39 assert_that(result.initial_issues_count).is_equal_to(2)
40 assert_that(result.fixed_issues_count).is_equal_to(2)
41 assert_that(result.remaining_issues_count).is_equal_to(0)
42 assert_that(result.output).contains("Fixed 2 issue(s)")
45def test_execute_ruff_fix_with_no_issues(
46 mock_ruff_tool: MagicMock,
47 sample_ruff_json_empty_output: str,
48) -> None:
49 """Return no fixes message when no issues exist.
51 Args:
52 mock_ruff_tool: Mock RuffTool instance for testing.
53 sample_ruff_json_empty_output: Sample empty JSON output from ruff.
54 """
55 with patch(
56 "lintro.tools.implementations.ruff.fix.walk_files_with_excludes",
57 ) as mock_walk:
58 mock_walk.return_value = ["test.py"]
60 mock_ruff_tool._run_subprocess.side_effect = [
61 (True, sample_ruff_json_empty_output), # Initial check: no issues
62 (True, sample_ruff_json_empty_output), # Fix: no issues
63 ]
65 result = execute_ruff_fix(mock_ruff_tool, ["test.py"])
67 assert_that(result.success).is_true()
68 assert_that(result.output).is_equal_to("No fixes applied.")
69 assert_that(result.issues_count).is_equal_to(0)
72def test_execute_ruff_fix_with_unfixable_issues(
73 mock_ruff_tool: MagicMock,
74) -> None:
75 """Report remaining issues when some cannot be auto-fixed.
77 Args:
78 mock_ruff_tool: Mock RuffTool instance for testing.
79 """
80 unfixable_output = """[
81 {
82 "code": "E501",
83 "message": "Line too long (120 > 88)",
84 "filename": "test.py",
85 "location": {"row": 5, "column": 89},
86 "end_location": {"row": 5, "column": 120},
87 "fix": null
88 }
89 ]"""
91 with patch(
92 "lintro.tools.implementations.ruff.fix.walk_files_with_excludes",
93 ) as mock_walk:
94 mock_walk.return_value = ["test.py"]
96 mock_ruff_tool._run_subprocess.side_effect = [
97 (False, unfixable_output), # Initial check
98 (False, unfixable_output), # After fix attempt (still has issues)
99 (False, unfixable_output), # Unsafe fix check
100 ]
102 result = execute_ruff_fix(mock_ruff_tool, ["test.py"])
104 assert_that(result.success).is_false()
105 assert_that(result.remaining_issues_count).is_equal_to(1)
106 assert_that(result.output).contains("cannot be auto-fixed")