Skip to content

Commit

Permalink
Test a feature that enables a dev-dependency's feature
Browse files Browse the repository at this point in the history
Also check when we have a dependency that is both a regular dependency
and a dev-dependency.
  • Loading branch information
shepmaster committed Aug 30, 2024
1 parent bea49e0 commit baa9eb5
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use snafu::prelude::*;
use std::{
collections::BTreeMap,
future::Future,
io,
io, iter,
panic::AssertUnwindSafe,
path::{Path, PathBuf},
process::{self, ExitCode},
Expand Down Expand Up @@ -104,6 +104,8 @@ pub async fn test_conformance<R: Registry + Send + Sync + 'static>(
(Default::default, platform_specific_dependency_unused),
(Default::default, platform_specific_dependency_used),
(Default::default, build_only_dependency),
(Default::default, dev_only_dependency_with_feature),
(Default::default, regular_and_dev_dependency_with_feature),
(authorization_required_setup, authorization_required),
(Default::default, yank),
(Default::default, unyank),
Expand Down Expand Up @@ -799,6 +801,78 @@ async fn build_only_dependency(scratch: &ScratchSpace, registry: &mut impl Regis
Ok(())
}

async fn dev_only_dependency_with_feature(
scratch: &ScratchSpace,
registry: &mut impl Registry,
) -> BoxResult {
let registry_url = registry.registry_url().await;
let reg = CreatedRegistry::new("mine", registry_url);

let library_crate = Crate::new("the-library", "1.0.0")
.add_registry(&reg)
.add_dev_dependency(("not-published", "1.0.0"))
.lib_rs(r#"pub const ID: u8 = 1;"#)
.add_feature("default", ["not-published/target"])
.create_in(scratch)
.await?;
registry.publish_crate(&library_crate).await?;

let usage_crate = Crate::new("the-binary", "0.1.0")
.add_registry(&reg)
.add_dependency(library_crate.in_registry(&reg))
.main_rs(r#"fn main() { assert_eq!(1, the_library::ID); }"#)
.create_in(scratch)
.await?;

usage_crate.run().await?;
assert_downloaded_crates!(scratch, 1);

Ok(())
}

async fn regular_and_dev_dependency_with_feature(
scratch: &ScratchSpace,
registry: &mut impl Registry,
) -> BoxResult {
let registry_url = registry.registry_url().await;
let reg = CreatedRegistry::new("mine", registry_url);

let used_twice = Crate::new("used-twice", "0.0.1")
.add_registry(&reg)
.add_feature("target", iter::empty::<String>())
.lib_rs(
r#"
#[cfg(not(feature = "target"))] pub const ID: u8 = 100;
#[cfg(feature = "target")] pub const ID: u8 = 1;
"#,
)
.create_in(scratch)
.await?;
registry.publish_crate(&used_twice).await?;

let library_crate = Crate::new("the-library", "1.0.0")
.add_registry(&reg)
.add_dependency(used_twice.clone().in_registry(&reg))
.add_dev_dependency(used_twice.in_registry(&reg))
.lib_rs(r#"pub const ID: u8 = used_twice::ID * 2;"#)
.add_feature("default", ["used-twice/target"])
.create_in(scratch)
.await?;
registry.publish_crate(&library_crate).await?;

let usage_crate = Crate::new("the-binary", "0.1.0")
.add_registry(&reg)
.add_dependency(library_crate.in_registry(&reg))
.main_rs(r#"fn main() { assert_eq!(2, the_library::ID); }"#)
.create_in(scratch)
.await?;

usage_crate.run().await?;
assert_downloaded_crates!(scratch, 2);

Ok(())
}

fn authorization_required_setup<B: RegistryBuilder>() -> B {
B::default().enable_basic_auth("admin", "baseball123")
}
Expand Down Expand Up @@ -1366,6 +1440,7 @@ impl Crate {
},
dependencies: Default::default(),
build_dependencies: Default::default(),
dev_dependencies: Default::default(),
features: Default::default(),
target: Default::default(),
},
Expand Down Expand Up @@ -1407,6 +1482,11 @@ impl Crate {
self
}

fn add_dev_dependency(mut self, dependency: impl DependencyDefinition) -> Self {
Self::add_dependency_common(&mut self.cargo_toml.dev_dependencies, dependency);
self
}

pub fn add_target_dependency(
mut self,
target: impl Into<String>,
Expand Down Expand Up @@ -1799,6 +1879,7 @@ mod cargo_toml {
pub package: Package,
pub dependencies: Dependencies,
pub build_dependencies: Dependencies,
pub dev_dependencies: Dependencies,
pub features: BTreeMap<String, Vec<String>>,
pub target: BTreeMap<String, Target>,
}
Expand Down

0 comments on commit baa9eb5

Please sign in to comment.