Skip to content

Commit 8de3343

Browse files
committed
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 #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
2 parents dfdd5ff + 35d5fae commit 8de3343

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

crates/cargo-test-support/src/registry.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ pub struct Dependency {
558558
package: Option<String>,
559559
optional: bool,
560560
default_features: bool,
561+
public: bool,
561562
}
562563

563564
/// Entry with data that corresponds to [`tar::EntryType`].
@@ -1428,6 +1429,7 @@ impl Package {
14281429
"kind": dep.kind,
14291430
"registry": registry_url,
14301431
"package": dep.package,
1432+
"public": dep.public,
14311433
})
14321434
})
14331435
.collect::<Vec<_>>();
@@ -1678,6 +1680,7 @@ impl Dependency {
16781680
optional: false,
16791681
registry: None,
16801682
default_features: true,
1683+
public: false,
16811684
}
16821685
}
16831686

@@ -1731,6 +1734,12 @@ impl Dependency {
17311734
self
17321735
}
17331736

1737+
/// Changes this to an public dependency.
1738+
pub fn public(&mut self, public: bool) -> &mut Self {
1739+
self.public = public;
1740+
self
1741+
}
1742+
17341743
/// Adds `default-features = false` if the argument is `false`.
17351744
pub fn default_features(&mut self, default_features: bool) -> &mut Self {
17361745
self.default_features = default_features;

tests/testsuite/pub_priv.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Tests for public/private dependencies.
22
33
use cargo_test_support::project;
4-
use cargo_test_support::registry::Package;
4+
use cargo_test_support::registry::{Dependency, Package};
55

66
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
77
fn exported_priv_warning() {
@@ -479,3 +479,58 @@ fn allow_priv_in_custom_build() {
479479
)
480480
.run()
481481
}
482+
483+
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
484+
fn publish_package_with_public_dependency() {
485+
Package::new("pub_bar", "0.1.0")
486+
.file("src/lib.rs", "pub struct FromPub;")
487+
.publish();
488+
Package::new("bar", "0.1.0")
489+
.cargo_feature("public-dependency")
490+
.add_dep(Dependency::new("pub_bar", "0.1.0").public(true))
491+
.file(
492+
"src/lib.rs",
493+
"
494+
extern crate pub_bar;
495+
pub use pub_bar::FromPub as BarFromPub;
496+
",
497+
)
498+
.publish();
499+
500+
let p = project()
501+
.file(
502+
"Cargo.toml",
503+
r#"
504+
cargo-features = ["public-dependency"]
505+
[package]
506+
name = "foo"
507+
version = "0.0.1"
508+
[dependencies]
509+
bar = {version = "0.1.0", public = true}
510+
"#,
511+
)
512+
.file(
513+
"src/lib.rs",
514+
"
515+
extern crate bar;
516+
pub fn use_pub(_: bar::BarFromPub) {}
517+
",
518+
)
519+
.build();
520+
521+
p.cargo("check --message-format=short")
522+
.masquerade_as_nightly_cargo(&["public-dependency"])
523+
.with_stderr(
524+
"\
525+
[UPDATING] `[..]` index
526+
[DOWNLOADING] crates ...
527+
[DOWNLOADED] pub_bar v0.1.0 ([..])
528+
[DOWNLOADED] bar v0.1.0 ([..])
529+
[CHECKING] pub_bar v0.1.0
530+
[CHECKING] bar v0.1.0
531+
[CHECKING] foo v0.0.1 ([..])
532+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
533+
",
534+
)
535+
.run()
536+
}

0 commit comments

Comments
 (0)