Coverage for tests / unit / tools / oxlint / test_set_options.py: 100%

58 statements  

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

1"""Tests for OxlintPlugin.set_options() method.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7import pytest 

8from assertpy import assert_that 

9 

10if TYPE_CHECKING: 

11 from lintro.tools.definitions.oxlint import OxlintPlugin 

12 

13 

14# ============================================================================= 

15# Tests for config and tsconfig options validation 

16# ============================================================================= 

17 

18 

19def test_config_accepts_string(oxlint_plugin: OxlintPlugin) -> None: 

20 """Config option accepts a string value. 

21 

22 Args: 

23 oxlint_plugin: The OxlintPlugin instance to test. 

24 """ 

25 oxlint_plugin.set_options(config=".oxlintrc.custom.json") 

26 assert_that(oxlint_plugin.options.get("config")).is_equal_to( 

27 ".oxlintrc.custom.json", 

28 ) 

29 

30 

31def test_config_rejects_non_string(oxlint_plugin: OxlintPlugin) -> None: 

32 """Config option rejects non-string values. 

33 

34 Args: 

35 oxlint_plugin: The OxlintPlugin instance to test. 

36 """ 

37 with pytest.raises(ValueError, match="config must be a string"): 

38 oxlint_plugin.set_options(config=123) # type: ignore[arg-type] 

39 

40 

41def test_tsconfig_accepts_string(oxlint_plugin: OxlintPlugin) -> None: 

42 """Tsconfig option accepts a string value. 

43 

44 Args: 

45 oxlint_plugin: The OxlintPlugin instance to test. 

46 """ 

47 oxlint_plugin.set_options(tsconfig="tsconfig.app.json") 

48 assert_that(oxlint_plugin.options.get("tsconfig")).is_equal_to( 

49 "tsconfig.app.json", 

50 ) 

51 

52 

53def test_tsconfig_rejects_non_string(oxlint_plugin: OxlintPlugin) -> None: 

54 """Tsconfig option rejects non-string values. 

55 

56 Args: 

57 oxlint_plugin: The OxlintPlugin instance to test. 

58 """ 

59 with pytest.raises(ValueError, match="tsconfig must be a string"): 

60 oxlint_plugin.set_options(tsconfig=True) # type: ignore[arg-type] 

61 

62 

63# ============================================================================= 

64# Tests for rule list options (allow, deny, warn) 

65# ============================================================================= 

66 

67 

68@pytest.mark.parametrize( 

69 ("option_name", "value", "expected"), 

70 [ 

71 ("allow", ["no-console", "no-unused-vars"], ["no-console", "no-unused-vars"]), 

72 ("allow", "no-console", ["no-console"]), 

73 ("deny", ["no-debugger", "eqeqeq"], ["no-debugger", "eqeqeq"]), 

74 ("deny", "no-debugger", ["no-debugger"]), 

75 ("warn", ["no-console", "complexity"], ["no-console", "complexity"]), 

76 ("warn", "no-console", ["no-console"]), 

77 ], 

78 ids=[ 

79 "allow_accepts_list", 

80 "allow_accepts_string", 

81 "deny_accepts_list", 

82 "deny_accepts_string", 

83 "warn_accepts_list", 

84 "warn_accepts_string", 

85 ], 

86) 

87def test_rule_option_accepts_valid_input( 

88 oxlint_plugin: OxlintPlugin, 

89 option_name: str, 

90 value: list[str] | str, 

91 expected: list[str], 

92) -> None: 

93 """Rule options accept lists and strings. 

94 

95 Args: 

96 oxlint_plugin: The OxlintPlugin instance to test. 

97 option_name: The name of the option to set. 

98 value: The value to set. 

99 expected: The expected normalized value. 

100 """ 

101 # Mypy can't infer types from parametrized kwargs 

102 oxlint_plugin.set_options(**{option_name: value}) # type: ignore[arg-type] 

103 assert_that(oxlint_plugin.options.get(option_name)).is_equal_to(expected) 

104 

105 

106def test_rule_list_rejects_invalid_type(oxlint_plugin: OxlintPlugin) -> None: 

107 """Rule list options reject invalid types. 

108 

109 Args: 

110 oxlint_plugin: The OxlintPlugin instance to test. 

111 """ 

112 with pytest.raises(ValueError, match="allow must be a string or list"): 

113 # Intentionally passing wrong type to test validation 

114 oxlint_plugin.set_options(allow=123) # type: ignore[arg-type] 

115 

116 

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

118# Tests for setting multiple options 

119# ============================================================================= 

120 

121 

122def test_set_multiple_options(oxlint_plugin: OxlintPlugin) -> None: 

123 """Multiple options can be set in a single call. 

124 

125 Args: 

126 oxlint_plugin: The OxlintPlugin instance to test. 

