11//! Tests for the `cargo pkgid` command.
22
3+ use std:: path:: PathBuf ;
4+
5+ use cargo_test_support:: basic_bin_manifest;
36use cargo_test_support:: basic_lib_manifest;
47use cargo_test_support:: compare:: assert_e2e;
58use cargo_test_support:: git;
@@ -291,11 +294,12 @@ Please re-run this command with one of the following specifications:
291294// * Package ID specifications
292295// * machine-readable message via `--message-format=json`
293296// * `cargo metadata` output
297+ // * SBOMs
294298#[ cargo_test]
295299fn pkgid_json_message_metadata_consistency ( ) {
296300 let p = project ( )
297- . file ( "Cargo.toml" , & basic_lib_manifest ( "foo" ) )
298- . file ( "src/lib .rs" , "fn unused () {}" )
301+ . file ( "Cargo.toml" , & basic_bin_manifest ( "foo" ) )
302+ . file ( "src/main .rs" , "fn main () {}" )
299303 . file ( "build.rs" , "fn main() {}" )
300304 . build ( ) ;
301305
@@ -321,12 +325,6 @@ fn pkgid_json_message_metadata_consistency() {
321325 "reason": "build-script-executed",
322326 "...": "{...}"
323327 },
324- {
325- "manifest_path": "[ROOT]/foo/Cargo.toml",
326- "package_id": "path+[ROOTURL]/foo#0.5.0",
327- "reason": "compiler-message",
328- "...": "{...}"
329- },
330328 {
331329 "manifest_path": "[ROOT]/foo/Cargo.toml",
332330 "package_id": "path+[ROOTURL]/foo#0.5.0",
@@ -404,4 +402,53 @@ fn pkgid_json_message_metadata_consistency() {
404402 . is_json ( ) ,
405403 )
406404 . run ( ) ;
405+
406+ p. cargo ( "build -Zsbom" )
407+ . env ( "CARGO_BUILD_SBOM" , "true" )
408+ . masquerade_as_nightly_cargo ( & [ "sbom" ] )
409+ . run ( ) ;
410+
411+ let path = {
412+ let mut path = p. bin ( "foo" ) . into_os_string ( ) ;
413+ path. push ( ".cargo-sbom.json" ) ;
414+ PathBuf :: from ( path)
415+ } ;
416+
417+ assert ! ( path. is_file( ) ) ;
418+ let output = std:: fs:: read_to_string ( & path) . unwrap ( ) ;
419+ assert_e2e ( ) . eq (
420+ output,
421+ snapbox:: str![ [ r#"
422+ {
423+ "crates": [
424+ {
425+ "dependencies": [
426+ {
427+ "index": 1,
428+ "kind": "build"
429+ }
430+ ],
431+ "features": [],
432+ "id": "path+[ROOTURL]/foo#0.5.0",
433+ "kind": [
434+ "bin"
435+ ]
436+ },
437+ {
438+ "dependencies": [],
439+ "features": [],
440+ "id": "path+[ROOTURL]/foo#0.5.0",
441+ "kind": [
442+ "custom-build"
443+ ]
444+ }
445+ ],
446+ "root": 0,
447+ "rustc": "{...}",
448+ "target": "[HOST_TARGET]",
449+ "version": 1
450+ }
451+ "# ] ]
452+ . is_json ( ) ,
453+ ) ;
407454}
0 commit comments