Skip to content

Commit

Permalink
Merge pull request #6371 from matklad/update-dry-run
Browse files Browse the repository at this point in the history
add `--dry-run` option to cargo update
  • Loading branch information
nrc authored Dec 12, 2018
2 parents d4af223 + 7be09e3 commit 4786249
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn cli() -> App {
.arg_target_dir()
.arg_manifest_path()
.arg_jobs()
.arg(opt("dry-run", "Perform all checks without uploading"))
.arg_dry_run("Perform all checks without uploading")
.arg(opt("registry", "Registry to publish to").value_name("REGISTRY"))
}

Expand Down
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn cli() -> App {
"aggressive",
"Force updating all dependencies of <name> as well",
))
.arg_dry_run("Don't actually write the lockfile")
.arg(opt("precise", "Update a single dependency to exactly PRECISE").value_name("PRECISE"))
.arg_manifest_path()
.after_help(
Expand Down Expand Up @@ -44,6 +45,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
aggressive: args.is_present("aggressive"),
precise: args.value_of("precise"),
to_update: values(args, "package"),
dry_run: args.is_present("dry-run"),
config,
};
ops::update_lockfile(&ws, &update_opts)?;
Expand Down
10 changes: 8 additions & 2 deletions src/cargo/ops/cargo_generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct UpdateOptions<'a> {
pub to_update: Vec<String>,
pub precise: Option<&'a str>,
pub aggressive: bool,
pub dry_run: bool,
}

pub fn generate_lockfile(ws: &Workspace<'_>) -> CargoResult<()> {
Expand Down Expand Up @@ -119,8 +120,13 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
}
}
}

ops::write_pkg_lockfile(ws, &resolve)?;
if opts.dry_run {
opts.config
.shell()
.warn("not updating lockfile due to dry run")?;
} else {
ops::write_pkg_lockfile(ws, &resolve)?;
}
return Ok(());

fn fill_with_deps<'a>(
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ pub trait AppExt: Sized {
.hidden(true),
)
}

fn arg_dry_run(self, dry_run: &'static str) -> Self {
self._arg(opt("dry-run", dry_run))
}
}

impl AppExt for App {
Expand Down
55 changes: 55 additions & 0 deletions tests/testsuite/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,58 @@ fn preserve_top_comment() {

assert!(lockfile == lockfile2);
}

#[test]
fn dry_run_update() {
Package::new("log", "0.1.0").publish();
Package::new("serde", "0.1.0").dep("log", "0.1").publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "bar"
version = "0.0.1"
authors = []
[dependencies]
serde = "0.1"
log = "0.1"
foo = { path = "foo" }
"#,
)
.file("src/lib.rs", "")
.file(
"foo/Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
serde = "0.1"
"#,
)
.file("foo/src/lib.rs", "")
.build();

p.cargo("build").run();
let old_lockfile = p.read_file("Cargo.lock");

Package::new("log", "0.1.1").publish();
Package::new("serde", "0.1.1").dep("log", "0.1").publish();

p.cargo("update -p serde --dry-run")
.with_stderr(
"\
[UPDATING] `[..]` index
[UPDATING] serde v0.1.0 -> v0.1.1
[WARNING] not updating lockfile due to dry run
",
)
.run();
let new_lockfile = p.read_file("Cargo.lock");
assert_eq!(old_lockfile, new_lockfile)
}

0 comments on commit 4786249

Please sign in to comment.