Skip to content

Commit

Permalink
Auto merge of #11612 - hi-rustin:rustin-patch-cargo-add, r=epage
Browse files Browse the repository at this point in the history
`cargo add` check `[dependencies]` order without considering the dotted item

### What does this PR try to resolve?

Try to close #11584

`cargo check` check `[dependencies]` order without considering the dotted item.

### How should we test and review this PR?
See the unit test.
  • Loading branch information
bors committed Jan 25, 2023
2 parents 3fee600 + 1d67d0f commit 04519ad
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
.get_table(&dep_table)
.map(TomlItem::as_table)
.map_or(true, |table_option| {
table_option.map_or(true, |table| is_sorted(table.iter().map(|(name, _)| name)))
table_option.map_or(true, |table| {
is_sorted(table.get_values().iter_mut().map(|(key, _)| {
// get_values key paths always have at least one key.
key.remove(0)
}))
})
});
for dep in deps {
print_action_msg(&mut options.config.shell(), &dep, &dep_table)?;
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ mod quiet;
mod registry;
mod rename;
mod require_weak;
mod sorted_table_with_dotted_item;
mod target;
mod target_cfg;
mod unknown_inherited_feature;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
toml = "0.1.1"
versioned-package = "0.1.1"

[dependencies.my-build-package1]
version = "0.1.1"

Empty file.
25 changes: 25 additions & 0 deletions tests/testsuite/cargo_add/sorted_table_with_dotted_item/mod.rs
Original file line number Diff line number Diff line change
@@ -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 sorted_table_with_dotted_item() {
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("unrelateed-crate")
.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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
toml = "0.1.1"
unrelateed-crate = "99999.0.0"
versioned-package = "0.1.1"

[dependencies.my-build-package1]
version = "0.1.1"

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Updating `dummy-registry` index
Adding unrelateed-crate v99999.0.0 to dependencies.
Empty file.

0 comments on commit 04519ad

Please sign in to comment.