Coverage for tests / unit / tools / ruff / fix / test_unsafe_fixes.py: 100%
20 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 - Unsafe fixes 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_unsafe_fixes_enabled(
13 mock_ruff_tool: MagicMock,
14 sample_ruff_json_empty_output: str,
15) -> None:
16 """Include unsafe fixes when option is enabled.
18 Args:
19 mock_ruff_tool: Mock RuffTool instance for testing.
20 sample_ruff_json_empty_output: Sample empty JSON output from ruff.
21 """
22 mock_ruff_tool.options["unsafe_fixes"] = True
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 mock_ruff_tool._run_subprocess.side_effect = [
30 (True, sample_ruff_json_empty_output), # Initial check
31 (True, sample_ruff_json_empty_output), # Fix with unsafe
32 ]
34 result = execute_ruff_fix(mock_ruff_tool, ["test.py"])
36 assert_that(result.success).is_true()
39def test_execute_ruff_fix_warns_about_unsafe_fixes(
40 mock_ruff_tool: MagicMock,
41) -> None:
42 """Warn when remaining issues could be fixed with unsafe fixes.
44 Args:
45 mock_ruff_tool: Mock RuffTool instance for testing.
46 """
47 remaining_output = """[
48 {
49 "code": "F841",
50 "message": "Local variable 'x' is assigned but never used",
51 "filename": "test.py",
52 "location": {"row": 1, "column": 1},
53 "end_location": {"row": 1, "column": 5},
54 "fix": {"applicability": "unsafe"}
55 }
56 ]"""
58 with (
59 patch(
60 "lintro.tools.implementations.ruff.fix.walk_files_with_excludes",
61 ) as mock_walk,
62 patch("lintro.tools.implementations.ruff.fix.logger") as mock_logger,
63 ):
64 mock_walk.return_value = ["test.py"]
66 mock_ruff_tool._run_subprocess.side_effect = [
67 (False, remaining_output), # Initial check
68 (False, remaining_output), # Fix attempt
69 (True, "[]"), # Unsafe fix would fix it
70 ]
72 execute_ruff_fix(mock_ruff_tool, ["test.py"])
74 mock_logger.warning.assert_called()
75 warning_msg = str(mock_logger.warning.call_args)
76 assert_that(warning_msg).contains("unsafe")