From 32f80ebb857e799769ca350cb65692bd25069b00 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 25 Jul 2022 09:51:58 -0500 Subject: [PATCH] fix(add): Respect --locked This is prep for #10901 to avoid the most common failure case for the lock file. We are assuming if the users action caused a change in the manifes, then it will cause a change in the lock file. This isn't entirely true but close enough and I think these semantics can make sense. --- src/cargo/ops/cargo_add/manifest.rs | 9 +++++-- src/cargo/ops/cargo_add/mod.rs | 11 ++++++++ tests/testsuite/cargo_add/locked_changed/in | 1 + .../testsuite/cargo_add/locked_changed/mod.rs | 25 +++++++++++++++++++ .../cargo_add/locked_changed/out/Cargo.toml | 5 ++++ .../cargo_add/locked_changed/stderr.log | 3 +++ .../cargo_add/locked_changed/stdout.log | 0 .../cargo_add/locked_unchanged/in/Cargo.toml | 8 ++++++ .../cargo_add/locked_unchanged/in/src/lib.rs | 0 .../cargo_add/locked_unchanged/mod.rs | 25 +++++++++++++++++++ .../cargo_add/locked_unchanged/out/Cargo.toml | 8 ++++++ .../cargo_add/locked_unchanged/stderr.log | 2 ++ .../cargo_add/locked_unchanged/stdout.log | 0 tests/testsuite/cargo_add/mod.rs | 2 ++ 14 files changed, 97 insertions(+), 2 deletions(-) create mode 120000 tests/testsuite/cargo_add/locked_changed/in create mode 100644 tests/testsuite/cargo_add/locked_changed/mod.rs create mode 100644 tests/testsuite/cargo_add/locked_changed/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/locked_changed/stderr.log create mode 100644 tests/testsuite/cargo_add/locked_changed/stdout.log create mode 100644 tests/testsuite/cargo_add/locked_unchanged/in/Cargo.toml create mode 100644 tests/testsuite/cargo_add/locked_unchanged/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/locked_unchanged/mod.rs create mode 100644 tests/testsuite/cargo_add/locked_unchanged/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/locked_unchanged/stderr.log create mode 100644 tests/testsuite/cargo_add/locked_unchanged/stdout.log diff --git a/src/cargo/ops/cargo_add/manifest.rs b/src/cargo/ops/cargo_add/manifest.rs index b90b78083921..1c96b4770620 100644 --- a/src/cargo/ops/cargo_add/manifest.rs +++ b/src/cargo/ops/cargo_add/manifest.rs @@ -238,8 +238,7 @@ impl str::FromStr for Manifest { impl std::fmt::Display for Manifest { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let s = self.data.to_string(); - s.fmt(f) + self.data.fmt(f) } } @@ -433,6 +432,12 @@ impl LocalManifest { } } +impl std::fmt::Display for LocalManifest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.manifest.fmt(f) + } +} + #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] enum DependencyStatus { None, diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index a1cc0d272aae..c584a03f8584 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -62,6 +62,7 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<( let manifest_path = options.spec.manifest_path().to_path_buf(); let mut manifest = LocalManifest::try_new(&manifest_path)?; + let original_raw_manifest = manifest.to_string(); let legacy = manifest.get_legacy_sections(); if !legacy.is_empty() { anyhow::bail!( @@ -142,6 +143,16 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<( } } + if options.config.locked() { + let new_raw_manifest = manifest.to_string(); + if original_raw_manifest != new_raw_manifest { + anyhow::bail!( + "the manifest file {} needs to be updated but --locked was passed to prevent this", + manifest.path.display() + ); + } + } + if options.dry_run { options.config.shell().warn("aborting add due to dry run")?; } else { diff --git a/tests/testsuite/cargo_add/locked_changed/in b/tests/testsuite/cargo_add/locked_changed/in new file mode 120000 index 000000000000..6c6a27fcfb58 --- /dev/null +++ b/tests/testsuite/cargo_add/locked_changed/in @@ -0,0 +1 @@ +../add-basic.in \ No newline at end of file diff --git a/tests/testsuite/cargo_add/locked_changed/mod.rs b/tests/testsuite/cargo_add/locked_changed/mod.rs new file mode 100644 index 000000000000..f4156f8e5f00 --- /dev/null +++ b/tests/testsuite/cargo_add/locked_changed/mod.rs @@ -0,0 +1,25 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +use crate::cargo_add::init_registry; +use cargo_test_support::curr_dir; + +#[cargo_test] +fn locked_changed() { + init_registry(); + let project = Project::from_template(curr_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("add") + .arg_line("my-package --locked") + .current_dir(cwd) + .assert() + .failure() + .stdout_matches_path(curr_dir!().join("stdout.log")) + .stderr_matches_path(curr_dir!().join("stderr.log")); + + assert_ui().subset_matches(curr_dir!().join("out"), &project_root); +} diff --git a/tests/testsuite/cargo_add/locked_changed/out/Cargo.toml b/tests/testsuite/cargo_add/locked_changed/out/Cargo.toml new file mode 100644 index 000000000000..3ecdb6681676 --- /dev/null +++ b/tests/testsuite/cargo_add/locked_changed/out/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" diff --git a/tests/testsuite/cargo_add/locked_changed/stderr.log b/tests/testsuite/cargo_add/locked_changed/stderr.log new file mode 100644 index 000000000000..8af168373963 --- /dev/null +++ b/tests/testsuite/cargo_add/locked_changed/stderr.log @@ -0,0 +1,3 @@ + Updating `dummy-registry` index + Adding my-package v99999.0.0 to dependencies. +error: the manifest file [ROOT]/case/Cargo.toml needs to be updated but --locked was passed to prevent this diff --git a/tests/testsuite/cargo_add/locked_changed/stdout.log b/tests/testsuite/cargo_add/locked_changed/stdout.log new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/testsuite/cargo_add/locked_unchanged/in/Cargo.toml b/tests/testsuite/cargo_add/locked_unchanged/in/Cargo.toml new file mode 100644 index 000000000000..5964c87be748 --- /dev/null +++ b/tests/testsuite/cargo_add/locked_unchanged/in/Cargo.toml @@ -0,0 +1,8 @@ +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package = "99999.0.0" diff --git a/tests/testsuite/cargo_add/locked_unchanged/in/src/lib.rs b/tests/testsuite/cargo_add/locked_unchanged/in/src/lib.rs new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/testsuite/cargo_add/locked_unchanged/mod.rs b/tests/testsuite/cargo_add/locked_unchanged/mod.rs new file mode 100644 index 000000000000..8352155f93e6 --- /dev/null +++ b/tests/testsuite/cargo_add/locked_unchanged/mod.rs @@ -0,0 +1,25 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +use crate::cargo_add::init_registry; +use cargo_test_support::curr_dir; + +#[cargo_test] +fn locked_unchanged() { + init_registry(); + let project = Project::from_template(curr_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("add") + .arg_line("my-package --locked") + .current_dir(cwd) + .assert() + .success() + .stdout_matches_path(curr_dir!().join("stdout.log")) + .stderr_matches_path(curr_dir!().join("stderr.log")); + + assert_ui().subset_matches(curr_dir!().join("out"), &project_root); +} diff --git a/tests/testsuite/cargo_add/locked_unchanged/out/Cargo.toml b/tests/testsuite/cargo_add/locked_unchanged/out/Cargo.toml new file mode 100644 index 000000000000..5964c87be748 --- /dev/null +++ b/tests/testsuite/cargo_add/locked_unchanged/out/Cargo.toml @@ -0,0 +1,8 @@ +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package = "99999.0.0" diff --git a/tests/testsuite/cargo_add/locked_unchanged/stderr.log b/tests/testsuite/cargo_add/locked_unchanged/stderr.log new file mode 100644 index 000000000000..fd6b711e3862 --- /dev/null +++ b/tests/testsuite/cargo_add/locked_unchanged/stderr.log @@ -0,0 +1,2 @@ + Updating `dummy-registry` index + Adding my-package v99999.0.0 to dependencies. diff --git a/tests/testsuite/cargo_add/locked_unchanged/stdout.log b/tests/testsuite/cargo_add/locked_unchanged/stdout.log new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/testsuite/cargo_add/mod.rs b/tests/testsuite/cargo_add/mod.rs index 1eb682ef2ed9..914e1c3ae506 100644 --- a/tests/testsuite/cargo_add/mod.rs +++ b/tests/testsuite/cargo_add/mod.rs @@ -48,6 +48,8 @@ mod invalid_vers; mod list_features; mod list_features_path; mod list_features_path_no_default; +mod locked_changed; +mod locked_unchanged; mod manifest_path_package; mod merge_activated_features; mod multiple_conflicts_with_features;