Coverage for tests / integration / tools / test_taplo_integration.py: 100%
60 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"""Integration tests for Taplo tool definition.
3These tests require taplo to be installed and available in PATH.
4They verify the TaploPlugin definition, check command, fix command,
5and set_options method.
6"""
8from __future__ import annotations
10import shutil
11from collections.abc import Callable
12from pathlib import Path
13from typing import TYPE_CHECKING
15import pytest
16from assertpy import assert_that
18if TYPE_CHECKING:
19 from lintro.plugins.base import BaseToolPlugin
21# Skip all tests if taplo is not installed
22pytestmark = pytest.mark.skipif(
23 shutil.which("taplo") is None,
24 reason="taplo not installed",
25)
28@pytest.fixture
29def temp_toml_file_with_issues(tmp_path: Path) -> str:
30 """Create a temporary TOML file with formatting issues.
32 Creates a file containing TOML with deliberate formatting issues
33 that taplo should detect, including:
34 - Inconsistent spacing around equals
35 - Unaligned entries
37 Args:
38 tmp_path: Pytest fixture providing a temporary directory.
40 Returns:
41 Path to the created file as a string.
42 """
43 file_path = tmp_path / "config.toml"
44 file_path.write_text(
45 """\
46# TOML file with formatting issues
47[package]
48name="example"
49version = "0.1.0"
50description = "Inconsistent spacing"
52[dependencies]
53zlib = "1.0"
54alib = "2.0"
55""",
56 )
57 return str(file_path)
60@pytest.fixture
61def temp_toml_file_clean(tmp_path: Path) -> str:
62 """Create a temporary TOML file with no issues.
64 Creates a file containing properly formatted TOML that should pass
65 taplo checking without issues.
67 Args:
68 tmp_path: Pytest fixture providing a temporary directory.
70 Returns:
71 Path to the created file as a string.
72 """
73 file_path = tmp_path / "clean.toml"
74 file_path.write_text(
75 """\
76# Clean TOML file
77[package]
78name = "example"
79version = "0.1.0"
80description = "A properly formatted TOML file"
82[dependencies]
83alib = "2.0"
84zlib = "1.0"
85""",
86 )
87 return str(file_path)
90# --- Tests for TaploPlugin definition ---
93@pytest.mark.parametrize(
94 ("attr", "expected"),
95 [
96 ("name", "taplo"),
97 ("can_fix", True),
98 ],
99 ids=["name", "can_fix"],
100)
101def test_definition_attributes(
102 get_plugin: Callable[[str], BaseToolPlugin],
103 attr: str,
104 expected: object,
105) -> None:
106 """Verify TaploPlugin definition has correct attribute values.
108 Tests that the plugin definition exposes the expected values for
109 name and can_fix attributes.
111 Args:
112 get_plugin: Fixture factory to get plugin instances.
113 attr: The attribute name to check on the definition.
114 expected: The expected value of the attribute.
115 """
116 taplo_plugin = get_plugin("taplo")
117 assert_that(getattr(taplo_plugin.definition, attr)).is_equal_to(expected)
120def test_definition_file_patterns(
121 get_plugin: Callable[[str], BaseToolPlugin],
122) -> None:
123 """Verify TaploPlugin definition includes expected file patterns.
125 Tests that the plugin is configured to handle TOML files.
127 Args:
128 get_plugin: Fixture factory to get plugin instances.
129 """
130 taplo_plugin = get_plugin("taplo")
131 assert_that(taplo_plugin.definition.file_patterns).contains("*.toml")
134def test_definition_tool_type(
135 get_plugin: Callable[[str], BaseToolPlugin],
136) -> None:
137 """Verify TaploPlugin is both a linter and formatter.
139 Args:
140 get_plugin: Fixture factory to get plugin instances.
141 """
142 from lintro.enums.tool_type import ToolType
144 taplo_plugin = get_plugin("taplo")
145 expected_type = ToolType.LINTER | ToolType.FORMATTER
146 assert_that(taplo_plugin.definition.tool_type).is_equal_to(expected_type)
149# --- Integration tests for taplo check command ---
152def test_check_file_with_issues(
153 get_plugin: Callable[[str], BaseToolPlugin],
154 temp_toml_file_with_issues: str,
155) -> None:
156 """Verify taplo check detects formatting issues in TOML files.
158 Runs taplo on a file containing deliberate formatting issues
159 and verifies that issues are found.
161 Args:
162 get_plugin: Fixture factory to get plugin instances.
163 temp_toml_file_with_issues: Path to file with formatting issues.
164 """
165 taplo_plugin = get_plugin("taplo")
166 result = taplo_plugin.check([temp_toml_file_with_issues], {})
168 assert_that(result).is_not_none()
169 assert_that(result.name).is_equal_to("taplo")
170 # Taplo should detect formatting issues - check success=False (exit code)
171 # as primary indicator since output parsing may vary by environment
172 assert_that(result.success).is_false()
175def test_check_clean_file(
176 get_plugin: Callable[[str], BaseToolPlugin],
177 temp_toml_file_clean: str,
178) -> None:
179 """Verify taplo check passes on properly formatted files.
181 Runs taplo on a properly formatted file and verifies no issues.
183 Args:
184 get_plugin: Fixture factory to get plugin instances.
185 temp_toml_file_clean: Path to file with no issues.
186 """
187 taplo_plugin = get_plugin("taplo")
188 result = taplo_plugin.check([temp_toml_file_clean], {})
190 assert_that(result).is_not_none()
191 assert_that(result.name).is_equal_to("taplo")
192 assert_that(result.success).is_true()
195def test_check_empty_directory(
196 get_plugin: Callable[[str], BaseToolPlugin],
197 tmp_path: Path,
198) -> None:
199 """Verify taplo check handles empty directories gracefully.
201 Runs taplo on an empty directory and verifies a result is returned
202 without errors.
204 Args:
205 get_plugin: Fixture factory to get plugin instances.
206 tmp_path: Pytest fixture providing a temporary directory.
207 """
208 taplo_plugin = get_plugin("taplo")
209 result = taplo_plugin.check([str(tmp_path)], {})
211 assert_that(result).is_not_none()
214# --- Integration tests for taplo fix command ---
217def test_fix_formats_toml_file(
218 get_plugin: Callable[[str], BaseToolPlugin],
219 temp_toml_file_with_issues: str,
220) -> None:
221 """Verify taplo fix formats TOML files.
223 Runs taplo fix on a file with formatting issues and verifies
224 that fixes are applied by checking both the result and file content.
226 Args:
227 get_plugin: Fixture factory to get plugin instances.
228 temp_toml_file_with_issues: Path to file with formatting issues.
229 """
230 taplo_plugin = get_plugin("taplo")
231 result = taplo_plugin.fix([temp_toml_file_with_issues], {})
233 assert_that(result).is_not_none()
234 assert_that(result.name).is_equal_to("taplo")
235 # After fix, the file should be formatted
236 assert_that(result.fixed_issues_count).is_greater_than_or_equal_to(0)
238 # Verify the file was actually reformatted
239 fixed_content = Path(temp_toml_file_with_issues).read_text()
240 # Taplo normalizes spacing around '=' to single space
241 assert_that(fixed_content).contains('name = "example"')
242 assert_that(fixed_content).contains('description = "Inconsistent spacing"')
245# --- Tests for TaploPlugin.set_options method ---
248@pytest.mark.parametrize(
249 ("option_name", "option_value"),
250 [
251 ("schema", "/path/to/schema.json"),
252 ("aligned_arrays", True),
253 ("aligned_entries", True),
254 ("array_trailing_comma", True),
255 ("indent_string", " "),
256 ("reorder_keys", True),
257 ],
258 ids=[
259 "schema_path",
260 "aligned_arrays",
261 "aligned_entries",
262 "trailing_comma",
263 "indent_string",
264 "reorder_keys",
265 ],
266)
267def test_set_options(
268 get_plugin: Callable[[str], BaseToolPlugin],
269 option_name: str,
270 option_value: object,
271) -> None:
272 """Verify TaploPlugin.set_options correctly sets various options.
274 Tests that plugin options can be set and retrieved correctly.
276 Args:
277 get_plugin: Fixture factory to get plugin instances.
278 option_name: Name of the option to set.
279 option_value: Value to set for the option.
280 """
281 taplo_plugin = get_plugin("taplo")
282 taplo_plugin.set_options(**{option_name: option_value})
283 assert_that(taplo_plugin.options.get(option_name)).is_equal_to(option_value)