Coverage for tests / unit / plugins / test_base_plugin_config.py: 100%

69 statements  

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

1"""Unit tests for BaseToolPlugin Lintro config support methods. 

2 

3This module contains tests for the config injection and enforcement methods 

4in the BaseToolPlugin class. 

5""" 

6 

7from __future__ import annotations 

8 

9from typing import TYPE_CHECKING 

10from unittest.mock import MagicMock, patch 

11 

12import pytest 

13from assertpy import assert_that 

14 

15if TYPE_CHECKING: 

16 from tests.unit.plugins.conftest import FakeToolPlugin 

17 

18 

19# ============================================================================= 

20# BaseToolPlugin._get_lintro_config Tests 

21# ============================================================================= 

22 

23 

24def test_get_lintro_config_returns_config(fake_tool_plugin: FakeToolPlugin) -> None: 

25 """Verify _get_lintro_config returns the Lintro configuration object. 

26 

27 Args: 

28 fake_tool_plugin: The fake tool plugin instance to test. 

29 """ 

30 mock_config = MagicMock() 

31 mock_config.enforce = {"line_length": 100} 

32 

33 with patch( 

34 "lintro.tools.core.config_injection._get_lintro_config", 

35 return_value=mock_config, 

36 ): 

37 result = fake_tool_plugin._get_lintro_config() 

38 

39 assert_that(result).is_equal_to(mock_config) 

40 assert_that(result.enforce).is_equal_to({"line_length": 100}) 

41 

42 

43def test_get_lintro_config_returns_none_when_not_found( 

44 fake_tool_plugin: FakeToolPlugin, 

45) -> None: 

46 """Verify _get_lintro_config returns None when no config is found. 

47 

48 Args: 

49 fake_tool_plugin: The fake tool plugin instance to test. 

50 """ 

51 with patch( 

52 "lintro.tools.core.config_injection._get_lintro_config", 

53 return_value=None, 

54 ): 

55 result = fake_tool_plugin._get_lintro_config() 

56 

57 assert_that(result).is_none() 

58 

59 

60# ============================================================================= 

61# BaseToolPlugin._get_enforced_settings Tests 

62# ============================================================================= 

63 

64 

65def test_get_enforced_settings_returns_settings( 

66 fake_tool_plugin: FakeToolPlugin, 

67) -> None: 

68 """Verify _get_enforced_settings returns enforced settings dictionary. 

69 

70 Args: 

71 fake_tool_plugin: The fake tool plugin instance to test. 

72 """ 

73 expected_settings = {"line_length": 100, "indent_size": 4} 

74 

75 with ( 

76 patch( 

77 "lintro.tools.core.config_injection._get_lintro_config", 

78 return_value=MagicMock(), 

79 ), 

80 patch( 

81 "lintro.tools.core.config_injection._get_enforced_settings", 

82 return_value=expected_settings, 

83 ), 

84 ): 

85 result = fake_tool_plugin._get_enforced_settings() 

86 

87 assert_that(result).is_equal_to(expected_settings) 

88 assert_that(result).contains_key("line_length") 

89 assert_that(result).contains_key("indent_size") 

90 

91 

92def test_get_enforced_settings_returns_empty_dict_when_no_enforcement( 

93 fake_tool_plugin: FakeToolPlugin, 

94) -> None: 

95 """Verify _get_enforced_settings returns empty dict when no enforcement. 

96 

97 Args: 

98 fake_tool_plugin: The fake tool plugin instance to test. 

99 """ 

100 with ( 

101 patch( 

102 "lintro.tools.core.config_injection._get_lintro_config", 

103 return_value=MagicMock(), 

104 ), 

105 patch( 

106 "lintro.tools.core.config_injection._get_enforced_settings", 

107 return_value={}, 

108 ), 

109 ): 

110 result = fake_tool_plugin._get_enforced_settings() 

111 

112 assert_that(result).is_empty() 

113 

114 

115# ============================================================================= 

116# BaseToolPlugin._get_enforce_cli_args Tests 

117# ============================================================================= 

118 

119 

120def test_get_enforce_cli_args_returns_args(fake_tool_plugin: FakeToolPlugin) -> None: 

121 """Verify _get_enforce_cli_args returns CLI arguments for enforcement. 

122 

123 Args: 

124 fake_tool_plugin: The fake tool plugin instance to test. 

125 """ 

126 expected_args = ["--line-length", "100", "--indent", "4"] 

127 

128 with ( 

129 patch( 

130 "lintro.tools.core.config_injection._get_lintro_config", 

131 return_value=MagicMock(), 

132 ), 

133 patch( 

134 "lintro.tools.core.config_injection._get_enforce_cli_args", 

135 return_value=expected_args, 

136 ), 

137 ): 

138 result = fake_tool_plugin._get_enforce_cli_args() 

139 

140 assert_that(result).is_equal_to(expected_args) 

141 assert_that(result).is_length(4) 

142 assert_that(result).contains("--line-length", "100") 

143 

144 

145def test_get_enforce_cli_args_returns_empty_list_when_no_args( 

146 fake_tool_plugin: FakeToolPlugin, 

147) -> None: 

148 """Verify _get_enforce_cli_args returns empty list when no enforcement args. 

149 

150 Args: 

151 fake_tool_plugin: The fake tool plugin instance to test. 

152 """ 

153 with ( 

154 patch( 

155 "lintro.tools.core.config_injection._get_lintro_config", 

156 return_value=MagicMock(), 

157 ), 

158 patch( 

159 "lintro.tools.core.config_injection._get_enforce_cli_args", 

160 return_value=[], 

161 ), 

162 ): 

