Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: support publish package with a public field. #13245

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ pub struct Dependency {
package: Option<String>,
optional: bool,
default_features: bool,
public: bool,
}

/// Entry with data that corresponds to [`tar::EntryType`].
Expand Down Expand Up @@ -1428,6 +1429,7 @@ impl Package {
"kind": dep.kind,
"registry": registry_url,
"package": dep.package,
"public": dep.public,
})
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -1678,6 +1680,7 @@ impl Dependency {
optional: false,
registry: None,
default_features: true,
public: false,
}
}

Expand Down Expand Up @@ -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;
Expand Down
57 changes: 56 additions & 1 deletion tests/testsuite/pub_priv.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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()
}