Skip to content

Fix "go to latest version" for /src/ when an item was renamed #1363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/test/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::index::api::{CrateData, CrateOwner, ReleaseData};
use crate::storage::Storage;
use crate::utils::{Dependency, MetadataPackage, Target};
use chrono::{DateTime, Utc};
use failure::Error;
use failure::{Error, ResultExt};
use postgres::Client;
use std::collections::HashMap;
use std::sync::Arc;
Expand Down Expand Up @@ -251,8 +251,16 @@ impl<'a> FakeRelease<'a> {
// In real life, these would be highlighted HTML, but for testing we just use the files themselves.
for (source_path, data) in &self.source_files {
if let Some(src) = source_path.strip_prefix("src/") {
let updated = ["src", &package.name, src].join("/");
rustdoc_files.push((Box::leak(Box::new(updated)), data));
let mut updated = ["src", &package.name, src].join("/");
updated += ".html";
let source_html = format!(
"<html><head></head><body>{}</body></html>",
std::str::from_utf8(data).expect("invalid utf8")
);
rustdoc_files.push((
Box::leak(Box::new(updated)),
Box::leak(source_html.into_bytes().into_boxed_slice()),
));
Comment on lines +254 to +263
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This took me so long to debug - the HTML rewriter doesn't actually do anything unless at least these tags exist.

}
}

Expand All @@ -264,9 +272,14 @@ impl<'a> FakeRelease<'a> {
fs::create_dir(&path_prefix)?;

for (path, data) in files {
if path.starts_with('/') {
failure::bail!("absolute paths not supported");
}
// allow `src/main.rs`
if let Some(parent) = Path::new(path).parent() {
fs::create_dir_all(path_prefix.join(parent))?;
let path = path_prefix.join(parent);
fs::create_dir_all(&path)
.with_context(|_| format!("failed to create {}", path.display()))?;
}
let file = path_prefix.join(&path);
log::debug!("writing file {}", file.display());
Expand Down
2 changes: 1 addition & 1 deletion src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ mod test {
assert_success("/crate/regex/0.3.0/source/src/main.rs", web)?;
assert_success("/crate/regex/0.3.0/source", web)?;
assert_success("/crate/regex/0.3.0/source/src", web)?;
assert_success("/regex/0.3.0/src/regex/main.rs", web)?;
assert_success("/regex/0.3.0/src/regex/main.rs.html", web)?;
Ok(())
})
}
Expand Down
40 changes: 38 additions & 2 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,13 @@ fn path_for_version(
} else {
""
};
let is_source_view = if platform.is_empty() {
// /{name}/{version}/src/{crate}/index.html
req_path.get(3).copied() == Some("src")
} else {
// /{name}/{version}/{platform}/src/{crate}/index.html
req_path.get(4).copied() == Some("src")
};
// this page doesn't exist in the latest version
let last_component = *req_path.last().unwrap();
let search_item = if last_component == "index.html" {
Expand All @@ -502,9 +509,12 @@ fn path_for_version(
} else if last_component == platform {
// nothing to search for
None
} else {
} else if !is_source_view {
// this is an item
last_component.split('.').nth(1)
} else {
// this is a source file; try searching for the module
Some(last_component.strip_suffix(".rs.html").unwrap())
};
if let Some(search) = search_item {
format!("{}?search={}", platform, search)
Expand Down Expand Up @@ -687,7 +697,7 @@ mod test {
) -> Result<Option<String>, failure::Error> {
assert_success(path, web)?;
let data = web.get(path).send()?.text()?;
log::info!("fetched path {} and got content {}", path, data);
log::info!("fetched path {} and got content {}\nhelp: if this is missing the header, remember to add <html><head></head><body></body></html>", path, data);
let dom = kuchiki::parse_html().one(data);

if let Some(elem) = dom
Expand Down Expand Up @@ -1643,6 +1653,32 @@ mod test {
});
}

#[test]
fn latest_version_works_when_source_deleted() {
wrapper(|env| {
env.fake_release()
.name("pyo3")
.version("0.2.7")
.source_file("src/objects/exc.rs", b"//! some docs")
.create()?;
env.fake_release().name("pyo3").version("0.13.2").create()?;
let target_redirect = "/crate/pyo3/0.13.2/target-redirect/x86_64-unknown-linux-gnu/src/pyo3/objects/exc.rs.html";
assert_eq!(
latest_version_redirect(
"/pyo3/0.2.7/src/pyo3/objects/exc.rs.html",
env.frontend()
)?,
target_redirect
);
assert_redirect(
target_redirect,
"/pyo3/0.13.2/pyo3/?search=exc",
env.frontend(),
)?;
Ok(())
})
}

#[test]
fn test_version_link_goes_to_docs() {
wrapper(|env| {
Expand Down