Coverage for tests / unit / tools / oxfmt / test_check_method.py: 100%
55 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 OxfmtPlugin.check method."""
3from __future__ import annotations
5import subprocess
6from typing import TYPE_CHECKING, Any
7from unittest.mock import MagicMock, patch
9from assertpy import assert_that
11from lintro.models.core.tool_result import ToolResult
12from lintro.parsers.oxfmt.oxfmt_issue import OxfmtIssue
14if TYPE_CHECKING:
15 from lintro.tools.definitions.oxfmt import OxfmtPlugin
18def test_check_returns_success_when_no_issues(
19 oxfmt_plugin: OxfmtPlugin,
20 mock_execution_context_for_tool: Any,
21) -> None:
22 """Check returns success when all files are formatted.
24 Args:
25 oxfmt_plugin: The OxfmtPlugin instance to test.
26 mock_execution_context_for_tool: Mock execution context factory.
27 """
28 with (
29 patch.object(oxfmt_plugin, "_prepare_execution") as mock_prepare,
30 patch.object(oxfmt_plugin, "_run_subprocess") as mock_run,
31 patch.object(oxfmt_plugin, "_get_executable_command") as mock_exec,
32 patch.object(oxfmt_plugin, "_build_config_args") as mock_config,
33 ):
34 mock_prepare.return_value = mock_execution_context_for_tool(
35 files=["test.js"],
36 rel_files=["test.js"],
37 cwd="/tmp",
38 )
40 mock_exec.return_value = ["oxfmt"]
41 mock_config.return_value = []
42 mock_run.return_value = (True, "")
44 result = oxfmt_plugin.check(["/tmp/test.js"], {})
46 assert_that(result.success).is_true()
47 assert_that(result.issues_count).is_equal_to(0)
50def test_check_returns_issues_when_unformatted_files(
51 oxfmt_plugin: OxfmtPlugin,
52 mock_execution_context_for_tool: Any,
53) -> None:
54 """Check returns issues when files need formatting.
56 Args:
57 oxfmt_plugin: The OxfmtPlugin instance to test.
58 mock_execution_context_for_tool: Mock execution context factory.
59 """
60 with (
61 patch.object(oxfmt_plugin, "_prepare_execution") as mock_prepare,
62 patch.object(oxfmt_plugin, "_run_subprocess") as mock_run,
63 patch.object(oxfmt_plugin, "_get_executable_command") as mock_exec,
64 patch.object(oxfmt_plugin, "_build_config_args") as mock_config,
65 ):
66 mock_prepare.return_value = mock_execution_context_for_tool(
67 files=["test.js", "other.ts"],
68 rel_files=["test.js", "other.ts"],
69 cwd="/tmp",
70 )
72 mock_exec.return_value = ["oxfmt"]
73 mock_config.return_value = []
74 mock_run.return_value = (False, "test.js\nother.ts")
76 result = oxfmt_plugin.check(["/tmp/test.js", "/tmp/other.ts"], {})
78 assert_that(result.success).is_false()
79 assert_that(result.issues_count).is_equal_to(2)
80 assert_that(result.issues).is_not_none()
81 issues = result.issues
82 assert issues is not None
83 assert_that(issues[0].file).is_equal_to("test.js")
84 assert_that(issues[1].file).is_equal_to("other.ts")
87def test_check_timeout_handling(
88 oxfmt_plugin: OxfmtPlugin,
89 mock_execution_context_for_tool: Any,
90) -> None:
91 """Check handles timeout correctly.
93 Args:
94 oxfmt_plugin: The OxfmtPlugin instance to test.
95 mock_execution_context_for_tool: Mock execution context factory.
96 """
97 timeout_result = ToolResult(
98 name="oxfmt",
99 success=False,
100 output="Oxfmt execution timed out (30s limit exceeded).",
101 issues_count=1,
102 issues=[
103 OxfmtIssue(
104 file="execution",
105 line=1,
106 column=1,
107 code="TIMEOUT",
108 message="Oxfmt execution timed out",
109 ),
110 ],
111 initial_issues_count=1,
112 fixed_issues_count=0,
113 remaining_issues_count=1,
114 )
116 with (
117 patch.object(oxfmt_plugin, "_prepare_execution") as mock_prepare,
118 patch.object(oxfmt_plugin, "_run_subprocess") as mock_run,
119 patch.object(oxfmt_plugin, "_get_executable_command") as mock_exec,
120 patch.object(oxfmt_plugin, "_build_config_args") as mock_config,
121 patch.object(
122 oxfmt_plugin,
123 "_create_timeout_result",
124 return_value=timeout_result,
125 ),
126 ):
127 mock_prepare.return_value = mock_execution_context_for_tool(
128 files=["test.js"],
129 rel_files=["test.js"],
130 cwd="/tmp",
131 )
133 mock_exec.return_value = ["oxfmt"]
134 mock_config.return_value = []
135 mock_run.side_effect = subprocess.TimeoutExpired(cmd="oxfmt", timeout=30)
137 result = oxfmt_plugin.check(["/tmp/test.js"], {})
139 assert_that(result.success).is_false()
140 assert_that(result.output).contains("timed out")
143def test_check_early_return_when_should_skip(
144 oxfmt_plugin: OxfmtPlugin,
145 mock_execution_context_for_tool: Any,
146) -> None:
147 """Check returns early result when should_skip is True.
149 Args:
150 oxfmt_plugin: The OxfmtPlugin instance to test.
151 mock_execution_context_for_tool: Mock execution context factory.
152 """
153 with patch.object(oxfmt_plugin, "_prepare_execution") as mock_prepare:
154 ctx = mock_execution_context_for_tool(should_skip=True)
155 ctx.early_result = MagicMock(success=True, issues_count=0)
156 mock_prepare.return_value = ctx
158 result = oxfmt_plugin.check(["/tmp"], {})
160 assert_that(result.success).is_true()
163def test_check_suppresses_output_on_success(
164 oxfmt_plugin: OxfmtPlugin,
165 mock_execution_context_for_tool: Any,
166) -> None:
167 """Check returns None output when no issues found.
169 Args:
170 oxfmt_plugin: The OxfmtPlugin instance to test.
171 mock_execution_context_for_tool: Mock execution context factory.
172 """
173 with (
174 patch.object(oxfmt_plugin, "_prepare_execution") as mock_prepare,
175 patch.object(oxfmt_plugin, "_run_subprocess") as mock_run,
176 patch.object(oxfmt_plugin, "_get_executable_command") as mock_exec,
177 patch.object(oxfmt_plugin, "_build_config_args") as mock_config,
178 ):
179 mock_prepare.return_value = mock_execution_context_for_tool(
180 files=["test.js"],
181 rel_files=["test.js"],
182 cwd="/tmp",
183 )
185 mock_exec.return_value = ["oxfmt"]
186 mock_config.return_value = []
187 mock_run.return_value = (True, "")
189 result = oxfmt_plugin.check(["/tmp/test.js"], {})
191 assert_that(result.output).is_none()