Skip to content

Commit

Permalink
Add UI cases to check SBOM output
Browse files Browse the repository at this point in the history
* check UI cases for sbom output
* use locally published package instead of fetching crate from crates.io
  index
  • Loading branch information
justahero committed May 6, 2024
1 parent 54e7cd6 commit 1cfd71a
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 4 deletions.
2 changes: 2 additions & 0 deletions tests/testsuite/cargo_build/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
mod help;
mod sbom_single_binary;
mod sbom_with_build_dependencies;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
sbom = true
4 changes: 4 additions & 0 deletions tests/testsuite/cargo_build/sbom_single_binary/in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "foo"
version = "0.0.1"
authors = []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
24 changes: 24 additions & 0 deletions tests/testsuite/cargo_build/sbom_single_binary/mod.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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": "[..]"
}
}
2 changes: 2 additions & 0 deletions tests/testsuite/cargo_build/sbom_single_binary/stderr.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[..] foo v0.0.1 ([ROOT]/case)
[..] `dev` profile [unoptimized + debuginfo] target(s) in [..]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
sbom = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "foo"
version = "0.0.1"
authors = []

[dependencies]
bar = { path = "./bar" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "bar"
version = "0.1.0"
build = "build.rs"

[build-dependencies]
baz = { path = "../baz" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo::rustc-cfg=foo");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn bar() -> i32 {
2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "baz"
version = "0.1.0"
authors = []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn hello() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let _i = bar::bar();
}
24 changes: 24 additions & 0 deletions tests/testsuite/cargo_build/sbom_with_build_dependencies/mod.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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": "[..]"
}
}
Original file line number Diff line number Diff line change
@@ -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 [..]
Empty file.
9 changes: 5 additions & 4 deletions tests/testsuite/sbom.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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()
);
}
Expand Down Expand Up @@ -149,14 +149,15 @@ fn build_sbom_with_build_dependencies() {
build = "build.rs"
[build-dependencies]
cc = "1.0.46"
baz = "0.0.1"
"#,
)
.file(
"bar/build.rs",
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"])
Expand Down

0 comments on commit 1cfd71a

Please sign in to comment.