Debian Codegraph @

tests/test_config.py

raw
1 # SPDX-FileCopyrightText: 2021-present Ofek Lev <[email protected]>
2 #
3 # SPDX-License-Identifier: MIT
4 from os.path import join as pjoin
5
6 import pytest
7 from hatchling.builders.config import BuilderConfig
8 from hatchling.builders.wheel import WheelBuilder
9
10 from hatch_mypyc.plugin import MypycBuildHook
11
12
13 class TestMypyArgs:
14 def test_correct(self, new_project):
15 config = {'mypy-args': ['foo']}
16 build_dir = new_project / 'dist'
17 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
18
19 assert build_hook.config_mypy_args == ['foo']
20
21 def test_not_array(self, new_project):
22 config = {'mypy-args': 9000}
23 build_dir = new_project / 'dist'
24 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
25
26 with pytest.raises(TypeError, match='Option `mypy-args` for build hook `mypyc` must be an array'):
27 _ = build_hook.config_mypy_args
28
29 def test_argument_not_string(self, new_project):
30 config = {'mypy-args': [9000]}
31 build_dir = new_project / 'dist'
32 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
33
34 with pytest.raises(
35 TypeError, match='Argument #1 of option `mypy-args` for build hook `mypyc` must be a string'
36 ):
37 _ = build_hook.config_mypy_args
38
39 def test_argument_empty_string(self, new_project):
40 config = {'mypy-args': ['']}
41 build_dir = new_project / 'dist'
42 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
43
44 with pytest.raises(
45 ValueError, match='Argument #1 of option `mypy-args` for build hook `mypyc` cannot be an empty string'
46 ):
47 _ = build_hook.config_mypy_args
48
49
50 class TestOptions:
51 def test_correct(self, new_project):
52 config = {'options': {'opt_level': '3'}}
53 build_dir = new_project / 'dist'
54 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
55
56 assert build_hook.config_options == {'opt_level': '3'}
57
58 def test_not_table(self, new_project):
59 config = {'options': 9000}
60 build_dir = new_project / 'dist'
61 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
62
63 with pytest.raises(TypeError, match='Option `options` for build hook `mypyc` must be a table'):
64 _ = build_hook.config_options
65
66
67 class TestInclude:
68 def test_correct(self, new_project):
69 config = {'include': ['foo']}
70 build_dir = new_project / 'dist'
71 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
72
73 assert build_hook.config_include == ['foo']
74
75 def test_not_array(self, new_project):
76 config = {'include': 9000}
77 build_dir = new_project / 'dist'
78 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
79
80 with pytest.raises(TypeError, match='Option `include` for build hook `mypyc` must be an array'):
81 _ = build_hook.config_include
82
83 def test_pattern_not_string(self, new_project):
84 config = {'include': [9000]}
85 build_dir = new_project / 'dist'
86 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
87
88 with pytest.raises(TypeError, match='Pattern #1 of option `include` for build hook `mypyc` must be a string'):
89 _ = build_hook.config_include
90
91 def test_pattern_empty_string(self, new_project):
92 config = {'include': ['']}
93 build_dir = new_project / 'dist'
94 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
95
96 with pytest.raises(
97 ValueError, match='Pattern #1 of option `include` for build hook `mypyc` cannot be an empty string'
98 ):
99 _ = build_hook.config_include
100
101
102 class TestExclude:
103 def test_correct(self, new_project):
104 config = {'exclude': ['foo']}
105 build_dir = new_project / 'dist'
106 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
107
108 assert build_hook.config_exclude == ['foo']
109
110 def test_not_array(self, new_project):
111 config = {'exclude': 9000}
112 build_dir = new_project / 'dist'
113 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
114
115 with pytest.raises(TypeError, match='Option `exclude` for build hook `mypyc` must be an array'):
116 _ = build_hook.config_exclude
117
118 def test_pattern_not_string(self, new_project):
119 config = {'exclude': [9000]}
120 build_dir = new_project / 'dist'
121 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
122
123 with pytest.raises(TypeError, match='Pattern #1 of option `exclude` for build hook `mypyc` must be a string'):
124 _ = build_hook.config_exclude
125
126 def test_pattern_empty_string(self, new_project):
127 config = {'exclude': ['']}
128 build_dir = new_project / 'dist'
129 build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')
130
131 with pytest.raises(
132 ValueError, match='Pattern #1 of option `exclude` for build hook `mypyc` cannot be an empty string'
133 ):
134 _ = build_hook.config_exclude
135
136
137 class TestPatternMatching:
138 def test_default_include(self, tmp_path):
139 build_dir = tmp_path / 'dist'
140 config = {
141 'project': {'name': 'my_app', 'version': '0.0.1'},
142 'tool': {
143 'hatch': {
144 'build': {
145 'targets': {'wheel': {'include': ['foo'], 'hooks': {'mypyc': {}}}},
146 },
147 },
148 },
149 }
150 builder = WheelBuilder(str(tmp_path), config=config)
151 build_hook = MypycBuildHook(
152 str(tmp_path),
153 builder.config.hook_config['mypyc'],
154 builder.config,
155 None,
156 str(build_dir),
157 builder.PLUGIN_NAME,
158 )
159
160 foo_dir = tmp_path / 'foo'
161 foo_dir.mkdir()
162 (foo_dir / 'bar.py').touch()
163 (foo_dir / 'baz.py').touch()
164
165 bar_dir = tmp_path / 'bar'
166 bar_dir.mkdir()
167 (bar_dir / 'foo.py').touch()
168 (bar_dir / 'baz.py').touch()
169
170 assert build_hook.included_files == [pjoin('foo', 'bar.py'), pjoin('foo', 'baz.py')]
171
172 def test_include(self, tmp_path):
173 build_dir = tmp_path / 'dist'
174 config = {
175 'project': {'name': 'my_app', 'version': '0.0.1'},
176 'tool': {
177 'hatch': {
178 'build': {
179 'targets': {'wheel': {'include': ['foo'], 'hooks': {'mypyc': {'include': ['bar', 'baz.py']}}}}
180 },
181 },
182 },
183 }
184 builder = WheelBuilder(str(tmp_path), config=config)
185 build_hook = MypycBuildHook(
186 str(tmp_path),
187 builder.config.hook_config['mypyc'],
188 builder.config,
189 None,
190 str(build_dir),
191 builder.PLUGIN_NAME,
192 )
193
194 foo_dir = tmp_path / 'foo'
195 foo_dir.mkdir()
196 (foo_dir / 'bar.py').touch()
197 (foo_dir / 'baz.py').touch()
198
199 bar_dir = tmp_path / 'bar'
200 bar_dir.mkdir()
201 (bar_dir / 'foo.py').touch()
202 (bar_dir / 'baz.py').touch()
203
204 assert build_hook.included_files == [pjoin('foo', 'baz.py')]
205
206 def test_default_exclude(self, tmp_path):
207 build_dir = tmp_path / 'dist'
208 config = {
209 'project': {'name': 'my_app', 'version': '0.0.1'},
210 'tool': {
211 'hatch': {
212 'build': {
213 'targets': {'wheel': {'exclude': ['foo'], 'hooks': {'mypyc': {}}}},
214 },
215 },
216 },
217 }
218 builder = WheelBuilder(str(tmp_path), config=config)
219 build_hook = MypycBuildHook(
220 str(tmp_path),
221 builder.config.hook_config['mypyc'],
222 builder.config,
223 None,
224 str(build_dir),
225 builder.PLUGIN_NAME,
226 )
227
228 foo_dir = tmp_path / 'foo'
229 foo_dir.mkdir()
230 (foo_dir / 'bar.py').touch()
231 (foo_dir / 'baz.py').touch()
232
233 bar_dir = tmp_path / 'bar'
234 bar_dir.mkdir()
235 (bar_dir / 'foo.py').touch()
236 (bar_dir / 'baz.py').touch()
237
238 assert build_hook.included_files == [pjoin('bar', 'baz.py'), pjoin('bar', 'foo.py')]
239
240 def test_exclude(self, tmp_path):
241 build_dir = tmp_path / 'dist'
242 config = {
243 'project': {'name': 'my_app', 'version': '0.0.1'},
244 'tool': {
245 'hatch': {
246 'build': {
247 'targets': {'wheel': {'exclude': ['foo'], 'hooks': {'mypyc': {'exclude': ['baz.py']}}}},
248 },
249 },
250 },
251 }
252 builder = WheelBuilder(str(tmp_path), config=config)
253 build_hook = MypycBuildHook(
254 str(tmp_path),
255 builder.config.hook_config['mypyc'],
256 builder.config,
257 None,
258 str(build_dir),
259 builder.PLUGIN_NAME,
260 )
261
262 foo_dir = tmp_path / 'foo'
263 foo_dir.mkdir()
264 (foo_dir / 'bar.py').touch()
265 (foo_dir / 'baz.py').touch()
266
267 bar_dir = tmp_path / 'bar'
268 bar_dir.mkdir()
269 (bar_dir / 'foo.py').touch()
270 (bar_dir / 'baz.py').touch()
271
272 assert build_hook.included_files == [pjoin('bar', 'foo.py')]
273
274
275 class TestPackageSource:
276 def test_none(self, new_project):
277 build_dir = new_project / 'dist'
278 config = {}
279 target_config = {'packages': ['foo']}
280 build_config = BuilderConfig(None, str(new_project), 'wheel', {}, target_config)
281 build_hook = MypycBuildHook(str(new_project), config, build_config, None, str(build_dir), 'wheel')
282
283 assert build_hook.package_source == ''
284
285 def test_correct(self, new_project):
286 build_dir = new_project / 'dist'
287 config = {}
288 target_config = {'packages': ['src/foo']}
289 build_config = BuilderConfig(None, str(new_project), 'wheel', {}, target_config)
290 build_hook = MypycBuildHook(str(new_project), config, build_config, None, str(build_dir), 'wheel')
291
292 assert build_hook.package_source == 'src'
293
294
295 def test_coverage(new_project):
296 config = {
297 'project': {'name': 'my_app', 'version': '0.0.1'},
298 'tool': {
299 'hatch': {
300 'build': {
301 'targets': {'wheel': {'hooks': {'mypyc': {}}}},
302 },
303 },
304 },
305 }
306 builder = WheelBuilder(str(new_project), config=config)
307 build_hook = MypycBuildHook(
308 str(new_project),
309 builder.config.hook_config['mypyc'],
310 builder.config,
311 None,
312 str(new_project / 'dist'),
313 builder.PLUGIN_NAME,
314 )
315
316 assert build_hook.config_mypy_args is build_hook.config_mypy_args
317 assert build_hook.config_options is build_hook.config_options
318 assert build_hook.config_include is build_hook.config_include
319 assert build_hook.config_exclude is build_hook.config_exclude
320 assert build_hook.package_source is build_hook.package_source
321 assert build_hook.include_spec is build_hook.include_spec
322 assert build_hook.exclude_spec is build_hook.exclude_spec
323 assert build_hook.included_files is build_hook.included_files
324 assert build_hook.normalized_included_files is build_hook.normalized_included_files
325 assert build_hook.artifact_globs is build_hook.artifact_globs
326 assert build_hook.normalized_artifact_globs is build_hook.normalized_artifact_globs
327 assert build_hook.artifact_patterns is build_hook.artifact_patterns