diff --git a/vergen/src/emitter.rs b/vergen/src/emitter.rs index da29328d..1eb9556c 100644 --- a/vergen/src/emitter.rs +++ b/vergen/src/emitter.rs @@ -988,4 +988,27 @@ pub(crate) mod test { teardown(); Ok(()) } + + #[cfg(all( + feature = "build", + feature = "rustc", + all( + feature = "git", + any(feature = "gitcl", feature = "git2", feature = "gix") + ), + feature = "cargo", + feature = "si" + ))] + #[test] + #[serial_test::serial] + fn all_features_no_output() -> Result<()> { + use crate::utils::testutils::{setup, teardown}; + + setup(); + let mut stdout_buf = vec![]; + _ = EmitBuilder::builder().emit_to(&mut stdout_buf)?; + assert!(stdout_buf.is_empty()); + teardown(); + Ok(()) + } } diff --git a/vergen/src/feature/build.rs b/vergen/src/feature/build.rs index ac11b7bd..9d7f274b 100644 --- a/vergen/src/feature/build.rs +++ b/vergen/src/feature/build.rs @@ -17,6 +17,12 @@ pub(crate) struct Config { pub(crate) build_timestamp: bool, } +impl Config { + pub(crate) fn any(self) -> bool { + self.build_date || self.build_timestamp + } +} + /// The `VERGEN_BUILD_*` configuration features /// /// | Variable | Sample | @@ -175,7 +181,9 @@ impl EmitBuilder { map: &mut RustcEnvMap, warnings: &mut Vec, ) -> Result<()> { - self.add_timestamp_entries(idempotent, map, warnings)?; + if self.build_config.any() { + self.add_timestamp_entries(idempotent, map, warnings)?; + } Ok(()) } diff --git a/vergen/src/feature/cargo.rs b/vergen/src/feature/cargo.rs index d447062d..d16f1b37 100644 --- a/vergen/src/feature/cargo.rs +++ b/vergen/src/feature/cargo.rs @@ -24,6 +24,12 @@ pub(crate) struct Config { pub(crate) cargo_target_triple: bool, } +impl Config { + pub(crate) fn any(self) -> bool { + self.cargo_debug || self.cargo_features || self.cargo_opt_level || self.cargo_target_triple + } +} + /// Copnfigure the emission of `VERGEN_CARGO_*` instructions /// /// **NOTE** - All cargo instructions are considered deterministic. If you change @@ -176,40 +182,41 @@ impl EmitBuilder { } pub(crate) fn add_cargo_map_entries(&self, map: &mut RustcEnvMap) -> Result<()> { - if self.cargo_config.cargo_debug { - if let Ok(value) = env::var(CARGO_DEBUG) { - add_map_entry(VergenKey::CargoDebug, value, map); - } else { - add_map_entry(VergenKey::CargoDebug, env::var("DEBUG")?, map); + if self.cargo_config.any() { + if self.cargo_config.cargo_debug { + if let Ok(value) = env::var(CARGO_DEBUG) { + add_map_entry(VergenKey::CargoDebug, value, map); + } else { + add_map_entry(VergenKey::CargoDebug, env::var("DEBUG")?, map); + } } - } - if self.cargo_config.cargo_features { - if let Ok(value) = env::var(CARGO_FEATURES) { - add_map_entry(VergenKey::CargoFeatures, value, map); - } else { - let features: Vec = env::vars().filter_map(is_cargo_feature).collect(); - let feature_str = features.as_slice().join(","); - add_map_entry(VergenKey::CargoFeatures, feature_str, map); + if self.cargo_config.cargo_features { + if let Ok(value) = env::var(CARGO_FEATURES) { + add_map_entry(VergenKey::CargoFeatures, value, map); + } else { + let features: Vec = env::vars().filter_map(is_cargo_feature).collect(); + let feature_str = features.as_slice().join(","); + add_map_entry(VergenKey::CargoFeatures, feature_str, map); + } } - } - if self.cargo_config.cargo_opt_level { - if let Ok(value) = env::var(CARGO_OPT_LEVEL) { - add_map_entry(VergenKey::CargoOptLevel, value, map); - } else { - add_map_entry(VergenKey::CargoOptLevel, env::var("OPT_LEVEL")?, map); + if self.cargo_config.cargo_opt_level { + if let Ok(value) = env::var(CARGO_OPT_LEVEL) { + add_map_entry(VergenKey::CargoOptLevel, value, map); + } else { + add_map_entry(VergenKey::CargoOptLevel, env::var("OPT_LEVEL")?, map); + } } - } - if self.cargo_config.cargo_target_triple { - if let Ok(value) = env::var(CARGO_TARGET_TRIPLE) { - add_map_entry(VergenKey::CargoTargetTriple, value, map); - } else { - add_map_entry(VergenKey::CargoTargetTriple, env::var("TARGET")?, map); + if self.cargo_config.cargo_target_triple { + if let Ok(value) = env::var(CARGO_TARGET_TRIPLE) { + add_map_entry(VergenKey::CargoTargetTriple, value, map); + } else { + add_map_entry(VergenKey::CargoTargetTriple, env::var("TARGET")?, map); + } } } - Ok(()) } } diff --git a/vergen/src/feature/git/cmd.rs b/vergen/src/feature/git/cmd.rs index 810c5d96..a76c3a44 100644 --- a/vergen/src/feature/git/cmd.rs +++ b/vergen/src/feature/git/cmd.rs @@ -497,13 +497,16 @@ impl EmitBuilder { warnings: &mut Vec, rerun_if_changed: &mut Vec, ) -> Result<()> { - let git_cmd = if let Some(cmd) = self.git_config.git_cmd { - cmd - } else { - "git --version" - }; - check_git(git_cmd).and_then(check_inside_git_worktree)?; - self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed) + if self.any() { + let git_cmd = if let Some(cmd) = self.git_config.git_cmd { + cmd + } else { + "git --version" + }; + check_git(git_cmd).and_then(check_inside_git_worktree)?; + self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed)?; + } + Ok(()) } #[allow(clippy::needless_pass_by_value)] diff --git a/vergen/src/feature/git/git2.rs b/vergen/src/feature/git/git2.rs index ace76111..d96fedbc 100644 --- a/vergen/src/feature/git/git2.rs +++ b/vergen/src/feature/git/git2.rs @@ -300,7 +300,10 @@ impl EmitBuilder { warnings: &mut Vec, rerun_if_changed: &mut Vec, ) -> Result<()> { - self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed) + if self.any() { + self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed)?; + } + Ok(()) } #[cfg(test)] @@ -312,11 +315,13 @@ impl EmitBuilder { warnings: &mut Vec, rerun_if_changed: &mut Vec, ) -> Result<()> { - if self.git_config.fail { - Err(anyhow!("failed to create entries")) - } else { - self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed) + if self.any() { + if self.git_config.fail { + return Err(anyhow!("failed to create entries")); + } + self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed)?; } + Ok(()) } fn inner_add_git_map_entries( diff --git a/vergen/src/feature/git/gix.rs b/vergen/src/feature/git/gix.rs index 117fce16..c546042f 100644 --- a/vergen/src/feature/git/gix.rs +++ b/vergen/src/feature/git/gix.rs @@ -298,7 +298,10 @@ impl EmitBuilder { warnings: &mut Vec, rerun_if_changed: &mut Vec, ) -> Result<()> { - self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed) + if self.any() { + self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed)?; + } + Ok(()) } #[cfg(test)] @@ -310,11 +313,13 @@ impl EmitBuilder { warnings: &mut Vec, rerun_if_changed: &mut Vec, ) -> Result<()> { - if self.git_config.fail { - Err(anyhow!("failed to create entries")) - } else { - self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed) + if self.any() { + if self.git_config.fail { + return Err(anyhow!("failed to create entries")); + } + self.inner_add_git_map_entries(path, idempotent, map, warnings, rerun_if_changed)?; } + Ok(()) } #[allow(clippy::unnecessary_wraps)] diff --git a/vergen/src/feature/rustc.rs b/vergen/src/feature/rustc.rs index f9691f25..18074020 100644 --- a/vergen/src/feature/rustc.rs +++ b/vergen/src/feature/rustc.rs @@ -24,6 +24,17 @@ pub(crate) struct Config { rustc_str_to_test: Option<&'static str>, } +impl Config { + pub(crate) fn any(self) -> bool { + self.rustc_channel + || self.rustc_commit_date + || self.rustc_commit_hash + || self.rustc_host_triple + || self.rustc_llvm_version + || self.rustc_semver + } +} + /// The `VERGEN_RUSTC_*` configuration features /// /// **NOTE** - All rustc instructions are considered deterministic. If you change @@ -169,7 +180,10 @@ impl EmitBuilder { map: &mut RustcEnvMap, warnings: &mut Vec, ) -> Result<()> { - self.add_rustc_to_map(version_meta(), map, warnings) + if self.rustc_config.any() { + self.add_rustc_to_map(version_meta(), map, warnings)?; + } + Ok(()) } #[cfg(test)] @@ -185,7 +199,10 @@ impl EmitBuilder { } else { version_meta() }; - self.add_rustc_to_map(vm, map, warnings) + if self.rustc_config.any() { + self.add_rustc_to_map(vm, map, warnings)?; + } + Ok(()) } fn add_rustc_to_map( diff --git a/vergen/src/feature/si.rs b/vergen/src/feature/si.rs index b1c3e7ab..2d986956 100644 --- a/vergen/src/feature/si.rs +++ b/vergen/src/feature/si.rs @@ -37,6 +37,20 @@ pub(crate) struct Config { fail_pid: bool, } +impl Config { + pub(crate) fn any(&self) -> bool { + self.si_name + || self.si_os_version + || self.si_user + || self.si_memory + || self.si_cpu_vendor + || self.si_cpu_core_count + || self.si_cpu_name + || self.si_cpu_brand + || self.si_cpu_frequency + } +} + /// The `VERGEN_SYSINFO_*` configuration features /// /// | Variable | Sample | @@ -213,17 +227,19 @@ impl EmitBuilder { map: &mut RustcEnvMap, warnings: &mut Vec, ) { - let system = setup_system(); - - self.add_sysinfo_name(&system, idempotent, map, warnings); - self.add_sysinfo_os_verison(&system, idempotent, map, warnings); - self.add_sysinfo_user(&system, idempotent, map, warnings); - self.add_sysinfo_total_memory(&system, idempotent, map, warnings); - self.add_sysinfo_cpu_vendor(&system, idempotent, map, warnings); - self.add_sysinfo_cpu_core_count(&system, idempotent, map, warnings); - self.add_sysinfo_cpu_name(&system, idempotent, map, warnings); - self.add_sysinfo_cpu_brand(&system, idempotent, map, warnings); - self.add_sysinfo_cpu_frequency(&system, idempotent, map, warnings); + if self.sysinfo_config.any() { + let system = setup_system(); + + self.add_sysinfo_name(&system, idempotent, map, warnings); + self.add_sysinfo_os_verison(&system, idempotent, map, warnings); + self.add_sysinfo_user(&system, idempotent, map, warnings); + self.add_sysinfo_total_memory(&system, idempotent, map, warnings); + self.add_sysinfo_cpu_vendor(&system, idempotent, map, warnings); + self.add_sysinfo_cpu_core_count(&system, idempotent, map, warnings); + self.add_sysinfo_cpu_name(&system, idempotent, map, warnings); + self.add_sysinfo_cpu_brand(&system, idempotent, map, warnings); + self.add_sysinfo_cpu_frequency(&system, idempotent, map, warnings); + } } fn add_sysinfo_name(