Skip to content

Commit

Permalink
Support admin render_readme with Cargo.toml without optional readme f…
Browse files Browse the repository at this point in the history
…ield
  • Loading branch information
nipunn1313 committed Oct 1, 2021
1 parent 2c1ba64 commit 89dc057
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions src/admin/render_readmes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,22 @@ fn render_pkg_readme<R: Read>(mut pkg: Archive<R>, pkg_name: &str) -> Option<Str
.unwrap_or_else(|_| panic!("[{}] Invalid tar archive entries", pkg_name));
let manifest: Manifest = {
let path = format!("{}/Cargo.toml", pkg_name);
let contents = find_file_by_path(&mut entries, Path::new(&path), pkg_name);
let contents = find_file_by_path(&mut entries, Path::new(&path), pkg_name).unwrap();
toml::from_str(&contents)
.unwrap_or_else(|_| panic!("[{}] Syntax error in manifest file", pkg_name))
};

let rendered = {
let readme_path = manifest.package.readme.as_ref()?;
let readme_path = manifest
.package
.readme
.clone()
.unwrap_or("README.md".into());
let path = format!("{}/{}", pkg_name, readme_path);
let contents = find_file_by_path(&mut entries, Path::new(&path), pkg_name);
let contents = find_file_by_path(&mut entries, Path::new(&path), pkg_name)?;
text_to_html(
&contents,
readme_path,
&readme_path,
manifest.package.repository.as_deref(),
)
};
Expand All @@ -238,7 +242,7 @@ fn find_file_by_path<R: Read>(
entries: &mut tar::Entries<'_, R>,
path: &Path,
pkg_name: &str,
) -> String {
) -> Option<String> {
let mut file = entries
.find(|entry| match *entry {
Err(_) => false,
Expand All @@ -249,13 +253,12 @@ fn find_file_by_path<R: Read>(
};
filepath == path
}
})
.unwrap_or_else(|| panic!("[{}] couldn't open file: {}", pkg_name, path.display()))
})?
.unwrap_or_else(|_| panic!("[{}] file is not present: {}", pkg_name, path.display()));
let mut contents = String::new();
file.read_to_string(&mut contents)
.unwrap_or_else(|_| panic!("[{}] Couldn't read file contents", pkg_name));
contents
Some(contents)
}

#[cfg(test)]
Expand Down Expand Up @@ -290,6 +293,37 @@ readme = "README.md"
assert!(result.contains("readme"))
}

#[test]
fn test_render_pkg_no_readme() {
let mut pkg = tar::Builder::new(vec![]);
add_file(
&mut pkg,
"foo-0.0.1/Cargo.toml",
br#"
[package]
"#,
);
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() {
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_readme_w_link() {
let mut pkg = tar::Builder::new(vec![]);
Expand Down

0 comments on commit 89dc057

Please sign in to comment.