Coverage for tests / unit / tools / ruff / fix / test_combined_issues.py: 100%
24 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 - Mixed lint and format issues."""
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_combined_lint_and_format_issues(
13 mock_ruff_tool: MagicMock,
14 sample_ruff_json_output: str,
15 sample_ruff_format_check_output: str,
16) -> None:
17 """Handle both lint and format issues correctly.
19 Args:
20 mock_ruff_tool: Mock RuffTool instance for testing.
21 sample_ruff_json_output: Sample JSON output from ruff.
22 sample_ruff_format_check_output: Sample format check output from ruff.
23 """
24 mock_ruff_tool.options["format"] = True
26 with patch(
27 "lintro.tools.implementations.ruff.fix.walk_files_with_excludes",
28 ) as mock_walk:
29 mock_walk.return_value = ["test.py"]
31 mock_ruff_tool._run_subprocess.side_effect = [
32 (False, sample_ruff_json_output), # Initial lint: 2 issues
33 (False, sample_ruff_format_check_output), # Format check: 2 files
34 (True, "[]"), # Lint fix: all fixed
35 (True, ""), # Format fix: success
36 ]
38 result = execute_ruff_fix(mock_ruff_tool, ["test.py"])
40 assert_that(result.success).is_true()
41 # 2 lint + 2 format = 4 total initial
42 assert_that(result.initial_issues_count).is_equal_to(4)
43 # All fixed
44 assert_that(result.fixed_issues_count).is_equal_to(4)
47def test_execute_ruff_fix_partial_fix_with_format(
48 mock_ruff_tool: MagicMock,
49 sample_ruff_format_check_output: str,
50) -> None:
51 """Report partial fix when some lint issues remain after format.
53 Args:
54 mock_ruff_tool: Mock RuffTool instance for testing.
55 sample_ruff_format_check_output: Sample format check output from ruff.
56 """
57 mock_ruff_tool.options["format"] = True
59 lint_with_unfixable = """[
60 {
61 "code": "E501",
62 "message": "Line too long",
63 "filename": "test.py",
64 "location": {"row": 1, "column": 89},
65 "end_location": {"row": 1, "column": 120},
66 "fix": null
67 }
68 ]"""
70 with patch(
71 "lintro.tools.implementations.ruff.fix.walk_files_with_excludes",
72 ) as mock_walk:
73 mock_walk.return_value = ["test.py"]
75 mock_ruff_tool._run_subprocess.side_effect = [
76 (False, lint_with_unfixable), # Initial lint: 1 unfixable
77 (False, sample_ruff_format_check_output), # Format check: 2 files
78 (False, lint_with_unfixable), # Lint fix: still 1 remaining
79 (False, lint_with_unfixable), # Unsafe check
80 (True, ""), # Format fix
81 ]
83 result = execute_ruff_fix(mock_ruff_tool, ["test.py"])
85 assert_that(result.success).is_false()
86 # 1 lint + 2 format initial
87 assert_that(result.initial_issues_count).is_equal_to(3)
88 # 0 lint fixed + 2 format fixed
89 assert_that(result.fixed_issues_count).is_equal_to(2)
90 # 1 remaining unfixable
91 assert_that(result.remaining_issues_count).is_equal_to(1)