163 result = fake_tool_plugin._get_enforce_cli_args() 

164 

165 assert_that(result).is_empty() 

166 

167 

168# ============================================================================= 

169# BaseToolPlugin._get_defaults_config_args Tests 

170# ============================================================================= 

171 

172 

173def test_get_defaults_config_args_returns_args( 

174 fake_tool_plugin: FakeToolPlugin, 

175) -> None: 

176 """Verify _get_defaults_config_args returns default config arguments. 

177 

178 Args: 

179 fake_tool_plugin: The fake tool plugin instance to test. 

180 """ 

181 expected_args = ["--config", "defaults"] 

182 

183 with ( 

184 patch( 

185 "lintro.tools.core.config_injection._get_lintro_config", 

186 return_value=MagicMock(), 

187 ), 

188 patch( 

189 "lintro.tools.core.config_injection._get_defaults_config_args", 

190 return_value=expected_args, 

191 ), 

192 ): 

193 result = fake_tool_plugin._get_defaults_config_args() 

194 

195 assert_that(result).is_equal_to(expected_args) 

196 assert_that(result).is_length(2) 

197 

198 

199def test_get_defaults_config_args_returns_empty_list_when_no_defaults( 

200 fake_tool_plugin: FakeToolPlugin, 

201) -> None: 

202 """Verify _get_defaults_config_args returns empty list when no defaults. 

203 

204 Args: 

205 fake_tool_plugin: The fake tool plugin instance to test. 

206 """ 

207 with ( 

208 patch( 

209 "lintro.tools.core.config_injection._get_lintro_config", 

210 return_value=MagicMock(), 

211 ), 

212 patch( 

213 "lintro.tools.core.config_injection._get_defaults_config_args", 

214 return_value=[], 

215 ), 

216 ): 

217 result = fake_tool_plugin._get_defaults_config_args() 

218 

219 assert_that(result).is_empty() 

220 

221 

222# ============================================================================= 

223# BaseToolPlugin._should_use_lintro_config Tests 

224# ============================================================================= 

225 

226 

227@pytest.mark.parametrize( 

228 ("should_use", "expected"), 

229 [ 

230 pytest.param(True, True, id="returns_true"), 

231 pytest.param(False, False, id="returns_false"), 

232 ], 

233) 

234def test_should_use_lintro_config( 

235 fake_tool_plugin: FakeToolPlugin, 

236 should_use: bool, 

237 expected: bool, 

238) -> None: 

239 """Verify _should_use_lintro_config returns the expected boolean. 

240 

241 Args: 

242 fake_tool_plugin: The fake tool plugin instance to test. 

243 should_use: The value to mock for should_use_lintro_config. 

244 expected: The expected return value. 

245 """ 

246 with patch( 

247 "lintro.tools.core.config_injection._should_use_lintro_config", 

248 return_value=should_use, 

249 ): 

250 result = fake_tool_plugin._should_use_lintro_config() 

251 

252 assert_that(result).is_equal_to(expected) 

253 

254 

255# ============================================================================= 

256# BaseToolPlugin._build_config_args Tests 

257# ============================================================================= 

258 

259 

260def test_build_config_args_returns_args(fake_tool_plugin: FakeToolPlugin) -> None: 

261 """Verify _build_config_args returns built configuration arguments. 

262 

263 Args: 

264 fake_tool_plugin: The fake tool plugin instance to test. 

265 """ 

266 expected_args = ["--config-arg", "--another-arg"] 

267 

268 with ( 

269 patch( 

270 "lintro.tools.core.config_injection._get_lintro_config", 

271 return_value=MagicMock(), 

272 ), 

273 patch( 

274 "lintro.tools.core.config_injection._build_config_args", 

275 return_value=expected_args, 

276 ), 

277 ): 

278 result = fake_tool_plugin._build_config_args() 

279 

280 assert_that(result).is_equal_to(expected_args) 

281 assert_that(result).is_length(2) 

282 

283 

284def test_build_config_args_returns_empty_list_when_no_config( 

285 fake_tool_plugin: FakeToolPlugin, 

286) -> None: 

287 """Verify _build_config_args returns empty list when no config args. 

288 

289 Args: 

290 fake_tool_plugin: The fake tool plugin instance to test. 

291 """ 

292 with ( 

293 patch( 

294 "lintro.tools.core.config_injection._get_lintro_config", 

295 return_value=MagicMock(), 

296 ), 

297 patch( 

298 "lintro.tools.core.config_injection._build_config_args", 

299 return_value=[], 

300 ), 

301 ): 

302 result = fake_tool_plugin._build_config_args() 

303 

304 assert_that(result).is_empty() 

305 

306 

307def test_build_config_args_combines_multiple_sources( 

308 fake_tool_plugin: FakeToolPlugin, 

309) -> None: 

310 """Verify _build_config_args can combine args from multiple config sources. 

311 

312 Args: 

313 fake_tool_plugin: The fake tool plugin instance to test. 

314 """ 

315 combined_args = [ 

316 "--line-length", 

317 "100", 

318 "--config", 

319 "defaults", 

320 "--strict", 

321 ] 

322 

323 with ( 

324 patch( 

325 "lintro.tools.core.config_injection._get_lintro_config", 

326 return_value=MagicMock(), 

327 ), 

328 patch( 

329 "lintro.tools.core.config_injection._build_config_args", 

330 return_value=combined_args, 

331 ), 

332 ): 

333 result = fake_tool_plugin._build_config_args() 

334 

335 assert_that(result).is_length(5) 

336 assert_that(result).contains("--line-length", "100", "--strict")