Skip to content

Commit

Permalink
Auto merge of #3970 - nipunn1313:rerender_readme3, r=Turbo87
Browse files Browse the repository at this point in the history
Support admin render_readme with Cargo.toml without optional readme field

Depends on #3969

(Because of community/community#4477 (comment) - github ends up rendering both diffs together. Go to the commits tab and just look at the most recent commit to review).

https://doc.rust-lang.org/cargo/reference/manifest.html#the-readme-field
  • Loading branch information
bors committed Oct 5, 2021
2 parents 20d51c0 + 824055f commit 84495e3
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/admin/render_readmes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,23 @@ fn render_pkg_readme<R: Read>(mut archive: Archive<R>, pkg_name: &str) -> Option

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_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))
};

let rendered = {
let readme_path = manifest.package.readme.as_ref()?;
let readme_path = manifest
.package
.readme
.clone()
.unwrap_or_else(|| "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 @@ -237,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 @@ -248,14 +253,13 @@ 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 +294,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 84495e3

Please sign in to comment.