]> git.proxmox.com Git - cargo.git/commitdiff
test(add): Ensure comments are preserved
authorEd Page <eopage@gmail.com>
Mon, 11 Jul 2022 21:14:34 +0000 (16:14 -0500)
committerEd Page <eopage@gmail.com>
Wed, 13 Jul 2022 18:38:10 +0000 (13:38 -0500)
A comment on killercup/cargo-edit#15 had me worried that `cargo add` was
deleting comments now.  It appears that isn't the case for inline
tables.

Standard tables however do delete comments.  The work to make sure they
don't conflicts with another need.  When changing the source, we delete
the old source fields and append the new which can cause some formatting
to be carried over unnecessarily.

For example, what would normally look like
```toml
cargo-list-test-fixture-dependency = { optional = true, path = "../dependency", version = "0.0.0" }
```
When fixed to preserve comments with my naive solution looks like
```toml
cargo-list-test-fixture-dependency = { optional = true , path = "../dependency", version = "0.0.0" }
```
Note that `optional = true` used to be last, so space separating it and
`}` was kept, now separating it and `,`.

More work will be needed to get this into an ideal state but we can at
least have confidence with inline tables for now.

tests/testsuite/cargo_add/mod.rs
tests/testsuite/cargo_add/overwrite_preserves_inline_table/in/Cargo.toml [new file with mode: 0644]
tests/testsuite/cargo_add/overwrite_preserves_inline_table/in/src/lib.rs [new file with mode: 0644]
tests/testsuite/cargo_add/overwrite_preserves_inline_table/mod.rs [new file with mode: 0644]
tests/testsuite/cargo_add/overwrite_preserves_inline_table/out/Cargo.toml [new file with mode: 0644]
tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log [new file with mode: 0644]
tests/testsuite/cargo_add/overwrite_preserves_inline_table/stdout.log [new file with mode: 0644]

index 75213d9789d567a3333f68a2bb4977f49f1b8f18..1eb682ef2ed969f45d9a0ac316534ac64e706e95 100644 (file)
@@ -76,6 +76,7 @@ mod overwrite_optional;
 mod overwrite_optional_with_no_optional;
 mod overwrite_path_noop;
 mod overwrite_path_with_version;
+mod overwrite_preserves_inline_table;
 mod overwrite_rename_with_no_rename;
 mod overwrite_rename_with_rename;
 mod overwrite_rename_with_rename_noop;
diff --git a/tests/testsuite/cargo_add/overwrite_preserves_inline_table/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/in/Cargo.toml
new file mode 100644 (file)
index 0000000..3dddbbd
--- /dev/null
@@ -0,0 +1,8 @@
+[workspace]
+
+[package]
+name = "cargo-list-test-fixture"
+version = "0.0.0"
+
+[dependencies]
+your-face={version="99999.0.0",features=["eyes"]}  # Hello world
diff --git a/tests/testsuite/cargo_add/overwrite_preserves_inline_table/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/in/src/lib.rs
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/testsuite/cargo_add/overwrite_preserves_inline_table/mod.rs b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/mod.rs
new file mode 100644 (file)
index 0000000..1faf5de
--- /dev/null
@@ -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 overwrite_preserves_inline_table() {
+    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("your-face --features nose")
+        .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/overwrite_preserves_inline_table/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/out/Cargo.toml
new file mode 100644 (file)
index 0000000..f204a89
--- /dev/null
@@ -0,0 +1,8 @@
+[workspace]
+
+[package]
+name = "cargo-list-test-fixture"
+version = "0.0.0"
+
+[dependencies]
+your-face={ version = "99999.0.0", features = ["eyes", "nose"] }  # Hello world
diff --git a/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stderr.log
new file mode 100644 (file)
index 0000000..6154590
--- /dev/null
@@ -0,0 +1,7 @@
+    Updating `dummy-registry` index
+      Adding your-face v99999.0.0 to dependencies.
+             Features:
+             + eyes
+             + nose
+             - ears
+             - mouth
diff --git a/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stdout.log b/tests/testsuite/cargo_add/overwrite_preserves_inline_table/stdout.log
new file mode 100644 (file)
index 0000000..e69de29