Skip to content

Commit 37506b2

Browse files
committed
Expand end-to-end tests
This expands the tests to reflect end-to-end tests by comparing the generated JSON output files with expected strings. * add test helper to compare actual & expected JSON content * refactor setup of packages in test
1 parent 3fdc574 commit 37506b2

File tree

1 file changed

+193
-22
lines changed

1 file changed

+193
-22
lines changed

tests/testsuite/sbom.rs

Lines changed: 193 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
//! Tests for cargo-sbom precursor files.
22
3-
use cargo_test_support::{basic_bin_manifest, project, ProjectBuilder};
3+
use std::path::PathBuf;
4+
5+
use cargo_test_support::basic_bin_manifest;
6+
use cargo_test_support::compare;
7+
use cargo_test_support::project;
8+
use cargo_test_support::registry::Package;
9+
use cargo_test_support::ProjectBuilder;
10+
11+
/// Helper function to compare expected JSON output against actual.
12+
#[track_caller]
13+
fn assert_json_output(actual_json_file: PathBuf, expected_json: &str) {
14+
assert!(actual_json_file.is_file());
15+
16+
let actual_json = std::fs::read_to_string(actual_json_file).expect("Failed to read file");
17+
let actual_json: serde_json::Value =
18+
serde_json::from_str(actual_json.as_str()).expect("Failed to parse JSON");
19+
let actual_json = serde_json::to_string(&actual_json).expect("Failed to convert JSON");
20+
21+
if let Err(error) = compare::match_json(expected_json, &actual_json, None) {
22+
panic!("{}", error.to_string());
23+
}
24+
}
425

