From 7a5f0369cbf747e28d718e2919602d2612abd890 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Thu, 11 Jun 2020 14:55:33 -0700 Subject: [PATCH] Fix failure with missing readme. --- src/cargo/ops/registry.rs | 11 ++++++--- src/cargo/util/toml/mod.rs | 9 +------ tests/testsuite/publish.rs | 42 +++++++++++++++++++++++++++++++- tests/testsuite/read_manifest.rs | 15 +----------- 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 6e0e4b89ef6..83d09694830 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -233,10 +233,13 @@ fn transmit( ref badges, ref links, } = *manifest.metadata(); - let readme_content = match *readme { - Some(ref readme) => Some(paths::read(&pkg.root().join(readme))?), - None => None, - }; + let readme_content = readme + .as_ref() + .map(|readme| { + paths::read(&pkg.root().join(readme)) + .chain_err(|| format!("failed to read `readme` file for package `{}`", pkg)) + }) + .transpose()?; if let Some(ref file) = *license_file { if !pkg.root().join(file).exists() { bail!("the license file `{}` does not exist", file) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index c6cb49d9981..c88de42142a 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1209,18 +1209,11 @@ impl TomlManifest { project.namespaced_features.unwrap_or(false), )?; - let readme = readme_for_project(package_root, project); - if let Some(ref r) = readme { - if !package_root.join(r).is_file() { - bail!("readme file with name '{}' was not found", r); - } - }; - let metadata = ManifestMetadata { description: project.description.clone(), homepage: project.homepage.clone(), documentation: project.documentation.clone(), - readme, + readme: readme_for_project(package_root, project), authors: project.authors.clone().unwrap_or_default(), license: project.license.clone(), license_file: project.license_file.clone(), diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 1632be5a02f..0cc165e395a 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -3,7 +3,7 @@ use cargo_test_support::git::{self, repo}; use cargo_test_support::paths; use cargo_test_support::registry::{self, registry_path, registry_url, Package}; -use cargo_test_support::{basic_manifest, project, publish}; +use cargo_test_support::{basic_manifest, no_such_file_err_msg, project, publish}; use std::fs; const CLEAN_FOO_JSON: &str = r#" @@ -1341,3 +1341,43 @@ See [..] ) .run(); } + +#[cargo_test] +fn publish_with_missing_readme() { + registry::init(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + authors = [] + license = "MIT" + description = "foo" + homepage = "https://example.com/" + readme = "foo.md" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("publish --no-verify --token sekrit") + .with_status(101) + .with_stderr(&format!( + "\ +[UPDATING] [..] +[PACKAGING] foo v0.1.0 [..] +[UPLOADING] foo v0.1.0 [..] +[ERROR] failed to read `readme` file for package `foo v0.1.0 ([ROOT]/foo)` + +Caused by: + failed to read `[ROOT]/foo/foo.md` + +Caused by: + {} +", + no_such_file_err_msg() + )) + .run(); +} diff --git a/tests/testsuite/read_manifest.rs b/tests/testsuite/read_manifest.rs index 780ef710ccc..e0ef83aebe0 100644 --- a/tests/testsuite/read_manifest.rs +++ b/tests/testsuite/read_manifest.rs @@ -195,19 +195,6 @@ fn cargo_read_manifest_defaults_readme_if_true() { .build(); p.cargo("read-manifest") - .with_json(&manifest_output(&format!(r#""{}""#, "README.md"))) + .with_json(&manifest_output(r#""README.md""#)) .run(); } - -// If a file named README.md does not exist, and `readme = true`, it should panic. -#[cargo_test] -#[should_panic] -fn cargo_read_manifest_panics_if_default_readme_not_found() { - let p = project() - .file("Cargo.toml", &basic_bin_manifest_with_readme("foo", "true")) - .file("README.txt", "Sample project") - .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) - .build(); - - p.cargo("read-manifest").run(); -}