diff --git a/tests/testsuite/cargo_build/mod.rs b/tests/testsuite/cargo_build/mod.rs index c0ce11180711..65a023431f0a 100644 --- a/tests/testsuite/cargo_build/mod.rs +++ b/tests/testsuite/cargo_build/mod.rs @@ -1 +1,3 @@ mod help; +mod sbom_single_binary; +mod sbom_with_build_dependencies; diff --git a/tests/testsuite/cargo_build/sbom_single_binary/in/.cargo/config.toml b/tests/testsuite/cargo_build/sbom_single_binary/in/.cargo/config.toml new file mode 100644 index 000000000000..d782bbe59f69 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_single_binary/in/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +sbom = true diff --git a/tests/testsuite/cargo_build/sbom_single_binary/in/Cargo.toml b/tests/testsuite/cargo_build/sbom_single_binary/in/Cargo.toml new file mode 100644 index 000000000000..52b60fc26fb6 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_single_binary/in/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "foo" +version = "0.0.1" +authors = [] diff --git a/tests/testsuite/cargo_build/sbom_single_binary/in/src/main.rs b/tests/testsuite/cargo_build/sbom_single_binary/in/src/main.rs new file mode 100644 index 000000000000..e71fdf55421d --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_single_binary/in/src/main.rs @@ -0,0 +1 @@ +fn main() {} \ No newline at end of file diff --git a/tests/testsuite/cargo_build/sbom_single_binary/mod.rs b/tests/testsuite/cargo_build/sbom_single_binary/mod.rs new file mode 100644 index 000000000000..3ff9511f02cc --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_single_binary/mod.rs @@ -0,0 +1,24 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::current_dir; +use cargo_test_support::file; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project: Project = Project::from_template(current_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("build") + .masquerade_as_nightly_cargo(&["-Zsbom"]) + .args(&["-Z", "sbom"]) + .current_dir(cwd) + .assert() + .success() + .stdout_matches(file!("stdout.log")) + .stderr_matches(file!("stderr.log")); + + assert_ui().subset_matches(current_dir!().join("out"), &project_root); +} diff --git a/tests/testsuite/cargo_build/sbom_single_binary/out/target/debug/foo.cargo-sbom.json b/tests/testsuite/cargo_build/sbom_single_binary/out/target/debug/foo.cargo-sbom.json new file mode 100644 index 000000000000..4faf6b23f2b0 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_single_binary/out/target/debug/foo.cargo-sbom.json @@ -0,0 +1,40 @@ +{ + "format_version": 1, + "package_id": "path+[ROOTURL]/case#foo@0.0.1", + "name": "foo", + "version": "0.0.1", + "source": "[ROOT]/case", + "target": { + "kind": [ + "bin" + ], + "crate_type": "bin", + "name": "foo", + "edition": "2015" + }, + "profile": { + "name": "dev", + "opt_level": "0", + "lto": "false", + "codegen_backend": null, + "codegen_units": null, + "debuginfo": 2, + "split_debuginfo": "unpacked", + "debug_assertions": true, + "overflow_checks": true, + "rpath": false, + "incremental": false, + "panic": "unwind", + "strip": { + "deferred": "None" + } + }, + "packages": [], + "features": [], + "rustc": { + "version": "[..]", + "wrapper": null, + "commit_hash": "9b00956e56009bab2aa15d7bff10916599e3d6d6", + "host": "[..]" + } +} \ No newline at end of file diff --git a/tests/testsuite/cargo_build/sbom_single_binary/stderr.log b/tests/testsuite/cargo_build/sbom_single_binary/stderr.log new file mode 100644 index 000000000000..00a629b2401b --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_single_binary/stderr.log @@ -0,0 +1,2 @@ +[..] foo v0.0.1 ([ROOT]/case) +[..] `dev` profile [unoptimized + debuginfo] target(s) in [..] diff --git a/tests/testsuite/cargo_build/sbom_single_binary/stdout.log b/tests/testsuite/cargo_build/sbom_single_binary/stdout.log new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/.cargo/config.toml b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/.cargo/config.toml new file mode 100644 index 000000000000..d782bbe59f69 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +sbom = true diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/Cargo.toml b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/Cargo.toml new file mode 100644 index 000000000000..464badc2d26d --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "foo" +version = "0.0.1" +authors = [] + +[dependencies] +bar = { path = "./bar" } diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/bar/Cargo.toml b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/bar/Cargo.toml new file mode 100644 index 000000000000..2a72df17fb49 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/bar/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "bar" +version = "0.1.0" +build = "build.rs" + +[build-dependencies] +baz = { path = "../baz" } diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/bar/build.rs b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/bar/build.rs new file mode 100644 index 000000000000..249d66bfbbe2 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/bar/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo::rustc-cfg=foo"); +} diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/bar/src/lib.rs b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/bar/src/lib.rs new file mode 100644 index 000000000000..d3a43a71ce89 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/bar/src/lib.rs @@ -0,0 +1,3 @@ +pub fn bar() -> i32 { + 2 +} diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/baz/Cargo.toml b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/baz/Cargo.toml new file mode 100644 index 000000000000..a987aa92cc94 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/baz/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "baz" +version = "0.1.0" +authors = [] \ No newline at end of file diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/baz/src/lib.rs b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/baz/src/lib.rs new file mode 100644 index 000000000000..a5535835d614 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/baz/src/lib.rs @@ -0,0 +1 @@ +pub fn hello() {} \ No newline at end of file diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/src/main.rs b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/src/main.rs new file mode 100644 index 000000000000..844dc8167cdd --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/in/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + let _i = bar::bar(); +} diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/mod.rs b/tests/testsuite/cargo_build/sbom_with_build_dependencies/mod.rs new file mode 100644 index 000000000000..3ff9511f02cc --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/mod.rs @@ -0,0 +1,24 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::current_dir; +use cargo_test_support::file; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project: Project = Project::from_template(current_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("build") + .masquerade_as_nightly_cargo(&["-Zsbom"]) + .args(&["-Z", "sbom"]) + .current_dir(cwd) + .assert() + .success() + .stdout_matches(file!("stdout.log")) + .stderr_matches(file!("stderr.log")); + + assert_ui().subset_matches(current_dir!().join("out"), &project_root); +} diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/out/target/debug/foo.cargo-sbom.json b/tests/testsuite/cargo_build/sbom_with_build_dependencies/out/target/debug/foo.cargo-sbom.json new file mode 100644 index 000000000000..3e78c3083ae8 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/out/target/debug/foo.cargo-sbom.json @@ -0,0 +1,98 @@ +{ + "format_version": 1, + "package_id": "path+[ROOTURL]/case#foo@0.0.1", + "name": "foo", + "version": "0.0.1", + "source": "[ROOT]/case", + "target": { + "kind": [ + "bin" + ], + "crate_type": "bin", + "name": "foo", + "edition": "2015" + }, + "profile": { + "name": "dev", + "opt_level": "0", + "lto": "false", + "codegen_backend": null, + "codegen_units": null, + "debuginfo": 2, + "split_debuginfo": "unpacked", + "debug_assertions": true, + "overflow_checks": true, + "rpath": false, + "incremental": false, + "panic": "unwind", + "strip": { + "deferred": "None" + } + }, + "packages": [ + { + "package_id": "bar 0.1.0 (path+[ROOTURL]/case/bar)", + "package": "bar", + "version": "0.1.0", + "features": [], + "build_type": "normal", + "extern_crate_name": "bar", + "dependencies": [ + { + "name": "bar", + "package_id": "bar 0.1.0 (path+[ROOTURL]/case/bar)", + "version": "0.1.0", + "features": [] + } + ] + }, + { + "package_id": "bar 0.1.0 (path+[ROOTURL]/case/bar)", + "package": "bar", + "version": "0.1.0", + "features": [], + "build_type": "build", + "extern_crate_name": "build_script_build", + "dependencies": [ + { + "name": "bar", + "package_id": "bar 0.1.0 (path+[ROOTURL]/case/bar)", + "version": "0.1.0", + "features": [] + } + ] + }, + { + "package_id": "bar 0.1.0 (path+[ROOTURL]/case/bar)", + "package": "bar", + "version": "0.1.0", + "features": [], + "build_type": "normal", + "extern_crate_name": "build_script_build", + "dependencies": [ + { + "name": "baz", + "package_id": "baz 0.1.0 (path+[ROOTURL]/case/baz)", + "version": "0.1.0", + "features": [] + } + ] + }, + { + "package_id": "baz 0.1.0 (path+[ROOTURL]/case/baz)", + "package": "baz", + "version": "0.1.0", + "features": [], + "build_type": "normal", + "extern_crate_name": "baz", + "dependencies": [] + } + ], + "features": [], + "rustc": { + "version": "[..]", + "wrapper": null, + "commit_hash": "9b00956e56009bab2aa15d7bff10916599e3d6d6", + "host": "[..]" + } +} \ No newline at end of file diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/stderr.log b/tests/testsuite/cargo_build/sbom_with_build_dependencies/stderr.log new file mode 100644 index 000000000000..695e19b2df46 --- /dev/null +++ b/tests/testsuite/cargo_build/sbom_with_build_dependencies/stderr.log @@ -0,0 +1,5 @@ +[..] 3 packages to latest compatible versions +[..] baz v0.1.0 ([ROOT]/case/baz) +[..] bar v0.1.0 ([ROOT]/case/bar) +[..] foo v0.0.1 ([ROOT]/case) +[..] `dev` profile [unoptimized + debuginfo] target(s) in [..] diff --git a/tests/testsuite/cargo_build/sbom_with_build_dependencies/stdout.log b/tests/testsuite/cargo_build/sbom_with_build_dependencies/stdout.log new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/testsuite/sbom.rs b/tests/testsuite/sbom.rs index 681879f7d8fa..87bdb76c4b60 100644 --- a/tests/testsuite/sbom.rs +++ b/tests/testsuite/sbom.rs @@ -1,6 +1,6 @@ //! Tests for cargo-sbom precursor files. -use cargo_test_support::{basic_bin_manifest, project, ProjectBuilder}; +use cargo_test_support::{basic_bin_manifest, project, registry::Package, ProjectBuilder}; fn configured_project() -> ProjectBuilder { project().file( @@ -89,8 +89,8 @@ fn build_sbom_project_bin_and_lib() { assert!(p.bin("foo").with_extension("cargo-sbom.json").is_file()); assert_eq!( - 1, - p.glob(p.target_debug_dir().join("libfoo.cargo-sbom.json")) + 2, + p.glob(p.target_debug_dir().join("*.cargo-sbom.json")) .count() ); } @@ -149,7 +149,7 @@ fn build_sbom_with_build_dependencies() { build = "build.rs" [build-dependencies] - cc = "1.0.46" + baz = "0.0.1" "#, ) .file( @@ -157,6 +157,7 @@ fn build_sbom_with_build_dependencies() { r#"fn main() { println!("cargo::rustc-cfg=foo"); }"#, ) .build(); + Package::new("baz", "0.0.1").publish(); p.cargo("build -Zsbom") .masquerade_as_nightly_cargo(&["sbom"])