526
fn configured_project() -> ProjectBuilder {
627
project().file(
@@ -45,7 +66,51 @@ fn build_sbom_using_cargo_config() {
4566
.run();
4667

4768
let file = p.bin("foo").with_extension("cargo-sbom.json");
48-
assert!(file.is_file());
69+
assert_json_output(
70+
file,
71+
r#"
72+
{
73+
"format_version": 1,
74+
"package_id": "path+file:///[..]/foo#0.5.0",
75+
"name": "foo",
76+
"version": "0.5.0",
77+
"source": "[ROOT]/foo",
78+
"target": {
79+
"kind": [
80+
"bin"
81+
],
82+
"crate_type": "bin",
83+
"name": "foo",
84+
"edition": "2015"
85+
},
86+
"profile": {
87+
"name": "dev",
88+
"opt_level": "0",
89+
"lto": "false",
90+
"codegen_backend": null,
91+
"codegen_units": null,
92+
"debuginfo": 2,
93+
"split_debuginfo": "{...}",
94+
"debug_assertions": true,
95+
"overflow_checks": true,
96+
"rpath": false,
97+
"incremental": false,
98+
"panic": "unwind",
99+
"strip": {
100+
"deferred": "None"
101+
}
102+
},
103+
"packages": [],
104+
"features": [],
105+
"rustc": {
106+
"version": "[..]",
107+
"wrapper": null,
108+
"commit_hash": "[..]",
109+
"host": "[..]"
110+
}
111+
}
112+
"#,
113+
);
49114
}
50115

51116
#[cargo_test]
@@ -89,8 +154,8 @@ fn build_sbom_project_bin_and_lib() {
89154

90155
assert!(p.bin("foo").with_extension("cargo-sbom.json").is_file());
91156
assert_eq!(
92-
1,
93-
p.glob(p.target_debug_dir().join("libfoo.cargo-sbom.json"))
157+
2,
158+
p.glob(p.target_debug_dir().join("*.cargo-sbom.json"))
94159
.count()
95160
);
96161
}
@@ -125,43 +190,149 @@ fn build_sbom_with_simple_build_script() {
125190

126191
#[cargo_test]
127192
fn build_sbom_with_build_dependencies() {
128-
let p = configured_project()
193+
Package::new("baz", "0.1.0").publish();
194+
Package::new("bar", "0.1.0")
195+
.build_dep("baz", "0.1.0")
129196
.file(
130197
"Cargo.toml",
131-
r#"
132-
[package]
133-
name = "foo"
134-
version = "0.0.1"
135-
authors = []
136-
137-
[dependencies]
138-
bar = { path = "./bar" }
139-
"#,
140-
)
141-
.file("src/main.rs", "fn main() { let _i = bar::bar(); }")
142-
.file("bar/src/lib.rs", "pub fn bar() -> i32 { 2 }")
143-
.file(
144-
"bar/Cargo.toml",
145198
r#"
146199
[package]
147200
name = "bar"
148201
version = "0.1.0"
149202
build = "build.rs"
150203
151204
[build-dependencies]
152-
cc = "1.0.46"
205+
baz = "0.1.0"
153206
"#,
154207
)
208+
.file("src/lib.rs", "pub fn bar() -> i32 { 2 }")
155209
.file(
156-
"bar/build.rs",
210+
"build.rs",
157211
r#"fn main() { println!("cargo::rustc-cfg=foo"); }"#,
158212
)
213+
.publish();
214+
215+
let p = configured_project()
216+
.file(
217+
"Cargo.toml",
218+
r#"
219+
[package]
220+
name = "foo"
221+
version = "0.0.1"
222+
authors = []
223+
224+
[dependencies]
225+
bar = "0.1.0"
226+
"#,
227+
)
228+
.file("src/main.rs", "fn main() { let _i = bar::bar(); }")
159229
.build();
160230

161231
p.cargo("build -Zsbom")
162232
.masquerade_as_nightly_cargo(&["sbom"])
163233
.run();
164234

165235
let path = p.bin("foo").with_extension("cargo-sbom.json");
166-
assert!(path.is_file());
236+
assert_json_output(
237+
path,
238+
r#"
239+
{
240+
"format_version": 1,
241+
"package_id": "path+file:///[..]/foo#0.0.1",
242+
"name": "foo",
243+
"version": "0.0.1",
244+
"source": "[ROOT]/foo",
245+
"target": {
246+
"kind": [
247+
"bin"
248+
],
249+
"crate_type": "bin",
250+
"name": "foo",
251+
"edition": "2015"
252+
},
253+
"profile": {
254+
"name": "dev",
255+
"opt_level": "0",
256+
"lto": "false",
257+
"codegen_backend": null,
258+
"codegen_units": null,
259+
"debuginfo": 2,
260+
"split_debuginfo": "{...}",
261+
"debug_assertions": true,
262+
"overflow_checks": true,
263+
"rpath": false,
264+
"incremental": false,
265+
"panic": "unwind",
266+
"strip": {
267+
"deferred": "None"
268+
}
269+
},
270+
"packages": [
271+
{
272+
"package_id": "bar 0.1.0 (registry+[..])",
273+
"package": "bar",
274+
"version": "0.1.0",
275+
"features": [],
276+
"build_type": "normal",
277+
"extern_crate_name": "bar",
278+
"dependencies": [
279+
{
280+
"name": "bar",
281+
"package_id": "bar 0.1.0 (registry+[..])",
282+
"version": "0.1.0",
283+
"features": []
284+
}
285+
]
286+
},
287+
{
288+
"package_id": "bar 0.1.0 (registry+[..])",
289+
"package": "bar",
290+
"version": "0.1.0",
291+
"features": [],
292+
"build_type": "build",
293+
"extern_crate_name": "build_script_build",
294+
"dependencies": [
295+
{
296+
"name": "bar",
297+
"package_id": "bar 0.1.0 (registry+[..])",
298+
"version": "0.1.0",
299+
"features": []
300+
}
301+
]
302+
},
303+
{
304+
"package_id": "bar 0.1.0 (registry+[..])",
305+
"package": "bar",
306+
"version": "0.1.0",
307+
"features": [],
308+
"build_type": "normal",
309+
"extern_crate_name": "build_script_build",
310+
"dependencies": [
311+
{
312+
"name": "baz",
313+
"package_id": "baz 0.1.0 (registry+[..])",
314+
"version": "0.1.0",
315+
"features": []
316+
}
317+
]
318+
},
319+
{
320+
"package_id": "baz 0.1.0 (registry+[..])",
321+
"package": "baz",
322+
"version": "0.1.0",
323+
"features": [],
324+
"build_type": "normal",
325+
"extern_crate_name": "baz",
326+
"dependencies": []
327+
}
328+
],
329+
"features": [],
330+
"rustc": {
331+
"version": "[..]",
332+
"wrapper": null,
333+
"commit_hash": "[..]",
334+
"host": "[..]"
335+
}
336+
}"#,
337+
);
167338
}

0 commit comments

Comments
 (0)