Skip to content

Commit

Permalink
Add option to continue if an Error occurs in one section (#128)
Browse files Browse the repository at this point in the history
* Collect and emit cargo:warnings.

* Optionally continue if Build block failed.

* Optionally continue if Git block failed.

* Optionally continue if Sysinfo block failed.

* Optionally continue if Rustc block failed.

* Add test for Git skip_if_error

  - This test needs a directory that is not a git repository or
    a subdirectory of a git repository.
  - Use a temporary directory, provided by the tempfile crate.
  - The tempfile dependency is only needed for this one test. It is only
    compiled in the test configuration and if the git feature is active.
    Sadly, there is no way to define optional dev-dependencies[0], so
    deactivate the unused_crate_dependencies lint to allow compilation
    of the other features.

  [0]: rust-lang/cargo#1596

* Add docs for skip_if_error.

* Fix typos

* Fix mistake in docs.

If the "git" feature is not active, no git2 Errors will be generated.
  • Loading branch information
puzzlewolf authored Aug 8, 2022
1 parent 160336f commit a80a880
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 120 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ rustversion = "1"
lazy_static = "1"
regex = "1"
serial_test = "0"
tempfile = "3"

6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ pub(crate) struct Config {
cfg_map: BTreeMap<VergenKey, Option<String>>,
head_path: Option<PathBuf>,
ref_path: Option<PathBuf>,
warnings: Vec<String>,
}

impl Default for Config {
Expand All @@ -296,6 +297,7 @@ impl Default for Config {
.collect(),
head_path: Option::default(),
ref_path: Option::default(),
warnings: Vec::new(),
}
}
}
Expand All @@ -319,6 +321,7 @@ mod test {
assert_eq!(*config.timezone(), TimeZone::Utc);
assert_eq!(*config.kind(), TimestampKind::Timestamp);
assert!(config.semver());
assert!(!config.skip_if_error());
}

#[cfg(not(feature = "build"))]
Expand Down Expand Up @@ -353,6 +356,7 @@ mod test {
assert_eq!(*config.semver_kind(), SemverKind::Normal);
assert!(config.sha());
assert_eq!(*config.sha_kind(), ShaKind::Normal);
assert!(!config.skip_if_error());
}

#[cfg(not(feature = "git"))]
Expand All @@ -367,6 +371,7 @@ mod test {
assert!(config.host_triple());
assert!(config.llvm_version());
assert!(config.sha());
assert!(!config.skip_if_error());
}

#[cfg(not(feature = "rustc"))]
Expand All @@ -382,6 +387,7 @@ mod test {
assert!(config.memory());
assert!(config.cpu_vendor());
assert!(config.cpu_core_count());
assert!(!config.skip_if_error());
}

#[cfg(not(feature = "si"))]
Expand Down
30 changes: 28 additions & 2 deletions src/feature/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use {
/// * If the `semver` field is false, the semver instruction will not be generated.
/// * **NOTE** - By default, the date/time related instructions will use [`UTC`](TimeZone::Utc).
/// * **NOTE** - The date/time instruction output is determined by the [`kind`](TimestampKind) field and can be any combination of the three.
/// * **NOTE** - To keep processing other sections if an Error occurs in this one, set
/// [`Build::skip_if_error`](Build::skip_if_error_mut()) to true.
///
/// # Example
///
Expand Down Expand Up @@ -89,6 +91,9 @@ pub struct Build {
kind: TimestampKind,
/// Enable/Disable the `VERGEN_BUILD_SEMVER` instruction.
semver: bool,
/// Enable/Disable skipping [`Build`] if an Error occurs.
/// Use [`option_env!`](std::option_env!) to read the generated environment variables.
skip_if_error: bool,
}

#[cfg(feature = "build")]
Expand All @@ -100,6 +105,7 @@ impl Default for Build {
timezone: TimeZone::Utc,
kind: TimestampKind::Timestamp,
semver: true,
skip_if_error: false,
}
}
}
Expand All @@ -115,7 +121,7 @@ impl Build {
pub(crate) fn configure_build(instructions: &Instructions, config: &mut Config) -> Result<()> {
let build_config = instructions.build();

if build_config.has_enabled() {
let mut add_entries = || {
if *build_config.timestamp() {
match build_config.timezone() {
TimeZone::Utc => {
Expand All @@ -135,8 +141,28 @@ pub(crate) fn configure_build(instructions: &Instructions, config: &mut Config)
env::var("CARGO_PKG_VERSION").ok(),
);
}
Ok(())
};

if build_config.has_enabled() {
if build_config.skip_if_error {
// hide errors, but emit a warning
let result = add_entries();
if result.is_err() {
let warning = format!(
"An Error occurred during processing of {}. \
VERGEN_{}_* may be incomplete.",
"Build", "BUILD"
);
config.warnings_mut().push(warning);
}
Ok(())
} else {
add_entries()
}
} else {
Ok(())
}
Ok(())
}

#[cfg(feature = "build")]
Expand Down
Loading

0 comments on commit a80a880

Please sign in to comment.