Skip to content

Commit dce8e0a

Browse files
committed
Fix "go to latest version" for /src/ when an item was renamed
1 parent 5195cdc commit dce8e0a

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/test/fakes.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::index::api::{CrateData, CrateOwner, ReleaseData};
44
use crate::storage::Storage;
55
use crate::utils::{Dependency, MetadataPackage, Target};
66
use chrono::{DateTime, Utc};
7-
use failure::Error;
7+
use failure::{Error, ResultExt};
88
use postgres::Client;
99
use std::collections::HashMap;
1010
use std::sync::Arc;
@@ -251,8 +251,16 @@ impl<'a> FakeRelease<'a> {
251251
// In real life, these would be highlighted HTML, but for testing we just use the files themselves.
252252
for (source_path, data) in &self.source_files {
253253
if let Some(src) = source_path.strip_prefix("src/") {
254-
let updated = ["src", &package.name, src].join("/");
255-
rustdoc_files.push((Box::leak(Box::new(updated)), data));
254+
let mut updated = ["src", &package.name, src].join("/");
255+
updated += ".html";
256+
let source_html = format!(
257+
"<html><head></head><body>{}</body></html>",
258+
std::str::from_utf8(data).expect("invalid utf8")
259+
);
260+
rustdoc_files.push((
261+
Box::leak(Box::new(updated)),
262+
Box::leak(source_html.into_bytes().into_boxed_slice()),
263+
));
256264
}
257265
}
258266

@@ -266,7 +274,9 @@ impl<'a> FakeRelease<'a> {
266274
for (path, data) in files {
267275
// allow `src/main.rs`
268276
if let Some(parent) = Path::new(path).parent() {
269-
fs::create_dir_all(path_prefix.join(parent))?;
277+
let path = path_prefix.join(parent);
278+
fs::create_dir_all(&path)
279+
.with_context(|_| format!("failed to create {}", path.display()))?;
270280
}
271281
let file = path_prefix.join(&path);
272282
log::debug!("writing file {}", file.display());

src/web/rustdoc.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,11 @@ fn path_for_version(
493493
} else {
494494
""
495495
};
496+
let is_source_view = if platform.is_empty() {
497+
req_path.get(3).copied() == Some("src")
498+
} else {
499+
req_path.get(4).copied() == Some("src")
500+
};
496501
// this page doesn't exist in the latest version
497502
let last_component = *req_path.last().unwrap();
498503
let search_item = if last_component == "index.html" {
@@ -502,9 +507,12 @@ fn path_for_version(
502507
} else if last_component == platform {
503508
// nothing to search for
504509
None
505-
} else {
510+
} else if !is_source_view {
506511
// this is an item
507512
last_component.split('.').nth(1)
513+
} else {
514+
// this is a source file; try searching for the module
515+
Some(last_component.strip_suffix(".rs.html").unwrap())
508516
};
509517
if let Some(search) = search_item {
510518
format!("{}?search={}", platform, search)
@@ -687,7 +695,7 @@ mod test {
687695
) -> Result<Option<String>, failure::Error> {
688696
assert_success(path, web)?;
689697
let data = web.get(path).send()?.text()?;
690-
log::info!("fetched path {} and got content {}", path, data);
698+
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);
691699
let dom = kuchiki::parse_html().one(data);
692700

693701
if let Some(elem) = dom
@@ -1643,6 +1651,33 @@ mod test {
16431651
});
16441652
}
16451653

1654+
#[test]
1655+
fn latest_version_works_when_source_deleted() {
1656+
wrapper(|env| {
1657+
env.fake_release()
1658+
.name("pyo3")
1659+
.version("0.2.7")
1660+
.source_file("src/objects/exc.rs", b"//! some docs")
1661+
//.rustdoc_file("pyo3/0.2.7/src/pyo3/objects/exc.rs.html")
1662+
.create()?;
1663+
env.fake_release().name("pyo3").version("0.13.2").create()?;
1664+
let target_redirect = "/crate/pyo3/0.13.2/target-redirect/x86_64-unknown-linux-gnu/src/pyo3/objects/exc.rs.html";
1665+
assert_eq!(
1666+
latest_version_redirect(
1667+
"/pyo3/0.2.7/src/pyo3/objects/exc.rs.html",
1668+
env.frontend()
1669+
)?,
1670+
target_redirect
1671+
);
1672+
assert_redirect(
1673+
target_redirect,
1674+
"/pyo3/0.13.2/pyo3/?search=exc",
1675+
env.frontend(),
1676+
)?;
1677+
Ok(())
1678+
})
1679+
}
1680+
16461681
#[test]
16471682
fn test_version_link_goes_to_docs() {
16481683
wrapper(|env| {

0 commit comments

Comments
 (0)