From b23390b27f32c8935e1dc796a70a6e49a92fbc56 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Sat, 16 May 2020 23:24:47 -0700 Subject: [PATCH 01/15] Set default for readme in Manifest if it's not specified. If the readme field is not specified, assume a default of "README.md" (temporarily). Further, if the readme field is set to "false", then this defaulting behavior is suppressed. --- src/cargo/util/toml/mod.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 246bd37f145..910fdd9c3e8 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1198,11 +1198,20 @@ impl TomlManifest { project.links.as_deref(), project.namespaced_features.unwrap_or(false), )?; + + let readme = match &project.readme { + None => Some(String::from("README.md")), + Some(value) => match value.as_str() { + "false" => None, + _ => Some(value.clone()) + } + }; + let metadata = ManifestMetadata { description: project.description.clone(), homepage: project.homepage.clone(), documentation: project.documentation.clone(), - readme: project.readme.clone(), + readme, authors: project.authors.clone().unwrap_or_default(), license: project.license.clone(), license_file: project.license_file.clone(), From 3f46046efca79b48542d429f53ca4f42684c3270 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Sun, 17 May 2020 00:30:10 -0700 Subject: [PATCH 02/15] Use one of the default README file names if they exist --- src/cargo/util/toml/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 910fdd9c3e8..5dc6be49f5c 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1200,7 +1200,7 @@ impl TomlManifest { )?; let readme = match &project.readme { - None => Some(String::from("README.md")), + None => default_readme_from_package_root(package_root), Some(value) => match value.as_str() { "false" => None, _ => Some(value.clone()) @@ -1522,6 +1522,19 @@ impl TomlManifest { } } +const DEFAULT_README_FILES: [&str; 3] = ["README.md", "README.txt", "README"]; + +/// Checks if a file with any of the default README file names exists in the package root. +/// If so, returns a String representing that name. +fn default_readme_from_package_root(package_root: &Path) -> Option { + DEFAULT_README_FILES + .iter() + .map(|&fname| package_root.join(Path::new(fname))) + .filter(|path| path.is_file()) + .flat_map(|path| path.file_name().map(|fname| fname.to_string_lossy().into_owned())) + .nth(0) +} + /// Checks a list of build targets, and ensures the target names are unique within a vector. /// If not, the name of the offending build target is returned. fn unique_build_targets(targets: &[Target], package_root: &Path) -> Result<(), String> { From f42863c458f7c26c8b05bfd7a1d013b6811a812e Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Sun, 17 May 2020 00:59:15 -0700 Subject: [PATCH 03/15] Use next() instead of nth(0) --- src/cargo/util/toml/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 5dc6be49f5c..523bc46d689 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1532,7 +1532,7 @@ fn default_readme_from_package_root(package_root: &Path) -> Option { .map(|&fname| package_root.join(Path::new(fname))) .filter(|path| path.is_file()) .flat_map(|path| path.file_name().map(|fname| fname.to_string_lossy().into_owned())) - .nth(0) + .next() } /// Checks a list of build targets, and ensures the target names are unique within a vector. From ffe5fbbfb726b968d2ea9473b85c1753e9647be0 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Sun, 17 May 2020 13:25:34 -0700 Subject: [PATCH 04/15] Refactor --- src/cargo/util/toml/mod.rs | 44 ++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 523bc46d689..dc713d392d3 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1199,19 +1199,11 @@ impl TomlManifest { project.namespaced_features.unwrap_or(false), )?; - let readme = match &project.readme { - None => default_readme_from_package_root(package_root), - Some(value) => match value.as_str() { - "false" => None, - _ => Some(value.clone()) - } - }; - 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(), @@ -1522,17 +1514,37 @@ impl TomlManifest { } } +/// Returns the name of the README file for a `TomlProject`. +fn readme_for_project(package_root: &Path, project: &Box) -> Option { + match &project.readme { + None => default_readme_from_package_root(package_root), + Some(value) => match value.as_str() { + "false" => None, + _ => Some(value.clone()) + } + } +} + const DEFAULT_README_FILES: [&str; 3] = ["README.md", "README.txt", "README"]; /// Checks if a file with any of the default README file names exists in the package root. -/// If so, returns a String representing that name. +/// If so, returns a `String` representing that name. fn default_readme_from_package_root(package_root: &Path) -> Option { - DEFAULT_README_FILES - .iter() - .map(|&fname| package_root.join(Path::new(fname))) - .filter(|path| path.is_file()) - .flat_map(|path| path.file_name().map(|fname| fname.to_string_lossy().into_owned())) - .next() + _default_readme_from_package_root(package_root).ok() +} + +fn _default_readme_from_package_root(package_root: &Path) -> CargoResult { + for entry in package_root.read_dir()? { + let entry = entry?; + + let fname = entry.file_name(); + + if entry.metadata()?.is_file() && DEFAULT_README_FILES.contains(&fname.to_str().unwrap()) { + return Ok(fname.into_string().map_err(|_| anyhow!("Could not convert the README's file name into a String"))?); + } + } + + Err(anyhow!("No files with the default README file names found in the package root.")) } /// Checks a list of build targets, and ensures the target names are unique within a vector. From 0dfdba6f7a7702a7f45d523b028c896084411de8 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Sun, 17 May 2020 13:26:14 -0700 Subject: [PATCH 05/15] Rustfmt --- src/cargo/util/toml/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index dc713d392d3..d8c331e0cb5 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1520,8 +1520,8 @@ fn readme_for_project(package_root: &Path, project: &Box) -> Option None => default_readme_from_package_root(package_root), Some(value) => match value.as_str() { "false" => None, - _ => Some(value.clone()) - } + _ => Some(value.clone()), + }, } } @@ -1540,11 +1540,15 @@ fn _default_readme_from_package_root(package_root: &Path) -> CargoResult let fname = entry.file_name(); if entry.metadata()?.is_file() && DEFAULT_README_FILES.contains(&fname.to_str().unwrap()) { - return Ok(fname.into_string().map_err(|_| anyhow!("Could not convert the README's file name into a String"))?); + return Ok(fname + .into_string() + .map_err(|_| anyhow!("Could not convert the README's file name into a String"))?); } } - Err(anyhow!("No files with the default README file names found in the package root.")) + Err(anyhow!( + "No files with the default README file names found in the package root." + )) } /// Checks a list of build targets, and ensures the target names are unique within a vector. From d1fe89e1604028630ab190eb2478c747f67dd5f5 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Mon, 25 May 2020 00:07:16 -0700 Subject: [PATCH 06/15] Add tests --- crates/cargo-test-support/src/lib.rs | 18 +++++++++ tests/testsuite/read_manifest.rs | 57 ++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index c99259fb29e..b3c1ae640f5 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -1622,6 +1622,24 @@ pub fn basic_lib_manifest(name: &str) -> String { ) } +pub fn basic_bin_manifest_with_readme(name: &str, readme_filename: &str) -> String { + format!( + r#" + [package] + + name = "{}" + version = "0.5.0" + authors = ["wycats@example.com"] + readme = "{}" + + [[bin]] + + name = "{}" + "#, + name, readme_filename, name + ) +} + pub fn path2url>(p: P) -> Url { Url::from_file_path(p).ok().unwrap() } diff --git a/tests/testsuite/read_manifest.rs b/tests/testsuite/read_manifest.rs index 7ca0c6f9bd4..85246793644 100644 --- a/tests/testsuite/read_manifest.rs +++ b/tests/testsuite/read_manifest.rs @@ -1,15 +1,17 @@ //! Tests for the `cargo read-manifest` command. -use cargo_test_support::{basic_bin_manifest, main_file, project}; +use cargo_test_support::{basic_bin_manifest, basic_bin_manifest_with_readme, main_file, project}; -static MANIFEST_OUTPUT: &str = r#" -{ +fn manifest_output(readme_value: &str) -> String { + format!( + r#" +{{ "authors": [ "wycats@example.com" ], "categories": [], "name":"foo", - "readme": null, + "readme": {}, "repository": null, "version":"0.5.0", "id":"foo[..]0.5.0[..](path+file://[..]/foo)", @@ -21,19 +23,25 @@ static MANIFEST_OUTPUT: &str = r#" "edition": "2015", "source":null, "dependencies":[], - "targets":[{ + "targets":[{{ "kind":["bin"], "crate_types":["bin"], "doctest": false, "edition": "2015", "name":"foo", "src_path":"[..]/foo/src/foo.rs" - }], - "features":{}, + }}], + "features":{{}}, "manifest_path":"[..]Cargo.toml", "metadata": null, "publish": null -}"#; +}}"#, readme_value + ) +} + +fn manifest_output_no_readme() -> String { + manifest_output("null") +} #[cargo_test] fn cargo_read_manifest_path_to_cargo_toml_relative() { @@ -44,7 +52,7 @@ fn cargo_read_manifest_path_to_cargo_toml_relative() { p.cargo("read-manifest --manifest-path foo/Cargo.toml") .cwd(p.root().parent().unwrap()) - .with_json(MANIFEST_OUTPUT) + .with_json(&manifest_output_no_readme()) .run(); } @@ -58,7 +66,7 @@ fn cargo_read_manifest_path_to_cargo_toml_absolute() { p.cargo("read-manifest --manifest-path") .arg(p.root().join("Cargo.toml")) .cwd(p.root().parent().unwrap()) - .with_json(MANIFEST_OUTPUT) + .with_json(&manifest_output_no_readme()) .run(); } @@ -104,5 +112,32 @@ fn cargo_read_manifest_cwd() { .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) .build(); - p.cargo("read-manifest").with_json(MANIFEST_OUTPUT).run(); + p.cargo("read-manifest").with_json(&manifest_output_no_readme()).run(); +} + +#[cargo_test] +fn cargo_read_manifest_default_readme() { + let readme_filenames = ["README.md", "README.txt", "README"]; + + for readme in readme_filenames.iter() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file(readme, "Sample project") + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("read-manifest").with_json(&manifest_output(&format!(r#""{}""#, readme))).run(); + } +} + +#[cargo_test] +fn cargo_read_manifest_suppress_default_readme() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest_with_readme("foo", "false")) + .file("README.txt", "Sample project") + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("read-manifest").with_json(&manifest_output_no_readme()).run(); } + From dcae5fcf2dc6e040d1cadac14276fd306c058b74 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Mon, 25 May 2020 00:09:12 -0700 Subject: [PATCH 07/15] Rustfmt --- tests/testsuite/read_manifest.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/testsuite/read_manifest.rs b/tests/testsuite/read_manifest.rs index 85246793644..0ba45a031b9 100644 --- a/tests/testsuite/read_manifest.rs +++ b/tests/testsuite/read_manifest.rs @@ -35,7 +35,8 @@ fn manifest_output(readme_value: &str) -> String { "manifest_path":"[..]Cargo.toml", "metadata": null, "publish": null -}}"#, readme_value +}}"#, + readme_value ) } @@ -112,7 +113,9 @@ fn cargo_read_manifest_cwd() { .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) .build(); - p.cargo("read-manifest").with_json(&manifest_output_no_readme()).run(); + p.cargo("read-manifest") + .with_json(&manifest_output_no_readme()) + .run(); } #[cargo_test] @@ -126,18 +129,24 @@ fn cargo_read_manifest_default_readme() { .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) .build(); - p.cargo("read-manifest").with_json(&manifest_output(&format!(r#""{}""#, readme))).run(); + p.cargo("read-manifest") + .with_json(&manifest_output(&format!(r#""{}""#, readme))) + .run(); } } #[cargo_test] fn cargo_read_manifest_suppress_default_readme() { let p = project() - .file("Cargo.toml", &basic_bin_manifest_with_readme("foo", "false")) + .file( + "Cargo.toml", + &basic_bin_manifest_with_readme("foo", "false"), + ) .file("README.txt", "Sample project") .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) .build(); - p.cargo("read-manifest").with_json(&manifest_output_no_readme()).run(); + p.cargo("read-manifest") + .with_json(&manifest_output_no_readme()) + .run(); } - From 54b0432ff8de312ad57ff1505f76fbac1dfe2d36 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Mon, 25 May 2020 00:59:26 -0700 Subject: [PATCH 08/15] Update documentation for the readme field --- src/doc/src/reference/manifest.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/doc/src/reference/manifest.md b/src/doc/src/reference/manifest.md index 25c6d6819c5..7940d9c3afd 100644 --- a/src/doc/src/reference/manifest.md +++ b/src/doc/src/reference/manifest.md @@ -165,6 +165,11 @@ will interpret it as Markdown and render it on the crate's page. readme = "README.md" ``` +If no value is specified for this field, and a file named `README.md`, +`README.txt` or `README` exists in the package root, then the name of that +file will be used. You can suppress this behavior by setting this field to +`"false"`. + #### The `homepage` field The `homepage` field should be a URL to a site that is the home page for your From 37836e69001a2b4f9674d1e51f2b314f8fef52cd Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Thu, 28 May 2020 09:55:33 -0700 Subject: [PATCH 09/15] Simplify default_readme_from_package_root --- src/cargo/util/toml/mod.rs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index d8c331e0cb5..72c6930ec2e 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1515,7 +1515,7 @@ impl TomlManifest { } /// Returns the name of the README file for a `TomlProject`. -fn readme_for_project(package_root: &Path, project: &Box) -> Option { +fn readme_for_project(package_root: &Path, project: &TomlProject) -> Option { match &project.readme { None => default_readme_from_package_root(package_root), Some(value) => match value.as_str() { @@ -1530,25 +1530,13 @@ const DEFAULT_README_FILES: [&str; 3] = ["README.md", "README.txt", "README"]; /// Checks if a file with any of the default README file names exists in the package root. /// If so, returns a `String` representing that name. fn default_readme_from_package_root(package_root: &Path) -> Option { - _default_readme_from_package_root(package_root).ok() -} - -fn _default_readme_from_package_root(package_root: &Path) -> CargoResult { - for entry in package_root.read_dir()? { - let entry = entry?; - - let fname = entry.file_name(); - - if entry.metadata()?.is_file() && DEFAULT_README_FILES.contains(&fname.to_str().unwrap()) { - return Ok(fname - .into_string() - .map_err(|_| anyhow!("Could not convert the README's file name into a String"))?); + for &readme_filename in DEFAULT_README_FILES.iter() { + if package_root.join(readme_filename).is_file() { + return Some(readme_filename.to_string()); } } - Err(anyhow!( - "No files with the default README file names found in the package root." - )) + None } /// Checks a list of build targets, and ensures the target names are unique within a vector. From 623c2d5bad059a00facbfb84d65615e2b5c98de2 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Thu, 28 May 2020 21:56:23 -0700 Subject: [PATCH 10/15] Change readme field in TomlProject to StringOrBool --- src/cargo/util/toml/mod.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 72c6930ec2e..c2003b423c8 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -798,7 +798,7 @@ pub struct TomlProject { description: Option, homepage: Option, documentation: Option, - readme: Option, + readme: Option, keywords: Option>, categories: Option>, license: Option, @@ -1518,9 +1518,10 @@ impl TomlManifest { fn readme_for_project(package_root: &Path, project: &TomlProject) -> Option { match &project.readme { None => default_readme_from_package_root(package_root), - Some(value) => match value.as_str() { - "false" => None, - _ => Some(value.clone()), + Some(value) => match value { + StringOrBool::Bool(false) => None, + StringOrBool::Bool(true) => default_readme_from_package_root(package_root), + StringOrBool::String(v) => Some(v.clone()), }, } } From 05cfcb6b8e5025a63fefb8ebd2a2d802346706af Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Thu, 28 May 2020 22:07:12 -0700 Subject: [PATCH 11/15] Update tests --- crates/cargo-test-support/src/lib.rs | 2 +- tests/testsuite/read_manifest.rs | 30 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index b3c1ae640f5..e252fe55e2b 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -1630,7 +1630,7 @@ pub fn basic_bin_manifest_with_readme(name: &str, readme_filename: &str) -> Stri name = "{}" version = "0.5.0" authors = ["wycats@example.com"] - readme = "{}" + readme = {} [[bin]] diff --git a/tests/testsuite/read_manifest.rs b/tests/testsuite/read_manifest.rs index 0ba45a031b9..08656b37fb7 100644 --- a/tests/testsuite/read_manifest.rs +++ b/tests/testsuite/read_manifest.rs @@ -118,6 +118,22 @@ fn cargo_read_manifest_cwd() { .run(); } +#[cargo_test] +fn cargo_read_manifest_with_specified_readme() { + let p = project() + .file( + "Cargo.toml", + &basic_bin_manifest_with_readme("foo", r#""SomeReadme.txt""#), + ) + .file("SomeReadme.txt", "Sample Project") + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("read-manifest") + .with_json(&manifest_output(&format!(r#""{}""#, "SomeReadme.txt"))) + .run(); +} + #[cargo_test] fn cargo_read_manifest_default_readme() { let readme_filenames = ["README.md", "README.txt", "README"]; @@ -150,3 +166,17 @@ fn cargo_read_manifest_suppress_default_readme() { .with_json(&manifest_output_no_readme()) .run(); } + +// If a file named README.txt exists, and `readme = true`, the value `README.txt` should be defaulted in. +#[cargo_test] +fn cargo_read_manifest_defaults_readme_if_true() { + 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") + .with_json(&manifest_output(&format!(r#""{}""#, "README.txt"))) + .run(); +} From 4d8e1ea923414c8b0b006627bc1299b583fd98a8 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Thu, 28 May 2020 22:11:24 -0700 Subject: [PATCH 12/15] Update Manifest docs --- src/doc/src/reference/manifest.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/src/reference/manifest.md b/src/doc/src/reference/manifest.md index 7940d9c3afd..9a6ac864b0f 100644 --- a/src/doc/src/reference/manifest.md +++ b/src/doc/src/reference/manifest.md @@ -168,7 +168,7 @@ readme = "README.md" If no value is specified for this field, and a file named `README.md`, `README.txt` or `README` exists in the package root, then the name of that file will be used. You can suppress this behavior by setting this field to -`"false"`. +`false`. #### The `homepage` field From 77c52c2b4f8dfd57e3bf9f044408ee53bc512544 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Sat, 30 May 2020 14:14:50 -0700 Subject: [PATCH 13/15] Move constructor fn for manifest with readme to test file --- crates/cargo-test-support/src/lib.rs | 18 ------------------ tests/testsuite/read_manifest.rs | 20 +++++++++++++++++++- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index e252fe55e2b..c99259fb29e 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -1622,24 +1622,6 @@ pub fn basic_lib_manifest(name: &str) -> String { ) } -pub fn basic_bin_manifest_with_readme(name: &str, readme_filename: &str) -> String { - format!( - r#" - [package] - - name = "{}" - version = "0.5.0" - authors = ["wycats@example.com"] - readme = {} - - [[bin]] - - name = "{}" - "#, - name, readme_filename, name - ) -} - pub fn path2url>(p: P) -> Url { Url::from_file_path(p).ok().unwrap() } diff --git a/tests/testsuite/read_manifest.rs b/tests/testsuite/read_manifest.rs index 08656b37fb7..7edd36aa42d 100644 --- a/tests/testsuite/read_manifest.rs +++ b/tests/testsuite/read_manifest.rs @@ -1,6 +1,6 @@ //! Tests for the `cargo read-manifest` command. -use cargo_test_support::{basic_bin_manifest, basic_bin_manifest_with_readme, main_file, project}; +use cargo_test_support::{basic_bin_manifest, main_file, project}; fn manifest_output(readme_value: &str) -> String { format!( @@ -44,6 +44,24 @@ fn manifest_output_no_readme() -> String { manifest_output("null") } +pub fn basic_bin_manifest_with_readme(name: &str, readme_filename: &str) -> String { + format!( + r#" + [package] + + name = "{}" + version = "0.5.0" + authors = ["wycats@example.com"] + readme = {} + + [[bin]] + + name = "{}" + "#, + name, readme_filename, name + ) +} + #[cargo_test] fn cargo_read_manifest_path_to_cargo_toml_relative() { let p = project() From b0a3cc6b7a42a15937cc63d6b26aa4d25af6ce99 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Tue, 9 Jun 2020 00:03:15 -0700 Subject: [PATCH 14/15] Assume README.md if readme=true --- src/cargo/util/toml/mod.rs | 11 +++++++++-- src/doc/src/reference/manifest.md | 3 ++- tests/testsuite/read_manifest.rs | 19 ++++++++++++++++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index c2003b423c8..d89a1efd717 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1199,11 +1199,18 @@ 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_for_project(package_root, project), + readme, authors: project.authors.clone().unwrap_or_default(), license: project.license.clone(), license_file: project.license_file.clone(), @@ -1520,7 +1527,7 @@ fn readme_for_project(package_root: &Path, project: &TomlProject) -> Option default_readme_from_package_root(package_root), Some(value) => match value { StringOrBool::Bool(false) => None, - StringOrBool::Bool(true) => default_readme_from_package_root(package_root), + StringOrBool::Bool(true) => Some("README.md".to_string()), StringOrBool::String(v) => Some(v.clone()), }, } diff --git a/src/doc/src/reference/manifest.md b/src/doc/src/reference/manifest.md index 9a6ac864b0f..6b7b81bb007 100644 --- a/src/doc/src/reference/manifest.md +++ b/src/doc/src/reference/manifest.md @@ -168,7 +168,8 @@ readme = "README.md" If no value is specified for this field, and a file named `README.md`, `README.txt` or `README` exists in the package root, then the name of that file will be used. You can suppress this behavior by setting this field to -`false`. +`false`. If the field is set to `true`, a default value of `README.md` will +be assumed. #### The `homepage` field diff --git a/tests/testsuite/read_manifest.rs b/tests/testsuite/read_manifest.rs index 7edd36aa42d..780ef710ccc 100644 --- a/tests/testsuite/read_manifest.rs +++ b/tests/testsuite/read_manifest.rs @@ -185,16 +185,29 @@ fn cargo_read_manifest_suppress_default_readme() { .run(); } -// If a file named README.txt exists, and `readme = true`, the value `README.txt` should be defaulted in. +// If a file named README.md exists, and `readme = true`, the value `README.md` should be defaulted in. #[cargo_test] fn cargo_read_manifest_defaults_readme_if_true() { let p = project() .file("Cargo.toml", &basic_bin_manifest_with_readme("foo", "true")) - .file("README.txt", "Sample project") + .file("README.md", "Sample project") .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) .build(); p.cargo("read-manifest") - .with_json(&manifest_output(&format!(r#""{}""#, "README.txt"))) + .with_json(&manifest_output(&format!(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(); +} From 58ee013f177c9a9ec1b51633831761e14cd23010 Mon Sep 17 00:00:00 2001 From: Tarun Verghis Date: Tue, 9 Jun 2020 00:15:28 -0700 Subject: [PATCH 15/15] Create README file so tests pass --- tests/testsuite/metadata.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index 51e401e1d54..97c26635b00 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -1115,6 +1115,7 @@ fn package_metadata() { baz = "quux" "#, ) + .file("README.md", "") .file("src/lib.rs", "") .build(); @@ -1186,6 +1187,7 @@ fn package_publish() { publish = ["my-registry"] "#, ) + .file("README.md", "") .file("src/lib.rs", "") .build();