Skip to content

Commit

Permalink
Add unit test for invalid authors, refactor name
Browse files Browse the repository at this point in the history
  • Loading branch information
Ky Phan committed Aug 8, 2022
1 parent ee1d688 commit 8f1a75d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,33 +1041,33 @@ impl<T> MaybeWorkspace<T> {
}
}

fn workspace_vec_string<'de, D>(
fn maybe_workspace_vec_string<'de, D>(
deserializer: D,
) -> Result<Option<MaybeWorkspace<Vec<String>>>, D::Error>
where
D: de::Deserializer<'de>,
where
D: de::Deserializer<'de>,
{
struct Visitor;

impl<'de> de::Visitor<'de> for Visitor {
type Value = Option<MaybeWorkspace<Vec<String>>>;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("an array")
formatter.write_str("vector of strings")
}

fn visit_seq<V>(self, v: V) -> Result<Self::Value, V::Error>
where
V: de::SeqAccess<'de>,
where
V: de::SeqAccess<'de>,
{
let seq = de::value::SeqAccessDeserializer::new(v);
let defined = Vec::<String>::deserialize(seq).map(MaybeWorkspace::Defined)?;
Ok(Some(defined))
}

fn visit_map<V>(self, map: V) -> Result<Self::Value, V::Error>
where
V: de::MapAccess<'de>,
where
V: de::MapAccess<'de>,
{
let mvd = de::value::MapAccessDeserializer::new(map);
let workspace = TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace)?;
Expand Down Expand Up @@ -1098,7 +1098,7 @@ pub struct TomlProject {
#[serde(deserialize_with = "version_trim_whitespace")]
version: MaybeWorkspace<semver::Version>,
#[serde(default)]
#[serde(deserialize_with = "workspace_vec_string")]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
authors: Option<MaybeWorkspace<Vec<String>>>,
build: Option<StringOrBool>,
metabuild: Option<StringOrVec>,
Expand All @@ -1108,10 +1108,10 @@ pub struct TomlProject {
forced_target: Option<String>,
links: Option<String>,
#[serde(default)]
#[serde(deserialize_with = "workspace_vec_string")]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
exclude: Option<MaybeWorkspace<Vec<String>>>,
#[serde(default)]
#[serde(deserialize_with = "workspace_vec_string")]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
include: Option<MaybeWorkspace<Vec<String>>>,
publish: Option<MaybeWorkspace<VecStringOrBool>>,
workspace: Option<String>,
Expand All @@ -1128,10 +1128,10 @@ pub struct TomlProject {
documentation: Option<MaybeWorkspace<String>>,
readme: Option<MaybeWorkspace<StringOrBool>>,
#[serde(default)]
#[serde(deserialize_with = "workspace_vec_string")]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
keywords: Option<MaybeWorkspace<Vec<String>>>,
#[serde(default)]
#[serde(deserialize_with = "workspace_vec_string")]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
categories: Option<MaybeWorkspace<Vec<String>>>,
license: Option<MaybeWorkspace<String>>,
license_file: Option<MaybeWorkspace<String>>,
Expand Down
24 changes: 24 additions & 0 deletions tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,30 @@ Caused by:
.run();
}

#[cargo_test]
fn cargo_metadata_with_invalid_authors_field() {
let p = project()
.file("src/foo.rs", "")
.file(
"Cargo.toml",
r#"
[package]
authors = ""
"#,
)
.build();

p.cargo("metadata")
.with_status(101)
.with_stderr(
r#"[ERROR] failed to parse manifest at `[..]`
Caused by:
invalid type: string "", expected vector of strings for key `package.authors`"#,
)
.run();
}

const MANIFEST_OUTPUT: &str = r#"
{
"packages": [{
Expand Down

0 comments on commit 8f1a75d

Please sign in to comment.