File: test_config.py

package info (click to toggle)
python-hatch-mypyc 0.16.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: python: 1,025; makefile: 5
file content (327 lines) | stat: -rw-r--r-- 11,922 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# SPDX-FileCopyrightText: 2021-present Ofek Lev <oss@ofek.dev>
#
# SPDX-License-Identifier: MIT
from os.path import join as pjoin

import pytest
from hatchling.builders.config import BuilderConfig
from hatchling.builders.wheel import WheelBuilder

from hatch_mypyc.plugin import MypycBuildHook


class TestMypyArgs:
    def test_correct(self, new_project):
        config = {'mypy-args': ['foo']}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        assert build_hook.config_mypy_args == ['foo']

    def test_not_array(self, new_project):
        config = {'mypy-args': 9000}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(TypeError, match='Option `mypy-args` for build hook `mypyc` must be an array'):
            _ = build_hook.config_mypy_args

    def test_argument_not_string(self, new_project):
        config = {'mypy-args': [9000]}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(
            TypeError, match='Argument #1 of option `mypy-args` for build hook `mypyc` must be a string'
        ):
            _ = build_hook.config_mypy_args

    def test_argument_empty_string(self, new_project):
        config = {'mypy-args': ['']}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(
            ValueError, match='Argument #1 of option `mypy-args` for build hook `mypyc` cannot be an empty string'
        ):
            _ = build_hook.config_mypy_args


class TestOptions:
    def test_correct(self, new_project):
        config = {'options': {'opt_level': '3'}}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        assert build_hook.config_options == {'opt_level': '3'}

    def test_not_table(self, new_project):
        config = {'options': 9000}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(TypeError, match='Option `options` for build hook `mypyc` must be a table'):
            _ = build_hook.config_options


class TestInclude:
    def test_correct(self, new_project):
        config = {'include': ['foo']}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        assert build_hook.config_include == ['foo']

    def test_not_array(self, new_project):
        config = {'include': 9000}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(TypeError, match='Option `include` for build hook `mypyc` must be an array'):
            _ = build_hook.config_include

    def test_pattern_not_string(self, new_project):
        config = {'include': [9000]}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(TypeError, match='Pattern #1 of option `include` for build hook `mypyc` must be a string'):
            _ = build_hook.config_include

    def test_pattern_empty_string(self, new_project):
        config = {'include': ['']}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(
            ValueError, match='Pattern #1 of option `include` for build hook `mypyc` cannot be an empty string'
        ):
            _ = build_hook.config_include


class TestExclude:
    def test_correct(self, new_project):
        config = {'exclude': ['foo']}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        assert build_hook.config_exclude == ['foo']

    def test_not_array(self, new_project):
        config = {'exclude': 9000}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(TypeError, match='Option `exclude` for build hook `mypyc` must be an array'):
            _ = build_hook.config_exclude

    def test_pattern_not_string(self, new_project):
        config = {'exclude': [9000]}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(TypeError, match='Pattern #1 of option `exclude` for build hook `mypyc` must be a string'):
            _ = build_hook.config_exclude

    def test_pattern_empty_string(self, new_project):
        config = {'exclude': ['']}
        build_dir = new_project / 'dist'
        build_hook = MypycBuildHook(str(new_project), config, None, None, str(build_dir), 'wheel')

        with pytest.raises(
            ValueError, match='Pattern #1 of option `exclude` for build hook `mypyc` cannot be an empty string'
        ):
            _ = build_hook.config_exclude


