Coverage for tests / unit / tools / taplo / test_execution.py: 100%

77 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Tests for TaploPlugin check and fix method execution.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6from unittest.mock import patch 

7 

8from assertpy import assert_that 

9 

10from lintro.tools.definitions.taplo import TaploPlugin 

11 

12# Tests for TaploPlugin.check method 

13 

14 

15def test_check_with_mocked_subprocess_success( 

16 taplo_plugin: TaploPlugin, 

17 tmp_path: Path, 

18) -> None: 

19 """Check returns success when no issues found. 

20 

21 Args: 

22 taplo_plugin: The TaploPlugin instance to test. 

23 tmp_path: Temporary directory path for test files. 

24 """ 

25 test_file = tmp_path / "test.toml" 

26 test_file.write_text('[project]\nname = "test"\n') 

27 

28 with patch( 

29 "lintro.plugins.execution_preparation.verify_tool_version", 

30 return_value=None, 

31 ): 

32 with patch.object( 

33 taplo_plugin, 

34 "_run_subprocess", 

35 return_value=(True, ""), 

36 ): 

37 result = taplo_plugin.check([str(test_file)], {}) 

38 

39 assert_that(result.success).is_true() 

40 assert_that(result.issues_count).is_equal_to(0) 

41 

42 

43def test_check_with_mocked_subprocess_lint_errors( 

44 taplo_plugin: TaploPlugin, 

45 tmp_path: Path, 

46) -> None: 

47 """Check returns issues when taplo lint finds problems. 

48 

49 Args: 

50 taplo_plugin: The TaploPlugin instance to test. 

51 tmp_path: Temporary directory path for test files. 

52 """ 

53 test_file = tmp_path / "test.toml" 

54 test_file.write_text("invalid = \n") 

55 

56 taplo_output = """error[invalid_value]: invalid value 

57 --> test.toml:1:10 

58 | 

59 1 | invalid = 

60 | ^ expected a value 

61""" 

62 

63 with patch( 

64 "lintro.plugins.execution_preparation.verify_tool_version", 

65 return_value=None, 

66 ): 

67 with patch.object( 

68 taplo_plugin, 

69 "_run_subprocess", 

70 side_effect=[(False, taplo_output), (True, "")], 

71 ): 

72 result = taplo_plugin.check([str(test_file)], {}) 

73 

74 assert_that(result.success).is_false() 

75 assert_that(result.issues_count).is_greater_than(0) 

76 

77 

78def test_check_with_mocked_subprocess_format_issues( 

79 taplo_plugin: TaploPlugin, 

80 tmp_path: Path, 

81) -> None: 

82 """Check returns issues when taplo fmt --check finds formatting problems. 

83 

84 Args: 

85 taplo_plugin: The TaploPlugin instance to test. 

86 tmp_path: Temporary directory path for test files. 

87 """ 

88 test_file = tmp_path / "test.toml" 

89 test_file.write_text('[project]\nname="test"\n') 

90 

91 format_output = """error[formatting]: the file is not properly formatted 

92 --> test.toml:2:1 

93 | 

94 2 | name="test" 

95 | ^ formatting issue 

96""" 

97 

98 with patch( 

99 "lintro.plugins.execution_preparation.verify_tool_version", 

100 return_value=None, 

101 ): 

102 with patch.object( 

103 taplo_plugin, 

104 "_run_subprocess", 

105 side_effect=[(True, ""), (False, format_output)], 

106 ): 

107 result = taplo_plugin.check([str(test_file)], {}) 

108 

109 assert_that(result.success).is_false() 

110 assert_that(result.issues_count).is_greater_than(0) 

111 

112 

113def test_check_with_no_toml_files( 

114 taplo_plugin: TaploPlugin, 

115 tmp_path: Path, 

116) -> None: 

117 """Check returns success when no TOML files found. 

118 

119 Args: 

120 taplo_plugin: The TaploPlugin instance to test. 

121 tmp_path: Temporary directory path for test files. 

122 """ 

123 non_toml_file = tmp_path / "test.txt" 

124 non_toml_file.write_text("Not a TOML file") 

125 

126 with patch( 

127 "lintro.plugins.execution_preparation.verify_tool_version", 

128 return_value=None, 

129 ): 

130 result = taplo_plugin.check([str(non_toml_file)], {}) 

131 

132 assert_that(result.success).is_true() 

133 assert_that(result.output).contains("No") 

134 

135 

136# Tests for TaploPlugin.fix method 

137 

138 

139def test_fix_with_mocked_subprocess_success( 

140 taplo_plugin: TaploPlugin, 

141 tmp_path: Path, 

142) -> None: 

143 """Fix returns success when formatting applied successfully. 

144 

145 Args: 

146 taplo_plugin: The TaploPlugin instance to test. 

147 tmp_path: Temporary directory path for test files. 

148 """ 

