Skip to content

Commit 4924b95

Browse files
committed
Fix "go to latest version" for /src/ when an item was renamed
1 parent 31dd541 commit 4924b95

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

src/test/fakes.rs

+17-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

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

266274
for (path, data) in files {
275+
if path.starts_with('/') {
276+
failure::bail!("absolute paths not supported");
277+
}
267278
// allow `src/main.rs`
268279
if let Some(parent) = Path::new(path).parent() {
269-
fs::create_dir_all(path_prefix.join(parent))?;
280+
let path = path_prefix.join(parent);
281+
fs::create_dir_all(&path)
282+
.with_context(|_| format!("failed to create {}", path.display()))?;
270283
}
271284
let file = path_prefix.join(&path);
272285
log::debug!("writing file {}", file.display());

src/web/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ mod test {
799799
assert_success("/crate/regex/0.3.0/source/src/main.rs", web)?;
800800
assert_success("/crate/regex/0.3.0/source", web)?;
801801
assert_success("/crate/regex/0.3.0/source/src", web)?;
802-
assert_success("/regex/0.3.0/src/regex/main.rs", web)?;
802+
assert_success("/regex/0.3.0/src/regex/main.rs.html", web)?;
803803
Ok(())
804804
})
805805
}

src/web/rustdoc.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,13 @@ fn path_for_version(
501501
} else {
502502
""
503503
};
504+
let is_source_view = if platform.is_empty() {
505+
// /{name}/{version}/src/{crate}/index.html
506+
req_path.get(3).copied() == Some("src")
507+
} else {
508+
// /{name}/{version}/{platform}/src/{crate}/index.html
509+
req_path.get(4).copied() == Some("src")
510+
};
504511
// this page doesn't exist in the latest version
505512
let last_component = *req_path.last().unwrap();
506513
let search_item = if last_component == "index.html" {
@@ -510,9 +517,12 @@ fn path_for_version(
510517
} else if last_component == platform {
511518
// nothing to search for
512519
None
513-
} else {
520+
} else if !is_source_view {
514521
// this is an item
515522
last_component.split('.').nth(1)
523+
} else {
524+
// this is a source file; try searching for the module
525+
Some(last_component.strip_suffix(".rs.html").unwrap())
516526
};
517527
if let Some(search) = search_item {
518528
format!("{}?search={}", platform, search)
@@ -695,7 +705,7 @@ mod test {
695705
) -> Result<Option<String>, failure::Error> {
696706
assert_success(path, web)?;
697707
let data = web.get(path).send()?.text()?;
698-
log::info!("fetched path {} and got content {}", path, data);
708+
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);
699709
let dom = kuchiki::parse_html().one(data);
700710

701711
if let Some(elem) = dom
@@ -1651,6 +1661,32 @@ mod test {
16511661
});
16521662
}
16531663

1664+
#[test]
1665+
fn latest_version_works_when_source_deleted() {
1666+
wrapper(|env| {
1667+
env.fake_release()
1668+
.name("pyo3")
1669+
.version("0.2.7")
1670+
.source_file("src/objects/exc.rs", b"//! some docs")
1671+
.create()?;
1672+
env.fake_release().name("pyo3").version("0.13.2").create()?;
1673+
let target_redirect = "/crate/pyo3/0.13.2/target-redirect/x86_64-unknown-linux-gnu/src/pyo3/objects/exc.rs.html";
1674+
assert_eq!(
1675+
latest_version_redirect(
1676+
"/pyo3/0.2.7/src/pyo3/objects/exc.rs.html",
1677+
env.frontend()
1678+
)?,
1679+
target_redirect
1680+
);
1681+
assert_redirect(
1682+
target_redirect,
1683+
"/pyo3/0.13.2/pyo3/?search=exc",
1684+
env.frontend(),
1685+
)?;
1686+
Ok(())
1687+
})
1688+
}
1689+
16541690
#[test]
16551691
fn test_version_link_goes_to_docs() {
16561692
wrapper(|env| {

0 commit comments

Comments
 (0)