class TestPatternMatching:
    def test_default_include(self, tmp_path):
        build_dir = tmp_path / 'dist'
        config = {
            'project': {'name': 'my_app', 'version': '0.0.1'},
            'tool': {
                'hatch': {
                    'build': {
                        'targets': {'wheel': {'include': ['foo'], 'hooks': {'mypyc': {}}}},
                    },
                },
            },
        }
        builder = WheelBuilder(str(tmp_path), config=config)
        build_hook = MypycBuildHook(
            str(tmp_path),
            builder.config.hook_config['mypyc'],
            builder.config,
            None,
            str(build_dir),
            builder.PLUGIN_NAME,
        )

        foo_dir = tmp_path / 'foo'
        foo_dir.mkdir()
        (foo_dir / 'bar.py').touch()
        (foo_dir / 'baz.py').touch()

        bar_dir = tmp_path / 'bar'
        bar_dir.mkdir()
        (bar_dir / 'foo.py').touch()
        (bar_dir / 'baz.py').touch()

        assert build_hook.included_files == [pjoin('foo', 'bar.py'), pjoin('foo', 'baz.py')]

    def test_include(self, tmp_path):
        build_dir = tmp_path / 'dist'
        config = {
            'project': {'name': 'my_app', 'version': '0.0.1'},
            'tool': {
                'hatch': {
                    'build': {
                        'targets': {'wheel': {'include': ['foo'], 'hooks': {'mypyc': {'include': ['bar', 'baz.py']}}}}
                    },
                },
            },
        }
        builder = WheelBuilder(str(tmp_path), config=config)
        build_hook = MypycBuildHook(
            str(tmp_path),
            builder.config.hook_config['mypyc'],
            builder.config,
            None,
            str(build_dir),
            builder.PLUGIN_NAME,
        )

        foo_dir = tmp_path / 'foo'
        foo_dir.mkdir()
        (foo_dir / 'bar.py').touch()
        (foo_dir / 'baz.py').touch()

        bar_dir = tmp_path / 'bar'
        bar_dir.mkdir()
        (bar_dir / 'foo.py').touch()
        (bar_dir / 'baz.py').touch()

        assert build_hook.included_files == [pjoin('foo', 'baz.py')]

    def test_default_exclude(self, tmp_path):
        build_dir = tmp_path / 'dist'
        config = {
            'project': {'name': 'my_app', 'version': '0.0.1'},
            'tool': {
                'hatch': {
                    'build': {
                        'targets': {'wheel': {'exclude': ['foo'], 'hooks': {'mypyc': {}}}},
                    },
                },
            },
        }
        builder = WheelBuilder(str(tmp_path), config=config)
        build_hook = MypycBuildHook(
            str(tmp_path),
            builder.config.hook_config['mypyc'],
            builder.config,
            None,
            str(build_dir),
            builder.PLUGIN_NAME,
        )

        foo_dir = tmp_path / 'foo'
        foo_dir.mkdir()
        (foo_dir / 'bar.py').touch()
        (foo_dir / 'baz.py').touch()

        bar_dir = tmp_path / 'bar'
        bar_dir.mkdir()
        (bar_dir / 'foo.py').touch()
        (bar_dir / 'baz.py').touch()

        assert build_hook.included_files == [pjoin('bar', 'baz.py'), pjoin('bar', 'foo.py')]

    def test_exclude(self, tmp_path):
        build_dir = tmp_path / 'dist'
        config = {
            'project': {'name': 'my_app', 'version': '0.0.1'},
            'tool': {
                'hatch': {
                    'build': {
                        'targets': {'wheel': {'exclude': ['foo'], 'hooks': {'mypyc': {'exclude': ['baz.py']}}}},
                    },
                },
            },
        }
        builder = WheelBuilder(str(tmp_path), config=config)
        build_hook = MypycBuildHook(
            str(tmp_path),
            builder.config.hook_config['mypyc'],
            builder.config,
            None,
            str(build_dir),
            builder.PLUGIN_NAME,
        )

        foo_dir = tmp_path / 'foo'
        foo_dir.mkdir()
        (foo_dir / 'bar.py').touch()
        (foo_dir / 'baz.py').touch()

        bar_dir = tmp_path / 'bar'
        bar_dir.mkdir()
        (bar_dir / 'foo.py').touch()
        (bar_dir / 'baz.py').touch()

        assert build_hook.included_files == [pjoin('bar', 'foo.py')]


class TestPackageSource:
    def test_none(self, new_project):
        build_dir = new_project / 'dist'
        config = {}
        target_config = {'packages': ['foo']}
        build_config = BuilderConfig(None, str(new_project), 'wheel', {}, target_config)
        build_hook = MypycBuildHook(str(new_project), config, build_config, None, str(build_dir), 'wheel')

        assert build_hook.package_source == ''

    def test_correct(self, new_project):
        build_dir = new_project / 'dist'
        config = {}
        target_config = {'packages': ['src/foo']}
        build_config = BuilderConfig(None, str(new_project), 'wheel', {}, target_config)
        build_hook = MypycBuildHook(str(new_project), config, build_config, None, str(build_dir), 'wheel')

        assert build_hook.package_source == 'src'


def test_coverage(new_project):
    config = {
        'project': {'name': 'my_app', 'version': '0.0.1'},
        'tool': {
            'hatch': {
                'build': {
                    'targets': {'wheel': {'hooks': {'mypyc': {}}}},
                },
            },
        },
    }
    builder = WheelBuilder(str(new_project), config=config)
    build_hook = MypycBuildHook(
        str(new_project),
        builder.config.hook_config['mypyc'],
        builder.config,
        None,
        str(new_project / 'dist'),
        builder.PLUGIN_NAME,
    )

    assert build_hook.config_mypy_args is build_hook.config_mypy_args
    assert build_hook.config_options is build_hook.config_options
    assert build_hook.config_include is build_hook.config_include
    assert build_hook.config_exclude is build_hook.config_exclude
    assert build_hook.package_source is build_hook.package_source
    assert build_hook.include_spec is build_hook.include_spec
    assert build_hook.exclude_spec is build_hook.exclude_spec
    assert build_hook.included_files is build_hook.included_files
    assert build_hook.normalized_included_files is build_hook.normalized_included_files
    assert build_hook.artifact_globs is build_hook.artifact_globs
    assert build_hook.normalized_artifact_globs is build_hook.normalized_artifact_globs
    assert build_hook.artifact_patterns is build_hook.artifact_patterns