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

50 statements  

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

1"""Unit tests for astro-check plugin options and definition.""" 

2 

3from __future__ import annotations 

4 

5import pytest 

6from assertpy import assert_that 

7 

8from lintro.enums.tool_type import ToolType 

9from lintro.tools.definitions.astro_check import ( 

10 ASTRO_CHECK_DEFAULT_PRIORITY, 

11 ASTRO_CHECK_DEFAULT_TIMEOUT, 

12 ASTRO_CHECK_FILE_PATTERNS, 

13 AstroCheckPlugin, 

14) 

15 

16# Tests for tool definition attributes 

17 

18 

19def test_definition_name(astro_check_plugin: AstroCheckPlugin) -> None: 

20 """Verify tool name is 'astro-check'. 

21 

22 Args: 

23 astro_check_plugin: The AstroCheckPlugin instance to test. 

24 """ 

25 assert_that(astro_check_plugin.definition.name).is_equal_to("astro-check") 

26 

27 

28def test_definition_description(astro_check_plugin: AstroCheckPlugin) -> None: 

29 """Verify tool has a description. 

30 

31 Args: 

32 astro_check_plugin: The AstroCheckPlugin instance to test. 

33 """ 

34 assert_that(astro_check_plugin.definition.description).is_not_empty() 

35 

36 

37def test_definition_can_fix_is_false(astro_check_plugin: AstroCheckPlugin) -> None: 

38 """Verify astro-check cannot auto-fix issues. 

39 

40 Args: 

41 astro_check_plugin: The AstroCheckPlugin instance to test. 

42 """ 

43 assert_that(astro_check_plugin.definition.can_fix).is_false() 

44 

45 

46def test_definition_tool_type(astro_check_plugin: AstroCheckPlugin) -> None: 

47 """Verify tool type is LINTER | TYPE_CHECKER. 

48 

49 Args: 

50 astro_check_plugin: The AstroCheckPlugin instance to test. 

51 """ 

52 expected_type = ToolType.LINTER | ToolType.TYPE_CHECKER 

53 assert_that(astro_check_plugin.definition.tool_type).is_equal_to(expected_type) 

54 

55 

56def test_definition_file_patterns(astro_check_plugin: AstroCheckPlugin) -> None: 

57 """Verify file patterns include .astro files. 

58 

59 Args: 

60 astro_check_plugin: The AstroCheckPlugin instance to test. 

61 """ 

62 assert_that(astro_check_plugin.definition.file_patterns).is_equal_to( 

63 ASTRO_CHECK_FILE_PATTERNS, 

64 ) 

65 assert_that(astro_check_plugin.definition.file_patterns).contains("*.astro") 

66 

67 

68def test_definition_priority(astro_check_plugin: AstroCheckPlugin) -> None: 

69 """Verify tool priority is set correctly. 

70 

71 Args: 

72 astro_check_plugin: The AstroCheckPlugin instance to test. 

73 """ 

74 assert_that(astro_check_plugin.definition.priority).is_equal_to( 

75 ASTRO_CHECK_DEFAULT_PRIORITY, 

76 ) 

77 

78 

79def test_definition_native_configs(astro_check_plugin: AstroCheckPlugin) -> None: 

80 """Verify native configs include astro.config files. 

81 

82 Args: 

83 astro_check_plugin: The AstroCheckPlugin instance to test. 

84 """ 

85 native_configs = astro_check_plugin.definition.native_configs 

86 assert_that(native_configs).is_not_empty() 

87 assert_that(native_configs).contains("astro.config.mjs") 

88 assert_that(native_configs).contains("astro.config.ts") 

89 assert_that(native_configs).contains("astro.config.js") 

90 

91 

92# Tests for default options 

93 

94 

95@pytest.mark.parametrize( 

96 ("option_name", "expected_value"), 

97 [ 

98 ("timeout", ASTRO_CHECK_DEFAULT_TIMEOUT), 

99 ("root", None), 

100 ], 

101 ids=[ 

102 "timeout_equals_default", 

103 "root_is_none", 

104 ], 

105) 

106def test_default_options_values( 

107 astro_check_plugin: AstroCheckPlugin, 

108 option_name: str, 

109 expected_value: object, 

110) -> None: 

111 """Default options have correct values. 

112 

113 Args: 

114 astro_check_plugin: The AstroCheckPlugin instance to test. 

115 option_name: The name of the option to check. 

116 expected_value: The expected value for the option. 

117 """ 

118 assert_that( 

119 astro_check_plugin.definition.default_options[option_name], 

120 ).is_equal_to(expected_value) 

121 

122 

123# Tests for set_options method - valid options 

124 

125 

126def test_set_options_root_valid(astro_check_plugin: AstroCheckPlugin) -> None: 

127 """Set root option with valid string path. 

128 

129 Args: 

130 astro_check_plugin: The AstroCheckPlugin instance to test. 

131 """ 

132 astro_check_plugin.set_options(root="/path/to/project") 

133 assert_that(astro_check_plugin.options.get("root")).is_equal_to("/path/to/project") 

134 

135 

136def test_set_options_root_none(astro_check_plugin: AstroCheckPlugin) -> None: 

137 """Set root option to None (default). 

138 

139 Args: 

140 astro_check_plugin: The AstroCheckPlugin instance to test. 

141 """ 

142 astro_check_plugin.set_options(root=None) 

143 # None values are filtered out, so key won't exist 

144 assert_that(astro_check_plugin.options.get("root")).is_none() 

145 

146 

147# Tests for set_options method - invalid types 

148 

149 

150def test_set_options_root_invalid_type(astro_check_plugin: AstroCheckPlugin) -> None: 

151 """Raise ValueError for invalid root type. 

152 

153 Args: 

154 astro_check_plugin: The AstroCheckPlugin instance to test. 

155 """ 

156 with pytest.raises(ValueError, match="root must be a string path"): 

157 astro_check_plugin.set_options(root=123) # type: ignore[arg-type] 

158 

159 

160def test_set_options_root_invalid_bool(astro_check_plugin: AstroCheckPlugin) -> None: 

161 """Raise ValueError for boolean root. 

162 

163 Args: 

164 astro_check_plugin: The AstroCheckPlugin instance to test. 

165 """ 

166 with pytest.raises(ValueError, match="root must be a string path"): 

167 astro_check_plugin.set_options(root=True) # type: ignore[arg-type] 

168 

169 

170# Tests for _build_command method 

171 

172 

173def test_build_command_basic(astro_check_plugin: AstroCheckPlugin) -> None: 

174 """Build basic command with default options. 

175 

176 Args: 

177 astro_check_plugin: The AstroCheckPlugin instance to test. 

178 """ 

179 cmd = astro_check_plugin._build_command() 

180 

181 # Should contain astro and check subcommand 

182 assert_that(cmd).contains("check") 

183 # First element should be astro command (or bunx/npx wrapper) 

184 assert_that(cmd[0]).is_in("astro", "bunx", "npx") 

185 

186 

187def test_build_command_with_root(astro_check_plugin: AstroCheckPlugin) -> None: 

188 """Build command with root option. 

189 

190 Args: 

191 astro_check_plugin: The AstroCheckPlugin instance to test. 

192 """ 

193 astro_check_plugin.set_options(root="/path/to/project") 

194 cmd = astro_check_plugin._build_command() 

195 

196 assert_that(cmd).contains("--root") 

197 root_idx = cmd.index("--root") 

198 assert_that(cmd[root_idx + 1]).is_equal_to("/path/to/project")