@@ -3922,3 +3922,71 @@ def test_sbom_file_invalid_item(self, isolation):
39223922 TypeError , match = "SBOM file #1 in field `tool.hatch.build.targets.wheel.sbom-files` must be a string"
39233923 ):
39243924 _ = builder .config .sbom_files
3925+
3926+ def test_sbom_from_build_data (self , hatch , helpers , temp_dir , config_file ):
3927+ config_file .model .template .plugins ["default" ]["tests" ] = False
3928+ config_file .save ()
3929+
3930+ with temp_dir .as_cwd ():
3931+ result = hatch ("new" , "My.App" )
3932+
3933+ assert result .exit_code == 0 , result .output
3934+
3935+ project_path = temp_dir / "my-app"
3936+ (project_path / "sbom1.cyclonedx.json" ).write_text ('{"bomFormat": "CycloneDX"}' )
3937+ (project_path / "sbom2.spdx.json" ).write_text ('{"spdxVersion": "SPDX-2.3"}' )
3938+
3939+ build_script = project_path / DEFAULT_BUILD_SCRIPT
3940+ build_script .write_text (
3941+ helpers .dedent (
3942+ """
3943+ import pathlib
3944+
3945+ from hatchling.builders.hooks.plugin.interface import BuildHookInterface
3946+
3947+ class CustomHook(BuildHookInterface):
3948+ def initialize(self, version, build_data):
3949+ build_data["sbom_files"].append("sbom2.spdx.json")
3950+ """
3951+ )
3952+ )
3953+
3954+ config = {
3955+ "project" : {"name" : "My.App" , "dynamic" : ["version" ]},
3956+ "tool" : {
3957+ "hatch" : {
3958+ "version" : {"path" : "src/my_app/__about__.py" },
3959+ "build" : {
3960+ "targets" : {"wheel" : {"sbom-files" : ["sbom1.cyclonedx.json" ]}},
3961+ "hooks" : {"custom" : {"path" : DEFAULT_BUILD_SCRIPT }},
3962+ },
3963+ }
3964+ },
3965+ }
3966+ builder = WheelBuilder (str (project_path ), config = config )
3967+
3968+ build_path = project_path / "dist"
3969+ build_path .mkdir ()
3970+
3971+ with project_path .as_cwd ():
3972+ artifacts = list (builder .build (directory = str (build_path )))
3973+
3974+ assert len (artifacts ) == 1
3975+
3976+ extraction_directory = temp_dir / "_archive"
3977+ extraction_directory .mkdir ()
3978+
3979+ with zipfile .ZipFile (str (artifacts [0 ]), "r" ) as zip_archive :
3980+ zip_archive .extractall (str (extraction_directory ))
3981+
3982+ metadata_directory = f"{ builder .project_id } .dist-info"
3983+ expected_files = helpers .get_template_files (
3984+ "wheel.standard_default_sbom" ,
3985+ "My.App" ,
3986+ metadata_directory = metadata_directory ,
3987+ sbom_files = [
3988+ ("sbom1.cyclonedx.json" , '{"bomFormat": "CycloneDX"}' ),
3989+ ("sbom2.spdx.json" , '{"spdxVersion": "SPDX-2.3"}' ),
3990+ ],
3991+ )
3992+ helpers .assert_files (extraction_directory , expected_files )
0 commit comments