Skip to content

Commit

Permalink
Fix panic when invoked on crate missing metadata (#377)
Browse files Browse the repository at this point in the history
Also add a test that ensures the error is reported and a panic doesn't
occur.
  • Loading branch information
gkelly committed Feb 21, 2021
1 parent 0cbe0ef commit dbe5c0b
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions impl/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
metadata::{MetadataFetcher, DEFAULT_CRATE_INDEX_URL, DEFAULT_CRATE_REGISTRY_URL},
util,
};
use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use cargo_metadata::{Metadata, MetadataCommand, Package};
use semver::VersionReq;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -626,14 +626,20 @@ fn parse_raze_settings_any_package(metadata: &Metadata) -> Result<RazeSettings>
}

// There should only be one package with raze
if settings_packages.len() > 1 {
return Err(anyhow!(
let settings_count = settings_packages.len();
if settings_count == 0 {
bail!(
"No raze settings were specified in the Cargo.toml file, see README.md for details on \
expected fields"
);
} else if settings_count > 1 {
bail!(
"Multiple packages contain primary raze settings: {:?}",
settings_packages
.iter()
.map(|pkg| &pkg.name)
.collect::<Vec<&String>>()
));
);
}

// UNWRAP: Safe due to checks above
Expand Down Expand Up @@ -803,6 +809,25 @@ pub mod tests {
}
}

#[test]
fn test_loading_without_package_settings() {
let toml_contents = indoc! { r#"
[package]
name = "test"
version = "0.0.1"
[dependencies]
"# };

let temp_workspace_dir = TempDir::new()
.ok()
.expect("Failed to set up temporary directory");
let cargo_toml_path = temp_workspace_dir.path().join("Cargo.toml");
std::fs::write(&cargo_toml_path, &toml_contents).unwrap();

assert!(load_settings_from_manifest(cargo_toml_path, None).is_err());
}

#[test]
fn test_loading_package_settings() {
let toml_contents = indoc! { r#"
Expand Down

0 comments on commit dbe5c0b

Please sign in to comment.