Skip to content

Commit b78f918

Browse files
committed
test(add): Ensure comments are preserved
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.
1 parent 92ff479 commit b78f918

File tree

7 files changed

+49
-0
lines changed

7 files changed

+49
-0
lines changed

tests/testsuite/cargo_add/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ mod overwrite_optional;
7676
mod overwrite_optional_with_no_optional;
7777
mod overwrite_path_noop;
7878
mod overwrite_path_with_version;
79+
mod overwrite_preserves_inline_table;
7980
mod overwrite_rename_with_no_rename;
8081
mod overwrite_rename_with_rename;
8182
mod overwrite_rename_with_rename_noop;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[workspace]
2+
3+
[package]
4+
name = "cargo-list-test-fixture"
5+
version = "0.0.0"
6+
7+
[dependencies]
8+
your-face={version="99999.0.0",features=["eyes"]} # Hello world

tests/testsuite/cargo_add/overwrite_preserves_inline_table/in/src/lib.rs

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::prelude::*;
3+
use cargo_test_support::Project;
4+
5+
use crate::cargo_add::init_registry;
6+
use cargo_test_support::curr_dir;
7+
8+
#[cargo_test]
9+
fn overwrite_preserves_inline_table() {
10+
init_registry();
11+
let project = Project::from_template(curr_dir!().join("in"));
12+
let project_root = project.root();
13+
let cwd = &project_root;
14+
15+
snapbox::cmd::Command::cargo_ui()
16+
.arg("add")
17+
.arg_line("your-face --features nose")
18+
.current_dir(cwd)
19+
.assert()
20+
.success()
21+
.stdout_matches_path(curr_dir!().join("stdout.log"))
22+
.stderr_matches_path(curr_dir!().join("stderr.log"));
23+
24+
assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[workspace]
2+
3+
[package]
4+
name = "cargo-list-test-fixture"
5+
version = "0.0.0"
6+
7+
[dependencies]
8+
your-face={ version = "99999.0.0", features = ["eyes", "nose"] } # Hello world
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Updating `dummy-registry` index
2+
Adding your-face v99999.0.0 to dependencies.
3+
Features:
4+
+ eyes
5+
+ nose
6+
- ears
7+
- mouth

tests/testsuite/cargo_add/overwrite_preserves_inline_table/stdout.log

Whitespace-only changes.

0 commit comments

Comments
 (0)