Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support overwriting a foo.workspace = true with a dependency from a… #4

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = []
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"] }
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.
46 changes: 46 additions & 0 deletions tests/testsuite/cargo_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,52 @@ fn overwrite_with_rename() {
);
}

#[cargo_test]
fn overwrite_workspace_dep() {
init_registry();
let project = Project::from_template("tests/snapshots/add/overwrite_workspace_dep.in");
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo()
.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 = Project::from_template("tests/snapshots/add/overwrite_workspace_dep_features.in");
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo()
.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