Skip to content

Commit

Permalink
Support overwriting a foo.workspace = true with a dependency from a…
Browse files Browse the repository at this point in the history
… different source
  • Loading branch information
Muscraft authored and epage committed Apr 27, 2022
1 parent cf2202c commit aa7d116
Show file tree
Hide file tree
Showing 23 changed files with 158 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/cargo/ops/cargo_add/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};

use indexmap::IndexSet;
use toml_edit::KeyMut;

use super::manifest::str_or_1_len_table;
use crate::core::FeatureMap;
Expand Down Expand Up @@ -476,10 +477,18 @@ impl Dependency {
}

/// Modify existing entry to match this dependency
pub fn update_toml(&self, crate_root: &Path, item: &mut toml_edit::Item) {
pub fn update_toml<'k>(
&self,
crate_root: &Path,
key: &mut KeyMut<'k>,
item: &mut toml_edit::Item,
) {
if str_or_1_len_table(item) {
// Nothing to preserve
*item = self.to_toml(crate_root);
if self.source != Some(Source::Workspace(WorkspaceSource)) {
key.fmt();
}
} else if let Some(table) = item.as_table_like_mut() {
match &self.source {
Some(Source::Registry(src)) => {
Expand Down
8 changes: 6 additions & 2 deletions src/cargo/ops/cargo_add/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,12 @@ impl LocalManifest {
let dep_key = dep.toml_key();

let table = self.get_table_mut(table_path)?;
if let Some(dep_item) = table.as_table_like_mut().unwrap().get_mut(dep_key) {
dep.update_toml(&crate_root, dep_item);
if let Some((mut dep_key, dep_item)) = table
.as_table_like_mut()
.unwrap()
.get_key_value_mut(dep_key)
{
dep.update_toml(&crate_root, &mut dep_key, dep_item);
} else {
let new_dependency = dep.to_toml(&crate_root);
table[dep_key] = new_dependency;
Expand Down
5 changes: 5 additions & 0 deletions tests/snapshots/add/overwrite_workspace_dep.in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "foo"
version = "0.0.0"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"

[dependencies]
foo.workspace = true
Empty file.
5 changes: 5 additions & 0 deletions tests/snapshots/add/overwrite_workspace_dep.out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "foo"
version = "0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"

[dependencies]
foo = { version = "0.0.0", path = "../dependency" }
1 change: 1 addition & 0 deletions tests/snapshots/add/overwrite_workspace_dep.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding foo (local) to dependencies.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "foo"
version = "0.0.0"

[features]
default-base = []
default-test-base = []
default-merge-base = []
default = ["default-base", "default-test-base", "default-merge-base"]
test-base = []
test = ["test-base", "default-test-base"]
merge-base = []
merge = ["merge-base", "default-merge-base"]
unrelated = []
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"

[dependencies]
foo = { workspace = true, features = ["test"] }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "foo"
version = "0.0.0"

[features]
default-base = []
default-test-base = []
default-merge-base = []
default = ["default-base", "default-test-base", "default-merge-base"]
test-base = []
test = ["test-base", "default-test-base"]
merge-base = []
merge = ["merge-base", "default-merge-base"]
unrelated = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"

[dependencies]
foo = { features = ["test"], path = "../dependency", version = "0.0.0" }
10 changes: 10 additions & 0 deletions tests/snapshots/add/overwrite_workspace_dep_features.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Adding foo (local) to dependencies.
Features:
+ default-base
+ default-merge-base
+ default-test-base
+ test
+ test-base
- merge
- merge-base
- unrelated
Empty file.
45 changes: 45 additions & 0 deletions tests/testsuite/cargo_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,51 @@ fn overwrite_with_rename() {
);
}

#[cargo_test]
fn overwrite_workspace_dep() {
init_registry();
let project_root = project_from_template("tests/snapshots/add/overwrite_workspace_dep.in");
let cwd = &project_root;

cargo_command()
.masquerade_as_nightly_cargo()
.arg("add")
.args(["foo", "--path", "./dependency", "-p", "bar"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path("tests/snapshots/add/overwrite_workspace_dep.stdout")
.stderr_matches_path("tests/snapshots/add/overwrite_workspace_dep.stderr");

assert().subset_matches(
"tests/snapshots/add/overwrite_workspace_dep.out",
&project_root,
);
}

#[cargo_test]
fn overwrite_workspace_dep_features() {
init_registry();
let project_root =
project_from_template("tests/snapshots/add/overwrite_workspace_dep_features.in");
let cwd = &project_root;

cargo_command()
.masquerade_as_nightly_cargo()
.arg("add")
.args(["foo", "--path", "./dependency", "-p", "bar"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path("tests/snapshots/add/overwrite_workspace_dep_features.stdout")
.stderr_matches_path("tests/snapshots/add/overwrite_workspace_dep_features.stderr");

assert().subset_matches(
"tests/snapshots/add/overwrite_workspace_dep_features.out",
&project_root,
);
}

#[cargo_test]
fn preserve_sorted() {
init_registry();
Expand Down

0 comments on commit aa7d116

Please sign in to comment.