Skip to content

Commit

Permalink
Detect cargo add foo will inherit a workspace dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Apr 27, 2022
1 parent 0116526 commit 7914967
Show file tree
Hide file tree
Showing 32 changed files with 198 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use dependency::RegistrySource;
use dependency::Source;
use manifest::LocalManifest;

use crate::ops::cargo_add::dependency::MaybeWorkspace;
use crate::ops::cargo_add::dependency::{MaybeWorkspace, WorkspaceSource};
pub use manifest::DepTable;

/// Information on what dependencies should be added
Expand Down Expand Up @@ -281,7 +281,11 @@ fn resolve_dependency(
};

if dependency.source().is_none() {
if let Some(package) = ws.members().find(|p| p.name().as_str() == dependency.name) {
// Checking for a workspace dependency happens first since a member could be specified
// in the workspace dependencies table as a dependency
if let Some(_dep) = find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok() {
dependency = dependency.set_source(WorkspaceSource::new());
} else if let Some(package) = ws.members().find(|p| p.name().as_str() == dependency.name) {
// Only special-case workspaces when the user doesn't provide any extra
// information, otherwise, trust the user.
let mut src = PathSource::new(package.root());
Expand Down
5 changes: 5 additions & 0 deletions tests/snapshots/add/detect_workspace_inherit.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,5 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"
Empty file.
5 changes: 5 additions & 0 deletions tests/snapshots/add/detect_workspace_inherit.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.workspace = true
1 change: 1 addition & 0 deletions tests/snapshots/add/detect_workspace_inherit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding foo (workspace) 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", features = ["merge"] }
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,5 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"
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", features = ["merge"] }
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"] }
10 changes: 10 additions & 0 deletions tests/snapshots/add/detect_workspace_inherit_features.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Adding foo (workspace) to dependencies.
Features:
+ default-base
+ default-merge-base
+ default-test-base
+ merge
+ merge-base
+ test
+ test-base
- unrelated
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,3 @@
[package]
name = "foo"
version = "0.0.0"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"
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,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 = { workspace = true, optional = true }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding foo (workspace) to optional dependencies.
Empty file.
71 changes: 71 additions & 0 deletions tests/testsuite/cargo_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,77 @@ fn require_weak() {
assert().subset_matches("tests/snapshots/add/require_weak.out", &project_root);
}

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

snapbox::cmd::Command::cargo()
.masquerade_as_nightly_cargo()
.arg("add")
.args(["foo", "-p", "bar"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path("tests/snapshots/add/detect_workspace_inherit.stdout")
.stderr_matches_path("tests/snapshots/add/detect_workspace_inherit.stderr");

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

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

snapbox::cmd::Command::cargo()
.masquerade_as_nightly_cargo()
.arg("add")
.args(["foo", "-p", "bar", "--features", "test"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path("tests/snapshots/add/detect_workspace_inherit_features.stdout")
.stderr_matches_path("tests/snapshots/add/detect_workspace_inherit_features.stderr");

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

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

snapbox::cmd::Command::cargo()
.masquerade_as_nightly_cargo()
.arg("add")
.args(["foo", "-p", "bar", "--optional"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path("tests/snapshots/add/detect_workspace_inherit_optional.stdout")
.stderr_matches_path("tests/snapshots/add/detect_workspace_inherit_optional.stderr");

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

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

0 comments on commit 7914967

Please sign in to comment.