Skip to content

Commit

Permalink
Correctly set use_direct_platform_links field value depending if it…
Browse files Browse the repository at this point in the history
… is a crate root or not
  • Loading branch information
GuillaumeGomez committed Aug 9, 2023
1 parent 26cfbea commit 4810adc
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ pub(crate) async fn get_all_platforms(
Extension(pool): Extension<Pool>,
uri: Uri,
) -> AxumResult<AxumResponse> {
let is_crate_root = params.path.as_ref().map(|path| path == "index.html").unwrap_or(true);
let req_path: String = params.path.unwrap_or_default();
let req_path: Vec<&str> = req_path.split('/').collect();

Expand Down Expand Up @@ -618,7 +619,7 @@ pub(crate) async fn get_all_platforms(
doc_targets,
},
inner_path,
use_direct_platform_links: true,
use_direct_platform_links: is_crate_root,
current_target,
};
Ok(res.into_response())
Expand Down Expand Up @@ -1235,12 +1236,38 @@ mod tests {

#[test]
fn platform_links_are_direct_and_without_nofollow() {
fn check_links(response_text: String, should_contain_redirect: bool) {
let platform_links: Vec<(String, String)> = kuchikiki::parse_html()
.one(response_text)
.select(r#"li a"#)
.expect("invalid selector")
.map(|el| {
let attributes = el.attributes.borrow();
let url = attributes.get("href").expect("href").to_string();
let rel = attributes.get("rel").unwrap_or("").to_string();
(url, rel)
})
.collect();

assert_eq!(platform_links.len(), 2);

for (url, rel) in platform_links {
assert_eq!(url.contains("/target-redirect/"), should_contain_redirect);
if !should_contain_redirect {
assert_eq!(rel, "");
} else {
assert_eq!(rel, "nofollow");
}
}
}

wrapper(|env| {
env.fake_release()
.name("dummy")
.version("0.4.0")
.rustdoc_file("dummy/index.html")
.rustdoc_file("x86_64-pc-windows-msvc/dummy/index.html")
.rustdoc_file("x86_64-pc-windows-msvc/dummy/struct.A.html")
.default_target("x86_64-unknown-linux-gnu")
.add_target("x86_64-pc-windows-msvc")
.create()?;
Expand All @@ -1250,25 +1277,14 @@ mod tests {
.get("/-/menus/platforms/dummy/0.4.0/x86_64-pc-windows-msvc")
.send()?;
assert!(response.status().is_success());
check_links(response.text()?, false);

let platform_links: Vec<(String, String)> = kuchikiki::parse_html()
.one(response.text()?)
.select(r#"li a"#)
.expect("invalid selector")
.map(|el| {
let attributes = el.attributes.borrow();
let url = attributes.get("href").expect("href").to_string();
let rel = attributes.get("rel").unwrap_or("").to_string();
(url, rel)
})
.collect();

assert_eq!(platform_links.len(), 2);

for (url, rel) in platform_links {
assert!(!url.contains("/target-redirect/"));
assert_eq!(rel, "");
}
let response = env
.frontend()
.get("/-/menus/platforms/dummy/0.4.0/x86_64-pc-windows-msvc/struct.A.html")
.send()?;
assert!(response.status().is_success());
check_links(response.text()?, true);

Ok(())
});
Expand Down

0 comments on commit 4810adc

Please sign in to comment.