-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_all.py
More file actions
1023 lines (824 loc) · 38 KB
/
test_all.py
File metadata and controls
1023 lines (824 loc) · 38 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
"""Comprehensive tests for check_dist."""
from __future__ import annotations
import io
import subprocess
import sys
import tarfile
import textwrap
import zipfile
from pathlib import Path
from unittest.mock import patch
import pytest
from check_dist._core import (
CheckDistError,
_filter_extras_by_hatch,
_find_pre_built,
_matches_hatch_pattern,
_module_name_from_project,
_sdist_expected_files,
check_absent,
check_dist,
check_present,
check_sdist_vs_vcs,
check_wrong_platform_extensions,
copier_defaults,
find_dist_files,
get_vcs_files,
list_sdist_files,
list_wheel_files,
load_config,
load_copier_config,
load_hatch_config,
matches_pattern,
translate_extension,
)
# ── translate_extension ───────────────────────────────────────────────
class TestTranslateExtension:
def test_no_change_on_native(self):
"""No translation when extension is already native."""
if sys.platform == "linux":
assert translate_extension("foo.so") == "foo.so"
elif sys.platform == "darwin":
assert translate_extension("foo.so") == "foo.so"
elif sys.platform == "win32":
assert translate_extension("foo.pyd") == "foo.pyd"
@patch("check_dist._core._get_platform_key", return_value="win32")
def test_so_to_pyd_on_windows(self, _mock):
assert translate_extension("mylib.so") == "mylib.pyd"
@patch("check_dist._core._get_platform_key", return_value="win32")
def test_dylib_to_dll_on_windows(self, _mock):
assert translate_extension("mylib.dylib") == "mylib.dll"
@patch("check_dist._core._get_platform_key", return_value="linux")
def test_pyd_to_so_on_linux(self, _mock):
assert translate_extension("mylib.pyd") == "mylib.so"
@patch("check_dist._core._get_platform_key", return_value="linux")
def test_dll_to_so_on_linux(self, _mock):
assert translate_extension("mylib.dll") == "mylib.so"
@patch("check_dist._core._get_platform_key", return_value="darwin")
def test_pyd_to_so_on_darwin(self, _mock):
assert translate_extension("mylib.pyd") == "mylib.so"
@patch("check_dist._core._get_platform_key", return_value="darwin")
def test_dll_to_dylib_on_darwin(self, _mock):
assert translate_extension("mylib.dll") == "mylib.dylib"
def test_unrecognized_extension(self):
assert translate_extension("file.txt") == "file.txt"
def test_no_extension(self):
assert translate_extension("Makefile") == "Makefile"
def test_glob_pattern(self):
"""Glob patterns with extensions are also translated."""
with patch("check_dist._core._get_platform_key", return_value="win32"):
assert translate_extension("*.so") == "*.pyd"
# ── matches_pattern ───────────────────────────────────────────────────
class TestMatchesPattern:
def test_exact_file(self):
assert matches_pattern("LICENSE", "LICENSE")
def test_directory_prefix(self):
assert matches_pattern("check_dist/__init__.py", "check_dist")
def test_directory_prefix_no_false_positive(self):
assert not matches_pattern("check_dist_extra/foo.py", "check_dist")
def test_glob_star(self):
assert matches_pattern("check_dist/__init__.py", "*.py")
def test_glob_full_path(self):
assert matches_pattern("check_dist/__init__.py", "check_dist/*.py")
def test_no_match(self):
assert not matches_pattern("README.md", "LICENSE")
def test_basename_match(self):
assert matches_pattern("some/deep/path/setup.cfg", "setup.cfg")
@patch("check_dist._core._get_platform_key", return_value="win32")
def test_cross_platform_extension(self, _mock):
"""On Windows, pattern ``*.so`` should match ``*.pyd``."""
assert matches_pattern("mylib.pyd", "*.so")
def test_question_mark_glob(self):
assert matches_pattern("a.py", "?.py")
assert not matches_pattern("ab.py", "?.py")
def test_bracket_glob(self):
assert matches_pattern("a1.txt", "a[0-9].txt")
assert not matches_pattern("ab.txt", "a[0-9].txt")
def test_dotfile(self):
assert matches_pattern(".gitignore", ".gitignore")
def test_nested_directory_pattern(self):
assert matches_pattern(".github/workflows/ci.yml", ".github")
# ── check_present / check_absent ─────────────────────────────────────
class TestCheckPresent:
FILES = [
"check_dist/__init__.py",
"check_dist/_core.py",
"LICENSE",
"pyproject.toml",
"README.md",
]
def test_all_present(self):
errors = check_present(self.FILES, ["check_dist", "LICENSE"], "sdist")
assert errors == []
def test_missing_pattern(self):
errors = check_present(self.FILES, ["CHANGELOG.md"], "sdist")
assert len(errors) == 1
assert "CHANGELOG.md" in errors[0]
def test_glob_present(self):
errors = check_present(self.FILES, ["*.md"], "sdist")
assert errors == []
def test_empty_patterns(self):
assert check_present(self.FILES, [], "sdist") == []
@patch("check_dist._core._get_platform_key", return_value="win32")
def test_cross_platform_present(self, _mock):
"""Pattern '*.so' on Windows should look for '*.pyd'."""
files = ["mypackage/ext.pyd"]
errors = check_present(files, ["*.so"], "wheel")
assert errors == []
@patch("check_dist._core._get_platform_key", return_value="win32")
def test_cross_platform_missing(self, _mock):
"""Pattern '*.so' on Windows should fail if no .pyd found."""
files = ["mypackage/__init__.py"]
errors = check_present(files, ["*.so"], "wheel")
assert len(errors) == 1
assert "translated" in errors[0]
class TestCheckAbsent:
FILES = [
"check_dist/__init__.py",
"Makefile",
".github/workflows/ci.yml",
"docs/index.md",
]
def test_unwanted_present(self):
errors = check_absent(self.FILES, ["Makefile"], "sdist")
assert len(errors) == 1
assert "Makefile" in errors[0]
def test_unwanted_directory(self):
errors = check_absent(self.FILES, [".github"], "sdist")
assert len(errors) == 1
assert ".github" in errors[0]
def test_all_clean(self):
errors = check_absent(self.FILES, ["dist", ".gitignore"], "sdist")
assert errors == []
def test_empty_patterns(self):
assert check_absent(self.FILES, [], "sdist") == []
def test_docs_directory(self):
errors = check_absent(self.FILES, ["docs"], "sdist")
assert len(errors) == 1
assert "docs" in errors[0]
def test_nested_in_present_pattern_skipped(self):
"""Files nested inside a 'present' dir are not flagged as absent."""
files = [
"lerna/__init__.py",
"lerna/test_utils/configs/missing_init_py/.gitignore",
"lerna/tests/fake_package/pyproject.toml",
"lerna/tests/fake_package2/pyproject.toml",
"pyproject.toml", # top-level: should still be caught
]
errors = check_absent(
files,
[".gitignore", "pyproject.toml"],
"wheel",
present_patterns=["lerna"],
)
assert len(errors) == 1
assert "pyproject.toml" in errors[0]
# Only the top-level pyproject.toml, not the nested ones
assert "lerna/" not in errors[0]
def test_present_patterns_none_flags_all(self):
"""Without present_patterns, nested files are still flagged."""
files = [
"lerna/tests/fake_package/pyproject.toml",
"pyproject.toml",
]
errors = check_absent(files, ["pyproject.toml"], "wheel")
assert len(errors) == 1
assert "lerna/tests/fake_package/pyproject.toml" in errors[0]
# ── check_wrong_platform_extensions ──────────────────────────────────
class TestCheckWrongPlatformExtensions:
@patch("check_dist._core._get_platform_key", return_value="win32")
def test_so_on_windows(self, _mock):
files = ["mypackage/ext.so"]
errors = check_wrong_platform_extensions(files, "wheel")
assert len(errors) == 1
assert ".so" in errors[0]
@patch("check_dist._core._get_platform_key", return_value="linux")
def test_pyd_on_linux(self, _mock):
files = ["mypackage/ext.pyd"]
errors = check_wrong_platform_extensions(files, "wheel")
assert len(errors) == 1
assert ".pyd" in errors[0]
@patch("check_dist._core._get_platform_key", return_value="linux")
def test_clean_on_linux(self, _mock):
files = ["mypackage/ext.so", "mypackage/__init__.py"]
errors = check_wrong_platform_extensions(files, "wheel")
assert errors == []
@patch("check_dist._core._get_platform_key", return_value="darwin")
def test_clean_on_darwin(self, _mock):
files = ["mypackage/ext.so", "mypackage/__init__.py"]
errors = check_wrong_platform_extensions(files, "wheel")
assert errors == []
# ── check_sdist_vs_vcs ───────────────────────────────────────────────
class TestCheckSdistVsVcs:
def test_matching(self):
sdist = ["check_dist/__init__.py", "pyproject.toml", "README.md"]
vcs = ["check_dist/__init__.py", "pyproject.toml", "README.md"]
errors = check_sdist_vs_vcs(sdist, vcs, {})
assert errors == []
def test_extra_in_sdist(self):
sdist = ["check_dist/__init__.py", "pyproject.toml", "stray_file.txt"]
vcs = ["check_dist/__init__.py", "pyproject.toml"]
errors = check_sdist_vs_vcs(sdist, vcs, {})
assert any("stray_file.txt" in e for e in errors)
def test_missing_from_sdist(self):
sdist = ["check_dist/__init__.py"]
vcs = ["check_dist/__init__.py", "pyproject.toml"]
errors = check_sdist_vs_vcs(sdist, vcs, {})
assert any("pyproject.toml" in e for e in errors)
def test_generated_files_ignored(self):
sdist = ["PKG-INFO", "check_dist/__init__.py"]
vcs = ["check_dist/__init__.py"]
errors = check_sdist_vs_vcs(sdist, vcs, {})
assert errors == []
def test_egg_info_ignored(self):
sdist = ["check_dist/__init__.py", "check_dist.egg-info/PKG-INFO"]
vcs = ["check_dist/__init__.py"]
errors = check_sdist_vs_vcs(sdist, vcs, {})
assert errors == []
def test_hatch_packages_filter(self):
"""When hatch sdist packages are set, only those should be expected."""
sdist = ["mylib/__init__.py", "pyproject.toml"]
vcs = [
"mylib/__init__.py",
"pyproject.toml",
"tests/test_foo.py",
"docs/index.md",
]
hatch = {"targets": {"sdist": {"packages": ["mylib"]}}}
errors = check_sdist_vs_vcs(sdist, vcs, hatch)
assert errors == []
def test_dotfiles_ignored_in_missing(self):
sdist = ["check_dist/__init__.py"]
vcs = ["check_dist/__init__.py", ".gitignore"]
errors = check_sdist_vs_vcs(sdist, vcs, {})
assert errors == []
def test_artifacts_not_flagged_as_extra(self):
"""Build artifacts (e.g. .so) should not be flagged as extra."""
sdist = ["pkg/__init__.py", "pkg/ext.so"]
vcs = ["pkg/__init__.py"]
hatch = {"artifacts": ["*.so"]}
errors = check_sdist_vs_vcs(sdist, vcs, hatch)
assert errors == []
def test_artifacts_in_sdist_section(self):
"""Artifacts listed in hatch sdist config should also be ignored."""
sdist = ["pkg/extension/index.js", "pkg/__init__.py", "pyproject.toml"]
vcs = ["pkg/__init__.py", "pyproject.toml"]
hatch = {"targets": {"sdist": {"artifacts": ["pkg/extension"]}}}
errors = check_sdist_vs_vcs(sdist, vcs, hatch)
assert errors == []
def test_only_include_vcs_check(self):
"""only-include config should scope the VCS comparison."""
sdist = ["pkg/__init__.py", "rust/lib.rs", "Cargo.toml", "pyproject.toml"]
vcs = ["pkg/__init__.py", "rust/lib.rs", "Cargo.toml", "pyproject.toml", "Makefile"]
hatch = {"targets": {"sdist": {"only-include": ["pkg", "rust", "Cargo.toml"]}}}
errors = check_sdist_vs_vcs(sdist, vcs, hatch)
assert errors == []
# ── _sdist_expected_files ────────────────────────────────────────────
class TestMatchesHatchPattern:
def test_exact_file(self):
assert _matches_hatch_pattern("Cargo.toml", "Cargo.toml")
def test_directory_prefix(self):
assert _matches_hatch_pattern("rust/src/lib.rs", "rust")
def test_rooted_pattern(self):
assert _matches_hatch_pattern(".gitignore", "/.gitignore")
def test_no_match(self):
assert not _matches_hatch_pattern("src/lib.rs", "rust")
def test_glob(self):
assert _matches_hatch_pattern("foo.pyc", "*.pyc")
def test_basename_match(self):
assert _matches_hatch_pattern("deep/Makefile", "Makefile")
class TestSdistExpectedFiles:
"""Covers hatch semantics: packages, include, exclude, only-include,
force-include, sources, and their interactions."""
VCS = [
"pkg/__init__.py",
"pkg/core.py",
"rust/src/lib.rs",
"Cargo.toml",
"Cargo.lock",
"tests/test_pkg.py",
"docs/index.md",
"setup.py",
".gitignore",
".gitattributes",
]
def test_no_hatch_config(self):
result = _sdist_expected_files(self.VCS, {})
assert result == set(self.VCS)
# ── only-include ─────────────────────────────────────────────
def test_only_include_exhaustive(self):
hatch = {"targets": {"sdist": {"only-include": ["pkg", "rust", "Cargo.toml"]}}}
result = _sdist_expected_files(self.VCS, hatch)
assert "pkg/__init__.py" in result
assert "pkg/core.py" in result
assert "rust/src/lib.rs" in result
assert "Cargo.toml" in result
assert "Cargo.lock" not in result
assert "tests/test_pkg.py" not in result
assert "docs/index.md" not in result
# ── packages (acts as only-include fallback) ─────────────────
def test_packages_acts_as_only_include(self):
hatch = {"targets": {"sdist": {"packages": ["pkg"]}}}
result = _sdist_expected_files(self.VCS, hatch)
assert "pkg/__init__.py" in result
assert "pkg/core.py" in result
assert "rust/src/lib.rs" not in result
assert "Cargo.toml" not in result
assert "tests/test_pkg.py" not in result
def test_packages_with_include(self):
"""packages + include: include is ignored (hatch treats packages
as only-include, making the walk 'explicit')."""
hatch = {
"targets": {
"sdist": {
"packages": ["pkg"],
"include": ["Cargo.toml", "Cargo.lock", "rust", "src"],
}
}
}
result = _sdist_expected_files(self.VCS, hatch)
# Only package files appear.
assert "pkg/__init__.py" in result
assert "pkg/core.py" in result
# include paths are NOT added when packages is set.
assert "Cargo.toml" not in result
assert "rust/src/lib.rs" not in result
assert "tests/test_pkg.py" not in result
# ── include (no packages, no only-include) ───────────────────
def test_include_filters_full_tree(self):
hatch = {"targets": {"sdist": {"include": ["pkg", "Cargo.toml"]}}}
result = _sdist_expected_files(self.VCS, hatch)
assert "pkg/__init__.py" in result
assert "Cargo.toml" in result
assert "rust/src/lib.rs" not in result
assert "tests/test_pkg.py" not in result
# ── exclude ──────────────────────────────────────────────────
def test_exclude_with_only_include(self):
hatch = {
"targets": {
"sdist": {
"only-include": ["pkg", "rust", "Cargo.toml", "Cargo.lock"],
"exclude": ["target"],
}
}
}
vcs = [*self.VCS, "rust/target/debug/foo"]
result = _sdist_expected_files(vcs, hatch)
assert "pkg/__init__.py" in result
assert "rust/src/lib.rs" in result
assert "rust/target/debug/foo" not in result
def test_exclude_with_packages(self):
hatch = {
"targets": {
"sdist": {
"packages": ["pkg"],
"exclude": ["*.pyc"],
}
}
}
vcs = ["pkg/__init__.py", "pkg/__init__.pyc"]
result = _sdist_expected_files(vcs, hatch)
assert "pkg/__init__.py" in result
assert "pkg/__init__.pyc" not in result
def test_exclude_with_no_constraints(self):
hatch = {"targets": {"sdist": {"exclude": ["docs"]}}}
result = _sdist_expected_files(self.VCS, hatch)
assert "pkg/__init__.py" in result
assert "docs/index.md" not in result
# ── force-include ────────────────────────────────────────────
def test_force_include_adds_files(self):
hatch = {
"targets": {
"sdist": {
"packages": ["pkg"],
"force-include": {"/abs/Cargo.toml": "Cargo.toml"},
}
}
}
result = _sdist_expected_files(self.VCS, hatch)
assert "pkg/__init__.py" in result
assert "Cargo.toml" in result
def test_force_include_survives_exclude(self):
hatch = {
"targets": {
"sdist": {
"only-include": ["pkg", "Cargo.toml"],
"exclude": ["Cargo.toml"],
"force-include": {"/abs/Cargo.toml": "Cargo.toml"},
}
}
}
result = _sdist_expected_files(self.VCS, hatch)
# force-include re-adds Cargo.toml even though exclude removed it
assert "Cargo.toml" in result
def test_global_force_include_fallback(self):
hatch = {
"force-include": {"outside/file.txt": "vendored/file.txt"},
"targets": {"sdist": {"packages": ["pkg"]}},
}
vcs = ["pkg/__init__.py", "vendored/file.txt"]
result = _sdist_expected_files(vcs, hatch)
assert "pkg/__init__.py" in result
assert "vendored/file.txt" in result
def test_target_force_include_overrides_global(self):
hatch = {
"force-include": {"global.txt": "global.txt"},
"targets": {
"sdist": {
"packages": ["pkg"],
"force-include": {"target.txt": "target.txt"},
}
},
}
vcs = ["pkg/__init__.py", "global.txt", "target.txt"]
result = _sdist_expected_files(vcs, hatch)
assert "target.txt" in result
# global force-include is overridden by target-level
assert "global.txt" not in result
# ── combined scenarios ───────────────────────────────────────
def test_only_include_with_exclude(self):
hatch = {
"targets": {
"sdist": {
"only-include": ["pkg", "rust", "Cargo.toml", "Cargo.lock"],
"exclude": ["target", "*.pyc"],
}
}
}
vcs = [*self.VCS, "rust/target/debug/foo", "pkg/__pycache__/core.pyc"]
result = _sdist_expected_files(vcs, hatch)
assert "pkg/__init__.py" in result
assert "rust/src/lib.rs" in result
assert "rust/target/debug/foo" not in result
assert "pkg/__pycache__/core.pyc" not in result
def test_empty_config(self):
result = _sdist_expected_files(self.VCS, {"targets": {"sdist": {}}})
assert result == set(self.VCS)
# ── _filter_extras_by_hatch ──────────────────────────────────────────
class TestFilterExtrasByHatch:
"""Verify that copier-derived extras are trimmed to match hatch config."""
EXTRAS = ["rust", "src", "Cargo.toml", "Cargo.lock", "target"]
def test_no_hatch_config(self):
assert _filter_extras_by_hatch(self.EXTRAS, {}) == self.EXTRAS
def test_only_include_keeps_matching(self):
hatch = {"targets": {"sdist": {"only-include": ["pkg", "rust", "Cargo.toml"]}}}
result = _filter_extras_by_hatch(self.EXTRAS, hatch)
assert "rust" in result
assert "Cargo.toml" in result
assert "src" not in result
assert "Cargo.lock" not in result
def test_packages_clears_root_extras(self):
"""When only packages is set, non-package extras are removed."""
hatch = {"targets": {"sdist": {"packages": ["pkg"]}}}
result = _filter_extras_by_hatch(self.EXTRAS, hatch)
assert result == []
def test_packages_plus_include(self):
"""include is ignored when packages is set (explicit walk)."""
hatch = {
"targets": {
"sdist": {
"packages": ["pkg"],
"include": ["Cargo.toml", "Cargo.lock", "rust", "src"],
}
}
}
result = _filter_extras_by_hatch(self.EXTRAS, hatch)
# packages acts as only-include; include is bypassed.
assert result == []
def test_include_only(self):
hatch = {"targets": {"sdist": {"include": ["rust", "Cargo.toml"]}}}
result = _filter_extras_by_hatch(self.EXTRAS, hatch)
assert "rust" in result
assert "Cargo.toml" in result
assert "src" not in result
def test_force_include_destinations_kept(self):
hatch = {
"targets": {
"sdist": {
"packages": ["pkg"],
"force-include": {"/abs/Cargo.toml": "Cargo.toml"},
}
}
}
result = _filter_extras_by_hatch(["Cargo.toml", "rust"], hatch)
assert "Cargo.toml" in result
assert "rust" not in result
def test_empty_extras(self):
hatch = {"targets": {"sdist": {"only-include": ["pkg"]}}}
assert _filter_extras_by_hatch([], hatch) == []
def test_global_force_include(self):
hatch = {
"force-include": {"vendored.c": "vendored.c"},
"targets": {"sdist": {"packages": ["pkg"]}},
}
result = _filter_extras_by_hatch(["vendored.c", "rust"], hatch)
assert "vendored.c" in result
assert "rust" not in result
# ── load_config ───────────────────────────────────────────────────────
class TestLoadConfig:
def test_missing_file(self, tmp_path):
cfg = load_config(tmp_path / "nonexistent.toml")
assert cfg["sdist"]["present"] == []
assert cfg["wheel"]["absent"] == []
def test_valid_config(self, tmp_path):
toml = tmp_path / "pyproject.toml"
toml.write_text(
textwrap.dedent("""\
[tool.check-dist.sdist]
present = ["src"]
absent = ["dist"]
[tool.check-dist.wheel]
present = ["mypkg"]
absent = [".github"]
""")
)
cfg = load_config(toml)
assert cfg["sdist"]["present"] == ["src"]
assert cfg["sdist"]["absent"] == ["dist"]
assert cfg["wheel"]["present"] == ["mypkg"]
assert cfg["wheel"]["absent"] == [".github"]
def test_partial_config(self, tmp_path):
toml = tmp_path / "pyproject.toml"
toml.write_text(
textwrap.dedent("""\
[tool.check-dist.sdist]
present = ["src"]
""")
)
cfg = load_config(toml)
assert cfg["sdist"]["present"] == ["src"]
assert cfg["sdist"]["absent"] == []
assert cfg["wheel"]["present"] == []
def test_no_check_dist_section(self, tmp_path):
toml = tmp_path / "pyproject.toml"
toml.write_text("[project]\nname = 'foo'\n")
cfg = load_config(toml)
assert cfg == {
"sdist": {"present": [], "absent": []},
"wheel": {"present": [], "absent": []},
}
class TestLoadHatchConfig:
def test_missing_file(self, tmp_path):
assert load_hatch_config(tmp_path / "nonexistent.toml") == {}
def test_valid_config(self, tmp_path):
toml = tmp_path / "pyproject.toml"
toml.write_text(
textwrap.dedent("""\
[tool.hatch.build.targets.sdist]
packages = ["mylib"]
[tool.hatch.build.targets.wheel]
packages = ["mylib"]
""")
)
cfg = load_hatch_config(toml)
assert cfg["targets"]["sdist"]["packages"] == ["mylib"]
# ── Copier defaults ──────────────────────────────────────────────────
class TestModuleNameFromProject:
def test_spaces(self):
assert _module_name_from_project("python template js") == "python_template_js"
def test_hyphens(self):
assert _module_name_from_project("my-project") == "my_project"
def test_mixed(self):
assert _module_name_from_project("python template-rust") == "python_template_rust"
def test_no_change(self):
assert _module_name_from_project("mypkg") == "mypkg"
class TestLoadCopierConfig:
def test_missing_file(self, tmp_path):
assert load_copier_config(tmp_path) == {}
def test_valid_file(self, tmp_path):
(tmp_path / ".copier-answers.yaml").write_text("add_extension: rust\nproject_name: my project\n")
cfg = load_copier_config(tmp_path)
assert cfg["add_extension"] == "rust"
assert cfg["project_name"] == "my project"
class TestCopierDefaults:
def test_cpp(self):
cfg = copier_defaults({"add_extension": "cpp", "project_name": "python template cpp"})
assert cfg is not None
assert "python_template_cpp" in cfg["sdist"]["present"]
assert "cpp" in cfg["sdist"]["present"]
assert ".clang-format" in cfg["sdist"]["absent"]
assert "cpp" in cfg["wheel"]["absent"]
def test_rust(self):
cfg = copier_defaults({"add_extension": "rust", "project_name": "python template rust"})
assert cfg is not None
assert "Cargo.toml" in cfg["sdist"]["present"]
assert "rust" in cfg["sdist"]["present"]
assert "target" in cfg["sdist"]["absent"]
def test_js(self):
cfg = copier_defaults({"add_extension": "js", "project_name": "python template js"})
assert cfg is not None
assert "js" in cfg["sdist"]["present"]
assert "js" in cfg["wheel"]["absent"]
def test_unknown_extension(self):
assert copier_defaults({"add_extension": "unknown", "project_name": "foo"}) is None
def test_no_extension(self):
assert copier_defaults({"project_name": "foo"}) is None
def test_no_project_name(self):
assert copier_defaults({"add_extension": "cpp"}) is None
def test_common_patterns_present(self):
cfg = copier_defaults({"add_extension": "cpp", "project_name": "foo"})
assert "LICENSE" in cfg["sdist"]["present"]
assert "pyproject.toml" in cfg["sdist"]["present"]
assert ".copier-answers.yaml" in cfg["sdist"]["absent"]
assert ".github" in cfg["sdist"]["absent"]
assert ".gitignore" in cfg["wheel"]["absent"]
assert "pyproject.toml" in cfg["wheel"]["absent"]
class TestLoadConfigWithCopierFallback:
def test_no_check_dist_section_uses_copier(self, tmp_path):
"""When pyproject.toml has no [tool.check-dist], copier defaults apply."""
toml = tmp_path / "pyproject.toml"
toml.write_text("[project]\nname = 'foo'\n")
(tmp_path / ".copier-answers.yaml").write_text("add_extension: rust\nproject_name: my project\n")
cfg = load_config(toml, source_dir=tmp_path)
assert "my_project" in cfg["sdist"]["present"]
assert "rust" in cfg["sdist"]["present"]
def test_explicit_config_takes_precedence(self, tmp_path):
"""When [tool.check-dist] exists, copier defaults are ignored."""
toml = tmp_path / "pyproject.toml"
toml.write_text(
textwrap.dedent("""\
[tool.check-dist.sdist]
present = ["custom"]
""")
)
(tmp_path / ".copier-answers.yaml").write_text("add_extension: rust\nproject_name: my project\n")
cfg = load_config(toml, source_dir=tmp_path)
assert cfg["sdist"]["present"] == ["custom"]
assert "rust" not in cfg["sdist"]["present"]
def test_no_copier_file_returns_empty(self, tmp_path):
toml = tmp_path / "pyproject.toml"
toml.write_text("[project]\nname = 'foo'\n")
cfg = load_config(toml, source_dir=tmp_path)
assert cfg["sdist"]["present"] == []
assert cfg["wheel"]["present"] == []
# ── list_sdist_files ──────────────────────────────────────────────────
class TestListSdistFiles:
def test_tar_gz(self, tmp_path):
archive = tmp_path / "pkg-1.0.tar.gz"
with tarfile.open(archive, "w:gz") as tf:
for name, content in [
("pkg-1.0/pyproject.toml", b"[project]\n"),
("pkg-1.0/src/__init__.py", b""),
]:
info = tarfile.TarInfo(name=name)
info.size = len(content)
tf.addfile(info, io.BytesIO(content))
files = list_sdist_files(str(archive))
assert files == ["pyproject.toml", "src/__init__.py"]
def test_zip(self, tmp_path):
archive = tmp_path / "pkg-1.0.zip"
with zipfile.ZipFile(archive, "w") as zf:
zf.writestr("pkg-1.0/pyproject.toml", "[project]\n")
zf.writestr("pkg-1.0/src/__init__.py", "")
files = list_sdist_files(str(archive))
assert files == ["pyproject.toml", "src/__init__.py"]
def test_unknown_format(self, tmp_path):
archive = tmp_path / "pkg-1.0.rpm"
archive.touch()
with pytest.raises(CheckDistError, match="Unknown sdist format"):
list_sdist_files(str(archive))
# ── list_wheel_files ──────────────────────────────────────────────────
class TestListWheelFiles:
def test_wheel(self, tmp_path):
archive = tmp_path / "pkg-1.0-py3-none-any.whl"
with zipfile.ZipFile(archive, "w") as zf:
zf.writestr("pkg/__init__.py", "")
zf.writestr("pkg-1.0.dist-info/METADATA", "")
files = list_wheel_files(str(archive))
assert files == ["pkg-1.0.dist-info/METADATA", "pkg/__init__.py"]
# ── find_dist_files ───────────────────────────────────────────────────
class TestFindDistFiles:
def test_finds_both(self, tmp_path):
(tmp_path / "pkg-1.0.tar.gz").touch()
(tmp_path / "pkg-1.0-py3-none-any.whl").touch()
sdist, wheel = find_dist_files(str(tmp_path))
assert sdist is not None and sdist.endswith(".tar.gz")
assert wheel is not None and wheel.endswith(".whl")
def test_finds_zip_sdist(self, tmp_path):
(tmp_path / "pkg-1.0.zip").touch()
sdist, wheel = find_dist_files(str(tmp_path))
assert sdist is not None and sdist.endswith(".zip")
assert wheel is None
def test_empty_dir(self, tmp_path):
sdist, wheel = find_dist_files(str(tmp_path))
assert sdist is None
assert wheel is None
def test_nonexistent_dir(self, tmp_path):
sdist, wheel = find_dist_files(str(tmp_path / "no-such-dir"))
assert sdist is None
assert wheel is None
class TestFindPreBuilt:
def test_finds_in_dist(self, tmp_path):
dist_dir = tmp_path / "dist"
dist_dir.mkdir()
(dist_dir / "pkg-1.0.tar.gz").touch()
assert _find_pre_built(str(tmp_path)) == str(dist_dir)
def test_finds_in_wheelhouse(self, tmp_path):
wh = tmp_path / "wheelhouse"
wh.mkdir()
(wh / "pkg-1.0-py3-none-any.whl").touch()
assert _find_pre_built(str(tmp_path)) == str(wh)
def test_prefers_dist_over_wheelhouse(self, tmp_path):
dist_dir = tmp_path / "dist"
dist_dir.mkdir()
(dist_dir / "pkg-1.0.tar.gz").touch()
wh = tmp_path / "wheelhouse"
wh.mkdir()
(wh / "pkg-1.0-py3-none-any.whl").touch()
assert _find_pre_built(str(tmp_path)) == str(dist_dir)
def test_none_when_empty(self, tmp_path):
(tmp_path / "dist").mkdir()
assert _find_pre_built(str(tmp_path)) is None
def test_none_when_no_dirs(self, tmp_path):
assert _find_pre_built(str(tmp_path)) is None
# ── get_vcs_files ─────────────────────────────────────────────────────
class TestGetVcsFiles:
def test_in_git_repo(self, tmp_path):
"""Integration test: create a real tiny git repo."""
subprocess.run(["git", "init", str(tmp_path)], capture_output=True, check=True)
subprocess.run(["git", "config", "user.email", "test@test.com"], cwd=str(tmp_path), capture_output=True)
subprocess.run(["git", "config", "user.name", "Test"], cwd=str(tmp_path), capture_output=True)
(tmp_path / "hello.py").write_text("print('hi')\n")
subprocess.run(["git", "add", "hello.py"], cwd=str(tmp_path), capture_output=True, check=True)
subprocess.run(["git", "commit", "-m", "init"], cwd=str(tmp_path), capture_output=True, check=True)
files = get_vcs_files(str(tmp_path))
assert "hello.py" in files
# ── Integration: check_dist ──────────────────────────────────────────
def _make_project(tmp_path: Path, *, extra_files: dict[str, str] | None = None) -> Path:
"""Create a minimal hatch-based project for integration tests."""
proj = tmp_path / "proj"
pkg = proj / "mypkg"
pkg.mkdir(parents=True)
(pkg / "__init__.py").write_text('__version__ = "0.0.1"\n')
pyproject = textwrap.dedent("""\
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "mypkg"
version = "0.0.1"
readme = "README.md"
[tool.hatch.build.targets.sdist]
packages = ["mypkg"]
[tool.hatch.build.targets.wheel]
packages = ["mypkg"]
[tool.check-dist.sdist]
present = ["mypkg", "pyproject.toml"]
absent = [".github"]
[tool.check-dist.wheel]
present = ["mypkg"]
absent = [".github", "pyproject.toml"]
""")
(proj / "pyproject.toml").write_text(pyproject)
(proj / "README.md").write_text("# mypkg\n")
if extra_files:
for relpath, content in extra_files.items():
p = proj / relpath
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(content)
# Set up a git repo so VCS checks work
subprocess.run(["git", "init", str(proj)], capture_output=True, check=True)
subprocess.run(["git", "config", "user.email", "t@t.com"], cwd=str(proj), capture_output=True)
subprocess.run(["git", "config", "user.name", "T"], cwd=str(proj), capture_output=True)
subprocess.run(["git", "add", "."], cwd=str(proj), capture_output=True, check=True)
subprocess.run(["git", "commit", "-m", "init"], cwd=str(proj), capture_output=True, check=True)
return proj
class TestCheckDistIntegration:
@pytest.mark.slow
def test_clean_project_passes(self, tmp_path):
proj = _make_project(tmp_path)
success, messages = check_dist(str(proj), no_isolation=False)
combined = "\n".join(messages)
assert success, combined
@pytest.mark.slow
def test_missing_present_pattern(self, tmp_path):
"""A missing required file should cause failure."""
proj = _make_project(tmp_path)
# Rewrite config to require CHANGELOG.md which doesn't exist
pp = proj / "pyproject.toml"
text = pp.read_text().replace(
'present = ["mypkg", "pyproject.toml"]',
'present = ["mypkg", "pyproject.toml", "CHANGELOG.md"]',
)
pp.write_text(text)
success, messages = check_dist(str(proj), no_isolation=False)
combined = "\n".join(messages)
assert not success, combined