Assume README.md if readme=true

This commit is contained in:
Tarun Verghis 2020-06-09 00:03:15 -07:00
parent 77c52c2b4f
commit b0a3cc6b7a
3 changed files with 28 additions and 7 deletions

View File

@ -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<Stri
None => 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()),
},
}

View File

@ -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

View File

@ -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.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.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")
.with_json(&manifest_output(&format!(r#""{}""#, "README.txt")))
.run();
p.cargo("read-manifest").run();
}