127 """ 

128 oxlint_plugin.set_options( 

129 config=".oxlintrc.json", 

130 tsconfig="tsconfig.json", 

131 quiet=True, 

132 deny=["no-debugger"], 

133 warn=["no-console"], 

134 ) 

135 

136 assert_that(oxlint_plugin.options.get("config")).is_equal_to(".oxlintrc.json") 

137 assert_that(oxlint_plugin.options.get("tsconfig")).is_equal_to("tsconfig.json") 

138 assert_that(oxlint_plugin.options.get("quiet")).is_true() 

139 assert_that(oxlint_plugin.options.get("deny")).is_equal_to(["no-debugger"]) 

140 assert_that(oxlint_plugin.options.get("warn")).is_equal_to(["no-console"]) 

141 

142 

143def test_none_values_do_not_override(oxlint_plugin: OxlintPlugin) -> None: 

144 """None values do not override previously set options. 

145 

146 Args: 

147 oxlint_plugin: The OxlintPlugin instance to test. 

148 """ 

149 oxlint_plugin.set_options(config=".oxlintrc.json") 

150 oxlint_plugin.set_options(config=None, tsconfig="tsconfig.json") 

151 

152 # config should still be set from first call 

153 assert_that(oxlint_plugin.options.get("config")).is_equal_to(".oxlintrc.json") 

154 assert_that(oxlint_plugin.options.get("tsconfig")).is_equal_to("tsconfig.json") 

155 

156 

157# ============================================================================= 

158# Tests for _build_oxlint_args() helper method 

159# ============================================================================= 

160 

161 

162def test_build_args_empty_options(oxlint_plugin: OxlintPlugin) -> None: 

163 """Empty options returns empty args list. 

164 

165 Args: 

166 oxlint_plugin: The OxlintPlugin instance to test. 

167 """ 

168 args = oxlint_plugin._build_oxlint_args(oxlint_plugin.options) 

169 assert_that(args).is_empty() 

170 

171 

172def test_build_args_config_adds_flag(oxlint_plugin: OxlintPlugin) -> None: 

173 """Config option adds --config flag. 

174 

175 Args: 

176 oxlint_plugin: The OxlintPlugin instance to test. 

177 """ 

178 oxlint_plugin.set_options(config=".oxlintrc.json") 

179 args = oxlint_plugin._build_oxlint_args(oxlint_plugin.options) 

180 assert_that(args).contains("--config", ".oxlintrc.json") 

181 

182 

183def test_build_args_tsconfig_adds_flag(oxlint_plugin: OxlintPlugin) -> None: 

184 """Tsconfig option adds --tsconfig flag. 

185 

186 Args: 

187 oxlint_plugin: The OxlintPlugin instance to test. 

188 """ 

189 oxlint_plugin.set_options(tsconfig="tsconfig.json") 

190 args = oxlint_plugin._build_oxlint_args(oxlint_plugin.options) 

191 assert_that(args).contains("--tsconfig", "tsconfig.json") 

192 

193 

194@pytest.mark.parametrize( 

195 ("option_name", "flag", "rules"), 

196 [ 

197 ("allow", "--allow", ["no-console", "no-unused-vars"]), 

198 ("deny", "--deny", ["no-debugger", "eqeqeq"]), 

199 ("warn", "--warn", ["complexity"]), 

200 ], 

201 ids=["allow_adds_flags", "deny_adds_flags", "warn_adds_flags"], 

202) 

203def test_build_args_rule_options_add_flags( 

204 oxlint_plugin: OxlintPlugin, 

205 option_name: str, 

206 flag: str, 

207 rules: list[str], 

208) -> None: 

209 """Rule options add flags for each rule. 

210 

211 Args: 

212 oxlint_plugin: The OxlintPlugin instance to test. 

213 option_name: The option name to set. 

214 flag: The expected CLI flag. 

215 rules: The rules to set. 

216 """ 

217 # Mypy can't infer types from parametrized kwargs 

218 oxlint_plugin.set_options(**{option_name: rules}) # type: ignore[arg-type] 

219 args = oxlint_plugin._build_oxlint_args(oxlint_plugin.options) 

220 for rule in rules: 

221 assert_that(args).contains(flag, rule) 

222 

223 

224def test_build_args_multiple_options_combine(oxlint_plugin: OxlintPlugin) -> None: 

225 """Multiple options combine into a single args list. 

226 

227 Args: 

228 oxlint_plugin: The OxlintPlugin instance to test. 

229 """ 

230 oxlint_plugin.set_options( 

231 config=".oxlintrc.json", 

232 deny=["no-debugger"], 

233 allow=["no-console"], 

234 ) 

235 args = oxlint_plugin._build_oxlint_args(oxlint_plugin.options) 

236 

237 assert_that(args).contains("--config", ".oxlintrc.json") 

238 assert_that(args).contains("--deny", "no-debugger") 

239 assert_that(args).contains("--allow", "no-console")