Skip to content

Commit

Permalink
admin::render_readmes: Support broader implicit readmes
Browse files Browse the repository at this point in the history
  • Loading branch information
nipunn1313 committed Oct 1, 2021
1 parent 38ce91a commit 6198360
Showing 1 changed file with 71 additions and 9 deletions.
80 changes: 71 additions & 9 deletions src/admin/render_readmes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,40 @@ fn render_pkg_readme<R: Read>(mut pkg: Archive<R>, pkg_name: &str) -> Option<Str
.get(&OsString::from(path))
.unwrap_or_else(|| panic!("[{}] couldn't open file: Cargo.toml", pkg_name));
toml::from_str(contents)
.unwrap_or_else(|_| panic!("[{}] Syntax error in manifest file", pkg_name))
.unwrap_or_else(|e| panic!("[{}] Syntax error in manifest file {}", pkg_name, e))
};

let rendered = {
let readme_path = manifest
.package
.readme
.clone()
.unwrap_or_else(|| "README.md".into());
let readme_path = match manifest.package.readme.as_ref() {
Some(toml::Value::String(readme_path)) => readme_path,
Some(toml::Value::Boolean(true)) | None => {
if entries.contains_key(&OsString::from(format!("{}/README.md", pkg_name))) {
"README.md"
} else if entries.contains_key(&OsString::from(format!("{}/README.txt", pkg_name)))
{
"README.txt"
} else if entries.contains_key(&OsString::from(format!("{}/README", pkg_name))) {
"README"
} else {
return None;
}
}
_ => return None,
};

let path = format!("{}/{}", pkg_name, readme_path);
let contents = entries.get(&OsString::from(path))?;
text_to_html(
contents,
&readme_path,
readme_path,
manifest.package.repository.as_deref(),
)
};
return Some(rendered);

#[derive(Debug, Deserialize)]
struct Package {
readme: Option<String>,
readme: Option<toml::Value>,
repository: Option<String>,
}

Expand Down Expand Up @@ -296,17 +308,67 @@ readme = "README.md"
}

#[test]
fn test_render_pkg_implicit_readme() {
fn test_render_pkg_readme_false() {
let mut pkg = tar::Builder::new(vec![]);
add_file(
&mut pkg,
"foo-0.0.1/Cargo.toml",
br#"
[package]
readme = false
"#,
);
add_file(&mut pkg, "foo-0.0.1/README.md", b"readme");
let serialized_archive = pkg.into_inner().unwrap();
assert!(render_pkg_readme(tar::Archive::new(&*serialized_archive), "foo-0.0.1").is_none());
}

#[test]
fn test_render_pkg_implicit_readme_md() {
let mut pkg = tar::Builder::new(vec![]);
add_file(
&mut pkg,
"foo-0.0.1/Cargo.toml",
br#"
[package]
"#,
);
add_file(&mut pkg, "foo-0.0.1/README.md", b"readme");
let serialized_archive = pkg.into_inner().unwrap();
let result =
render_pkg_readme(tar::Archive::new(&*serialized_archive), "foo-0.0.1").unwrap();
assert!(result.contains("readme"))
}

#[test]
fn test_render_pkg_implicit_readme_txt() {
let mut pkg = tar::Builder::new(vec![]);
add_file(
&mut pkg,
"foo-0.0.1/Cargo.toml",
br#"
[package]
"#,
);
add_file(&mut pkg, "foo-0.0.1/README.txt", b"readme");
let serialized_archive = pkg.into_inner().unwrap();
let result =
render_pkg_readme(tar::Archive::new(&*serialized_archive), "foo-0.0.1").unwrap();
assert!(result.contains("readme"))
}

#[test]
fn test_render_pkg_implicit_readme() {
let mut pkg = tar::Builder::new(vec![]);
add_file(
&mut pkg,
"foo-0.0.1/Cargo.toml",
br#"
[package]
"#,
);
add_file(&mut pkg, "foo-0.0.1/README", b"readme");
let serialized_archive = pkg.into_inner().unwrap();
let result =
render_pkg_readme(tar::Archive::new(&*serialized_archive), "foo-0.0.1").unwrap();
assert!(result.contains("readme"))
Expand Down

0 comments on commit 6198360

Please sign in to comment.