Skip to content

Commit 4786249

Browse files
authored
Merge pull request #6371 from matklad/update-dry-run
add `--dry-run` option to cargo update
2 parents d4af223 + 7be09e3 commit 4786249

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

src/bin/cargo/commands/publish.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn cli() -> App {
1919
.arg_target_dir()
2020
.arg_manifest_path()
2121
.arg_jobs()
22-
.arg(opt("dry-run", "Perform all checks without uploading"))
22+
.arg_dry_run("Perform all checks without uploading")
2323
.arg(opt("registry", "Registry to publish to").value_name("REGISTRY"))
2424
}
2525

src/bin/cargo/commands/update.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub fn cli() -> App {
1010
"aggressive",
1111
"Force updating all dependencies of <name> as well",
1212
))
13+
.arg_dry_run("Don't actually write the lockfile")
1314
.arg(opt("precise", "Update a single dependency to exactly PRECISE").value_name("PRECISE"))
1415
.arg_manifest_path()
1516
.after_help(
@@ -44,6 +45,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
4445
aggressive: args.is_present("aggressive"),
4546
precise: args.value_of("precise"),
4647
to_update: values(args, "package"),
48+
dry_run: args.is_present("dry-run"),
4749
config,
4850
};
4951
ops::update_lockfile(&ws, &update_opts)?;

src/cargo/ops/cargo_generate_lockfile.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct UpdateOptions<'a> {
1616
pub to_update: Vec<String>,
1717
pub precise: Option<&'a str>,
1818
pub aggressive: bool,
19+
pub dry_run: bool,
1920
}
2021

2122
pub fn generate_lockfile(ws: &Workspace<'_>) -> CargoResult<()> {
@@ -119,8 +120,13 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
119120
}
120121
}
121122
}
122-
123-
ops::write_pkg_lockfile(ws, &resolve)?;
123+
if opts.dry_run {
124+
opts.config
125+
.shell()
126+
.warn("not updating lockfile due to dry run")?;
127+
} else {
128+
ops::write_pkg_lockfile(ws, &resolve)?;
129+
}
124130
return Ok(());
125131

126132
fn fill_with_deps<'a>(

src/cargo/util/command_prelude.rs

+4
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ pub trait AppExt: Sized {
177177
.hidden(true),
178178
)
179179
}
180+
181+
fn arg_dry_run(self, dry_run: &'static str) -> Self {
182+
self._arg(opt("dry-run", dry_run))
183+
}
180184
}
181185

182186
impl AppExt for App {

tests/testsuite/update.rs

+55
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,58 @@ fn preserve_top_comment() {
415415

416416
assert!(lockfile == lockfile2);
417417
}
418+
419+
#[test]
420+
fn dry_run_update() {
421+
Package::new("log", "0.1.0").publish();
422+
Package::new("serde", "0.1.0").dep("log", "0.1").publish();
423+
424+
let p = project()
425+
.file(
426+
"Cargo.toml",
427+
r#"
428+
[package]
429+
name = "bar"
430+
version = "0.0.1"
431+
authors = []
432+
433+
[dependencies]
434+
serde = "0.1"
435+
log = "0.1"
436+
foo = { path = "foo" }
437+
"#,
438+
)
439+
.file("src/lib.rs", "")
440+
.file(
441+
"foo/Cargo.toml",
442+
r#"
443+
[package]
444+
name = "foo"
445+
version = "0.0.1"
446+
authors = []
447+
448+
[dependencies]
449+
serde = "0.1"
450+
"#,
451+
)
452+
.file("foo/src/lib.rs", "")
453+
.build();
454+
455+
p.cargo("build").run();
456+
let old_lockfile = p.read_file("Cargo.lock");
457+
458+
Package::new("log", "0.1.1").publish();
459+
Package::new("serde", "0.1.1").dep("log", "0.1").publish();
460+
461+
p.cargo("update -p serde --dry-run")
462+
.with_stderr(
463+
"\
464+
[UPDATING] `[..]` index
465+
[UPDATING] serde v0.1.0 -> v0.1.1
466+
[WARNING] not updating lockfile due to dry run
467+
",
468+
)
469+
.run();
470+
let new_lockfile = p.read_file("Cargo.lock");
471+
assert_eq!(old_lockfile, new_lockfile)
472+
}

0 commit comments

Comments
 (0)