From fe5c0a3039c0651dec90ddb70e4cd15e078e6ba1 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Fri, 24 Nov 2023 14:50:58 +0800 Subject: [PATCH 01/10] Add --public support for `cargo add` --- src/bin/cargo/commands/add.rs | 18 +++++++++++++++++ src/cargo/ops/cargo_add/mod.rs | 13 ++++++++++++ src/cargo/util/toml_mut/dependency.rs | 29 +++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/bin/cargo/commands/add.rs b/src/bin/cargo/commands/add.rs index 05b0fc97560..f2913393dbb 100644 --- a/src/bin/cargo/commands/add.rs +++ b/src/bin/cargo/commands/add.rs @@ -76,6 +76,18 @@ Example uses: "ignore-rust-version", "Ignore `rust-version` specification in packages (unstable)" ), + flag("public", "Mark the dependency as public") + .long_help("Mark the dependency as public + +The dependnecy will be visible in both of your crate and outside."), + flag("no-public", "Mark the dependency as private") + .conflicts_with("dev") + .long_help("Mark the dependency as private + +The dependnecy will be only visible in your crate other than outside.") + .conflicts_with("dev") + .overrides_with("public"), + ]) .arg_manifest_path_without_unsupported_path_tip() .arg_package("Package to modify") @@ -235,6 +247,7 @@ fn parse_dependencies(config: &Config, matches: &ArgMatches) -> CargoResult("crates") @@ -325,6 +338,7 @@ fn parse_dependencies(config: &Config, matches: &ArgMatches) -> CargoResult Option { resolve_bool_arg(matches.flag("optional"), matches.flag("no-optional")) } +fn public(matches: &ArgMatches) -> Option { + resolve_bool_arg(matches.flag("public"), matches.flag("no-public")) +} + fn resolve_bool_arg(yes: bool, no: bool) -> Option { match (yes, no) { (true, false) => Some(true), diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index 5afc8d0c9d8..b4f64fd8090 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -244,6 +244,9 @@ pub struct DepOp { /// Whether dependency is optional pub optional: Option, + /// Whether dependency is public + pub public: Option, + /// Registry for looking up dependency version pub registry: Option, @@ -758,6 +761,13 @@ fn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency { dependency.optional = None; } } + if let Some(value) = arg.public { + if value { + dependency.public = Some(true); + } else { + dependency.public = None; + } + } if let Some(value) = arg.default_features { if value { dependency.default_features = None; @@ -945,6 +955,9 @@ fn print_action_msg(shell: &mut Shell, dep: &DependencyUI, section: &[String]) - if dep.optional().unwrap_or(false) { write!(message, " optional")?; } + if dep.public().unwrap_or(false) { + write!(message, " public")?; + } let section = if section.len() == 1 { section[0].clone() } else { diff --git a/src/cargo/util/toml_mut/dependency.rs b/src/cargo/util/toml_mut/dependency.rs index 88298fa8d54..7e775b5151e 100644 --- a/src/cargo/util/toml_mut/dependency.rs +++ b/src/cargo/util/toml_mut/dependency.rs @@ -25,6 +25,9 @@ pub struct Dependency { /// Whether the dependency is opted-in with a feature flag. pub optional: Option, + /// Whether the dependency is marked as public. + pub public: Option, + /// List of features to add (or None to keep features unchanged). pub features: Option>, /// Whether default features are enabled. @@ -54,6 +57,7 @@ impl Dependency { source: None, registry: None, rename: None, + public: None, } } @@ -163,6 +167,11 @@ impl Dependency { self.optional } + /// Get whether the dep is public. + pub fn public(&self) -> Option { + self.public + } + /// Get the SourceID for this dependency. pub fn source_id(&self, config: &Config) -> CargoResult> { match &self.source.as_ref() { @@ -325,6 +334,7 @@ impl Dependency { }; let optional = table.get("optional").and_then(|v| v.as_bool()); + let public = table.get("public").and_then(|v| v.as_bool()); let dep = Self { name, @@ -334,6 +344,7 @@ impl Dependency { default_features, features, optional, + public, inherited_features: None, }; Ok(dep) @@ -366,6 +377,7 @@ impl Dependency { crate_root.display() ); let table: toml_edit::Item = match ( + self.public.unwrap_or(false), self.optional.unwrap_or(false), self.features.as_ref(), self.default_features.unwrap_or(true), @@ -375,6 +387,7 @@ impl Dependency { ) { // Extra short when version flag only ( + false, false, None, true, @@ -382,14 +395,14 @@ impl Dependency { None, None, ) => toml_edit::value(v), - (false, None, true, Some(Source::Workspace(WorkspaceSource {})), None, None) => { + (false, false, None, true, Some(Source::Workspace(WorkspaceSource {})), None, None) => { let mut table = toml_edit::InlineTable::default(); table.set_dotted(true); table.insert("workspace", true.into()); toml_edit::value(toml_edit::Value::InlineTable(table)) } // Other cases are represented as an inline table - (_, _, _, _, _, _) => { + (_, _, _, _, _, _, _) => { let mut table = toml_edit::InlineTable::default(); match &self.source { @@ -442,6 +455,9 @@ impl Dependency { if let Some(v) = self.optional { table.insert("optional", v.into()); } + if let Some(v) = self.public { + table.insert("public", v.into()); + } toml_edit::value(toml_edit::Value::InlineTable(table)) } @@ -579,6 +595,15 @@ impl Dependency { table.remove("optional"); } } + match self.public { + Some(v) => { + table.set_dotted(false); + overwrite_value(table, "public", v); + } + None => { + table.remove("public"); + } + } } else { unreachable!("Invalid dependency type: {}", item.type_name()); } From a1a75e81c96d403a23b1525e2e99419f5d070e08 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Fri, 24 Nov 2023 14:56:32 +0800 Subject: [PATCH 02/10] Update `cargo add --help` testcase --- tests/testsuite/cargo_add/help/stdout.log | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/testsuite/cargo_add/help/stdout.log b/tests/testsuite/cargo_add/help/stdout.log index 413e36e548d..6530be11b96 100644 --- a/tests/testsuite/cargo_add/help/stdout.log +++ b/tests/testsuite/cargo_add/help/stdout.log @@ -42,6 +42,16 @@ Options: --ignore-rust-version Ignore `rust-version` specification in packages (unstable) + --public + Mark the dependency as public + + The dependnecy will be visible in both of your crate and outside. + + --no-public + Mark the dependency as private + + The dependnecy will be only visible in your crate other than outside. + -n, --dry-run Don't actually write the manifest From 7c8d997e26c310140423b43c212c8a70269dd68b Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Fri, 24 Nov 2023 15:11:51 +0800 Subject: [PATCH 03/10] Add overwrite testcase for `cargo add --public` --- tests/testsuite/cargo_add/mod.rs | 4 ++ .../overwrite_no_public/in/Cargo.toml | 10 +++++ .../overwrite_no_public/in/src/lib.rs | 0 .../cargo_add/overwrite_no_public/mod.rs | 39 +++++++++++++++++++ .../overwrite_no_public/out/Cargo.toml | 10 +++++ .../cargo_add/overwrite_no_public/stderr.log | 3 ++ .../cargo_add/overwrite_no_public/stdout.log | 0 .../in/Cargo.toml | 10 +++++ .../in/src/lib.rs | 0 .../overwrite_no_public_with_public/mod.rs | 39 +++++++++++++++++++ .../out/Cargo.toml | 10 +++++ .../stderr.log | 3 ++ .../stdout.log | 0 .../cargo_add/overwrite_public/in/Cargo.toml | 10 +++++ .../cargo_add/overwrite_public/in/src/lib.rs | 0 .../cargo_add/overwrite_public/mod.rs | 39 +++++++++++++++++++ .../cargo_add/overwrite_public/out/Cargo.toml | 10 +++++ .../cargo_add/overwrite_public/stderr.log | 3 ++ .../cargo_add/overwrite_public/stdout.log | 0 .../in/Cargo.toml | 10 +++++ .../in/src/lib.rs | 0 .../overwrite_public_with_no_public/mod.rs | 39 +++++++++++++++++++ .../out/Cargo.toml | 10 +++++ .../stderr.log | 3 ++ .../stdout.log | 0 25 files changed, 252 insertions(+) create mode 100644 tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml create mode 100644 tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/overwrite_no_public/mod.rs create mode 100644 tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/overwrite_no_public/stderr.log create mode 100644 tests/testsuite/cargo_add/overwrite_no_public/stdout.log create mode 100644 tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml create mode 100644 tests/testsuite/cargo_add/overwrite_no_public_with_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs create mode 100644 tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/overwrite_no_public_with_public/stderr.log create mode 100644 tests/testsuite/cargo_add/overwrite_no_public_with_public/stdout.log create mode 100644 tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml create mode 100644 tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/overwrite_public/mod.rs create mode 100644 tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/overwrite_public/stderr.log create mode 100644 tests/testsuite/cargo_add/overwrite_public/stdout.log create mode 100644 tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml create mode 100644 tests/testsuite/cargo_add/overwrite_public_with_no_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/overwrite_public_with_no_public/mod.rs create mode 100644 tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/overwrite_public_with_no_public/stderr.log create mode 100644 tests/testsuite/cargo_add/overwrite_public_with_no_public/stdout.log diff --git a/tests/testsuite/cargo_add/mod.rs b/tests/testsuite/cargo_add/mod.rs index e8633b0c43a..ba6ead6d36e 100644 --- a/tests/testsuite/cargo_add/mod.rs +++ b/tests/testsuite/cargo_add/mod.rs @@ -69,6 +69,10 @@ mod offline_empty_cache; mod optional; mod overwrite_default_features; mod overwrite_default_features_with_no_default_features; +mod overwrite_no_public; +mod overwrite_no_public_with_public; +mod overwrite_public; +mod overwrite_public_with_no_public; mod overwrite_features; mod overwrite_git_with_path; mod overwrite_inherit_features_noop; diff --git a/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml new file mode 100644 index 00000000000..3cc396220a8 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = "99999.0.0" +my-package2 = "0.4.1" diff --git a/tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_no_public/mod.rs b/tests/testsuite/cargo_add/overwrite_no_public/mod.rs new file mode 100644 index 00000000000..43664b57af2 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_no_public/mod.rs @@ -0,0 +1,39 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +use cargo_test_support::curr_dir; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + for name in ["my-package1", "my-package2"] { + for ver in [ + "0.1.1+my-package", + "0.2.0+my-package", + "0.2.3+my-package", + "0.4.1+my-package", + "20.0.0+my-package", + "99999.0.0+my-package", + "99999.0.0-alpha.1+my-package", + ] { + cargo_test_support::registry::Package::new(name, ver).publish(); + } + } + + 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("my-package1 my-package2@0.4.1 --no-public") + .current_dir(cwd) + .masquerade_as_nightly_cargo(&["public-dependency"]) + .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_no_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml new file mode 100644 index 00000000000..3cc396220a8 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = "99999.0.0" +my-package2 = "0.4.1" diff --git a/tests/testsuite/cargo_add/overwrite_no_public/stderr.log b/tests/testsuite/cargo_add/overwrite_no_public/stderr.log new file mode 100644 index 00000000000..fb8d4903d7b --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_no_public/stderr.log @@ -0,0 +1,3 @@ + Updating `dummy-registry` index + Adding my-package1 v99999.0.0 to dependencies. + Adding my-package2 v0.4.1 to dependencies. diff --git a/tests/testsuite/cargo_add/overwrite_no_public/stdout.log b/tests/testsuite/cargo_add/overwrite_no_public/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml new file mode 100644 index 00000000000..2f3b348dfba --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = { version = "99999.0.0", public = false } +my-package2 = { version = "0.4.1", public = false } diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs b/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs new file mode 100644 index 00000000000..c8c2e3cf585 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs @@ -0,0 +1,39 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +use cargo_test_support::curr_dir; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + for name in ["my-package1", "my-package2"] { + for ver in [ + "0.1.1+my-package", + "0.2.0+my-package", + "0.2.3+my-package", + "0.4.1+my-package", + "20.0.0+my-package", + "99999.0.0+my-package", + "99999.0.0-alpha.1+my-package", + ] { + cargo_test_support::registry::Package::new(name, ver).publish(); + } + } + + 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("my-package1 my-package2@0.4.1 --public") + .current_dir(cwd) + .masquerade_as_nightly_cargo(&["public-dependency"]) + .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_no_public_with_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml new file mode 100644 index 00000000000..0a3d744cb6b --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = { version = "99999.0.0", public = true } +my-package2 = { version = "0.4.1", public = true } diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/stderr.log b/tests/testsuite/cargo_add/overwrite_no_public_with_public/stderr.log new file mode 100644 index 00000000000..04d71318dd6 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/stderr.log @@ -0,0 +1,3 @@ + Updating `dummy-registry` index + Adding my-package1 v99999.0.0 to public dependencies. + Adding my-package2 v0.4.1 to public dependencies. diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/stdout.log b/tests/testsuite/cargo_add/overwrite_no_public_with_public/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml new file mode 100644 index 00000000000..3cc396220a8 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = "99999.0.0" +my-package2 = "0.4.1" diff --git a/tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_public/mod.rs b/tests/testsuite/cargo_add/overwrite_public/mod.rs new file mode 100644 index 00000000000..c8c2e3cf585 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_public/mod.rs @@ -0,0 +1,39 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +use cargo_test_support::curr_dir; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + for name in ["my-package1", "my-package2"] { + for ver in [ + "0.1.1+my-package", + "0.2.0+my-package", + "0.2.3+my-package", + "0.4.1+my-package", + "20.0.0+my-package", + "99999.0.0+my-package", + "99999.0.0-alpha.1+my-package", + ] { + cargo_test_support::registry::Package::new(name, ver).publish(); + } + } + + 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("my-package1 my-package2@0.4.1 --public") + .current_dir(cwd) + .masquerade_as_nightly_cargo(&["public-dependency"]) + .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_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml new file mode 100644 index 00000000000..0a3d744cb6b --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = { version = "99999.0.0", public = true } +my-package2 = { version = "0.4.1", public = true } diff --git a/tests/testsuite/cargo_add/overwrite_public/stderr.log b/tests/testsuite/cargo_add/overwrite_public/stderr.log new file mode 100644 index 00000000000..04d71318dd6 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_public/stderr.log @@ -0,0 +1,3 @@ + Updating `dummy-registry` index + Adding my-package1 v99999.0.0 to public dependencies. + Adding my-package2 v0.4.1 to public dependencies. diff --git a/tests/testsuite/cargo_add/overwrite_public/stdout.log b/tests/testsuite/cargo_add/overwrite_public/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml new file mode 100644 index 00000000000..0a3d744cb6b --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = { version = "99999.0.0", public = true } +my-package2 = { version = "0.4.1", public = true } diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/mod.rs b/tests/testsuite/cargo_add/overwrite_public_with_no_public/mod.rs new file mode 100644 index 00000000000..43664b57af2 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/mod.rs @@ -0,0 +1,39 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +use cargo_test_support::curr_dir; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + for name in ["my-package1", "my-package2"] { + for ver in [ + "0.1.1+my-package", + "0.2.0+my-package", + "0.2.3+my-package", + "0.4.1+my-package", + "20.0.0+my-package", + "99999.0.0+my-package", + "99999.0.0-alpha.1+my-package", + ] { + cargo_test_support::registry::Package::new(name, ver).publish(); + } + } + + 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("my-package1 my-package2@0.4.1 --no-public") + .current_dir(cwd) + .masquerade_as_nightly_cargo(&["public-dependency"]) + .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_public_with_no_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml new file mode 100644 index 00000000000..e6bb86566b8 --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = { version = "99999.0.0" } +my-package2 = { version = "0.4.1" } diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/stderr.log b/tests/testsuite/cargo_add/overwrite_public_with_no_public/stderr.log new file mode 100644 index 00000000000..fb8d4903d7b --- /dev/null +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/stderr.log @@ -0,0 +1,3 @@ + Updating `dummy-registry` index + Adding my-package1 v99999.0.0 to dependencies. + Adding my-package2 v0.4.1 to dependencies. diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/stdout.log b/tests/testsuite/cargo_add/overwrite_public_with_no_public/stdout.log new file mode 100644 index 00000000000..e69de29bb2d From 9ac30323b342e313ca73ed35004e8f9459b7a4ee Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Fri, 24 Nov 2023 15:23:48 +0800 Subject: [PATCH 04/10] Add not overwrite testcase for `cargo add --public` --- tests/testsuite/cargo_add/mod.rs | 10 +++-- .../cargo_add/no_public/in/Cargo.toml | 6 +++ .../cargo_add/no_public/in/src/lib.rs | 0 tests/testsuite/cargo_add/no_public/mod.rs | 39 +++++++++++++++++++ .../cargo_add/no_public/out/Cargo.toml | 10 +++++ .../testsuite/cargo_add/no_public/stderr.log | 3 ++ .../testsuite/cargo_add/no_public/stdout.log | 0 tests/testsuite/cargo_add/public/in | 1 + tests/testsuite/cargo_add/public/mod.rs | 39 +++++++++++++++++++ .../testsuite/cargo_add/public/out/Cargo.toml | 10 +++++ tests/testsuite/cargo_add/public/stderr.log | 3 ++ tests/testsuite/cargo_add/public/stdout.log | 0 12 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 tests/testsuite/cargo_add/no_public/in/Cargo.toml create mode 100644 tests/testsuite/cargo_add/no_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/no_public/mod.rs create mode 100644 tests/testsuite/cargo_add/no_public/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/no_public/stderr.log create mode 100644 tests/testsuite/cargo_add/no_public/stdout.log create mode 120000 tests/testsuite/cargo_add/public/in create mode 100644 tests/testsuite/cargo_add/public/mod.rs create mode 100644 tests/testsuite/cargo_add/public/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/public/stderr.log create mode 100644 tests/testsuite/cargo_add/public/stdout.log diff --git a/tests/testsuite/cargo_add/mod.rs b/tests/testsuite/cargo_add/mod.rs index ba6ead6d36e..2da7e4c5242 100644 --- a/tests/testsuite/cargo_add/mod.rs +++ b/tests/testsuite/cargo_add/mod.rs @@ -65,14 +65,11 @@ mod namever; mod no_args; mod no_default_features; mod no_optional; +mod no_public; mod offline_empty_cache; mod optional; mod overwrite_default_features; mod overwrite_default_features_with_no_default_features; -mod overwrite_no_public; -mod overwrite_no_public_with_public; -mod overwrite_public; -mod overwrite_public_with_no_public; mod overwrite_features; mod overwrite_git_with_path; mod overwrite_inherit_features_noop; @@ -85,11 +82,15 @@ mod overwrite_no_default_features; mod overwrite_no_default_features_with_default_features; mod overwrite_no_optional; mod overwrite_no_optional_with_optional; +mod overwrite_no_public; +mod overwrite_no_public_with_public; 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_public; +mod overwrite_public_with_no_public; mod overwrite_rename_with_no_rename; mod overwrite_rename_with_rename; mod overwrite_rename_with_rename_noop; @@ -107,6 +108,7 @@ mod preserve_dep_std_table; mod preserve_features_table; mod preserve_sorted; mod preserve_unsorted; +mod public; mod quiet; mod registry; mod rename; diff --git a/tests/testsuite/cargo_add/no_public/in/Cargo.toml b/tests/testsuite/cargo_add/no_public/in/Cargo.toml new file mode 100644 index 00000000000..e9087535be3 --- /dev/null +++ b/tests/testsuite/cargo_add/no_public/in/Cargo.toml @@ -0,0 +1,6 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" diff --git a/tests/testsuite/cargo_add/no_public/in/src/lib.rs b/tests/testsuite/cargo_add/no_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/no_public/mod.rs b/tests/testsuite/cargo_add/no_public/mod.rs new file mode 100644 index 00000000000..43664b57af2 --- /dev/null +++ b/tests/testsuite/cargo_add/no_public/mod.rs @@ -0,0 +1,39 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +use cargo_test_support::curr_dir; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + for name in ["my-package1", "my-package2"] { + for ver in [ + "0.1.1+my-package", + "0.2.0+my-package", + "0.2.3+my-package", + "0.4.1+my-package", + "20.0.0+my-package", + "99999.0.0+my-package", + "99999.0.0-alpha.1+my-package", + ] { + cargo_test_support::registry::Package::new(name, ver).publish(); + } + } + + 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("my-package1 my-package2@0.4.1 --no-public") + .current_dir(cwd) + .masquerade_as_nightly_cargo(&["public-dependency"]) + .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/no_public/out/Cargo.toml b/tests/testsuite/cargo_add/no_public/out/Cargo.toml new file mode 100644 index 00000000000..3cc396220a8 --- /dev/null +++ b/tests/testsuite/cargo_add/no_public/out/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = "99999.0.0" +my-package2 = "0.4.1" diff --git a/tests/testsuite/cargo_add/no_public/stderr.log b/tests/testsuite/cargo_add/no_public/stderr.log new file mode 100644 index 00000000000..fb8d4903d7b --- /dev/null +++ b/tests/testsuite/cargo_add/no_public/stderr.log @@ -0,0 +1,3 @@ + Updating `dummy-registry` index + Adding my-package1 v99999.0.0 to dependencies. + Adding my-package2 v0.4.1 to dependencies. diff --git a/tests/testsuite/cargo_add/no_public/stdout.log b/tests/testsuite/cargo_add/no_public/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/public/in b/tests/testsuite/cargo_add/public/in new file mode 120000 index 00000000000..6c6a27fcfb5 --- /dev/null +++ b/tests/testsuite/cargo_add/public/in @@ -0,0 +1 @@ +../add-basic.in \ No newline at end of file diff --git a/tests/testsuite/cargo_add/public/mod.rs b/tests/testsuite/cargo_add/public/mod.rs new file mode 100644 index 00000000000..c8c2e3cf585 --- /dev/null +++ b/tests/testsuite/cargo_add/public/mod.rs @@ -0,0 +1,39 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +use cargo_test_support::curr_dir; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + for name in ["my-package1", "my-package2"] { + for ver in [ + "0.1.1+my-package", + "0.2.0+my-package", + "0.2.3+my-package", + "0.4.1+my-package", + "20.0.0+my-package", + "99999.0.0+my-package", + "99999.0.0-alpha.1+my-package", + ] { + cargo_test_support::registry::Package::new(name, ver).publish(); + } + } + + 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("my-package1 my-package2@0.4.1 --public") + .current_dir(cwd) + .masquerade_as_nightly_cargo(&["public-dependency"]) + .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/public/out/Cargo.toml b/tests/testsuite/cargo_add/public/out/Cargo.toml new file mode 100644 index 00000000000..0a3d744cb6b --- /dev/null +++ b/tests/testsuite/cargo_add/public/out/Cargo.toml @@ -0,0 +1,10 @@ +cargo-features = ["public-dependency"] +[workspace] + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" + +[dependencies] +my-package1 = { version = "99999.0.0", public = true } +my-package2 = { version = "0.4.1", public = true } diff --git a/tests/testsuite/cargo_add/public/stderr.log b/tests/testsuite/cargo_add/public/stderr.log new file mode 100644 index 00000000000..04d71318dd6 --- /dev/null +++ b/tests/testsuite/cargo_add/public/stderr.log @@ -0,0 +1,3 @@ + Updating `dummy-registry` index + Adding my-package1 v99999.0.0 to public dependencies. + Adding my-package2 v0.4.1 to public dependencies. diff --git a/tests/testsuite/cargo_add/public/stdout.log b/tests/testsuite/cargo_add/public/stdout.log new file mode 100644 index 00000000000..e69de29bb2d From 58e0ab825b97348f8d904acb096f7a8c69bae9f2 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Sat, 25 Nov 2023 12:41:01 +0800 Subject: [PATCH 05/10] reuse add_basic.in/lib.rs for testcase --- tests/testsuite/cargo_add/no_public/in/Cargo.toml | 3 +++ tests/testsuite/cargo_add/no_public/in/src/lib.rs | 0 tests/testsuite/cargo_add/no_public/out/Cargo.toml | 3 +++ .../cargo_add/overwrite_no_public/in/Cargo.toml | 3 +++ .../cargo_add/overwrite_no_public/in/src/lib.rs | 0 .../cargo_add/overwrite_no_public/out/Cargo.toml | 3 +++ .../overwrite_no_public_with_public/in/Cargo.toml | 3 +++ .../overwrite_no_public_with_public/in/src/lib.rs | 0 .../overwrite_no_public_with_public/out/Cargo.toml | 3 +++ tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml | 3 +++ tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs | 0 .../testsuite/cargo_add/overwrite_public/out/Cargo.toml | 3 +++ .../overwrite_public_with_no_public/in/Cargo.toml | 3 +++ .../overwrite_public_with_no_public/in/src/lib.rs | 0 .../overwrite_public_with_no_public/out/Cargo.toml | 3 +++ tests/testsuite/cargo_add/public/in | 1 - tests/testsuite/cargo_add/public/in/Cargo.toml | 9 +++++++++ tests/testsuite/cargo_add/public/out/Cargo.toml | 3 +++ 18 files changed, 42 insertions(+), 1 deletion(-) delete mode 100644 tests/testsuite/cargo_add/no_public/in/src/lib.rs delete mode 100644 tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs delete mode 100644 tests/testsuite/cargo_add/overwrite_no_public_with_public/in/src/lib.rs delete mode 100644 tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs delete mode 100644 tests/testsuite/cargo_add/overwrite_public_with_no_public/in/src/lib.rs delete mode 120000 tests/testsuite/cargo_add/public/in create mode 100644 tests/testsuite/cargo_add/public/in/Cargo.toml diff --git a/tests/testsuite/cargo_add/no_public/in/Cargo.toml b/tests/testsuite/cargo_add/no_public/in/Cargo.toml index e9087535be3..16d76f1a2b7 100644 --- a/tests/testsuite/cargo_add/no_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/no_public/in/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/no_public/in/src/lib.rs b/tests/testsuite/cargo_add/no_public/in/src/lib.rs deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/testsuite/cargo_add/no_public/out/Cargo.toml b/tests/testsuite/cargo_add/no_public/out/Cargo.toml index 3cc396220a8..f1e227d9ffe 100644 --- a/tests/testsuite/cargo_add/no_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/no_public/out/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml index 3cc396220a8..f1e227d9ffe 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml index 3cc396220a8..f1e227d9ffe 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml index 2f3b348dfba..8824eeb24cd 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/src/lib.rs deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml index 0a3d744cb6b..f1985106210 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml index 3cc396220a8..f1e227d9ffe 100644 --- a/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml index 0a3d744cb6b..f1985106210 100644 --- a/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml index 0a3d744cb6b..f1985106210 100644 --- a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/src/lib.rs deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml index e6bb86566b8..4620555610b 100644 --- a/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/public/in b/tests/testsuite/cargo_add/public/in deleted file mode 120000 index 6c6a27fcfb5..00000000000 --- a/tests/testsuite/cargo_add/public/in +++ /dev/null @@ -1 +0,0 @@ -../add-basic.in \ No newline at end of file diff --git a/tests/testsuite/cargo_add/public/in/Cargo.toml b/tests/testsuite/cargo_add/public/in/Cargo.toml new file mode 100644 index 00000000000..16d76f1a2b7 --- /dev/null +++ b/tests/testsuite/cargo_add/public/in/Cargo.toml @@ -0,0 +1,9 @@ +cargo-features = ["public-dependency"] +[workspace] + +[lib] +path = "../../add_basic.in/src/lib.rs" + +[package] +name = "cargo-list-test-fixture" +version = "0.0.0" diff --git a/tests/testsuite/cargo_add/public/out/Cargo.toml b/tests/testsuite/cargo_add/public/out/Cargo.toml index 0a3d744cb6b..f1985106210 100644 --- a/tests/testsuite/cargo_add/public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/public/out/Cargo.toml @@ -1,6 +1,9 @@ cargo-features = ["public-dependency"] [workspace] +[lib] +path = "../../add_basic.in/src/lib.rs" + [package] name = "cargo-list-test-fixture" version = "0.0.0" From 509535675b4c540623eee24bcd7af76997ce7fa0 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Mon, 27 Nov 2023 07:26:55 +0800 Subject: [PATCH 06/10] Make the `public/no-public` next to the pair `optional/no-optional` --- src/bin/cargo/commands/add.rs | 23 +++++++++++------------ src/cargo/util/toml_mut/dependency.rs | 12 ++++++------ tests/testsuite/cargo_add/help/stdout.log | 20 ++++++++++---------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/bin/cargo/commands/add.rs b/src/bin/cargo/commands/add.rs index f2913393dbb..991107009ce 100644 --- a/src/bin/cargo/commands/add.rs +++ b/src/bin/cargo/commands/add.rs @@ -62,6 +62,17 @@ The package name will be exposed as feature of your crate.") The package will be removed from your features.") .conflicts_with("dev") .overrides_with("optional"), + flag("public", "Mark the dependency as public") + .long_help("Mark the dependency as public + +The dependnecy will be visible in both of your crate and outside."), + flag("no-public", "Mark the dependency as private") + .conflicts_with("dev") + .conflicts_with("build") + .overrides_with("public") + .long_help("Mark the dependency as private + +The dependnecy will be only visible in your crate other than outside."), clap::Arg::new("rename") .long("rename") .action(ArgAction::Set) @@ -76,18 +87,6 @@ Example uses: "ignore-rust-version", "Ignore `rust-version` specification in packages (unstable)" ), - flag("public", "Mark the dependency as public") - .long_help("Mark the dependency as public - -The dependnecy will be visible in both of your crate and outside."), - flag("no-public", "Mark the dependency as private") - .conflicts_with("dev") - .long_help("Mark the dependency as private - -The dependnecy will be only visible in your crate other than outside.") - .conflicts_with("dev") - .overrides_with("public"), - ]) .arg_manifest_path_without_unsupported_path_tip() .arg_package("Package to modify") diff --git a/src/cargo/util/toml_mut/dependency.rs b/src/cargo/util/toml_mut/dependency.rs index 7e775b5151e..6ffd214b473 100644 --- a/src/cargo/util/toml_mut/dependency.rs +++ b/src/cargo/util/toml_mut/dependency.rs @@ -51,13 +51,13 @@ impl Dependency { Self { name: name.into(), optional: None, + public: None, features: None, default_features: None, inherited_features: None, source: None, registry: None, rename: None, - public: None, } } @@ -338,14 +338,14 @@ impl Dependency { let dep = Self { name, - rename, - source: Some(source), - registry, - default_features, - features, optional, public, + features, + default_features, inherited_features: None, + source: Some(source), + registry, + rename, }; Ok(dep) } else { diff --git a/tests/testsuite/cargo_add/help/stdout.log b/tests/testsuite/cargo_add/help/stdout.log index 6530be11b96..6853480cdb7 100644 --- a/tests/testsuite/cargo_add/help/stdout.log +++ b/tests/testsuite/cargo_add/help/stdout.log @@ -32,16 +32,6 @@ Options: The package will be removed from your features. - --rename - Rename the dependency - - Example uses: - - Depending on multiple versions of a crate - - Depend on crates with the same name from different registries - - --ignore-rust-version - Ignore `rust-version` specification in packages (unstable) - --public Mark the dependency as public @@ -52,6 +42,16 @@ Options: The dependnecy will be only visible in your crate other than outside. + --rename + Rename the dependency + + Example uses: + - Depending on multiple versions of a crate + - Depend on crates with the same name from different registries + + --ignore-rust-version + Ignore `rust-version` specification in packages (unstable) + -n, --dry-run Don't actually write the manifest From 07ba57dd8ba2a4c4f0e4aac8198a3fc69b48e07b Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Mon, 27 Nov 2023 08:54:05 +0800 Subject: [PATCH 07/10] Remove unnecessary code for `cargo add --public` testcases --- tests/testsuite/cargo_add/no_public/mod.rs | 17 ++--------------- .../cargo_add/no_public/out/Cargo.toml | 3 +-- tests/testsuite/cargo_add/no_public/stderr.log | 3 +-- .../cargo_add/overwrite_no_public/in/Cargo.toml | 3 +-- .../cargo_add/overwrite_no_public/mod.rs | 17 ++--------------- .../overwrite_no_public/out/Cargo.toml | 3 +-- .../cargo_add/overwrite_no_public/stderr.log | 3 +-- .../in/Cargo.toml | 3 +-- .../overwrite_no_public_with_public/mod.rs | 17 ++--------------- .../out/Cargo.toml | 3 +-- .../overwrite_no_public_with_public/stderr.log | 3 +-- .../cargo_add/overwrite_public/in/Cargo.toml | 3 +-- .../testsuite/cargo_add/overwrite_public/mod.rs | 17 ++--------------- .../cargo_add/overwrite_public/out/Cargo.toml | 3 +-- .../cargo_add/overwrite_public/stderr.log | 3 +-- .../in/Cargo.toml | 3 +-- .../overwrite_public_with_no_public/mod.rs | 17 ++--------------- .../out/Cargo.toml | 3 +-- .../overwrite_public_with_no_public/stderr.log | 3 +-- tests/testsuite/cargo_add/public/mod.rs | 17 ++--------------- tests/testsuite/cargo_add/public/out/Cargo.toml | 3 +-- tests/testsuite/cargo_add/public/stderr.log | 3 +-- 22 files changed, 28 insertions(+), 122 deletions(-) diff --git a/tests/testsuite/cargo_add/no_public/mod.rs b/tests/testsuite/cargo_add/no_public/mod.rs index 43664b57af2..e93887040d2 100644 --- a/tests/testsuite/cargo_add/no_public/mod.rs +++ b/tests/testsuite/cargo_add/no_public/mod.rs @@ -7,27 +7,14 @@ use cargo_test_support::curr_dir; #[cargo_test] fn case() { cargo_test_support::registry::init(); - for name in ["my-package1", "my-package2"] { - for ver in [ - "0.1.1+my-package", - "0.2.0+my-package", - "0.2.3+my-package", - "0.4.1+my-package", - "20.0.0+my-package", - "99999.0.0+my-package", - "99999.0.0-alpha.1+my-package", - ] { - cargo_test_support::registry::Package::new(name, ver).publish(); - } - } - + cargo_test_support::registry::Package::new("my-package", "0.1.0").publish(); 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("my-package1 my-package2@0.4.1 --no-public") + .arg_line("my-package@0.1.0 --no-public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() diff --git a/tests/testsuite/cargo_add/no_public/out/Cargo.toml b/tests/testsuite/cargo_add/no_public/out/Cargo.toml index f1e227d9ffe..868d50e097a 100644 --- a/tests/testsuite/cargo_add/no_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/no_public/out/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = "99999.0.0" -my-package2 = "0.4.1" +my-package = "0.1.0" diff --git a/tests/testsuite/cargo_add/no_public/stderr.log b/tests/testsuite/cargo_add/no_public/stderr.log index fb8d4903d7b..8e025739f48 100644 --- a/tests/testsuite/cargo_add/no_public/stderr.log +++ b/tests/testsuite/cargo_add/no_public/stderr.log @@ -1,3 +1,2 @@ Updating `dummy-registry` index - Adding my-package1 v99999.0.0 to dependencies. - Adding my-package2 v0.4.1 to dependencies. + Adding my-package v0.1.0 to dependencies. diff --git a/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml index f1e227d9ffe..742c101083a 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = "99999.0.0" -my-package2 = "0.4.1" +my-package = "0.1.0" \ No newline at end of file diff --git a/tests/testsuite/cargo_add/overwrite_no_public/mod.rs b/tests/testsuite/cargo_add/overwrite_no_public/mod.rs index 43664b57af2..e93887040d2 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public/mod.rs +++ b/tests/testsuite/cargo_add/overwrite_no_public/mod.rs @@ -7,27 +7,14 @@ use cargo_test_support::curr_dir; #[cargo_test] fn case() { cargo_test_support::registry::init(); - for name in ["my-package1", "my-package2"] { - for ver in [ - "0.1.1+my-package", - "0.2.0+my-package", - "0.2.3+my-package", - "0.4.1+my-package", - "20.0.0+my-package", - "99999.0.0+my-package", - "99999.0.0-alpha.1+my-package", - ] { - cargo_test_support::registry::Package::new(name, ver).publish(); - } - } - + cargo_test_support::registry::Package::new("my-package", "0.1.0").publish(); 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("my-package1 my-package2@0.4.1 --no-public") + .arg_line("my-package@0.1.0 --no-public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() diff --git a/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml index f1e227d9ffe..868d50e097a 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = "99999.0.0" -my-package2 = "0.4.1" +my-package = "0.1.0" diff --git a/tests/testsuite/cargo_add/overwrite_no_public/stderr.log b/tests/testsuite/cargo_add/overwrite_no_public/stderr.log index fb8d4903d7b..8e025739f48 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_no_public/stderr.log @@ -1,3 +1,2 @@ Updating `dummy-registry` index - Adding my-package1 v99999.0.0 to dependencies. - Adding my-package2 v0.4.1 to dependencies. + Adding my-package v0.1.0 to dependencies. diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml index 8824eeb24cd..532b4b8c017 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = { version = "99999.0.0", public = false } -my-package2 = { version = "0.4.1", public = false } +my-package = { version = "0.1.0", public = false } diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs b/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs index c8c2e3cf585..0d07ba35968 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs @@ -7,27 +7,14 @@ use cargo_test_support::curr_dir; #[cargo_test] fn case() { cargo_test_support::registry::init(); - for name in ["my-package1", "my-package2"] { - for ver in [ - "0.1.1+my-package", - "0.2.0+my-package", - "0.2.3+my-package", - "0.4.1+my-package", - "20.0.0+my-package", - "99999.0.0+my-package", - "99999.0.0-alpha.1+my-package", - ] { - cargo_test_support::registry::Package::new(name, ver).publish(); - } - } - + cargo_test_support::registry::Package::new("my-package", "0.1.0").publish(); 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("my-package1 my-package2@0.4.1 --public") + .arg_line("my-package@0.1.0 --public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml index f1985106210..e24d5546072 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = { version = "99999.0.0", public = true } -my-package2 = { version = "0.4.1", public = true } +my-package = { version = "0.1.0", public = true } diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/stderr.log b/tests/testsuite/cargo_add/overwrite_no_public_with_public/stderr.log index 04d71318dd6..5259bbde8eb 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public_with_public/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/stderr.log @@ -1,3 +1,2 @@ Updating `dummy-registry` index - Adding my-package1 v99999.0.0 to public dependencies. - Adding my-package2 v0.4.1 to public dependencies. + Adding my-package v0.1.0 to public dependencies. diff --git a/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml index f1e227d9ffe..742c101083a 100644 --- a/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = "99999.0.0" -my-package2 = "0.4.1" +my-package = "0.1.0" \ No newline at end of file diff --git a/tests/testsuite/cargo_add/overwrite_public/mod.rs b/tests/testsuite/cargo_add/overwrite_public/mod.rs index c8c2e3cf585..bbf8d65a6e0 100644 --- a/tests/testsuite/cargo_add/overwrite_public/mod.rs +++ b/tests/testsuite/cargo_add/overwrite_public/mod.rs @@ -7,27 +7,14 @@ use cargo_test_support::curr_dir; #[cargo_test] fn case() { cargo_test_support::registry::init(); - for name in ["my-package1", "my-package2"] { - for ver in [ - "0.1.1+my-package", - "0.2.0+my-package", - "0.2.3+my-package", - "0.4.1+my-package", - "20.0.0+my-package", - "99999.0.0+my-package", - "99999.0.0-alpha.1+my-package", - ] { - cargo_test_support::registry::Package::new(name, ver).publish(); - } - } - + cargo_test_support::registry::Package::new("my-package", "0.1.0").publish(); 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("my-package1 my-package2@0.4.1 --public") + .arg_line("my-package --public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() diff --git a/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml index f1985106210..e24d5546072 100644 --- a/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = { version = "99999.0.0", public = true } -my-package2 = { version = "0.4.1", public = true } +my-package = { version = "0.1.0", public = true } diff --git a/tests/testsuite/cargo_add/overwrite_public/stderr.log b/tests/testsuite/cargo_add/overwrite_public/stderr.log index 04d71318dd6..5259bbde8eb 100644 --- a/tests/testsuite/cargo_add/overwrite_public/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_public/stderr.log @@ -1,3 +1,2 @@ Updating `dummy-registry` index - Adding my-package1 v99999.0.0 to public dependencies. - Adding my-package2 v0.4.1 to public dependencies. + Adding my-package v0.1.0 to public dependencies. diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml index f1985106210..726cd0c3f28 100644 --- a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = { version = "99999.0.0", public = true } -my-package2 = { version = "0.4.1", public = true } +my-package = { version = "0.1.0", public = true } \ No newline at end of file diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/mod.rs b/tests/testsuite/cargo_add/overwrite_public_with_no_public/mod.rs index 43664b57af2..912ac3fd3a7 100644 --- a/tests/testsuite/cargo_add/overwrite_public_with_no_public/mod.rs +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/mod.rs @@ -7,27 +7,14 @@ use cargo_test_support::curr_dir; #[cargo_test] fn case() { cargo_test_support::registry::init(); - for name in ["my-package1", "my-package2"] { - for ver in [ - "0.1.1+my-package", - "0.2.0+my-package", - "0.2.3+my-package", - "0.4.1+my-package", - "20.0.0+my-package", - "99999.0.0+my-package", - "99999.0.0-alpha.1+my-package", - ] { - cargo_test_support::registry::Package::new(name, ver).publish(); - } - } - + cargo_test_support::registry::Package::new("my-package", "0.1.0").publish(); 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("my-package1 my-package2@0.4.1 --no-public") + .arg_line("my-package --no-public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml index 4620555610b..2415dbc2a48 100644 --- a/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = { version = "99999.0.0" } -my-package2 = { version = "0.4.1" } +my-package = { version = "0.1.0" } diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/stderr.log b/tests/testsuite/cargo_add/overwrite_public_with_no_public/stderr.log index fb8d4903d7b..8e025739f48 100644 --- a/tests/testsuite/cargo_add/overwrite_public_with_no_public/stderr.log +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/stderr.log @@ -1,3 +1,2 @@ Updating `dummy-registry` index - Adding my-package1 v99999.0.0 to dependencies. - Adding my-package2 v0.4.1 to dependencies. + Adding my-package v0.1.0 to dependencies. diff --git a/tests/testsuite/cargo_add/public/mod.rs b/tests/testsuite/cargo_add/public/mod.rs index c8c2e3cf585..0d07ba35968 100644 --- a/tests/testsuite/cargo_add/public/mod.rs +++ b/tests/testsuite/cargo_add/public/mod.rs @@ -7,27 +7,14 @@ use cargo_test_support::curr_dir; #[cargo_test] fn case() { cargo_test_support::registry::init(); - for name in ["my-package1", "my-package2"] { - for ver in [ - "0.1.1+my-package", - "0.2.0+my-package", - "0.2.3+my-package", - "0.4.1+my-package", - "20.0.0+my-package", - "99999.0.0+my-package", - "99999.0.0-alpha.1+my-package", - ] { - cargo_test_support::registry::Package::new(name, ver).publish(); - } - } - + cargo_test_support::registry::Package::new("my-package", "0.1.0").publish(); 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("my-package1 my-package2@0.4.1 --public") + .arg_line("my-package@0.1.0 --public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() diff --git a/tests/testsuite/cargo_add/public/out/Cargo.toml b/tests/testsuite/cargo_add/public/out/Cargo.toml index f1985106210..e24d5546072 100644 --- a/tests/testsuite/cargo_add/public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/public/out/Cargo.toml @@ -9,5 +9,4 @@ name = "cargo-list-test-fixture" version = "0.0.0" [dependencies] -my-package1 = { version = "99999.0.0", public = true } -my-package2 = { version = "0.4.1", public = true } +my-package = { version = "0.1.0", public = true } diff --git a/tests/testsuite/cargo_add/public/stderr.log b/tests/testsuite/cargo_add/public/stderr.log index 04d71318dd6..5259bbde8eb 100644 --- a/tests/testsuite/cargo_add/public/stderr.log +++ b/tests/testsuite/cargo_add/public/stderr.log @@ -1,3 +1,2 @@ Updating `dummy-registry` index - Adding my-package1 v99999.0.0 to public dependencies. - Adding my-package2 v0.4.1 to public dependencies. + Adding my-package v0.1.0 to public dependencies. From 703446165bca054605283592d09e8dd12a013514 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Tue, 28 Nov 2023 11:25:10 +0800 Subject: [PATCH 08/10] chore: add the missing flag for `cargo add --public` --- src/bin/cargo/commands/add.rs | 2 ++ tests/testsuite/cargo_add/no_public/mod.rs | 2 +- tests/testsuite/cargo_add/overwrite_no_public/mod.rs | 2 +- .../testsuite/cargo_add/overwrite_no_public_with_public/mod.rs | 2 +- tests/testsuite/cargo_add/public/mod.rs | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/bin/cargo/commands/add.rs b/src/bin/cargo/commands/add.rs index 991107009ce..2d23343f0ae 100644 --- a/src/bin/cargo/commands/add.rs +++ b/src/bin/cargo/commands/add.rs @@ -63,6 +63,8 @@ The package will be removed from your features.") .conflicts_with("dev") .overrides_with("optional"), flag("public", "Mark the dependency as public") + .conflicts_with("dev") + .conflicts_with("build") .long_help("Mark the dependency as public The dependnecy will be visible in both of your crate and outside."), diff --git a/tests/testsuite/cargo_add/no_public/mod.rs b/tests/testsuite/cargo_add/no_public/mod.rs index e93887040d2..912ac3fd3a7 100644 --- a/tests/testsuite/cargo_add/no_public/mod.rs +++ b/tests/testsuite/cargo_add/no_public/mod.rs @@ -14,7 +14,7 @@ fn case() { snapbox::cmd::Command::cargo_ui() .arg("add") - .arg_line("my-package@0.1.0 --no-public") + .arg_line("my-package --no-public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() diff --git a/tests/testsuite/cargo_add/overwrite_no_public/mod.rs b/tests/testsuite/cargo_add/overwrite_no_public/mod.rs index e93887040d2..912ac3fd3a7 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public/mod.rs +++ b/tests/testsuite/cargo_add/overwrite_no_public/mod.rs @@ -14,7 +14,7 @@ fn case() { snapbox::cmd::Command::cargo_ui() .arg("add") - .arg_line("my-package@0.1.0 --no-public") + .arg_line("my-package --no-public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs b/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs index 0d07ba35968..bbf8d65a6e0 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/mod.rs @@ -14,7 +14,7 @@ fn case() { snapbox::cmd::Command::cargo_ui() .arg("add") - .arg_line("my-package@0.1.0 --public") + .arg_line("my-package --public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() diff --git a/tests/testsuite/cargo_add/public/mod.rs b/tests/testsuite/cargo_add/public/mod.rs index 0d07ba35968..bbf8d65a6e0 100644 --- a/tests/testsuite/cargo_add/public/mod.rs +++ b/tests/testsuite/cargo_add/public/mod.rs @@ -14,7 +14,7 @@ fn case() { snapbox::cmd::Command::cargo_ui() .arg("add") - .arg_line("my-package@0.1.0 --public") + .arg_line("my-package --public") .current_dir(cwd) .masquerade_as_nightly_cargo(&["public-dependency"]) .assert() From a6ca281948d28d642290826d34e34c478c830446 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Fri, 1 Dec 2023 10:21:33 +0800 Subject: [PATCH 09/10] chore: add lib.rs for new test cases --- src/bin/cargo/commands/add.rs | 4 ++-- tests/testsuite/cargo_add/help/stdout.log | 5 +++-- tests/testsuite/cargo_add/no_public/in/Cargo.toml | 3 --- tests/testsuite/cargo_add/no_public/in/src/lib.rs | 0 tests/testsuite/cargo_add/no_public/out/Cargo.toml | 3 --- tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml | 3 --- tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs | 0 tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml | 3 --- .../cargo_add/overwrite_no_public_with_public/in/Cargo.toml | 3 --- .../cargo_add/overwrite_no_public_with_public/in/src/lib.rs | 0 .../cargo_add/overwrite_no_public_with_public/out/Cargo.toml | 3 --- tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml | 3 --- tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs | 0 tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml | 3 --- .../cargo_add/overwrite_public_with_no_public/in/Cargo.toml | 3 --- .../cargo_add/overwrite_public_with_no_public/in/src/lib.rs | 0 .../cargo_add/overwrite_public_with_no_public/out/Cargo.toml | 3 --- tests/testsuite/cargo_add/public/in/Cargo.toml | 3 --- tests/testsuite/cargo_add/public/in/src/lib.rs | 0 tests/testsuite/cargo_add/public/out/Cargo.toml | 3 --- 20 files changed, 5 insertions(+), 40 deletions(-) create mode 100644 tests/testsuite/cargo_add/no_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/overwrite_no_public_with_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/overwrite_public_with_no_public/in/src/lib.rs create mode 100644 tests/testsuite/cargo_add/public/in/src/lib.rs diff --git a/src/bin/cargo/commands/add.rs b/src/bin/cargo/commands/add.rs index 2d23343f0ae..55a07428202 100644 --- a/src/bin/cargo/commands/add.rs +++ b/src/bin/cargo/commands/add.rs @@ -67,14 +67,14 @@ The package will be removed from your features.") .conflicts_with("build") .long_help("Mark the dependency as public -The dependnecy will be visible in both of your crate and outside."), +The dependency can be referenced in your library's public API."), flag("no-public", "Mark the dependency as private") .conflicts_with("dev") .conflicts_with("build") .overrides_with("public") .long_help("Mark the dependency as private -The dependnecy will be only visible in your crate other than outside."), +While you can use the crate in your implementation, it cannot be referenced in your public API."), clap::Arg::new("rename") .long("rename") .action(ArgAction::Set) diff --git a/tests/testsuite/cargo_add/help/stdout.log b/tests/testsuite/cargo_add/help/stdout.log index 6853480cdb7..d2931ae9cd4 100644 --- a/tests/testsuite/cargo_add/help/stdout.log +++ b/tests/testsuite/cargo_add/help/stdout.log @@ -35,12 +35,13 @@ Options: --public Mark the dependency as public - The dependnecy will be visible in both of your crate and outside. + The dependency can be referenced in your library's public API. --no-public Mark the dependency as private - The dependnecy will be only visible in your crate other than outside. + While you can use the crate in your implementation, it cannot be referenced in your public + API. --rename Rename the dependency diff --git a/tests/testsuite/cargo_add/no_public/in/Cargo.toml b/tests/testsuite/cargo_add/no_public/in/Cargo.toml index 16d76f1a2b7..e9087535be3 100644 --- a/tests/testsuite/cargo_add/no_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/no_public/in/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/no_public/in/src/lib.rs b/tests/testsuite/cargo_add/no_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/no_public/out/Cargo.toml b/tests/testsuite/cargo_add/no_public/out/Cargo.toml index 868d50e097a..b9f04511677 100644 --- a/tests/testsuite/cargo_add/no_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/no_public/out/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml index 742c101083a..43d0d8238a1 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_no_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml index 868d50e097a..b9f04511677 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml index 532b4b8c017..c6e61bdca3f 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_no_public_with_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml index e24d5546072..77fccaa6a57 100644 --- a/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml index 742c101083a..43d0d8238a1 100644 --- a/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml index e24d5546072..77fccaa6a57 100644 --- a/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml index 726cd0c3f28..cc7ec1a9b7e 100644 --- a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/src/lib.rs b/tests/testsuite/cargo_add/overwrite_public_with_no_public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml b/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml index 2415dbc2a48..cfa80cc1373 100644 --- a/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/public/in/Cargo.toml b/tests/testsuite/cargo_add/public/in/Cargo.toml index 16d76f1a2b7..e9087535be3 100644 --- a/tests/testsuite/cargo_add/public/in/Cargo.toml +++ b/tests/testsuite/cargo_add/public/in/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" diff --git a/tests/testsuite/cargo_add/public/in/src/lib.rs b/tests/testsuite/cargo_add/public/in/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/public/out/Cargo.toml b/tests/testsuite/cargo_add/public/out/Cargo.toml index e24d5546072..77fccaa6a57 100644 --- a/tests/testsuite/cargo_add/public/out/Cargo.toml +++ b/tests/testsuite/cargo_add/public/out/Cargo.toml @@ -1,9 +1,6 @@ cargo-features = ["public-dependency"] [workspace] -[lib] -path = "../../add_basic.in/src/lib.rs" - [package] name = "cargo-list-test-fixture" version = "0.0.0" From 48c998fec7ff84f3aec3547e6ba32ca40eae32cd Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Fri, 1 Dec 2023 11:22:26 +0800 Subject: [PATCH 10/10] Add detect_workspace_inherit_public test case. --- .../in/Cargo.toml | 5 ++++ .../in/dependency/Cargo.toml | 3 +++ .../in/dependency/src/lib.rs | 0 .../in/primary/Cargo.toml | 4 +++ .../in/primary/src/lib.rs | 0 .../detect_workspace_inherit_public/mod.rs | 26 +++++++++++++++++++ .../out/Cargo.toml | 5 ++++ .../out/dependency/Cargo.toml | 3 +++ .../out/primary/Cargo.toml | 7 +++++ .../stderr.log | 1 + .../stdout.log | 0 tests/testsuite/cargo_add/mod.rs | 1 + 12 files changed, 55 insertions(+) create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/in/Cargo.toml create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/in/dependency/Cargo.toml create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/in/dependency/src/lib.rs create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/in/primary/Cargo.toml create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/in/primary/src/lib.rs create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/mod.rs create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/out/Cargo.toml create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/out/dependency/Cargo.toml create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/out/primary/Cargo.toml create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/stderr.log create mode 100644 tests/testsuite/cargo_add/detect_workspace_inherit_public/stdout.log diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/Cargo.toml b/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/Cargo.toml new file mode 100644 index 00000000000..24c50556bb5 --- /dev/null +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] +members = ["primary", "dependency"] + +[workspace.dependencies] +foo = { version = "0.0.0", path = "./dependency"} \ No newline at end of file diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/dependency/Cargo.toml b/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/dependency/Cargo.toml new file mode 100644 index 00000000000..2d247d4d26c --- /dev/null +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/dependency/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "foo" +version = "0.0.0" diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/dependency/src/lib.rs b/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/dependency/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/primary/Cargo.toml b/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/primary/Cargo.toml new file mode 100644 index 00000000000..b5923a106b9 --- /dev/null +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/primary/Cargo.toml @@ -0,0 +1,4 @@ +cargo-features = ["public-dependency"] +[package] +name = "bar" +version = "0.0.0" \ No newline at end of file diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/primary/src/lib.rs b/tests/testsuite/cargo_add/detect_workspace_inherit_public/in/primary/src/lib.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/mod.rs b/tests/testsuite/cargo_add/detect_workspace_inherit_public/mod.rs new file mode 100644 index 00000000000..680d4c4e3d0 --- /dev/null +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_public/mod.rs @@ -0,0 +1,26 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +use cargo_test_support::curr_dir; + +#[cargo_test] +fn case() { + cargo_test_support::registry::init(); + + 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") + .args(["foo", "-p", "bar", "--public"]) + .masquerade_as_nightly_cargo(&["public-dependency"]) + .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/detect_workspace_inherit_public/out/Cargo.toml b/tests/testsuite/cargo_add/detect_workspace_inherit_public/out/Cargo.toml new file mode 100644 index 00000000000..24c50556bb5 --- /dev/null +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_public/out/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] +members = ["primary", "dependency"] + +[workspace.dependencies] +foo = { version = "0.0.0", path = "./dependency"} \ No newline at end of file diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/out/dependency/Cargo.toml b/tests/testsuite/cargo_add/detect_workspace_inherit_public/out/dependency/Cargo.toml new file mode 100644 index 00000000000..2d247d4d26c --- /dev/null +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_public/out/dependency/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "foo" +version = "0.0.0" diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/out/primary/Cargo.toml b/tests/testsuite/cargo_add/detect_workspace_inherit_public/out/primary/Cargo.toml new file mode 100644 index 00000000000..665c6ae5e02 --- /dev/null +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_public/out/primary/Cargo.toml @@ -0,0 +1,7 @@ +cargo-features = ["public-dependency"] +[package] +name = "bar" +version = "0.0.0" + +[dependencies] +foo = { workspace = true, public = true } diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/stderr.log b/tests/testsuite/cargo_add/detect_workspace_inherit_public/stderr.log new file mode 100644 index 00000000000..efa1ae9fa3d --- /dev/null +++ b/tests/testsuite/cargo_add/detect_workspace_inherit_public/stderr.log @@ -0,0 +1 @@ + Adding foo (workspace) to public dependencies. diff --git a/tests/testsuite/cargo_add/detect_workspace_inherit_public/stdout.log b/tests/testsuite/cargo_add/detect_workspace_inherit_public/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_add/mod.rs b/tests/testsuite/cargo_add/mod.rs index 2da7e4c5242..5a1136d33fa 100644 --- a/tests/testsuite/cargo_add/mod.rs +++ b/tests/testsuite/cargo_add/mod.rs @@ -12,6 +12,7 @@ mod deprecated_section; mod detect_workspace_inherit; mod detect_workspace_inherit_features; mod detect_workspace_inherit_optional; +mod detect_workspace_inherit_public; mod dev; mod dev_build_conflict; mod dev_prefer_existing_version;