-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathtest_core.py
More file actions
1873 lines (1449 loc) · 73.6 KB
/
test_core.py
File metadata and controls
1873 lines (1449 loc) · 73.6 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pytest
from hatchling.metadata.core import BuildMetadata, CoreMetadata, HatchMetadata, ProjectMetadata
from hatchling.metadata.spec import (
LATEST_METADATA_VERSION,
get_core_metadata_constructors,
project_metadata_from_core_metadata,
)
from hatchling.plugin.manager import PluginManager
from hatchling.utils.constants import DEFAULT_BUILD_SCRIPT
from hatchling.version.source.regex import RegexSource
@pytest.fixture(scope="module")
def latest_spec():
return get_core_metadata_constructors()[LATEST_METADATA_VERSION]
class TestConfig:
def test_default(self, isolation):
metadata = ProjectMetadata(str(isolation), None)
assert metadata.config == metadata.config == {}
def test_reuse(self, isolation):
config = {}
metadata = ProjectMetadata(str(isolation), None, config)
assert metadata.config is metadata.config is config
def test_read(self, temp_dir):
project_file = temp_dir / "pyproject.toml"
project_file.write_text("foo = 5")
with temp_dir.as_cwd():
metadata = ProjectMetadata(str(temp_dir), None)
assert metadata.config == metadata.config == {"foo": 5}
class TestInterface:
def test_types(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
assert isinstance(metadata.core, CoreMetadata)
assert isinstance(metadata.hatch, HatchMetadata)
assert isinstance(metadata.build, BuildMetadata)
def test_missing_core_metadata(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {})
with pytest.raises(ValueError, match="Missing `project` metadata table in configuration"):
_ = metadata.core
def test_core_metadata_not_table(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": "foo"})
with pytest.raises(TypeError, match="The `project` configuration must be a table"):
_ = metadata.core
def test_tool_metadata_not_table(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"tool": "foo"})
with pytest.raises(TypeError, match="The `tool` configuration must be a table"):
_ = metadata.hatch
def test_hatch_metadata_not_table(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"tool": {"hatch": "foo"}})
with pytest.raises(TypeError, match="The `tool.hatch` configuration must be a table"):
_ = metadata.hatch
def test_build_metadata_not_table(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"build-system": "foo"})
with pytest.raises(TypeError, match="The `build-system` configuration must be a table"):
_ = metadata.build
class TestDynamic:
def test_not_array(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"dynamic": 10}})
with pytest.raises(TypeError, match="Field `project.dynamic` must be an array"):
_ = metadata.core.dynamic
def test_entry_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"dynamic": [10]}})
with pytest.raises(TypeError, match="Field #1 of field `project.dynamic` must be a string"):
_ = metadata.core.dynamic
def test_correct(self, isolation):
dynamic = ["version"]
metadata = ProjectMetadata(str(isolation), None, {"project": {"dynamic": dynamic}})
assert metadata.core.dynamic == ["version"]
def test_cache_not_array(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"dynamic": 10}})
with pytest.raises(TypeError, match="Field `project.dynamic` must be an array"):
_ = metadata.dynamic
def test_cache_entry_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"dynamic": [10]}})
with pytest.raises(TypeError, match="Field #1 of field `project.dynamic` must be a string"):
_ = metadata.dynamic
def test_cache_correct(self, temp_dir, helpers):
metadata = ProjectMetadata(
str(temp_dir),
PluginManager(),
{
"project": {"name": "foo", "dynamic": ["version", "description"]},
"tool": {"hatch": {"version": {"path": "a/b"}, "metadata": {"hooks": {"custom": {}}}}},
},
)
file_path = temp_dir / "a" / "b"
file_path.ensure_parent_dir_exists()
file_path.write_text('__version__ = "0.0.1"')
file_path = temp_dir / DEFAULT_BUILD_SCRIPT
file_path.write_text(
helpers.dedent(
"""
from hatchling.metadata.plugin.interface import MetadataHookInterface
class CustomHook(MetadataHookInterface):
def update(self, metadata):
metadata['description'] = metadata['name'] + 'bar'
"""
)
)
# Trigger hooks with `metadata.core` first
assert metadata.core.dynamic == []
assert metadata.dynamic == ["version", "description"]
class TestRawName:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"name": 9000, "dynamic": ["name"]}})
with pytest.raises(
ValueError, match="Static metadata field `name` cannot be present in field `project.dynamic`"
):
_ = metadata.core.raw_name
def test_missing(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
with pytest.raises(ValueError, match="Missing required field `project.name`"):
_ = metadata.core.raw_name
def test_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"name": 9000}})
with pytest.raises(TypeError, match="Field `project.name` must be a string"):
_ = metadata.core.raw_name
def test_invalid(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"name": "my app"}})
with pytest.raises(
ValueError,
match=(
"Required field `project.name` must only contain ASCII letters/digits, underscores, "
"hyphens, and periods, and must begin and end with ASCII letters/digits."
),
):
_ = metadata.core.raw_name
def test_correct(self, isolation):
name = "My.App"
metadata = ProjectMetadata(str(isolation), None, {"project": {"name": name}})
assert metadata.core.raw_name is metadata.core.raw_name is name
class TestName:
@pytest.mark.parametrize("name", ["My--App", "My__App", "My..App"])
def test_normalization(self, isolation, name):
metadata = ProjectMetadata(str(isolation), None, {"project": {"name": name}})
assert metadata.core.name == metadata.core.name == "my-app"
class TestVersion:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"version": 9000, "dynamic": ["version"]}})
with pytest.raises(
ValueError,
match="Metadata field `version` cannot be both statically defined and listed in field `project.dynamic`",
):
_ = metadata.core.version
def test_static_missing(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
with pytest.raises(
ValueError,
match="Field `project.version` can only be resolved dynamically if `version` is in field `project.dynamic`",
):
_ = metadata.version
def test_static_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"version": 9000}})
with pytest.raises(TypeError, match="Field `project.version` must be a string"):
_ = metadata.version
def test_static_invalid(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"version": "0..0"}})
with pytest.raises(
ValueError,
match="Invalid version `0..0` from field `project.version`, see https://peps.python.org/pep-0440/",
):
_ = metadata.version
def test_static_normalization(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"version": "0.1.0.0-rc.1"}})
assert metadata.version == metadata.version == "0.1.0.0rc1"
assert metadata.core.version == metadata.core.version == "0.1.0.0-rc.1"
def test_dynamic_missing(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"dynamic": ["version"]}, "tool": {"hatch": {}}})
with pytest.raises(ValueError, match="Missing `tool.hatch.version` configuration"):
_ = metadata.version
def test_dynamic_not_table(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"dynamic": ["version"]}, "tool": {"hatch": {"version": "1.0"}}}
)
with pytest.raises(TypeError, match="Field `tool.hatch.version` must be a table"):
_ = metadata.version
def test_dynamic_source_empty(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"dynamic": ["version"]}, "tool": {"hatch": {"version": {"source": ""}}}}
)
with pytest.raises(
ValueError, match="The `source` option under the `tool.hatch.version` table must not be empty if defined"
):
_ = metadata.version.cached
def test_dynamic_source_not_string(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"dynamic": ["version"]}, "tool": {"hatch": {"version": {"source": 42}}}}
)
with pytest.raises(TypeError, match="Field `tool.hatch.version.source` must be a string"):
_ = metadata.version.cached
def test_dynamic_unknown_source(self, isolation):
metadata = ProjectMetadata(
str(isolation),
PluginManager(),
{"project": {"dynamic": ["version"]}, "tool": {"hatch": {"version": {"source": "foo"}}}},
)
with pytest.raises(ValueError, match="Unknown version source: foo"):
_ = metadata.version.cached
def test_dynamic_source_regex(self, temp_dir):
metadata = ProjectMetadata(
str(temp_dir),
PluginManager(),
{"project": {"dynamic": ["version"]}, "tool": {"hatch": {"version": {"source": "regex", "path": "a/b"}}}},
)
file_path = temp_dir / "a" / "b"
file_path.ensure_parent_dir_exists()
file_path.write_text('__version__ = "0.0.1"')
assert metadata.hatch.version.source is metadata.hatch.version.source
assert isinstance(metadata.hatch.version.source, RegexSource)
assert metadata.hatch.version.cached == metadata.hatch.version.cached == "0.0.1"
def test_dynamic_source_regex_invalid(self, temp_dir):
metadata = ProjectMetadata(
str(temp_dir),
PluginManager(),
{"project": {"dynamic": ["version"]}, "tool": {"hatch": {"version": {"source": "regex", "path": "a/b"}}}},
)
file_path = temp_dir / "a" / "b"
file_path.ensure_parent_dir_exists()
file_path.write_text('__version__ = "0..0"')
with pytest.raises(
ValueError, match="Invalid version `0..0` from source `regex`, see https://peps.python.org/pep-0440/"
):
_ = metadata.version
def test_dynamic_error(self, isolation):
metadata = ProjectMetadata(
str(isolation),
PluginManager(),
{"project": {"dynamic": ["version"]}, "tool": {"hatch": {"version": {"source": "regex"}}}},
)
with pytest.raises(
ValueError, match="Error getting the version from source `regex`: option `path` must be specified"
):
_ = metadata.version.cached
class TestDescription:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"description": 9000, "dynamic": ["description"]}})
with pytest.raises(
ValueError,
match=(
"Metadata field `description` cannot be both statically defined and listed in field `project.dynamic`"
),
):
_ = metadata.core.description
def test_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"description": 9000}})
with pytest.raises(TypeError, match="Field `project.description` must be a string"):
_ = metadata.core.description
def test_default(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
assert metadata.core.description == metadata.core.description == ""
def test_custom(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"description": "foo"}})
assert metadata.core.description == metadata.core.description == "foo"
def test_normaliza(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"description": "\nfirst line.\r\nsecond line"}})
assert metadata.core.description == metadata.core.description == " first line. second line"
class TestReadme:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"readme": 9000, "dynamic": ["readme"]}})
with pytest.raises(
ValueError,
match="Metadata field `readme` cannot be both statically defined and listed in field `project.dynamic`",
):
_ = metadata.core.readme
@pytest.mark.parametrize("attribute", ["readme", "readme_content_type"])
def test_unknown_type(self, isolation, attribute):
metadata = ProjectMetadata(str(isolation), None, {"project": {"readme": 9000}})
with pytest.raises(TypeError, match="Field `project.readme` must be a string or a table"):
_ = getattr(metadata.core, attribute)
def test_default(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
assert metadata.core.readme == metadata.core.readme == ""
assert metadata.core.readme_content_type == metadata.core.readme_content_type == "text/markdown"
assert metadata.core.readme_path == metadata.core.readme_path == ""
def test_string_path_unknown_content_type(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"readme": "foo"}})
with pytest.raises(
TypeError, match="Unable to determine the content-type based on the extension of readme file: foo"
):
_ = metadata.core.readme
def test_string_path_nonexistent(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"readme": "foo/bar.md"}})
with pytest.raises(OSError, match="Readme file does not exist: foo/bar\\.md"):
_ = metadata.core.readme
@pytest.mark.parametrize(
("extension", "content_type"), [(".md", "text/markdown"), (".rst", "text/x-rst"), (".txt", "text/plain")]
)
def test_string_correct(self, extension, content_type, temp_dir):
metadata = ProjectMetadata(str(temp_dir), None, {"project": {"readme": f"foo/bar{extension}"}})
file_path = temp_dir / "foo" / f"bar{extension}"
file_path.ensure_parent_dir_exists()
file_path.write_text("test content")
assert metadata.core.readme == metadata.core.readme == "test content"
assert metadata.core.readme_content_type == metadata.core.readme_content_type == content_type
assert metadata.core.readme_path == metadata.core.readme_path == f"foo/bar{extension}"
def test_table_content_type_missing(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"readme": {}}})
with pytest.raises(ValueError, match="Field `content-type` is required in the `project.readme` table"):
_ = metadata.core.readme
def test_table_content_type_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"readme": {"content-type": 5}}})
with pytest.raises(TypeError, match="Field `content-type` in the `project.readme` table must be a string"):
_ = metadata.core.readme
def test_table_content_type_not_unknown(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"readme": {"content-type": "foo"}}})
with pytest.raises(
ValueError,
match=(
"Field `content-type` in the `project.readme` table must be one of the following: "
"text/markdown, text/x-rst, text/plain"
),
):
_ = metadata.core.readme
def test_table_multiple_options(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"readme": {"content-type": "text/markdown", "file": "", "text": ""}}}
)
with pytest.raises(ValueError, match="Cannot specify both `file` and `text` in the `project.readme` table"):
_ = metadata.core.readme
def test_table_no_option(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"readme": {"content-type": "text/markdown"}}})
with pytest.raises(ValueError, match="Must specify either `file` or `text` in the `project.readme` table"):
_ = metadata.core.readme
def test_table_file_not_string(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"readme": {"content-type": "text/markdown", "file": 4}}}
)
with pytest.raises(TypeError, match="Field `file` in the `project.readme` table must be a string"):
_ = metadata.core.readme
def test_table_file_nonexistent(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"readme": {"content-type": "text/markdown", "file": "foo/bar.md"}}}
)
with pytest.raises(OSError, match="Readme file does not exist: foo/bar\\.md"):
_ = metadata.core.readme
def test_table_file_correct(self, temp_dir):
metadata = ProjectMetadata(
str(temp_dir), None, {"project": {"readme": {"content-type": "text/markdown", "file": "foo/bar.markdown"}}}
)
file_path = temp_dir / "foo" / "bar.markdown"
file_path.ensure_parent_dir_exists()
file_path.write_text("test content")
assert metadata.core.readme == metadata.core.readme == "test content"
assert metadata.core.readme_content_type == metadata.core.readme_content_type == "text/markdown"
assert metadata.core.readme_path == metadata.core.readme_path == "foo/bar.markdown"
def test_table_text_not_string(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"readme": {"content-type": "text/markdown", "text": 4}}}
)
with pytest.raises(TypeError, match="Field `text` in the `project.readme` table must be a string"):
_ = metadata.core.readme
def test_table_text_correct(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"readme": {"content-type": "text/markdown", "text": "test content"}}}
)
assert metadata.core.readme == metadata.core.readme == "test content"
assert metadata.core.readme_content_type == metadata.core.readme_content_type == "text/markdown"
assert metadata.core.readme_path == metadata.core.readme_path == ""
class TestRequiresPython:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"requires-python": 9000, "dynamic": ["requires-python"]}}
)
with pytest.raises(
ValueError,
match=(
"Metadata field `requires-python` cannot be both statically defined and "
"listed in field `project.dynamic`"
),
):
_ = metadata.core.requires_python
@pytest.mark.parametrize("attribute", ["requires_python", "python_constraint"])
def test_not_string(self, isolation, attribute):
metadata = ProjectMetadata(str(isolation), None, {"project": {"requires-python": 9000}})
with pytest.raises(TypeError, match="Field `project.requires-python` must be a string"):
_ = getattr(metadata.core, attribute)
def test_invalid(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"requires-python": "^1"}})
with pytest.raises(ValueError, match="Field `project.requires-python` is invalid: .+"):
_ = metadata.core.requires_python
def test_default(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
assert metadata.core.requires_python == metadata.core.requires_python == ""
for major_version in map(str, range(10)):
assert metadata.core.python_constraint.contains(major_version)
def test_custom(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"requires-python": ">2"}})
assert metadata.core.requires_python == metadata.core.requires_python == ">2"
assert not metadata.core.python_constraint.contains("2")
assert metadata.core.python_constraint.contains("3")
class TestLicense:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": 9000, "dynamic": ["license"]}})
with pytest.raises(
ValueError,
match="Metadata field `license` cannot be both statically defined and listed in field `project.dynamic`",
):
_ = metadata.core.license
def test_invalid_type(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": 9000}})
with pytest.raises(TypeError, match="Field `project.license` must be a string or a table"):
_ = metadata.core.license
def test_default(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
assert metadata.core.license == metadata.core.license == ""
assert metadata.core.license_expression == metadata.core.license_expression == ""
def test_normalization(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": "mit or apache-2.0"}})
assert metadata.core.license_expression == "MIT OR Apache-2.0"
def test_invalid_expression(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": "mit or foo"}})
with pytest.raises(ValueError, match="Error parsing field `project.license` - Unknown license: 'foo'"):
_ = metadata.core.license_expression
def test_multiple_options(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": {"file": "", "text": ""}}})
with pytest.raises(ValueError, match="Cannot specify both `file` and `text` in the `project.license` table"):
_ = metadata.core.license
def test_no_option(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": {}}})
with pytest.raises(ValueError, match="Must specify either `file` or `text` in the `project.license` table"):
_ = metadata.core.license
def test_file_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": {"file": 4}}})
with pytest.raises(TypeError, match="Field `file` in the `project.license` table must be a string"):
_ = metadata.core.license
def test_file_nonexistent(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": {"file": "foo/bar.md"}}})
with pytest.raises(OSError, match="License file does not exist: foo/bar\\.md"):
_ = metadata.core.license
def test_file_correct(self, temp_dir):
metadata = ProjectMetadata(str(temp_dir), None, {"project": {"license": {"file": "foo/bar.md"}}})
file_path = temp_dir / "foo" / "bar.md"
file_path.ensure_parent_dir_exists()
file_path.write_text("test content")
assert metadata.core.license == "test content"
def test_text_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": {"text": 4}}})
with pytest.raises(TypeError, match="Field `text` in the `project.license` table must be a string"):
_ = metadata.core.license
def test_text_correct(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license": {"text": "test content"}}})
assert metadata.core.license == "test content"
class TestLicenseFiles:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(
str(isolation), None, {"project": {"license-files": 9000, "dynamic": ["license-files"]}}
)
with pytest.raises(
ValueError,
match=(
"Metadata field `license-files` cannot be both statically defined and listed in field `project.dynamic`"
),
):
_ = metadata.core.license_files
def test_not_array(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license-files": 9000}})
with pytest.raises(TypeError, match="Field `project.license-files` must be an array"):
_ = metadata.core.license_files
def test_entry_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"license-files": [9000]}})
with pytest.raises(TypeError, match="Entry #1 of field `project.license-files` must be a string"):
_ = metadata.core.license_files
def test_default_globs_no_licenses(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
assert metadata.core.license_files == metadata.core.license_files == []
def test_default_globs_with_licenses(self, temp_dir):
metadata = ProjectMetadata(str(temp_dir), None, {"project": {}})
expected = []
(temp_dir / "foo").touch()
for name in ("LICENSE", "LICENCE", "COPYING", "NOTICE", "AUTHORS"):
(temp_dir / name).touch()
expected.append(name)
name_with_extension = f"{name}.txt"
(temp_dir / f"{name}.txt").touch()
expected.append(name_with_extension)
assert metadata.core.license_files == sorted(expected)
def test_globs_with_licenses(self, temp_dir):
metadata = ProjectMetadata(str(temp_dir), None, {"project": {"license-files": ["LICENSES/*"]}})
licenses_dir = temp_dir / "LICENSES"
licenses_dir.mkdir()
(licenses_dir / "MIT.txt").touch()
(licenses_dir / "Apache-2.0.txt").touch()
for name in ("LICENSE", "LICENCE", "COPYING", "NOTICE", "AUTHORS"):
(temp_dir / name).touch()
assert metadata.core.license_files == ["LICENSES/Apache-2.0.txt", "LICENSES/MIT.txt"]
def test_paths_with_licenses(self, temp_dir):
metadata = ProjectMetadata(
str(temp_dir),
None,
{"project": {"license-files": ["LICENSES/Apache-2.0.txt", "LICENSES/MIT.txt", "COPYING"]}},
)
licenses_dir = temp_dir / "LICENSES"
licenses_dir.mkdir()
(licenses_dir / "MIT.txt").touch()
(licenses_dir / "Apache-2.0.txt").touch()
for name in ("LICENSE", "LICENCE", "COPYING", "NOTICE", "AUTHORS"):
(temp_dir / name).touch()
assert metadata.core.license_files == ["COPYING", "LICENSES/Apache-2.0.txt", "LICENSES/MIT.txt"]
class TestAuthors:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"authors": 9000, "dynamic": ["authors"]}})
with pytest.raises(
ValueError,
match="Metadata field `authors` cannot be both statically defined and listed in field `project.dynamic`",
):
_ = metadata.core.authors
def test_not_array(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"authors": "foo"}})
with pytest.raises(TypeError, match="Field `project.authors` must be an array"):
_ = metadata.core.authors
def test_default(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
assert metadata.core.authors == metadata.core.authors == []
def test_not_table(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"authors": ["foo"]}})
with pytest.raises(TypeError, match="Author #1 of field `project.authors` must be an inline table"):
_ = metadata.core.authors
def test_no_data(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"authors": [{}]}})
with pytest.raises(
ValueError, match="Author #1 of field `project.authors` must specify either `name` or `email`"
):
_ = metadata.core.authors
def test_name_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"authors": [{"name": 9}]}})
with pytest.raises(TypeError, match="Name of author #1 of field `project.authors` must be a string"):
_ = metadata.core.authors
def test_name_only(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"authors": [{"name": "foo"}]}})
assert len(metadata.core.authors) == 1
assert metadata.core.authors[0] == {"name": "foo"}
assert metadata.core.authors_data == metadata.core.authors_data == {"name": ["foo"], "email": []}
def test_email_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"authors": [{"email": 9}]}})
with pytest.raises(TypeError, match="Email of author #1 of field `project.authors` must be a string"):
_ = metadata.core.authors
def test_email_only(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"authors": [{"email": "foo@bar.baz"}]}})
assert len(metadata.core.authors) == 1
assert metadata.core.authors[0] == {"email": "foo@bar.baz"}
assert metadata.core.authors_data == {"name": [], "email": ["foo@bar.baz"]}
def test_name_and_email(self, isolation):
metadata = ProjectMetadata(
str(isolation),
None,
{
"project": {
"authors": [{"name": "foo2", "email": "foo2@bar.baz"}, {"name": "foo1", "email": "foo1@bar.baz"}]
}
},
)
assert len(metadata.core.authors) == 2
assert metadata.core.authors[0] == {"name": "foo2", "email": "foo2@bar.baz"}
assert metadata.core.authors[1] == {"name": "foo1", "email": "foo1@bar.baz"}
assert metadata.core.authors_data == {"name": [], "email": ["foo2 <foo2@bar.baz>", "foo1 <foo1@bar.baz>"]}
class TestMaintainers:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"maintainers": 9000, "dynamic": ["maintainers"]}})
with pytest.raises(
ValueError,
match=(
"Metadata field `maintainers` cannot be both statically defined and listed in field `project.dynamic`"
),
):
_ = metadata.core.maintainers
def test_not_array(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"maintainers": "foo"}})
with pytest.raises(TypeError, match="Field `project.maintainers` must be an array"):
_ = metadata.core.maintainers
def test_default(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {}})
assert metadata.core.maintainers == metadata.core.maintainers == []
def test_not_table(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"maintainers": ["foo"]}})
with pytest.raises(TypeError, match="Maintainer #1 of field `project.maintainers` must be an inline table"):
_ = metadata.core.maintainers
def test_no_data(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"maintainers": [{}]}})
with pytest.raises(
ValueError, match="Maintainer #1 of field `project.maintainers` must specify either `name` or `email`"
):
_ = metadata.core.maintainers
def test_name_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"maintainers": [{"name": 9}]}})
with pytest.raises(TypeError, match="Name of maintainer #1 of field `project.maintainers` must be a string"):
_ = metadata.core.maintainers
def test_name_only(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"maintainers": [{"name": "foo"}]}})
assert len(metadata.core.maintainers) == 1
assert metadata.core.maintainers[0] == {"name": "foo"}
assert metadata.core.maintainers_data == metadata.core.maintainers_data == {"name": ["foo"], "email": []}
def test_email_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"maintainers": [{"email": 9}]}})
with pytest.raises(TypeError, match="Email of maintainer #1 of field `project.maintainers` must be a string"):
_ = metadata.core.maintainers
def test_email_only(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"maintainers": [{"email": "foo@bar.baz"}]}})
assert len(metadata.core.maintainers) == 1
assert metadata.core.maintainers[0] == {"email": "foo@bar.baz"}
assert metadata.core.maintainers_data == {"name": [], "email": ["foo@bar.baz"]}
def test_name_and_email(self, isolation):
metadata = ProjectMetadata(
str(isolation),
None,
{
"project": {
"maintainers": [
{"name": "foo2", "email": "foo2@bar.baz"},
{"name": "foo1", "email": "foo1@bar.baz"},
]
}
},
)
assert len(metadata.core.maintainers) == 2
assert metadata.core.maintainers[0] == {"name": "foo2", "email": "foo2@bar.baz"}
assert metadata.core.maintainers[1] == {"name": "foo1", "email": "foo1@bar.baz"}
assert metadata.core.maintainers_data == {"name": [], "email": ["foo2 <foo2@bar.baz>", "foo1 <foo1@bar.baz>"]}
class TestKeywords:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"keywords": 9000, "dynamic": ["keywords"]}})
with pytest.raises(
ValueError,
match="Metadata field `keywords` cannot be both statically defined and listed in field `project.dynamic`",
):
_ = metadata.core.keywords
def test_not_array(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"keywords": 10}})
with pytest.raises(TypeError, match="Field `project.keywords` must be an array"):
_ = metadata.core.keywords
def test_entry_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"keywords": [10]}})
with pytest.raises(TypeError, match="Keyword #1 of field `project.keywords` must be a string"):
_ = metadata.core.keywords
def test_correct(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"keywords": ["foo", "foo", "bar"]}})
assert metadata.core.keywords == metadata.core.keywords == ["bar", "foo"]
class TestClassifiers:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"classifiers": 9000, "dynamic": ["classifiers"]}})
with pytest.raises(
ValueError,
match=(
"Metadata field `classifiers` cannot be both statically defined and listed in field `project.dynamic`"
),
):
_ = metadata.core.classifiers
def test_not_array(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"classifiers": 10}})
with pytest.raises(TypeError, match="Field `project.classifiers` must be an array"):
_ = metadata.core.classifiers
def test_entry_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"classifiers": [10]}})
with pytest.raises(TypeError, match="Classifier #1 of field `project.classifiers` must be a string"):
_ = metadata.core.classifiers
def test_entry_unknown(self, isolation, monkeypatch):
monkeypatch.delenv("HATCH_METADATA_CLASSIFIERS_NO_VERIFY", False)
metadata = ProjectMetadata(str(isolation), None, {"project": {"classifiers": ["foo"]}})
with pytest.raises(ValueError, match="Unknown classifier in field `project.classifiers`: foo"):
_ = metadata.core.classifiers
def test_entry_unknown_no_verify(self, isolation, monkeypatch):
monkeypatch.setenv("HATCH_METADATA_CLASSIFIERS_NO_VERIFY", "1")
classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.9",
"Development Status :: 4 - Beta",
"Private :: Do Not Upload",
"Foo",
]
metadata = ProjectMetadata(str(isolation), None, {"project": {"classifiers": classifiers}})
assert (
metadata.core.classifiers
== metadata.core.classifiers
== [
"Private :: Do Not Upload",
"Development Status :: 4 - Beta",
"Foo",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.11",
]
)
def test_correct(self, isolation):
classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.9",
"Development Status :: 4 - Beta",
"Private :: Do Not Upload",
]
metadata = ProjectMetadata(str(isolation), None, {"project": {"classifiers": classifiers}})
assert (
metadata.core.classifiers
== metadata.core.classifiers
== [
"Private :: Do Not Upload",
"Development Status :: 4 - Beta",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.11",
]
)
class TestURLs:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"urls": 9000, "dynamic": ["urls"]}})
with pytest.raises(
ValueError,
match="Metadata field `urls` cannot be both statically defined and listed in field `project.dynamic`",
):
_ = metadata.core.urls
def test_not_table(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"urls": 10}})
with pytest.raises(TypeError, match="Field `project.urls` must be a table"):
_ = metadata.core.urls
def test_entry_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"urls": {"foo": 7}}})
with pytest.raises(TypeError, match="URL `foo` of field `project.urls` must be a string"):
_ = metadata.core.urls
def test_correct(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"urls": {"foo": "bar", "bar": "baz"}}})
assert metadata.core.urls == metadata.core.urls == {"bar": "baz", "foo": "bar"}
class TestScripts:
def test_dynamic(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"scripts": 9000, "dynamic": ["scripts"]}})
with pytest.raises(
ValueError,
match="Metadata field `scripts` cannot be both statically defined and listed in field `project.dynamic`",
):
_ = metadata.core.scripts
def test_not_table(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"scripts": 10}})
with pytest.raises(TypeError, match="Field `project.scripts` must be a table"):
_ = metadata.core.scripts
def test_entry_not_string(self, isolation):
metadata = ProjectMetadata(str(isolation), None, {"project": {"scripts": {"foo": 7}}})
with pytest.raises(TypeError, match="Object reference `foo` of field `project.scripts` must be a string"):