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

63 statements  

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

1"""Tests for TaploPlugin options configuration and command building.""" 

2 

3from __future__ import annotations 

4 

5import pytest 

6from assertpy import assert_that 

7 

8from lintro.tools.definitions.taplo import ( 

9 TAPLO_DEFAULT_TIMEOUT, 

10 TaploPlugin, 

11) 

12 

13# Tests for TaploPlugin default options 

14 

15 

16@pytest.mark.parametrize( 

17 ("option_name", "expected_value"), 

18 [ 

19 ("timeout", TAPLO_DEFAULT_TIMEOUT), 

20 ("schema", None), 

21 ("aligned_arrays", None), 

22 ("aligned_entries", None), 

23 ("array_trailing_comma", None), 

24 ("indent_string", None), 

25 ("reorder_keys", None), 

26 ], 

27 ids=[ 

28 "timeout_equals_default", 

29 "schema_is_none", 

30 "aligned_arrays_is_none", 

31 "aligned_entries_is_none", 

32 "array_trailing_comma_is_none", 

33 "indent_string_is_none", 

34 "reorder_keys_is_none", 

35 ], 

36) 

37def test_default_options_values( 

38 taplo_plugin: TaploPlugin, 

39 option_name: str, 

40 expected_value: object, 

41) -> None: 

42 """Default options have correct values. 

43 

44 Args: 

45 taplo_plugin: The TaploPlugin instance to test. 

46 option_name: The name of the option to check. 

47 expected_value: The expected value for the option. 

48 """ 

49 assert_that( 

50 taplo_plugin.definition.default_options[option_name], 

51 ).is_equal_to(expected_value) 

52 

53 

54# Tests for TaploPlugin.set_options method - valid options 

55 

56 

57@pytest.mark.parametrize( 

58 ("option_name", "option_value"), 

59 [ 

60 ("schema", "/path/to/schema.json"), 

61 ("aligned_arrays", True), 

62 ("aligned_entries", True), 

63 ("array_trailing_comma", True), 

64 ("indent_string", " "), 

65 ("indent_string", "\t"), 

66 ("reorder_keys", True), 

67 ("reorder_keys", False), 

68 ], 

69 ids=[ 

70 "schema_path", 

71 "aligned_arrays_true", 

72 "aligned_entries_true", 

73 "array_trailing_comma_true", 

74 "indent_string_spaces", 

75 "indent_string_tab", 

76 "reorder_keys_true", 

77 "reorder_keys_false", 

78 ], 

79) 

80def test_set_options_valid( 

81 taplo_plugin: TaploPlugin, 

82 option_name: str, 

83 option_value: object, 

84) -> None: 

85 """Set valid options correctly. 

86 

87 Args: 

88 taplo_plugin: The TaploPlugin instance to test. 

89 option_name: The name of the option to set. 

90 option_value: The value to set for the option. 

91 """ 

92 taplo_plugin.set_options(**{option_name: option_value}) # type: ignore[arg-type] 

93 assert_that(taplo_plugin.options.get(option_name)).is_equal_to(option_value) 

94 

95 

96# Tests for TaploPlugin.set_options method - invalid types 

97 

98 

99@pytest.mark.parametrize( 

100 ("option_name", "invalid_value", "error_match"), 

101 [ 

102 ("schema", 123, "schema must be a string"), 

103 ("schema", ["path"], "schema must be a string"), 

104 ("aligned_arrays", "yes", "aligned_arrays must be a boolean"), 

105 ("aligned_arrays", 1, "aligned_arrays must be a boolean"), 

106 ("aligned_entries", "true", "aligned_entries must be a boolean"), 

107 ("array_trailing_comma", 0, "array_trailing_comma must be a boolean"), 

108 ("indent_string", 4, "indent_string must be a string"), 

109 ("indent_string", True, "indent_string must be a string"), 

110 ("reorder_keys", "yes", "reorder_keys must be a boolean"), 

111 ], 

112 ids=[ 

113 "invalid_schema_int", 

114 "invalid_schema_list", 

115 "invalid_aligned_arrays_str", 

116 "invalid_aligned_arrays_int", 

117 "invalid_aligned_entries_str", 

118 "invalid_array_trailing_comma_int", 

119 "invalid_indent_string_int", 

120 "invalid_indent_string_bool", 

121 "invalid_reorder_keys_str", 

122 ], 

123) 

124def test_set_options_invalid_type( 

125 taplo_plugin: TaploPlugin, 

126 option_name: str, 

127 invalid_value: object, 

128 error_match: str, 

129) -> None: 

130 """Raise ValueError for invalid option types. 

131 

132 Args: 

133 taplo_plugin: The TaploPlugin instance to test. 

134 option_name: The name of the option being tested. 

135 invalid_value: An invalid value for the option. 

136 error_match: Pattern expected in the error message. 

137 """ 

138 with pytest.raises(ValueError, match=error_match): 

139 taplo_plugin.set_options(**{option_name: invalid_value}) # type: ignore[arg-type] 

140 

141 

142# Tests for TaploPlugin._build_format_args method 

143 

144 

