Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for publish to optionally take the index that can be used #4568

Merged
merged 12 commits into from
Nov 18, 2017
Merged
Prev Previous commit
Next Next commit
Switch over to use --registry for publish list.
  • Loading branch information
cswindle committed Oct 31, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit caf7681fe2f7c28f0d3e1e9efca03739da4d7a51
3 changes: 1 addition & 2 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
@@ -43,8 +43,7 @@ pub fn publish(ws: &Workspace, opts: &PublishOpts) -> CargoResult<()> {
let pkg = ws.current()?;

if let &Some(ref allowed_registries) = pkg.publish() {
let index = opts.index.clone();
if index.is_none() || !allowed_registries.contains(&index.unwrap()) {
if opts.registry.is_none() || !allowed_registries.contains(&opts.registry.clone().unwrap()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could be:

if let Some(ref registry) = opts.registry {
    if !allowed_registries.contains(registry) {
        bail!(...);
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work as if there are allowed registries setup, but we have not provided --registry then we also need to block. This covers the case of pushing to crates.io when publish is set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er so what I mostly mean here is getting rid of the is_none + clone + unwrap, basically restructuring this to avoid all the function calls (and make it a little easier to read)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have got rid of the is_none, unwrap etc.

bail!("some crates cannot be published.\n\
`{}` is marked as unpublishable", pkg.name());
}
13 changes: 12 additions & 1 deletion tests/cargotest/support/publish.rs
Original file line number Diff line number Diff line change
@@ -10,10 +10,21 @@ use url::Url;
pub fn setup() -> Repository {
let config = paths::root().join(".cargo/config");
t!(fs::create_dir_all(config.parent().unwrap()));
t!(t!(File::create(&config)).write_all(br#"
t!(t!(File::create(&config)).write_all(format!(r#"
[registry]
token = "api-token"

[registries.alternative]
index = "{registry}"
"#, registry = registry().to_string()).as_bytes()));

let credentials = paths::root().join("home/.cargo/credentials");
t!(fs::create_dir_all(credentials.parent().unwrap()));
t!(t!(File::create(&credentials)).write_all(br#"
[alternative]
token = "api-token"
"#));

t!(fs::create_dir_all(&upload_path().join("api/v1/crates")));

repo(&registry_path())
59 changes: 43 additions & 16 deletions tests/publish.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ use std::io::prelude::*;
use std::fs::File;
use std::io::SeekFrom;

use cargotest::ChannelChanger;
use cargotest::support::git::repo;
use cargotest::support::paths;
use cargotest::support::{project, execs, publish};
@@ -502,7 +503,7 @@ See [..]
}

#[test]
fn index_not_in_publish_list() {
fn registry_not_in_publish_list() {
publish::setup();

let p = project("foo")
@@ -517,10 +518,11 @@ fn index_not_in_publish_list() {
"test"
]
"#)
.file("src/main.rs", "fn main() {}");
.file("src/main.rs", "fn main() {}")
.build();

assert_that(p.cargo_process("publish")
.arg("--index").arg(publish::registry().to_string()),
assert_that(p.cargo("publish").masquerade_as_nightly_cargo()
.arg("--registry").arg("alternative").arg("-Zunstable-options"),
execs().with_status(101).with_stderr("\
[ERROR] some crates cannot be published.
`foo` is marked as unpublishable
@@ -541,25 +543,25 @@ fn publish_empty_list() {
description = "foo"
publish = []
"#)
.file("src/main.rs", "fn main() {}");
.file("src/main.rs", "fn main() {}")
.build();

assert_that(p.cargo_process("publish")
.arg("--index").arg(publish::registry().to_string()),
assert_that(p.cargo("publish").masquerade_as_nightly_cargo()
.arg("--registry").arg("alternative").arg("-Zunstable-options"),
execs().with_status(101).with_stderr("\
[ERROR] some crates cannot be published.
`foo` is marked as unpublishable
"));
}

#[test]
fn publish_allowed_index() {
fn publish_allowed_registry() {
publish::setup();

let p = project("foo");
p.build();
let p = project("foo").build();

repo(&paths::root().join("foo"))
.file("Cargo.toml", &format!(r#"
let _ = repo(&paths::root().join("foo"))
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
@@ -568,12 +570,37 @@ fn publish_allowed_index() {
description = "foo"
documentation = "foo"
homepage = "foo"
publish = ["{}"]
"#, publish::registry().to_string()))
publish = ["alternative"]
"#)
.file("src/main.rs", "fn main() {}")
.build();

assert_that(p.cargo("publish")
.arg("--index").arg(publish::registry().to_string()),
assert_that(p.cargo("publish").masquerade_as_nightly_cargo()
.arg("--registry").arg("alternative").arg("-Zunstable-options"),
execs().with_status(0));
}

#[test]
fn block_publish_no_registry() {
publish::setup();

let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"
publish = []
"#)
.file("src/main.rs", "fn main() {}")
.build();

assert_that(p.cargo("publish").masquerade_as_nightly_cargo()
.arg("--index").arg(publish::registry().to_string()),
execs().with_status(101).with_stderr("\
[ERROR] some crates cannot be published.
`foo` is marked as unpublishable
"));
}