Skip to content

Commit 3fdc574

Browse files
committed
Integrate review feedback
* disable `sbom` config when `-Zsbom` is not passed as unstable option * refactor tests * add test
1 parent 366432c commit 3fdc574

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ impl BuildConfig {
105105
}
106106

107107
// If sbom flag is set, it requires the unstable feature
108-
let sbom = match gctx.get_env_os("CARGO_BUILD_SBOM") {
108+
let mut sbom = match gctx.get_env_os("CARGO_BUILD_SBOM") {
109109
Some(sbom) => sbom == "true",
110110
None => cfg.sbom == Some(true),
111111
};
112112

113113
if sbom && !gctx.cli_unstable().sbom {
114-
anyhow::bail!("Cargo build config 'sbom' is unstable; pass `-Zsbom` to enable it");
114+
gctx.shell()
115+
.warn("ignoring 'sbom' config, pass `-Zsbom` to enable it")?;
116+
sbom = false;
115117
}
116118

117119
Ok(BuildConfig {

tests/testsuite/cargo/z_help/stdout.term.svg

Lines changed: 12 additions & 12 deletions
Loading

tests/testsuite/sbom.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
//! Tests for cargo-sbom precursor files.
22
3-
use std::{fs::File, io::BufReader, path::Path};
4-
53
use cargo_test_support::{basic_bin_manifest, project, ProjectBuilder};
64

7-
fn read_json<P: AsRef<Path>>(path: P) -> anyhow::Result<serde_json::Value> {
8-
let file = File::open(path)?;
9-
let reader = BufReader::new(file);
10-
Ok(serde_json::from_reader(reader)?)
11-
}
12-
135
fn configured_project() -> ProjectBuilder {
146
project().file(
157
".cargo/config.toml",
@@ -21,15 +13,29 @@ fn configured_project() -> ProjectBuilder {
2113
}
2214

2315
#[cargo_test]
24-
fn build_sbom_using_cargo_config() {
25-
let p = project()
26-
.file(
27-
".cargo/config.toml",
28-
r#"
29-
[build]
30-
sbom = true
31-
"#,
16+
fn build_sbom_without_passing_unstable_flag() {
17+
let p = configured_project()
18+
.file("Cargo.toml", &basic_bin_manifest("foo"))
19+
.file("src/main.rs", r#"fn main() {}"#)
20+
.build();
21+
22+
p.cargo("build")
23+
.masquerade_as_nightly_cargo(&["sbom"])
24+
.with_stderr(
25+
"\
26+
warning: ignoring 'sbom' config, pass `-Zsbom` to enable it\n\
27+
[COMPILING] foo v0.5.0 ([..])\n\
28+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]\n",
3229
)
30+
.run();
31+
32+
let file = p.bin("foo").with_extension("cargo-sbom.json");
33+
assert!(!file.exists());
34+
}
35+
36+
#[cargo_test]
37+
fn build_sbom_using_cargo_config() {
38+
let p = configured_project()
3339
.file("Cargo.toml", &basic_bin_manifest("foo"))
3440
.file("src/main.rs", r#"fn main() {}"#)
3541
.build();
@@ -115,9 +121,6 @@ fn build_sbom_with_simple_build_script() {
115121

116122
let path = p.bin("foo").with_extension("cargo-sbom.json");
117123
assert!(path.is_file());
118-
119-
let _json = read_json(path).expect("Failed to read JSON");
120-
// TODO: check SBOM output
121124
}
122125

123126
#[cargo_test]
@@ -158,7 +161,7 @@ fn build_sbom_with_build_dependencies() {
158161
p.cargo("build -Zsbom")
159162
.masquerade_as_nightly_cargo(&["sbom"])
160163
.run();
164+
161165
let path = p.bin("foo").with_extension("cargo-sbom.json");
162-
let _json = read_json(path).expect("Failed to read JSON");
163-
// TODO: check SBOM output
166+
assert!(path.is_file());
164167
}

0 commit comments

Comments
 (0)