145def test_build_format_args_no_options(taplo_plugin: TaploPlugin) -> None: 

146 """Build format args returns empty list when no options set. 

147 

148 Args: 

149 taplo_plugin: The TaploPlugin instance to test. 

150 """ 

151 args = taplo_plugin._build_format_args() 

152 assert_that(args).is_empty() 

153 

154 

155def test_build_format_args_with_aligned_arrays(taplo_plugin: TaploPlugin) -> None: 

156 """Build format args includes aligned_arrays option. 

157 

158 Args: 

159 taplo_plugin: The TaploPlugin instance to test. 

160 """ 

161 taplo_plugin.set_options(aligned_arrays=True) 

162 args = taplo_plugin._build_format_args() 

163 

164 assert_that(args).contains("--option=aligned_arrays=true") 

165 

166 

167def test_build_format_args_with_aligned_entries(taplo_plugin: TaploPlugin) -> None: 

168 """Build format args includes aligned_entries option. 

169 

170 Args: 

171 taplo_plugin: The TaploPlugin instance to test. 

172 """ 

173 taplo_plugin.set_options(aligned_entries=True) 

174 args = taplo_plugin._build_format_args() 

175 

176 assert_that(args).contains("--option=aligned_entries=true") 

177 

178 

179def test_build_format_args_with_array_trailing_comma( 

180 taplo_plugin: TaploPlugin, 

181) -> None: 

182 """Build format args includes array_trailing_comma option. 

183 

184 Args: 

185 taplo_plugin: The TaploPlugin instance to test. 

186 """ 

187 taplo_plugin.set_options(array_trailing_comma=True) 

188 args = taplo_plugin._build_format_args() 

189 

190 assert_that(args).contains("--option=array_trailing_comma=true") 

191 

192 

193def test_build_format_args_with_indent_string(taplo_plugin: TaploPlugin) -> None: 

194 """Build format args includes indent_string option. 

195 

196 Args: 

197 taplo_plugin: The TaploPlugin instance to test. 

198 """ 

199 taplo_plugin.set_options(indent_string=" ") 

200 args = taplo_plugin._build_format_args() 

201 

202 assert_that(args).contains("--option=indent_string= ") 

203 

204 

205def test_build_format_args_with_reorder_keys(taplo_plugin: TaploPlugin) -> None: 

206 """Build format args includes reorder_keys option. 

207 

208 Args: 

209 taplo_plugin: The TaploPlugin instance to test. 

210 """ 

211 taplo_plugin.set_options(reorder_keys=True) 

212 args = taplo_plugin._build_format_args() 

213 

214 assert_that(args).contains("--option=reorder_keys=true") 

215 

216 

217def test_build_format_args_with_all_options(taplo_plugin: TaploPlugin) -> None: 

218 """Build format args includes all formatting options. 

219 

220 Args: 

221 taplo_plugin: The TaploPlugin instance to test. 

222 """ 

223 taplo_plugin.set_options( 

224 aligned_arrays=True, 

225 aligned_entries=True, 

226 array_trailing_comma=True, 

227 indent_string="\t", 

228 reorder_keys=True, 

229 ) 

230 args = taplo_plugin._build_format_args() 

231 

232 assert_that(args).contains("--option=aligned_arrays=true") 

233 assert_that(args).contains("--option=aligned_entries=true") 

234 assert_that(args).contains("--option=array_trailing_comma=true") 

235 assert_that(args).contains("--option=indent_string=\t") 

236 assert_that(args).contains("--option=reorder_keys=true") 

237 assert_that(args).is_length(5) 

238 

239 

240# Tests for TaploPlugin._build_lint_args method 

241 

242 

243def test_build_lint_args_no_options(taplo_plugin: TaploPlugin) -> None: 

244 """Build lint args returns empty list when no options set. 

245 

246 Args: 

247 taplo_plugin: The TaploPlugin instance to test. 

248 """ 

249 args = taplo_plugin._build_lint_args() 

250 assert_that(args).is_empty() 

251 

252 

253def test_build_lint_args_with_schema(taplo_plugin: TaploPlugin) -> None: 

254 """Build lint args includes schema option. 

255 

256 Args: 

257 taplo_plugin: The TaploPlugin instance to test. 

258 """ 

259 taplo_plugin.set_options(schema="/path/to/schema.json") 

260 args = taplo_plugin._build_lint_args() 

261 

262 assert_that(args).contains("--schema") 

263 schema_idx = args.index("--schema") 

264 assert_that(args[schema_idx + 1]).is_equal_to("/path/to/schema.json") 

265 

266 

267def test_build_lint_args_with_url_schema(taplo_plugin: TaploPlugin) -> None: 

268 """Build lint args includes schema URL. 

269 

270 Args: 

271 taplo_plugin: The TaploPlugin instance to test. 

272 """ 

273 schema_url = "https://json.schemastore.org/pyproject.json" 

274 taplo_plugin.set_options(schema=schema_url) 

275 args = taplo_plugin._build_lint_args() 

276 

277 assert_that(args).contains("--schema") 

278 schema_idx = args.index("--schema") 

279 assert_that(args[schema_idx + 1]).is_equal_to(schema_url)