cargo/tests/testsuite/pub_priv.rs
Aaron Hill 87f1a4b217
Implement the 'frontend' of public-private dependencies
This is part of https://github.com/rust-lang/rust/issues/44663

This implements the 'frontend' portion of RFC 1977. Once PRs
https://github.com/rust-lang/rust/pull/59335 and
https://github.com/rust-lang/crates.io/pull/1685 are merged,
it will be possible to test the full public-private dependency feature:
marking a dependency a public, seeing exported_private_dependencies
warnings from rustc, and seeing pub-dep-reachability errors from Cargo.

Everything in this commit should be fully backwards-compatible - users
who don't enable the 'public-dependency' cargo feature won't notice any
changes.

Note that this commit does *not* implement the remaining two features of
the RFC:

* Choosing smallest versions when 'cargo publish' is run
* Turning exported_private_dependencies warnings into hard errors when
'cargo publish' is run

The former is a major change to Cargo's behavior, and should be done
in a separate PR with some kind of rollout plan.

The latter is described by the RFC as being enabled at 'some point in
the future'. This can be done via a follow-up PR.
2019-04-25 22:42:18 -04:00

181 lines
3.7 KiB
Rust

use crate::support::registry::Package;
use crate::support::{project, is_nightly};
#[test]
fn exported_priv_warning() {
if !is_nightly() {
return;
}
Package::new("priv_dep", "0.1.0")
.file(
"src/lib.rs",
"pub struct FromPriv;"
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["public-dependency"]
[package]
name = "foo"
version = "0.0.1"
[dependencies]
priv_dep = "0.1.0"
"#,
)
.file(
"src/lib.rs",
"
extern crate priv_dep;
pub fn use_priv(_: priv_dep::FromPriv) {}
",
)
.build();
p.cargo("build --message-format=short")
.masquerade_as_nightly_cargo()
.with_stderr(
"\
[UPDATING] `[..]` index
[DOWNLOADING] crates ...
[DOWNLOADED] priv_dep v0.1.0 ([..])
[COMPILING] priv_dep v0.1.0
[COMPILING] foo v0.0.1 ([CWD])
src/lib.rs:3:13: warning: type `priv_dep::FromPriv` from private dependency 'priv_dep' in public interface
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
"
)
.run()
}
#[test]
fn exported_pub_dep() {
if !is_nightly() {
return;
}
Package::new("pub_dep", "0.1.0")
.file(
"src/lib.rs",
"pub struct FromPub;"
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["public-dependency"]
[package]
name = "foo"
version = "0.0.1"
[dependencies]
pub_dep = {version = "0.1.0", public = true}
"#,
)
.file(
"src/lib.rs",
"
extern crate pub_dep;
pub fn use_pub(_: pub_dep::FromPub) {}
",
)
.build();
p.cargo("build --message-format=short")
.masquerade_as_nightly_cargo()
.with_stderr(
"\
[UPDATING] `[..]` index
[DOWNLOADING] crates ...
[DOWNLOADED] pub_dep v0.1.0 ([..])
[COMPILING] pub_dep v0.1.0
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
"
)
.run()
}
#[test]
pub fn requires_nightly_cargo() {
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["public-dependency"]
"#,
)
.file(
"src/lib.rs",
""
)
.build();
p.cargo("build --message-format=short")
.with_status(101)
.with_stderr(
"\
error: failed to parse manifest at `[..]`
Caused by:
the cargo feature `public-dependency` requires a nightly version of Cargo, but this is the `stable` channel
"
)
.run()
}
#[test]
fn requires_feature() {
Package::new("pub_dep", "0.1.0")
.file(
"src/lib.rs",
""
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
pub_dep = { version = "0.1.0", public = true }
"#,
)
.file(
"src/lib.rs",
""
)
.build();
p.cargo("build --message-format=short")
.masquerade_as_nightly_cargo()
.with_status(101)
.with_stderr(
"\
error: failed to parse manifest at `[..]`
Caused by:
feature `public-dependency` is required
consider adding `cargo-features = [\"public-dependency\"]` to the manifest
"
)
.run()
}