Skip to content

Commit

Permalink
test: Add test for optional suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai authored and syphar committed Feb 14, 2024
1 parent 87aeaa4 commit a522135
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/test/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ impl<'a> FakeRelease<'a> {
self
}

pub(crate) fn add_dependency(mut self, dependency: Dependency) -> Self {
self.package.dependencies.push(dependency);
self
}

pub(crate) fn release_time(mut self, new: DateTime<Utc>) -> Self {
self.registry_release_data.release_time = new;
self
Expand Down
19 changes: 19 additions & 0 deletions src/utils/cargo_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ pub(crate) struct Dependency {
pub(crate) optional: bool,
}

impl Dependency {
#[cfg(test)]
pub fn new(name: String, req: String) -> Dependency {
Dependency {
name,
req,
kind: None,
rename: None,
optional: false,
}
}

#[cfg(test)]
pub fn set_optional(mut self, optional: bool) -> Self {
self.optional = optional;
self
}
}

#[derive(Deserialize, Serialize)]
struct DeserializedMetadata {
packages: Vec<Package>,
Expand Down
37 changes: 36 additions & 1 deletion src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ pub(crate) async fn static_asset_handler(

#[cfg(test)]
mod test {
use crate::{test::*, web::cache::CachePolicy, Config};
use crate::{test::*, utils::Dependency, web::cache::CachePolicy, Config};
use anyhow::Context;
use kuchikiki::traits::TendrilSink;
use reqwest::{blocking::ClientBuilder, redirect, StatusCode};
Expand Down Expand Up @@ -2345,6 +2345,41 @@ mod test {
})
}

#[test]
fn test_dependency_optional_suffix() {
wrapper(|env| {
env.fake_release()
.name("testing")
.version("0.1.0")
.rustdoc_file("testing/index.html")
.add_dependency(
Dependency::new("optional-dep".to_string(), "1.2.3".to_string())
.set_optional(true),
)
.create()?;

let dom = kuchikiki::parse_html().one(
env.frontend()
.get("/testing/0.1.0/testing/")
.send()?
.text()?,
);
assert!(dom
.select(r#"a[href="/optional-dep/1.2.3"] > i[class="dependencies normal"] + i"#)
.expect("shoud have optional dependency")
.any(|el| { el.text_contents().contains("optional") }));
let dom = kuchikiki::parse_html()
.one(env.frontend().get("/crate/testing/0.1.0").send()?.text()?);
assert!(dom
.select(
r#"a[href="/crate/optional-dep/1.2.3"] > i[class="dependencies normal"] + i"#
)
.expect("shoud have optional dependency")
.any(|el| { el.text_contents().contains("optional") }));
Ok(())
})
}

#[test_case(true)]
#[test_case(false)]
fn test_missing_target_redirects_to_search(archive_storage: bool) {
Expand Down

0 comments on commit a522135

Please sign in to comment.