diff --git a/crates/cargo-test-support/src/registry.rs b/crates/cargo-test-support/src/registry.rs index 6f9d558a927..cc51707dbbf 100644 --- a/crates/cargo-test-support/src/registry.rs +++ b/crates/cargo-test-support/src/registry.rs @@ -558,6 +558,7 @@ pub struct Dependency { package: Option, optional: bool, default_features: bool, + public: bool, } /// Entry with data that corresponds to [`tar::EntryType`]. @@ -1428,6 +1429,7 @@ impl Package { "kind": dep.kind, "registry": registry_url, "package": dep.package, + "public": dep.public, }) }) .collect::>(); @@ -1678,6 +1680,7 @@ impl Dependency { optional: false, registry: None, default_features: true, + public: false, } } @@ -1731,6 +1734,12 @@ impl Dependency { self } + /// Changes this to an public dependency. + pub fn public(&mut self, public: bool) -> &mut Self { + self.public = public; + self + } + /// Adds `default-features = false` if the argument is `false`. pub fn default_features(&mut self, default_features: bool) -> &mut Self { self.default_features = default_features; diff --git a/tests/testsuite/pub_priv.rs b/tests/testsuite/pub_priv.rs index b3d87ce4c2f..4195f586313 100644 --- a/tests/testsuite/pub_priv.rs +++ b/tests/testsuite/pub_priv.rs @@ -1,7 +1,7 @@ //! Tests for public/private dependencies. use cargo_test_support::project; -use cargo_test_support::registry::Package; +use cargo_test_support::registry::{Dependency, Package}; #[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")] fn exported_priv_warning() { @@ -479,3 +479,58 @@ fn allow_priv_in_custom_build() { ) .run() } + +#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")] +fn publish_package_with_public_dependency() { + Package::new("pub_bar", "0.1.0") + .file("src/lib.rs", "pub struct FromPub;") + .publish(); + Package::new("bar", "0.1.0") + .cargo_feature("public-dependency") + .add_dep(Dependency::new("pub_bar", "0.1.0").public(true)) + .file( + "src/lib.rs", + " + extern crate pub_bar; + pub use pub_bar::FromPub as BarFromPub; + ", + ) + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["public-dependency"] + [package] + name = "foo" + version = "0.0.1" + [dependencies] + bar = {version = "0.1.0", public = true} + "#, + ) + .file( + "src/lib.rs", + " + extern crate bar; + pub fn use_pub(_: bar::BarFromPub) {} + ", + ) + .build(); + + p.cargo("check --message-format=short") + .masquerade_as_nightly_cargo(&["public-dependency"]) + .with_stderr( + "\ +[UPDATING] `[..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] pub_bar v0.1.0 ([..]) +[DOWNLOADED] bar v0.1.0 ([..]) +[CHECKING] pub_bar v0.1.0 +[CHECKING] bar v0.1.0 +[CHECKING] foo v0.0.1 ([..]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run() +}