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 option to cargo update #6371

Merged
merged 2 commits into from
Dec 12, 2018
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
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 @@ -15,6 +15,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 @@ -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>(
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)
}