From: Chris Swindle Date: Tue, 31 Oct 2017 09:51:37 +0000 (+0000) Subject: Add feature gate on publish field changes. X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~22^2~5^2~1^2~6 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=c86a7bcd9b07c1e193048532a9cb4c288941eb72;p=cargo.git Add feature gate on publish field changes. --- diff --git a/src/bin/package.rs b/src/bin/package.rs index 31e3330ad..9c91583d0 100644 --- a/src/bin/package.rs +++ b/src/bin/package.rs @@ -61,6 +61,7 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult { allow_dirty: options.flag_allow_dirty, target: options.flag_target.as_ref().map(|t| &t[..]), jobs: options.flag_jobs, + registry: None, })?; Ok(()) } diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 57e0f700e..1edc51b03 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -134,10 +134,10 @@ impl Package { } } - pub fn to_registry_toml(&self) -> String { + pub fn to_registry_toml(&self) -> CargoResult { let manifest = self.manifest().original().prepare_for_publish(); - let toml = toml::to_string(&manifest).unwrap(); - format!("\ + let toml = toml::to_string(&manifest)?; + Ok(format!("\ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO\n\ #\n\ # When uploading crates to the registry Cargo will automatically\n\ @@ -151,7 +151,7 @@ impl Package { # will likely look very different (and much more reasonable)\n\ \n\ {}\ - ", toml) + ", toml)) } } diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index f12ce12f3..04c3f6d97 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -23,13 +23,17 @@ pub struct PackageOpts<'cfg> { pub verify: bool, pub jobs: Option, pub target: Option<&'cfg str>, + pub registry: Option, } pub fn package(ws: &Workspace, opts: &PackageOpts) -> CargoResult> { let pkg = ws.current()?; let config = ws.config(); - if !pkg.manifest().features().activated().is_empty() { + + // Allow packaging if a registry has been provided, or if there are no nightly + // features enabled. + if opts.registry.is_none() && !pkg.manifest().features().activated().is_empty() { bail!("cannot package or publish crates which activate nightly-only \ cargo features") } @@ -251,7 +255,7 @@ fn tar(ws: &Workspace, })?; let mut header = Header::new_ustar(); - let toml = pkg.to_registry_toml(); + let toml = pkg.to_registry_toml()?; header.set_path(&path)?; header.set_entry_type(EntryType::file()); header.set_mode(0o644); diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 2bd696a34..a7e8ef82b 100755 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -69,6 +69,7 @@ pub fn publish(ws: &Workspace, opts: &PublishOpts) -> CargoResult<()> { allow_dirty: opts.allow_dirty, target: opts.target, jobs: opts.jobs, + registry: opts.registry.clone(), })?.unwrap(); // Upload said tarball to the specified destination diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 622c2674c..d3f9864cd 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -200,6 +200,8 @@ pub struct DetailedTomlDependency { #[derive(Debug, Deserialize, Serialize)] pub struct TomlManifest { + #[serde(rename = "cargo-features")] + cargo_features: Option>, package: Option>, project: Option>, profile: Option, @@ -223,8 +225,6 @@ pub struct TomlManifest { patch: Option>>, workspace: Option, badges: Option>>, - #[serde(rename = "cargo-features")] - cargo_features: Option>, } #[derive(Deserialize, Serialize, Clone, Debug, Default)] @@ -694,7 +694,12 @@ impl TomlManifest { }; let profiles = build_profiles(&me.profile); let publish = match project.publish { - Some(VecStringOrBool::VecString(ref vecstring)) => Some(vecstring.clone()), + Some(VecStringOrBool::VecString(ref vecstring)) => { + features.require(Feature::alternative_registries()).chain_err(|| { + "the `publish` manifest key is unstable for anything other than a value of true or false" + })?; + Some(vecstring.clone()) + }, Some(VecStringOrBool::Bool(false)) => Some(vec![]), _ => None, }; diff --git a/tests/publish.rs b/tests/publish.rs index 96aae5a3e..d4af2e99a 100755 --- a/tests/publish.rs +++ b/tests/publish.rs @@ -502,12 +502,48 @@ See [..] assert!(!publish::upload_path().join("api/v1/crates/new").exists()); } +#[test] +fn block_publish_feature_not_enabled() { + publish::setup(); + + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + license = "MIT" + description = "foo" + publish = [ + "test" + ] + "#) + .file("src/main.rs", "fn main() {}") + .build(); + + assert_that(p.cargo("publish").masquerade_as_nightly_cargo() + .arg("--registry").arg("alternative").arg("-Zunstable-options"), + execs().with_status(101).with_stderr("\ +error: failed to parse manifest at `[..]` + +Caused by: + the `publish` manifest key is unstable for anything other than a value of true or false + +Caused by: + feature `alternative-registries` is required + +consider adding `cargo-features = [\"alternative-registries\"]` to the manifest +")); +} + #[test] fn registry_not_in_publish_list() { publish::setup(); let p = project("foo") .file("Cargo.toml", r#" + cargo-features = ["alternative-registries"] + [project] name = "foo" version = "0.0.1" @@ -535,6 +571,8 @@ fn publish_empty_list() { let p = project("foo") .file("Cargo.toml", r#" + cargo-features = ["alternative-registries"] + [project] name = "foo" version = "0.0.1" @@ -562,6 +600,8 @@ fn publish_allowed_registry() { let _ = repo(&paths::root().join("foo")) .file("Cargo.toml", r#" + cargo-features = ["alternative-registries"] + [project] name = "foo" version = "0.0.1" @@ -586,6 +626,8 @@ fn block_publish_no_registry() { let p = project("foo") .file("Cargo.toml", r#" + cargo-features = ["alternative-registries"] + [project] name = "foo" version = "0.0.1"