From a2a82a297468f35926dcdba9b26fbbbe59c9a79d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 2 Dec 2018 14:41:16 +0300 Subject: [PATCH 1/2] add `--dry-run` option to cargo update --- src/bin/cargo/commands/publish.rs | 2 +- src/bin/cargo/commands/update.rs | 2 ++ src/cargo/ops/cargo_generate_lockfile.rs | 10 ++++++++-- src/cargo/util/command_prelude.rs | 4 ++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/bin/cargo/commands/publish.rs b/src/bin/cargo/commands/publish.rs index b2eb7abb00f..69845d033da 100644 --- a/src/bin/cargo/commands/publish.rs +++ b/src/bin/cargo/commands/publish.rs @@ -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")) } diff --git a/src/bin/cargo/commands/update.rs b/src/bin/cargo/commands/update.rs index 766226d0f24..246aab8867b 100644 --- a/src/bin/cargo/commands/update.rs +++ b/src/bin/cargo/commands/update.rs @@ -10,6 +10,7 @@ pub fn cli() -> App { "aggressive", "Force updating all dependencies of 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( @@ -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)?; diff --git a/src/cargo/ops/cargo_generate_lockfile.rs b/src/cargo/ops/cargo_generate_lockfile.rs index d62c133d412..0881667e198 100644 --- a/src/cargo/ops/cargo_generate_lockfile.rs +++ b/src/cargo/ops/cargo_generate_lockfile.rs @@ -15,6 +15,7 @@ pub struct UpdateOptions<'a> { pub to_update: Vec, pub precise: Option<&'a str>, pub aggressive: bool, + pub dry_run: bool, } pub fn generate_lockfile(ws: &Workspace) -> CargoResult<()> { @@ -118,8 +119,13 @@ pub fn update_lockfile(ws: &Workspace, opts: &UpdateOptions) -> CargoResult<()> } } } - - 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>( diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 1985cd575ed..4bc0f0a6c91 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -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 { From 7be09e3c7cb7bc83f1c90341ce199714f2b570f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Dec 2018 21:28:07 +0300 Subject: [PATCH 2/2] Add a test for update --dry-run --- tests/testsuite/update.rs | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index f1641d8f93c..6d6c95dc6b0 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -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) +}