Skip to content

Commit 8f1a75d

Browse files
author
Ky Phan
committed
Add unit test for invalid authors, refactor name
1 parent ee1d688 commit 8f1a75d

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,33 +1041,33 @@ impl<T> MaybeWorkspace<T> {
10411041
}
10421042
}
10431043

1044-
fn workspace_vec_string<'de, D>(
1044+
fn maybe_workspace_vec_string<'de, D>(
10451045
deserializer: D,
10461046
) -> Result<Option<MaybeWorkspace<Vec<String>>>, D::Error>
1047-
where
1048-
D: de::Deserializer<'de>,
1047+
where
1048+
D: de::Deserializer<'de>,
10491049
{
10501050
struct Visitor;
10511051

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

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

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

10681068
fn visit_map<V>(self, map: V) -> Result<Self::Value, V::Error>
1069-
where
1070-
V: de::MapAccess<'de>,
1069+
where
1070+
V: de::MapAccess<'de>,
10711071
{
10721072
let mvd = de::value::MapAccessDeserializer::new(map);
10731073
let workspace = TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace)?;
@@ -1098,7 +1098,7 @@ pub struct TomlProject {
10981098
#[serde(deserialize_with = "version_trim_whitespace")]
10991099
version: MaybeWorkspace<semver::Version>,
11001100
#[serde(default)]
1101-
#[serde(deserialize_with = "workspace_vec_string")]
1101+
#[serde(deserialize_with = "maybe_workspace_vec_string")]
11021102
authors: Option<MaybeWorkspace<Vec<String>>>,
11031103
build: Option<StringOrBool>,
11041104
metabuild: Option<StringOrVec>,
@@ -1108,10 +1108,10 @@ pub struct TomlProject {
11081108
forced_target: Option<String>,
11091109
links: Option<String>,
11101110
#[serde(default)]
1111-
#[serde(deserialize_with = "workspace_vec_string")]
1111+
#[serde(deserialize_with = "maybe_workspace_vec_string")]
11121112
exclude: Option<MaybeWorkspace<Vec<String>>>,
11131113
#[serde(default)]
1114-
#[serde(deserialize_with = "workspace_vec_string")]
1114+
#[serde(deserialize_with = "maybe_workspace_vec_string")]
11151115
include: Option<MaybeWorkspace<Vec<String>>>,
11161116
publish: Option<MaybeWorkspace<VecStringOrBool>>,
11171117
workspace: Option<String>,
@@ -1128,10 +1128,10 @@ pub struct TomlProject {
11281128
documentation: Option<MaybeWorkspace<String>>,
11291129
readme: Option<MaybeWorkspace<StringOrBool>>,
11301130
#[serde(default)]
1131-
#[serde(deserialize_with = "workspace_vec_string")]
1131+
#[serde(deserialize_with = "maybe_workspace_vec_string")]
11321132
keywords: Option<MaybeWorkspace<Vec<String>>>,
11331133
#[serde(default)]
1134-
#[serde(deserialize_with = "workspace_vec_string")]
1134+
#[serde(deserialize_with = "maybe_workspace_vec_string")]
11351135
categories: Option<MaybeWorkspace<Vec<String>>>,
11361136
license: Option<MaybeWorkspace<String>>,
11371137
license_file: Option<MaybeWorkspace<String>>,

tests/testsuite/metadata.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,30 @@ Caused by:
17061706
.run();
17071707
}
17081708

1709+
#[cargo_test]
1710+
fn cargo_metadata_with_invalid_authors_field() {
1711+
let p = project()
1712+
.file("src/foo.rs", "")
1713+
.file(
1714+
"Cargo.toml",
1715+
r#"
1716+
[package]
1717+
authors = ""
1718+
"#,
1719+
)
1720+
.build();
1721+
1722+
p.cargo("metadata")
1723+
.with_status(101)
1724+
.with_stderr(
1725+
r#"[ERROR] failed to parse manifest at `[..]`
1726+
1727+
Caused by:
1728+
invalid type: string "", expected vector of strings for key `package.authors`"#,
1729+
)
1730+
.run();
1731+
}
1732+
17091733
const MANIFEST_OUTPUT: &str = r#"
17101734
{
17111735
"packages": [{

0 commit comments

Comments
 (0)