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

1"""Integration tests for Taplo tool definition. 

2 

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""" 

7 

8from __future__ import annotations 

9 

10import shutil 

11from collections.abc import Callable 

12from pathlib import Path 

13from typing import TYPE_CHECKING 

14 

15import pytest 

16from assertpy import assert_that 

17 

18if TYPE_CHECKING: 

19 from lintro.plugins.base import BaseToolPlugin 

20 

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) 

26 

27 

28@pytest.fixture 

29def temp_toml_file_with_issues(tmp_path: Path) -> str: 

30 """Create a temporary TOML file with formatting issues. 

31 

32 Creates a file containing TOML with deliberate formatting issues 

33 that taplo should detect, including: 

34 - Inconsistent spacing around equals 

35 - Unaligned entries 

36 

37 Args: 

38 tmp_path: Pytest fixture providing a temporary directory. 

39 

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" 

51 

52[dependencies] 

53zlib = "1.0" 

54alib = "2.0" 

55""", 

56 ) 

57 return str(file_path) 

58 

59 

60@pytest.fixture 

61def temp_toml_file_clean(tmp_path: Path) -> str: 

62 """Create a temporary TOML file with no issues. 

63 

64 Creates a file containing properly formatted TOML that should pass 

65 taplo checking without issues. 

66 

67 Args: 

68 tmp_path: Pytest fixture providing a temporary directory. 

69 

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" 

81 

82[dependencies] 

83alib = "2.0" 

84zlib = "1.0" 

85""", 

86 ) 

87 return str(file_path) 

88 

89 

90# --- Tests for TaploPlugin definition --- 

91 

92 

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. 

107 

108 Tests that the plugin definition exposes the expected values for 

109 name and can_fix attributes. 

110 

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) 

118 

119 

120def test_definition_file_patterns( 

121 get_plugin: Callable[[str], BaseToolPlugin], 

122) -> None: 

123 """Verify TaploPlugin definition includes expected file patterns. 

124 

125 Tests that the plugin is configured to handle TOML files. 

126 

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") 

132 

133 

134def test_definition_tool_type( 

135 get_plugin: Callable[[str], BaseToolPlugin], 

136) -> None: 

137 """Verify TaploPlugin is both a linter and formatter. 

138 

139 Args: 

140 get_plugin: Fixture factory to get plugin instances. 

141 """ 

142 from lintro.enums.tool_type import ToolType 

143 

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) 

147 

148 

149# --- Integration tests for taplo check command --- 

150 

151 

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. 

157 

158 Runs taplo on a file containing deliberate formatting issues 

159 and verifies that issues are found. 

160 

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], {}) 

167 

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() 

173 

174 

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. 

180 

181 Runs taplo on a properly formatted file and verifies no issues. 

182 

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], {}) 

189 

190 assert_that(result).is_not_none() 

191 assert_that(result.name).is_equal_to("taplo") 

192 assert_that(result.success).is_true() 

193 

194 

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. 

200 

201 Runs taplo on an empty directory and verifies a result is returned 

202 without errors. 

203 

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)], {}) 

210 

211 assert_that(result).is_not_none() 

212 

213 

214# --- Integration tests for taplo fix command --- 

215 

216 

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. 

222 

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. 

225 

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], {}) 

232 

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) 

237 

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"') 

243 

244 

245# --- Tests for TaploPlugin.set_options method --- 

246 

247 

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. 

273 

274 Tests that plugin options can be set and retrieved correctly. 

275 

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)