Auto merge of #13245 - linyihai:publish-public-in-test, r=epage

test: support publish package with a `public` field.

### What does this PR try to resolve?

This PR add a `public` alike method to support add a dependency as public/private,
```
Package::new("foo", "0.1.0")
        .cargo_feature("public-dependency").add_dep(Dependency::new("bar", "0.1.0").public(true))
```

and then get it from registry in test.

This PR was seperated from the https://github.com/rust-lang/cargo/pull/13183.

### How should we test and review this PR?
You can review on per commit.

After running the test case `publish_package_with_public_dependency`,  you can check a "public" field in `./target/tmp/cit/t0/registry/3/b/bar`.

### Additional information
r? epage
This commit is contained in:
bors 2024-01-03 15:04:26 +00:00
commit 8de3343d80
2 changed files with 65 additions and 1 deletions

View file

@ -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`].
@ -1428,6 +1429,7 @@ impl Package {
"kind": dep.kind,
"registry": registry_url,
"package": dep.package,
"public": dep.public,
})
})
.collect::<Vec<_>>();
@ -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;

View file

@ -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()
}