|
1 | 1 | //! Tests for cargo-sbom precursor files.
|
2 | 2 |
|
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 | +} |
4 | 25 |
|
5 | 26 | fn configured_project() -> ProjectBuilder {
|
6 | 27 | project().file(
|
@@ -45,7 +66,51 @@ fn build_sbom_using_cargo_config() {
|
45 | 66 | .run();
|
46 | 67 |
|
47 | 68 | 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 | + ); |
49 | 114 | }
|
50 | 115 |
|
51 | 116 | #[cargo_test]
|
@@ -89,8 +154,8 @@ fn build_sbom_project_bin_and_lib() {
|
89 | 154 |
|
90 | 155 | assert!(p.bin("foo").with_extension("cargo-sbom.json").is_file());
|
91 | 156 | 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")) |
94 | 159 | .count()
|
95 | 160 | );
|
96 | 161 | }
|
@@ -125,43 +190,149 @@ fn build_sbom_with_simple_build_script() {
|
125 | 190 |
|
126 | 191 | #[cargo_test]
|
127 | 192 | 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") |
129 | 196 | .file(
|
130 | 197 | "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", |
145 | 198 | r#"
|
146 | 199 | [package]
|
147 | 200 | name = "bar"
|
148 | 201 | version = "0.1.0"
|
149 | 202 | build = "build.rs"
|
150 | 203 |
|
151 | 204 | [build-dependencies]
|
152 |
| - cc = "1.0.46" |
| 205 | + baz = "0.1.0" |
153 | 206 | "#,
|
154 | 207 | )
|
| 208 | + .file("src/lib.rs", "pub fn bar() -> i32 { 2 }") |
155 | 209 | .file(
|
156 |
| - "bar/build.rs", |
| 210 | + "build.rs", |
157 | 211 | r#"fn main() { println!("cargo::rustc-cfg=foo"); }"#,
|
158 | 212 | )
|
| 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(); }") |
159 | 229 | .build();
|
160 | 230 |
|
161 | 231 | p.cargo("build -Zsbom")
|
162 | 232 | .masquerade_as_nightly_cargo(&["sbom"])
|
163 | 233 | .run();
|
164 | 234 |
|
165 | 235 | 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 | + ); |
167 | 338 | }
|
0 commit comments