From 5b90c4bbde5ab3c6f02ff247b43fadc0ce60d08d Mon Sep 17 00:00:00 2001 From: Flowrey Date: Mon, 1 Apr 2024 11:10:00 +0200 Subject: [PATCH 1/2] add tests for preserve features sorted and usorted --- tests/testsuite/cargo_add/mod.rs | 2 + .../preserve_features_sorted/in/Cargo.toml | 9 ++++ .../preserve_features_sorted/in/src/lib.rs | 0 .../cargo_add/preserve_features_sorted/mod.rs | 33 ++++++++++++++ .../preserve_features_sorted/out/Cargo.toml | 9 ++++ .../preserve_features_sorted/stderr.term.svg | 43 +++++++++++++++++++ .../preserve_features_unsorted/in/Cargo.toml | 9 ++++ .../preserve_features_unsorted/in/src/lib.rs | 0 .../preserve_features_unsorted/mod.rs | 33 ++++++++++++++ .../preserve_features_unsorted/out/Cargo.toml | 9 ++++ .../stderr.term.svg | 43 +++++++++++++++++++ 11 files changed, 190 insertions(+) create mode 100644 tests/testsuite/cargo_add/preserve_features_sorted/in/Cargo.toml create mode 100644 tests/testsuite/cargo_add/preserve_features_sorted/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/preserve_features_sorted/mod.rs create mode 100644 tests/testsuite/cargo_add/preserve_features_sorted/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/preserve_features_sorted/stderr.term.svg create mode 100644 tests/testsuite/cargo_add/preserve_features_unsorted/in/Cargo.toml create mode 100644 tests/testsuite/cargo_add/preserve_features_unsorted/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/preserve_features_unsorted/mod.rs create mode 100644 tests/testsuite/cargo_add/preserve_features_unsorted/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/preserve_features_unsorted/stderr.term.svg diff --git a/tests/testsuite/cargo_add/mod.rs b/tests/testsuite/cargo_add/mod.rs index 8800ac23c67..4cfd1e0b4af 100644 --- a/tests/testsuite/cargo_add/mod.rs +++ b/tests/testsuite/cargo_add/mod.rs @@ -111,7 +111,9 @@ mod path_inferred_name; mod path_inferred_name_conflicts_full_feature; mod path_normalized_name; mod preserve_dep_std_table; +mod preserve_features_sorted; mod preserve_features_table; +mod preserve_features_unsorted; mod preserve_sorted; mod preserve_unsorted; mod public; diff --git a/tests/testsuite/cargo_add/preserve_features_sorted/in/Cargo.toml b/tests/testsuite/cargo_add/preserve_features_sorted/in/Cargo.toml new file mode 100644 index 00000000000..263affc7adf --- /dev/null +++ b/tests/testsuite/cargo_add/preserve_features_sorted/in/Cargo.toml @@ -0,0 +1,9 @@ +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" +edition = "2015" + +[dependencies] +my-package = { version = "99999.0.0", features = ["a", "b", "c", "e"] } diff --git a/tests/testsuite/cargo_add/preserve_features_sorted/in/src/lib.rs b/tests/testsuite/cargo_add/preserve_features_sorted/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/preserve_features_sorted/mod.rs b/tests/testsuite/cargo_add/preserve_features_sorted/mod.rs new file mode 100644 index 00000000000..3e9f8a659e5 --- /dev/null +++ b/tests/testsuite/cargo_add/preserve_features_sorted/mod.rs @@ -0,0 +1,33 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::current_dir; +use cargo_test_support::file; +use cargo_test_support::prelude::*; +use cargo_test_support::str; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + cargo_test_support::registry::Package::new("my-package", "99999.0.0+my-package") + .feature("a", &[]) + .feature("b", &[]) + .feature("c", &[]) + .feature("d", &[]) + .feature("e", &[]) + .publish(); + + let project = Project::from_template(current_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("add") + .arg_line("my-package -F d") + .current_dir(cwd) + .assert() + .success() + .stdout_matches(str![""]) + .stderr_matches(file!["stderr.term.svg"]); + + assert_ui().subset_matches(current_dir!().join("out"), &project_root); +} diff --git a/tests/testsuite/cargo_add/preserve_features_sorted/out/Cargo.toml b/tests/testsuite/cargo_add/preserve_features_sorted/out/Cargo.toml new file mode 100644 index 00000000000..cd8000d8c1f --- /dev/null +++ b/tests/testsuite/cargo_add/preserve_features_sorted/out/Cargo.toml @@ -0,0 +1,9 @@ +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" +edition = "2015" + +[dependencies] +my-package = { version = "99999.0.0", features = ["a", "b", "c", "e", "d"] } diff --git a/tests/testsuite/cargo_add/preserve_features_sorted/stderr.term.svg b/tests/testsuite/cargo_add/preserve_features_sorted/stderr.term.svg new file mode 100644 index 00000000000..beb2008bf8d --- /dev/null +++ b/tests/testsuite/cargo_add/preserve_features_sorted/stderr.term.svg @@ -0,0 +1,43 @@ + + + + + + + Updating `dummy-registry` index + + Adding my-package v99999.0.0 to dependencies + + Features: + + + a + + + b + + + c + + + d + + + e + + Locking 2 packages + + + + + + diff --git a/tests/testsuite/cargo_add/preserve_features_unsorted/in/Cargo.toml b/tests/testsuite/cargo_add/preserve_features_unsorted/in/Cargo.toml new file mode 100644 index 00000000000..aa05a86fd79 --- /dev/null +++ b/tests/testsuite/cargo_add/preserve_features_unsorted/in/Cargo.toml @@ -0,0 +1,9 @@ +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" +edition = "2015" + +[dependencies] +my-package = { version = "99999.0.0", features = ["b", "a", "d", "c"] } diff --git a/tests/testsuite/cargo_add/preserve_features_unsorted/in/src/lib.rs b/tests/testsuite/cargo_add/preserve_features_unsorted/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/preserve_features_unsorted/mod.rs b/tests/testsuite/cargo_add/preserve_features_unsorted/mod.rs new file mode 100644 index 00000000000..4cf09d67946 --- /dev/null +++ b/tests/testsuite/cargo_add/preserve_features_unsorted/mod.rs @@ -0,0 +1,33 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::current_dir; +use cargo_test_support::file; +use cargo_test_support::prelude::*; +use cargo_test_support::str; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + cargo_test_support::registry::Package::new("my-package", "99999.0.0+my-package") + .feature("a", &[]) + .feature("b", &[]) + .feature("c", &[]) + .feature("d", &[]) + .feature("e", &[]) + .publish(); + + let project = Project::from_template(current_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("add") + .arg_line("my-package -F e") + .current_dir(cwd) + .assert() + .success() + .stdout_matches(str![""]) + .stderr_matches(file!["stderr.term.svg"]); + + assert_ui().subset_matches(current_dir!().join("out"), &project_root); +} diff --git a/tests/testsuite/cargo_add/preserve_features_unsorted/out/Cargo.toml b/tests/testsuite/cargo_add/preserve_features_unsorted/out/Cargo.toml new file mode 100644 index 00000000000..5d8efeed9f0 --- /dev/null +++ b/tests/testsuite/cargo_add/preserve_features_unsorted/out/Cargo.toml @@ -0,0 +1,9 @@ +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" +edition = "2015" + +[dependencies] +my-package = { version = "99999.0.0", features = ["b", "a", "d", "c", "e"] } diff --git a/tests/testsuite/cargo_add/preserve_features_unsorted/stderr.term.svg b/tests/testsuite/cargo_add/preserve_features_unsorted/stderr.term.svg new file mode 100644 index 00000000000..beb2008bf8d --- /dev/null +++ b/tests/testsuite/cargo_add/preserve_features_unsorted/stderr.term.svg @@ -0,0 +1,43 @@ + + + + + + + Updating `dummy-registry` index + + Adding my-package v99999.0.0 to dependencies + + Features: + + + a + + + b + + + c + + + d + + + e + + Locking 2 packages + + + + + + From 318072f3cf30cb25ca69a6789eb9f87771423e2c Mon Sep 17 00:00:00 2001 From: Flowrey Date: Mon, 1 Apr 2024 14:04:13 +0200 Subject: [PATCH 2/2] preserve dependency features order --- src/cargo/util/toml_mut/dependency.rs | 9 ++++++++- .../cargo_add/overwrite_inline_features/out/Cargo.toml | 2 +- .../cargo_add/preserve_features_sorted/out/Cargo.toml | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/cargo/util/toml_mut/dependency.rs b/src/cargo/util/toml_mut/dependency.rs index 5e8d686fdbd..ce7e6229021 100644 --- a/src/cargo/util/toml_mut/dependency.rs +++ b/src/cargo/util/toml_mut/dependency.rs @@ -4,12 +4,14 @@ use std::fmt::{Display, Formatter}; use std::path::{Path, PathBuf}; use indexmap::IndexSet; +use itertools::Itertools; use toml_edit::KeyMut; use super::manifest::str_or_1_len_table; use crate::core::GitReference; use crate::core::SourceId; use crate::core::Summary; +use crate::util::toml_mut::is_sorted; use crate::CargoResult; use crate::GlobalContext; @@ -588,8 +590,13 @@ impl Dependency { .collect::>>() }) .unwrap_or_default(); + let is_already_sorted = is_sorted(features.iter()); features.extend(new_features.iter().map(|s| s.as_str())); - let features = features.into_iter().collect::(); + let features = if is_already_sorted { + features.into_iter().sorted().collect::() + } else { + features.into_iter().collect::() + }; table.set_dotted(false); overwrite_value(table, "features", features); } else { diff --git a/tests/testsuite/cargo_add/overwrite_inline_features/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_inline_features/out/Cargo.toml index 3e6bbc6f43f..861ed065c31 100644 --- a/tests/testsuite/cargo_add/overwrite_inline_features/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_inline_features/out/Cargo.toml @@ -8,5 +8,5 @@ edition = "2015" [dependencies] unrelateed-crate = "99999.0.0" # Before your-face -your-face = { version = "99999.0.0", features = ["eyes", "nose", "mouth", "ears"] } # After your-face +your-face = { version = "99999.0.0", features = ["ears", "eyes", "mouth", "nose"] } # After your-face # End diff --git a/tests/testsuite/cargo_add/preserve_features_sorted/out/Cargo.toml b/tests/testsuite/cargo_add/preserve_features_sorted/out/Cargo.toml index cd8000d8c1f..23526c7faaa 100644 --- a/tests/testsuite/cargo_add/preserve_features_sorted/out/Cargo.toml +++ b/tests/testsuite/cargo_add/preserve_features_sorted/out/Cargo.toml @@ -6,4 +6,4 @@ version = "0.0.0" edition = "2015" [dependencies] -my-package = { version = "99999.0.0", features = ["a", "b", "c", "e", "d"] } +my-package = { version = "99999.0.0", features = ["a", "b", "c", "d", "e"] }