-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hatchling.py
More file actions
436 lines (325 loc) · 14.1 KB
/
test_hatchling.py
File metadata and controls
436 lines (325 loc) · 14.1 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"""
Unit tests for hatchling metadata hook plugin.
"""
import configparser
import pytest
class TestParsePluginIni:
"""Tests for _parse_plux_ini helper function."""
def test_parse_simple_ini(self, tmp_path):
"""Test parsing a simple plux.ini file."""
from plux.build.hatchling import _parse_plux_ini
plux_ini = tmp_path / "plux.ini"
plux_ini.write_text(
"""[plux.test.plugins]
myplugin = mysrc.plugins:MyPlugin
"""
)
result = _parse_plux_ini(str(plux_ini))
assert result == {"plux.test.plugins": {"myplugin": "mysrc.plugins:MyPlugin"}}
def test_parse_multiple_groups(self, tmp_path):
"""Test parsing plux.ini with multiple entry point groups."""
from plux.build.hatchling import _parse_plux_ini
plux_ini = tmp_path / "plux.ini"
plux_ini.write_text(
"""[plux.test.plugins]
plugin1 = pkg.module:Plugin1
plugin2 = pkg.module:Plugin2
[console_scripts]
mycli = pkg.cli:main
"""
)
result = _parse_plux_ini(str(plux_ini))
assert result == {
"plux.test.plugins": {
"plugin1": "pkg.module:Plugin1",
"plugin2": "pkg.module:Plugin2",
},
"console_scripts": {"mycli": "pkg.cli:main"},
}
def test_parse_plugin_name_with_colon(self, tmp_path):
"""Test that plugin names containing colons are parsed correctly."""
from plux.build.hatchling import _parse_plux_ini
plux_ini = tmp_path / "plux.ini"
plux_ini.write_text(
"""[plux.test.plugins]
aws:s3 = pkg.aws.s3:S3Plugin
db:postgres = pkg.db.postgres:PostgresPlugin
"""
)
result = _parse_plux_ini(str(plux_ini))
assert result == {
"plux.test.plugins": {
"aws:s3": "pkg.aws.s3:S3Plugin",
"db:postgres": "pkg.db.postgres:PostgresPlugin",
}
}
def test_parse_missing_file(self, tmp_path):
"""Test that FileNotFoundError is raised for missing file."""
from plux.build.hatchling import _parse_plux_ini
nonexistent = tmp_path / "nonexistent.ini"
with pytest.raises(FileNotFoundError):
_parse_plux_ini(str(nonexistent))
def test_parse_invalid_ini_syntax(self, tmp_path):
"""Test that configparser.Error is raised for invalid INI syntax."""
from plux.build.hatchling import _parse_plux_ini
plux_ini = tmp_path / "plux.ini"
plux_ini.write_text(
"""[plux.test.plugins]
invalid syntax here without equals sign
"""
)
with pytest.raises(configparser.Error):
_parse_plux_ini(str(plux_ini))
def test_parse_empty_file(self, tmp_path):
"""Test parsing an empty plux.ini file."""
from plux.build.hatchling import _parse_plux_ini
plux_ini = tmp_path / "plux.ini"
plux_ini.write_text("")
result = _parse_plux_ini(str(plux_ini))
assert result == {}
class TestMergeEntryPoints:
"""Tests for _merge_entry_points helper function."""
def test_merge_into_empty_target(self):
"""Test merging into an empty target dictionary."""
from plux.build.hatchling import _merge_entry_points
target = {}
source = {
"plux.plugins": {"p1": "pkg:P1", "p2": "pkg:P2"},
"console_scripts": {"cli": "pkg:main"},
}
_merge_entry_points(target, source)
assert target == source
def test_merge_new_groups(self):
"""Test merging adds new groups that don't exist in target."""
from plux.build.hatchling import _merge_entry_points
target = {"console_scripts": {"app": "module:main"}}
source = {"plux.plugins": {"p1": "pkg:P1"}}
_merge_entry_points(target, source)
assert target == {
"console_scripts": {"app": "module:main"},
"plux.plugins": {"p1": "pkg:P1"},
}
def test_merge_existing_groups(self):
"""Test merging into existing groups combines entries."""
from plux.build.hatchling import _merge_entry_points
target = {"console_scripts": {"app": "module:main"}}
source = {"console_scripts": {"tool": "module:cli"}}
_merge_entry_points(target, source)
assert target == {"console_scripts": {"app": "module:main", "tool": "module:cli"}}
def test_merge_overwrites_duplicate_names(self):
"""Test that source entries overwrite target entries with same name."""
from plux.build.hatchling import _merge_entry_points
target = {"plux.plugins": {"p1": "old.module:OldPlugin"}}
source = {"plux.plugins": {"p1": "new.module:NewPlugin"}}
_merge_entry_points(target, source)
# Source should overwrite target
assert target == {"plux.plugins": {"p1": "new.module:NewPlugin"}}
def test_merge_preserves_other_entries(self):
"""Test that merging preserves entries not in source."""
from plux.build.hatchling import _merge_entry_points
target = {"plux.plugins": {"p1": "pkg:P1", "p2": "pkg:P2"}}
source = {"plux.plugins": {"p1": "pkg:NewP1", "p3": "pkg:P3"}}
_merge_entry_points(target, source)
assert target == {"plux.plugins": {"p1": "pkg:NewP1", "p2": "pkg:P2", "p3": "pkg:P3"}}
class TestPluxMetadataHook:
"""Tests for PluxMetadataHook class."""
def test_hook_raises_in_build_hook_mode(self, tmp_path):
"""Test that hook is no-op when entrypoint_build_mode is not manual."""
pytest.importorskip("hatchling")
from plux.build.hatchling import PluxMetadataHook
# Create a temporary pyproject.toml with build-hook mode
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
"""[tool.plux]
entrypoint_build_mode = "build-hook"
"""
)
# Create hook instance
hook = PluxMetadataHook(str(tmp_path), {})
metadata = {"name": "test-project"}
# expect to fail for non-manual build hook for now
with pytest.raises(RuntimeError, match="only supported for `entrypoint_build_mode=manual`"):
hook.update(metadata)
def test_hook_activates_in_manual_mode(self, tmp_path):
"""Test that hook processes plux.ini in manual mode."""
pytest.importorskip("hatchling")
from plux.build.hatchling import PluxMetadataHook
# Create pyproject.toml with manual mode
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
"""[tool.plux]
entrypoint_build_mode = "manual"
"""
)
# Create plux.ini
plux_ini = tmp_path / "plux.ini"
plux_ini.write_text(
"""[plux.test.plugins]
myplugin = mysrc.plugins:MyPlugin
"""
)
# Create hook instance
hook = PluxMetadataHook(str(tmp_path), {})
# Create metadata
metadata = {"name": "test-project"}
# Call update
hook.update(metadata)
# Entry points should be added
assert "entry-points" in metadata
assert metadata["entry-points"] == {"plux.test.plugins": {"myplugin": "mysrc.plugins:MyPlugin"}}
def test_hook_uses_custom_static_file_name(self, tmp_path):
"""Test that hook uses custom entrypoint_static_file setting."""
pytest.importorskip("hatchling")
from plux.build.hatchling import PluxMetadataHook
# Create pyproject.toml with custom file name
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
"""[tool.plux]
entrypoint_build_mode = "manual"
entrypoint_static_file = "custom.ini"
"""
)
# Create custom.ini
custom_ini = tmp_path / "custom.ini"
custom_ini.write_text(
"""[plux.plugins]
p1 = pkg:P1
"""
)
# Create hook instance
hook = PluxMetadataHook(str(tmp_path), {})
# Create metadata
metadata = {}
# Call update
hook.update(metadata)
# Entry points should be loaded from custom.ini
assert metadata["entry-points"] == {"plux.plugins": {"p1": "pkg:P1"}}
def test_hook_merges_with_existing_entry_points(self, tmp_path):
"""Test that hook merges plux.ini entries with existing entry-points."""
pytest.importorskip("hatchling")
from plux.build.hatchling import PluxMetadataHook
# Create config
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
"""[tool.plux]
entrypoint_build_mode = "manual"
"""
)
# Create plux.ini
plux_ini = tmp_path / "plux.ini"
plux_ini.write_text(
"""[plux.plugins]
p1 = pkg:P1
"""
)
# Create hook instance
hook = PluxMetadataHook(str(tmp_path), {})
# Create metadata with existing entry points
metadata = {"entry-points": {"console_scripts": {"app": "module:main"}}}
# Call update
hook.update(metadata)
# Both entry point groups should be present
assert metadata["entry-points"] == {
"console_scripts": {"app": "module:main"},
"plux.plugins": {"p1": "pkg:P1"},
}
def test_hook_handles_missing_plux_ini_gracefully(self, tmp_path):
"""Test that hook doesn't fail build when plux.ini is missing."""
pytest.importorskip("hatchling")
from plux.build.hatchling import PluxMetadataHook
# Create pyproject.toml but no plux.ini
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
"""[tool.plux]
entrypoint_build_mode = "manual"
"""
)
# Create hook instance
hook = PluxMetadataHook(str(tmp_path), {})
# Create metadata
metadata = {}
# Call update - should not raise exception
hook.update(metadata)
# No entry points should be added
assert "entry-points" not in metadata
def test_hook_fails_on_invalid_ini_syntax(self, tmp_path):
"""Test that hook raises ValueError for invalid INI syntax."""
pytest.importorskip("hatchling")
from plux.build.hatchling import PluxMetadataHook
# Create pyproject.toml
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
"""[tool.plux]
entrypoint_build_mode = "manual"
"""
)
# Create invalid plux.ini
plux_ini = tmp_path / "plux.ini"
plux_ini.write_text(
"""[plux.plugins]
invalid line without equals
"""
)
# Create hook instance
hook = PluxMetadataHook(str(tmp_path), {})
# Create metadata
metadata = {}
# Call update - should raise ValueError
with pytest.raises(ValueError, match="Failed to parse plux.ini"):
hook.update(metadata)
def test_plugin_name_attribute(self):
"""Test that PLUGIN_NAME attribute is set correctly."""
pytest.importorskip("hatchling")
from plux.build.hatchling import PluxMetadataHook
assert PluxMetadataHook.PLUGIN_NAME == "plux"
def test_hatch_register_metadata_hook():
"""Test that the hook registration function returns the correct class."""
pytest.importorskip("hatchling")
from plux.build.hatchling import PluxMetadataHook, hatch_register_metadata_hook
hook_class = hatch_register_metadata_hook()
assert hook_class is PluxMetadataHook
class TestHatchlingPackageFinderPath:
"""Tests for HatchlingPackageFinder.path property."""
def _make_finder(self, sources, root, packages=None):
pytest.importorskip("hatchling")
from unittest.mock import MagicMock
from plux.build.hatchling import HatchlingPackageFinder
builder_config = MagicMock()
builder_config.sources = sources
builder_config.root = root
builder_config.packages = packages or []
return HatchlingPackageFinder(builder_config)
def test_path_with_packages_in_subdirectory(self, tmp_path):
"""Regression test: KeyError when packages live in a subdirectory.
When hatchling is configured with ``packages = ["localstack-core/localstack"]``,
builder_config.sources becomes ``{'localstack-core/': ''}`` — the key is the
source directory, not ``''``. The old code did ``sources[""]`` which raised a
KeyError. The fix must return the sources root (``{root}/localstack-core``)
so that ``_list_module_names`` can build correct file-system paths for each
discovered package name.
"""
finder = self._make_finder(
sources={"localstack-core/": ""},
root=str(tmp_path),
packages=["localstack-core/localstack"],
)
# Must not raise KeyError, and must return the sources root directory
path = finder.path
import os
assert path == os.path.join(str(tmp_path), "localstack-core")
def test_path_with_empty_sources(self, tmp_path):
"""When sources is empty, path falls back to the project root."""
finder = self._make_finder(sources={}, root=str(tmp_path))
assert finder.path == str(tmp_path)
def test_path_with_packages_in_root(self, tmp_path):
"""When packages are in the project root (sources = {'': ''}), path returns root."""
finder = self._make_finder(sources={"": ""}, root=str(tmp_path))
assert finder.path == str(tmp_path)
def test_path_single_source_non_empty_dest_dir_falls_back_to_root(self, tmp_path):
"""When a single source has a non-empty dest dir, fall back to project root.
A mapping like ``{"src/": "lib/"}`` means the wheel destination is remapped,
which plux cannot reason about — so it must return the project root and warn.
"""
finder = self._make_finder(sources={"src/": "lib/"}, root=str(tmp_path))
path = finder.path
assert path == str(tmp_path)