From b29e37968ab478d740afdfe72e1ad54d72ad608f Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Wed, 9 Jan 2019 18:25:36 +0100 Subject: [PATCH] Adding support for `no-default-features` to package and publish --- src/bin/cargo/commands/package.rs | 1 + src/bin/cargo/commands/publish.rs | 1 + src/cargo/ops/cargo_package.rs | 3 +- src/cargo/ops/registry.rs | 4 +- tests/testsuite/package.rs | 31 ++++++++++++ tests/testsuite/publish.rs | 80 +++++++++++++++++++++++++------ 6 files changed, 103 insertions(+), 17 deletions(-) diff --git a/src/bin/cargo/commands/package.rs b/src/bin/cargo/commands/package.rs index 32bc431db..1964e138c 100644 --- a/src/bin/cargo/commands/package.rs +++ b/src/bin/cargo/commands/package.rs @@ -45,6 +45,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { jobs: args.jobs()?, features: args._values_of("features"), all_features: args.is_present("all-features"), + no_default_features: args.is_present("no-default-features"), }, )?; Ok(()) diff --git a/src/bin/cargo/commands/publish.rs b/src/bin/cargo/commands/publish.rs index 2c9f132d6..67b4ed2d2 100644 --- a/src/bin/cargo/commands/publish.rs +++ b/src/bin/cargo/commands/publish.rs @@ -43,6 +43,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { registry, features: args._values_of("features"), all_features: args.is_present("all-features"), + no_default_features: args.is_present("no-default-features"), }, )?; Ok(()) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 47601667a..a8f92ad2f 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -28,6 +28,7 @@ pub struct PackageOpts<'cfg> { pub target: Option, pub features: Vec, pub all_features: bool, + pub no_default_features: bool, } static VCS_INFO_FILE: &'static str = ".cargo_vcs_info.json"; @@ -450,7 +451,7 @@ fn run_verify(ws: &Workspace<'_>, tar: &FileLock, opts: &PackageOpts<'_>) -> Car config, build_config: BuildConfig::new(config, opts.jobs, &opts.target, CompileMode::Build)?, features: opts.features.clone(), - no_default_features: false, + no_default_features: opts.no_default_features, all_features: opts.all_features, spec: ops::Packages::Packages(Vec::new()), filter: ops::CompileFilter::Default { diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 9777394ba..71bd0e2cb 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -40,7 +40,8 @@ pub struct PublishOpts<'cfg> { pub dry_run: bool, pub registry: Option, pub features: Vec, - pub all_features: bool + pub all_features: bool, + pub no_default_features: bool, } pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { @@ -86,6 +87,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { jobs: opts.jobs, features: opts.features.clone(), all_features: opts.all_features, + no_default_features: opts.no_default_features, }, )? .unwrap(); diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index ba4d3e7ae..768b51f04 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1358,3 +1358,34 @@ fn package_with_all_features() { .with_status(0) .run(); } + +#[test] +fn package_no_default_features() { + let p = project() + .file( + "Cargo.toml", + r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + license = "MIT" + description = "foo" + + [features] + default = ["required"] + required = [] + "#, + ).file( + "src/main.rs", + "#[cfg(not(feature = \"required\"))] + compile_error!(\"This crate requires `required` feature!\"); + fn main() {}", + ).build(); + + p.cargo("package --no-default-features") + .masquerade_as_nightly_cargo() + .with_stderr_contains("error: This crate requires `required` feature!") + .with_status(101) + .run(); +} diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index caec99db1..628499a86 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -38,7 +38,8 @@ See [..] [UPLOADING] foo v0.0.1 ([CWD]) ", reg = publish::registry_path().to_str().unwrap() - )).run(); + )) + .run(); let mut f = File::open(&publish::upload_path().join("api/v1/crates/new")).unwrap(); // Skip the metadata payload and the size of the tarball @@ -112,7 +113,8 @@ See [..] [UPLOADING] foo v0.0.1 ([CWD]) ", reg = publish::registry_path().to_str().unwrap() - )).run(); + )) + .run(); let mut f = File::open(&publish::upload_path().join("api/v1/crates/new")).unwrap(); // Skip the metadata payload and the size of the tarball @@ -188,7 +190,8 @@ See [..] [UPLOADING] foo v0.0.1 ([CWD]) ", reg = publish::registry_path().to_str().unwrap() - )).run(); + )) + .run(); let mut f = File::open(&publish::upload_path().join("api/v1/crates/new")).unwrap(); // Skip the metadata payload and the size of the tarball @@ -266,7 +269,8 @@ See [..] [UPLOADING] foo v0.0.1 ([CWD]) ", reg = publish::registry_path().to_str().unwrap() - )).run(); + )) + .run(); let mut f = File::open(&publish::upload_path().join("api/v1/crates/new")).unwrap(); // Skip the metadata payload and the size of the tarball @@ -335,7 +339,8 @@ specify a crates.io version as a dependency or pull it into this \ repository and specify it with a path and version\n\ (crate `foo` has repository path `git://path/to/nowhere`)\ ", - ).run(); + ) + .run(); } #[test] @@ -371,7 +376,8 @@ fn path_dependency_no_version() { [ERROR] all path dependencies must have a version specified when publishing. dependency `bar` does not specify a version ", - ).run(); + ) + .run(); } #[test] @@ -402,7 +408,8 @@ fn unpublishable_crate() { [ERROR] some crates cannot be published. `foo` is marked as unpublishable ", - ).run(); + ) + .run(); } #[test] @@ -441,7 +448,8 @@ bar to proceed despite this, pass the `--allow-dirty` flag ", - ).run(); + ) + .run(); } #[test] @@ -555,7 +563,8 @@ fn ignore_when_crate_ignored() { homepage = "foo" repository = "foo" "#, - ).nocommit_file("bar/src/main.rs", "fn main() {}"); + ) + .nocommit_file("bar/src/main.rs", "fn main() {}"); p.cargo("publish") .cwd(p.root().join("bar")) .arg("--index") @@ -583,7 +592,8 @@ fn new_crate_rejected() { homepage = "foo" repository = "foo" "#, - ).nocommit_file("src/main.rs", "fn main() {}"); + ) + .nocommit_file("src/main.rs", "fn main() {}"); p.cargo("publish --index") .arg(publish::registry().to_string()) .with_status(101) @@ -623,7 +633,8 @@ See [..] [UPLOADING] foo v0.0.1 ([CWD]) [WARNING] aborting upload due to dry run ", - ).run(); + ) + .run(); // Ensure the API request wasn't actually made assert!(!publish::upload_path().join("api/v1/crates/new").exists()); @@ -666,7 +677,8 @@ Caused by: consider adding `cargo-features = [\"alternative-registries\"]` to the manifest ", - ).run(); + ) + .run(); } #[test] @@ -704,7 +716,8 @@ fn registry_not_in_publish_list() { [ERROR] some crates cannot be published. `foo` is marked as unpublishable ", - ).run(); + ) + .run(); } #[test] @@ -737,7 +750,8 @@ fn publish_empty_list() { [ERROR] some crates cannot be published. `foo` is marked as unpublishable ", - ).run(); + ) + .run(); } #[test] @@ -801,7 +815,8 @@ fn block_publish_no_registry() { [ERROR] some crates cannot be published. `foo` is marked as unpublishable ", - ).run(); + ) + .run(); } #[test] @@ -871,3 +886,38 @@ fn publish_with_all_features() { .with_stderr_contains("[UPLOADING] foo v0.0.1 ([CWD])") .run(); } + +#[test] +fn publish_with_no_default_features() { + publish::setup(); + + let p = project() + .file( + "Cargo.toml", + r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + license = "MIT" + description = "foo" + + [features] + default = ["required"] + required = [] + "#, + ) + .file( + "src/main.rs", + "#[cfg(not(feature = \"required\"))] + compile_error!(\"This crate requires `required` feature!\"); + fn main() {}", + ) + .build(); + + p.cargo("publish --no-default-features --index") + .arg(publish::registry().to_string()) + .with_stderr_contains("error: This crate requires `required` feature!") + .with_status(101) + .run(); +}