Skip to content

Commit bbe06e2

Browse files
committed
Auto merge of #3066 - Turbo87:errors, r=jtgeibel
tests/krate/publish: Use `assert_eq!()` to check response payload content Using the `json!()` macro makes it quite straight forward to assert against the exact response payload expectation and is ultimately less verbose than checking against `json.errors[0].detail` like we currently do. r? `@jtgeibel`
2 parents e4488b0 + 191e1e5 commit bbe06e2

File tree

4 files changed

+141
-185
lines changed

4 files changed

+141
-185
lines changed

src/controllers/krate/publish.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ use crate::render;
1616
use crate::util::{read_fill, read_le_u32, Maximums};
1717
use crate::views::{EncodableCrateUpload, GoodCrate, PublishWarnings};
1818

19+
pub const MISSING_RIGHTS_ERROR_MESSAGE: &str =
20+
"this crate exists but you don't seem to be an owner. \
21+
If you believe this is a mistake, perhaps you need \
22+
to accept an invitation to be an owner before \
23+
publishing.";
24+
1925
/// Handles the `PUT /crates/new` route.
2026
/// Used by `cargo publish` to publish a new crate or to publish a new version of an
2127
/// existing crate.
@@ -98,12 +104,7 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
98104

99105
let owners = krate.owners(&conn)?;
100106
if user.rights(req.app(), &owners)? < Rights::Publish {
101-
return Err(cargo_err(
102-
"this crate exists but you don't seem to be an owner. \
103-
If you believe this is a mistake, perhaps you need \
104-
to accept an invitation to be an owner before \
105-
publishing.",
106-
));
107+
return Err(cargo_err(MISSING_RIGHTS_ERROR_MESSAGE));
107108
}
108109

109110
if krate.name != *name {
@@ -260,13 +261,30 @@ fn parse_new_headers(req: &mut dyn RequestExt) -> AppResult<EncodableCrateUpload
260261
missing.push("authors");
261262
}
262263
if !missing.is_empty() {
263-
return Err(cargo_err(&format_args!(
264-
"missing or empty metadata fields: {}. Please \
265-
see https://doc.rust-lang.org/cargo/reference/manifest.html for \
266-
how to upload metadata",
267-
missing.join(", ")
268-
)));
264+
let message = missing_metadata_error_message(&missing);
265+
return Err(cargo_err(&message));
269266
}
270267

271268
Ok(new)
272269
}
270+
271+
pub fn missing_metadata_error_message(missing: &[&str]) -> String {
272+
format!(
273+
"missing or empty metadata fields: {}. Please \
274+
see https://doc.rust-lang.org/cargo/reference/manifest.html for \
275+
how to upload metadata",
276+
missing.join(", ")
277+
)
278+
}
279+
280+
#[cfg(test)]
281+
mod tests {
282+
use super::missing_metadata_error_message;
283+
284+
#[test]
285+
fn missing_metadata_error_message_test() {
286+
assert_eq!(missing_metadata_error_message(&["a"]), "missing or empty metadata fields: a. Please see https://doc.rust-lang.org/cargo/reference/manifest.html for how to upload metadata");
287+
assert_eq!(missing_metadata_error_message(&["a", "b"]), "missing or empty metadata fields: a, b. Please see https://doc.rust-lang.org/cargo/reference/manifest.html for how to upload metadata");
288+
assert_eq!(missing_metadata_error_message(&["a", "b", "c"]), "missing or empty metadata fields: a, b, c. Please see https://doc.rust-lang.org/cargo/reference/manifest.html for how to upload metadata");
289+
}
290+
}

src/models/dependency.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ use crate::models::{Crate, Version};
77
use crate::schema::*;
88
use crate::views::{EncodableCrateDependency, EncodableDependency};
99

10+
pub const WILDCARD_ERROR_MESSAGE: &str = "wildcard (`*`) dependency constraints are not allowed \
11+
on crates.io. See https://doc.rust-lang.org/cargo/faq.html#can-\
12+
libraries-use--as-a-version-for-their-dependencies for more \
13+
information";
14+
1015
#[derive(Identifiable, Associations, Debug, Queryable, QueryableByName)]
1116
#[belongs_to(Version)]
1217
#[belongs_to(Crate)]
@@ -91,12 +96,7 @@ pub fn add_dependencies(
9196
.first(&*conn)
9297
.map_err(|_| cargo_err(&format_args!("no known crate named `{}`", &*dep.name)))?;
9398
if dep.version_req == semver::VersionReq::parse("*").unwrap() {
94-
return Err(cargo_err(
95-
"wildcard (`*`) dependency constraints are not allowed \
96-
on crates.io. See https://doc.rust-lang.org/cargo/faq.html#can-\
97-
libraries-use--as-a-version-for-their-dependencies for more \
98-
information",
99-
));
99+
return Err(cargo_err(WILDCARD_ERROR_MESSAGE));
100100
}
101101

102102
// If this dependency has an explicit name in `Cargo.toml` that

0 commit comments

Comments
 (0)