149 test_file = tmp_path / "test.toml" 

150 test_file.write_text('[project]\nname="test"\n') 

151 

152 format_issue_output = """error[formatting]: the file is not properly formatted 

153 --> test.toml:2:1 

154 | 

155 2 | name="test" 

156 | ^ formatting issue 

157""" 

158 

159 with patch( 

160 "lintro.plugins.execution_preparation.verify_tool_version", 

161 return_value=None, 

162 ): 

163 with patch.object( 

164 taplo_plugin, 

165 "_run_subprocess", 

166 side_effect=[ 

167 (False, format_issue_output), # initial format check 

168 (True, ""), # lint check 

169 (True, ""), # fix command 

170 (True, ""), # final format check 

171 (True, ""), # final lint check 

172 ], 

173 ): 

174 result = taplo_plugin.fix([str(test_file)], {}) 

175 

176 assert_that(result.success).is_true() 

177 assert_that(result.fixed_issues_count).is_equal_to(1) 

178 assert_that(result.remaining_issues_count).is_equal_to(0) 

179 

180 

181def test_fix_with_mocked_subprocess_partial_fix( 

182 taplo_plugin: TaploPlugin, 

183 tmp_path: Path, 

184) -> None: 

185 """Fix returns partial success when some issues cannot be fixed. 

186 

187 Args: 

188 taplo_plugin: The TaploPlugin instance to test. 

189 tmp_path: Temporary directory path for test files. 

190 """ 

191 test_file = tmp_path / "test.toml" 

192 test_file.write_text("invalid = \n") 

193 

194 format_issue = """error[formatting]: the file is not properly formatted 

195 --> test.toml:1:1 

196 | 

197 1 | invalid = 

198 | ^ formatting issue 

199""" 

200 lint_issue = """error[invalid_value]: invalid value 

201 --> test.toml:1:10 

202 | 

203 1 | invalid = 

204 | ^ expected a value 

205""" 

206 

207 with patch( 

208 "lintro.plugins.execution_preparation.verify_tool_version", 

209 return_value=None, 

210 ): 

211 with patch.object( 

212 taplo_plugin, 

213 "_run_subprocess", 

214 side_effect=[ 

215 (False, format_issue), # initial format check 

216 (False, lint_issue), # initial lint check 

217 (True, ""), # fix command 

218 (True, ""), # final format check - format is fixed 

219 (False, lint_issue), # final lint check - syntax error remains 

220 ], 

221 ): 

222 result = taplo_plugin.fix([str(test_file)], {}) 

223 

224 assert_that(result.success).is_false() 

225 assert_that(result.initial_issues_count).is_equal_to(2) 

226 assert_that(result.fixed_issues_count).is_equal_to(1) 

227 assert_that(result.remaining_issues_count).is_equal_to(1) 

228 

229 

230def test_fix_with_no_changes_needed( 

231 taplo_plugin: TaploPlugin, 

232 tmp_path: Path, 

233) -> None: 

234 """Fix returns success when no changes are needed. 

235 

236 Args: 

237 taplo_plugin: The TaploPlugin instance to test. 

238 tmp_path: Temporary directory path for test files. 

239 """ 

240 test_file = tmp_path / "test.toml" 

241 test_file.write_text('[project]\nname = "test"\n') 

242 

243 with patch( 

244 "lintro.plugins.execution_preparation.verify_tool_version", 

245 return_value=None, 

246 ): 

247 with patch.object( 

248 taplo_plugin, 

249 "_run_subprocess", 

250 side_effect=[ 

251 (True, ""), # initial format check - no issues 

252 (True, ""), # initial lint check - no issues 

253 (True, ""), # fix command 

254 (True, ""), # final format check 

255 (True, ""), # final lint check 

256 ], 

257 ): 

258 result = taplo_plugin.fix([str(test_file)], {}) 

259 

260 assert_that(result.success).is_true() 

261 assert_that(result.initial_issues_count).is_equal_to(0) 

262 assert_that(result.fixed_issues_count).is_equal_to(0) 

263 assert_that(result.remaining_issues_count).is_equal_to(0) 

264 

265 

266def test_fix_with_no_toml_files( 

267 taplo_plugin: TaploPlugin, 

268 tmp_path: Path, 

269) -> None: 

270 """Fix returns success when no TOML files found. 

271 

272 Args: 

273 taplo_plugin: The TaploPlugin instance to test. 

274 tmp_path: Temporary directory path for test files. 

275 """ 

276 non_toml_file = tmp_path / "test.txt" 

277 non_toml_file.write_text("Not a TOML file") 

278 

279 with patch( 

280 "lintro.plugins.execution_preparation.verify_tool_version", 

281 return_value=None, 

282 ): 

283 result = taplo_plugin.fix([str(non_toml_file)], {}) 

284 

285 assert_that(result.success).is_true() 

286 assert_that(result.output).contains("No .toml files")