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 --dry-run to cargo publish #2849

Merged
merged 1 commit into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/bin/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Options {
flag_no_verify: bool,
flag_allow_dirty: bool,
flag_jobs: Option<u32>,
flag_dry_run: bool,
}

pub const USAGE: &'static str = "
Expand All @@ -30,6 +31,7 @@ Options:
--allow-dirty Allow publishing with a dirty source directory
--manifest-path PATH Path to the manifest of the package to publish
-j N, --jobs N Number of parallel jobs, defaults to # of CPUs
--dry-run Perform all checks without uploading
-v, --verbose ... Use verbose output
-q, --quiet No output printed to stdout
--color WHEN Coloring: auto, always, never
Expand All @@ -47,6 +49,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
flag_no_verify: no_verify,
flag_allow_dirty: allow_dirty,
flag_jobs: jobs,
flag_dry_run: dry_run,
..
} = options;

Expand All @@ -59,6 +62,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
verify: !no_verify,
allow_dirty: allow_dirty,
jobs: jobs,
dry_run: dry_run,
}));
Ok(None)
}
17 changes: 14 additions & 3 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct PublishOpts<'cfg> {
pub verify: bool,
pub allow_dirty: bool,
pub jobs: Option<u32>,
pub dry_run: bool,
}

pub fn publish(ws: &Workspace, opts: &PublishOpts) -> CargoResult<()> {
Expand Down Expand Up @@ -64,7 +65,7 @@ pub fn publish(ws: &Workspace, opts: &PublishOpts) -> CargoResult<()> {

// Upload said tarball to the specified destination
try!(opts.config.shell().status("Uploading", pkg.package_id().to_string()));
try!(transmit(&pkg, tarball.file(), &mut registry));
try!(transmit(opts.config, &pkg, tarball.file(), &mut registry, opts.dry_run));

Ok(())
}
Expand All @@ -87,8 +88,11 @@ fn verify_dependencies(pkg: &Package, registry_src: &SourceId)
Ok(())
}

fn transmit(pkg: &Package, tarball: &File, registry: &mut Registry)
-> CargoResult<()> {
fn transmit(config: &Config,
pkg: &Package,
tarball: &File,
registry: &mut Registry,
dry_run: bool) -> CargoResult<()> {
let deps = pkg.dependencies().iter().map(|dep| {
NewCrateDependency {
optional: dep.is_optional(),
Expand Down Expand Up @@ -121,6 +125,13 @@ fn transmit(pkg: &Package, tarball: &File, registry: &mut Registry)
}
None => {}
}

// Do not upload if performing a dry run
if dry_run {
try!(config.shell().warn("aborting upload due to dry run"));
return Ok(());
}

registry.publish(&NewCrate {
name: pkg.name().to_string(),
vers: pkg.version().to_string(),
Expand Down
32 changes: 32 additions & 0 deletions tests/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,35 @@ fn new_crate_rejected() {
assert_that(p.cargo("publish"),
execs().with_status(101));
}

#[test]
fn dry_run() {
setup();

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

assert_that(p.cargo_process("publish").arg("--dry-run"),
execs().with_status(0).with_stderr(&format!("\
[UPDATING] registry `{reg}`
[WARNING] manifest has no documentation, [..]
[PACKAGING] foo v0.0.1 ({dir})
[VERIFYING] foo v0.0.1 ({dir})
[COMPILING] foo v0.0.1 [..]
[UPLOADING] foo v0.0.1 ({dir})
[WARNING] aborting upload due to dry run
",
dir = p.url(),
reg = registry())));

// Ensure the API request wasn't actually made
assert!(!upload_path().join("api/v1/crates/new").exists());
}