From 1a8bfdf0cf56cac41e2534c1d1823a7612d5f37f Mon Sep 17 00:00:00 2001 From: David Calavera Date: Sat, 28 Oct 2023 07:31:47 -0700 Subject: [PATCH] Update workspace manifest with new members. When a user runs `cargo new` or `cargo init` within a workspace, Cargo will automatically add the new package to the members list in the workspace if necessary. The heuristic to add the new package is as follows: - If there is no `members` list in the workspace yet, a new `members` list is created. - If there is an `exclude` statement, Cargo checks if the new package should be excluded. If it doesn't match the `exclude` list, the package is added to the `members` list. - If there is a glob expression in the `members` list that matches the new package, the package is not added to the `members` list. - If the existent `members` list is sorted, Cargo tries to preserve the ordering when it adds the new package. This change doesn't try to format the resulting `members` list in any way, leaving the formatting decissions to the user. Signed-off-by: David Calavera --- src/cargo/ops/cargo_new.rs | 107 +++++++++++++++++- tests/testsuite/cargo_init/mod.rs | 1 + .../workspace_add_member/in/Cargo.toml | 2 + .../in/crates/foo}/.keep | 0 .../cargo_init/workspace_add_member/mod.rs | 21 ++++ .../workspace_add_member/out/Cargo.toml | 3 + .../out/crates/foo/Cargo.toml | 8 ++ .../out/crates/foo/src/main.rs | 3 + .../workspace_add_member/stderr.log | 1 + .../workspace_add_member/stdout.log | 0 .../in/Cargo.toml | 5 + .../mod.rs | 22 ++++ .../out/Cargo.toml | 6 + .../out/crates/foo/Cargo.toml | 9 ++ .../out/crates/foo/src/main.rs | 3 + .../stderr.log | 1 + .../stdout.log | 0 .../in/Cargo.toml | 3 + .../in/crates/bar/Cargo.toml | 8 ++ .../in/crates/bar/src/main.rs | 3 + .../in/crates/qux/Cargo.toml | 8 ++ .../in/crates/qux/src/main.rs | 3 + .../mod.rs | 22 ++++ .../out/Cargo.toml | 3 + .../out/crates/foo/Cargo.toml | 8 ++ .../out/crates/foo/src/main.rs | 3 + .../stderr.log | 1 + .../stdout.log | 0 .../in/Cargo.toml | 3 + .../mod.rs | 23 ++++ .../out/Cargo.toml | 3 + .../out/crates/foo/Cargo.toml | 8 ++ .../out/crates/foo/src/main.rs | 3 + .../stderr.log | 1 + .../stdout.log | 0 .../in/Cargo.toml | 3 + .../mod.rs | 22 ++++ .../out/Cargo.toml | 3 + .../out/crates/foo/Cargo.toml | 8 ++ .../out/crates/foo/src/main.rs | 3 + .../stderr.log | 1 + .../stdout.log | 0 .../in/Cargo.toml | 4 + .../in/src/lib.rs | 14 +++ .../mod.rs | 22 ++++ .../out/Cargo.toml | 4 + .../out/crates/foo/Cargo.toml | 8 ++ .../out/crates/foo/src/main.rs | 3 + .../stderr.log | 1 + .../stdout.log | 0 .../in/Cargo.toml | 3 + .../mod.rs | 22 ++++ .../out/Cargo.toml | 3 + .../out/crates/foo/Cargo.toml | 8 ++ .../out/crates/foo/src/main.rs | 3 + .../stderr.log | 1 + .../stdout.log | 0 tests/testsuite/cargo_new/mod.rs | 6 + .../out/Cargo.toml | 2 + .../stderr.log | 8 -- tests/testsuite/workspaces.rs | 17 +-- 61 files changed, 438 insertions(+), 26 deletions(-) create mode 100644 tests/testsuite/cargo_init/workspace_add_member/in/Cargo.toml rename tests/testsuite/cargo_init/{empty_dir => workspace_add_member/in/crates/foo}/.keep (100%) create mode 100644 tests/testsuite/cargo_init/workspace_add_member/mod.rs create mode 100644 tests/testsuite/cargo_init/workspace_add_member/out/Cargo.toml create mode 100644 tests/testsuite/cargo_init/workspace_add_member/out/crates/foo/Cargo.toml create mode 100644 tests/testsuite/cargo_init/workspace_add_member/out/crates/foo/src/main.rs create mode 100644 tests/testsuite/cargo_init/workspace_add_member/stderr.log create mode 100644 tests/testsuite/cargo_init/workspace_add_member/stdout.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/in/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/mod.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/crates/foo/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/crates/foo/src/main.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/stderr.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/stdout.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/bar/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/bar/src/main.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/qux/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/qux/src/main.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/mod.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/crates/foo/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/crates/foo/src/main.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/stderr.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/stdout.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/in/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/mod.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/crates/foo/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/crates/foo/src/main.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/stderr.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/stdout.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/in/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/mod.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/crates/foo/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/crates/foo/src/main.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/stderr.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/stdout.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/in/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/in/src/lib.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/mod.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/crates/foo/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/crates/foo/src/main.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/stderr.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/stdout.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/in/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/mod.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/crates/foo/Cargo.toml create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/crates/foo/src/main.rs create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/stderr.log create mode 100644 tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/stdout.log diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index db42525a519..1c06b5f8258 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -1,10 +1,11 @@ use crate::core::{Edition, Shell, Workspace}; use crate::util::errors::CargoResult; use crate::util::important_paths::find_root_manifest_for_wd; +use crate::util::toml_mut::is_sorted; use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo}; use crate::util::{restricted_names, Config}; -use anyhow::{anyhow, Context as _}; -use cargo_util::paths; +use anyhow::{anyhow, Context}; +use cargo_util::paths::{self, write_atomic}; use serde::de; use serde::Deserialize; use std::collections::BTreeMap; @@ -13,6 +14,7 @@ use std::io::{BufRead, BufReader, ErrorKind}; use std::path::{Path, PathBuf}; use std::str::FromStr; use std::{fmt, slice}; +use toml_edit::{Array, Value}; #[derive(Clone, Copy, Debug, PartialEq)] pub enum VersionControl { @@ -809,7 +811,7 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> { // Sometimes the root manifest is not a valid manifest, so we only try to parse it if it is. // This should not block the creation of the new project. It is only a best effort to // inherit the workspace package keys. - if let Ok(workspace_document) = root_manifest.parse::() { + if let Ok(mut workspace_document) = root_manifest.parse::() { if let Some(workspace_package_keys) = workspace_document .get("workspace") .and_then(|workspace| workspace.get("package")) @@ -832,6 +834,13 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> { table["workspace"] = toml_edit::value(true); manifest["lints"] = toml_edit::Item::Table(table); } + + // Try to add the new package to the workspace members. + update_manifest_with_new_member( + &root_manifest_path, + &mut workspace_document, + opts.path, + )?; } } @@ -931,3 +940,95 @@ fn update_manifest_with_inherited_workspace_package_keys( try_remove_and_inherit_package_key(key, manifest); } } + +/// Adds the new package member to the [workspace.members] array. +/// - It first checks if the name matches any element in [workspace.exclude], +/// and it ignores the name if there is a match. +/// - Then it check if the name matches any element already in [workspace.members], +/// and it ignores the name if there is a match. +/// - If [workspace.members] doesn't exist in the manifest, it will add a new section +/// with the new package in it. +fn update_manifest_with_new_member( + root_manifest_path: &Path, + workspace_document: &mut toml_edit::Document, + package_path: &Path, +) -> CargoResult<()> { + // Find the relative path for the package from the workspace root directory. + let workspace_root = root_manifest_path.parent().with_context(|| { + format!( + "workspace root manifest doesn't have a parent directory `{}`", + root_manifest_path.display() + ) + })?; + let relpath = pathdiff::diff_paths(package_path, workspace_root).with_context(|| { + format!( + "path comparison requires two absolute paths; package_path: `{}`, workspace_path: `{}`", + package_path.display(), + workspace_root.display() + ) + })?; + + let mut components = Vec::new(); + for comp in relpath.iter() { + let comp = comp.to_str().with_context(|| { + format!("invalid unicode component in path `{}`", relpath.display()) + })?; + components.push(comp); + } + let display_path = components.join("/"); + + // Don't add the new package to the workspace's members + // if there is an exclusion match for it. + if let Some(exclude) = workspace_document + .get("workspace") + .and_then(|workspace| workspace.get("exclude")) + .and_then(|exclude| exclude.as_array()) + { + for member in exclude { + let pat = member + .as_str() + .with_context(|| format!("invalid non-string exclude path `{}`", member))?; + if pat == display_path { + return Ok(()); + } + } + } + + // If the members element already exist, check if one of the patterns + // in the array already includes the new package's relative path. + // - Add the relative path if the members don't match the new package's path. + // - Create a new members array if there are no members element in the workspace yet. + if let Some(members) = workspace_document + .get_mut("workspace") + .and_then(|workspace| workspace.get_mut("members")) + .and_then(|members| members.as_array_mut()) + { + for member in members.iter() { + let pat = member + .as_str() + .with_context(|| format!("invalid non-string member `{}`", member))?; + let pattern = glob::Pattern::new(pat) + .with_context(|| format!("cannot build glob pattern from `{}`", pat))?; + + if pattern.matches(&display_path) { + return Ok(()); + } + } + + let was_sorted = is_sorted(members.iter().map(Value::as_str)); + members.push(&display_path); + if was_sorted { + members.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str())); + } + } else { + let mut array = Array::new(); + array.push(&display_path); + + workspace_document["workspace"]["members"] = toml_edit::value(array); + } + + write_atomic( + &root_manifest_path, + workspace_document.to_string().to_string().as_bytes(), + ) +} diff --git a/tests/testsuite/cargo_init/mod.rs b/tests/testsuite/cargo_init/mod.rs index a1988a06af6..0b397111e1e 100644 --- a/tests/testsuite/cargo_init/mod.rs +++ b/tests/testsuite/cargo_init/mod.rs @@ -42,3 +42,4 @@ mod simple_hg_ignore_exists; mod simple_lib; mod unknown_flags; mod with_argument; +mod workspace_add_member; diff --git a/tests/testsuite/cargo_init/workspace_add_member/in/Cargo.toml b/tests/testsuite/cargo_init/workspace_add_member/in/Cargo.toml new file mode 100644 index 00000000000..61bdb9cbf2e --- /dev/null +++ b/tests/testsuite/cargo_init/workspace_add_member/in/Cargo.toml @@ -0,0 +1,2 @@ +[workspace] +resolver = "2" diff --git a/tests/testsuite/cargo_init/empty_dir/.keep b/tests/testsuite/cargo_init/workspace_add_member/in/crates/foo/.keep similarity index 100% rename from tests/testsuite/cargo_init/empty_dir/.keep rename to tests/testsuite/cargo_init/workspace_add_member/in/crates/foo/.keep diff --git a/tests/testsuite/cargo_init/workspace_add_member/mod.rs b/tests/testsuite/cargo_init/workspace_add_member/mod.rs new file mode 100644 index 00000000000..87e2af0e50f --- /dev/null +++ b/tests/testsuite/cargo_init/workspace_add_member/mod.rs @@ -0,0 +1,21 @@ +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() { + let project = Project::from_template(curr_dir!().join("in")); + let project_root = &project.root(); + + snapbox::cmd::Command::cargo_ui() + .arg_line("init --bin --vcs none") + .current_dir(project_root.join("crates").join("foo")) + .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_init/workspace_add_member/out/Cargo.toml b/tests/testsuite/cargo_init/workspace_add_member/out/Cargo.toml new file mode 100644 index 00000000000..18a4e7cf286 --- /dev/null +++ b/tests/testsuite/cargo_init/workspace_add_member/out/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = ["crates/foo"] diff --git a/tests/testsuite/cargo_init/workspace_add_member/out/crates/foo/Cargo.toml b/tests/testsuite/cargo_init/workspace_add_member/out/crates/foo/Cargo.toml new file mode 100644 index 00000000000..1d9cfe3176c --- /dev/null +++ b/tests/testsuite/cargo_init/workspace_add_member/out/crates/foo/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "foo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/testsuite/cargo_init/workspace_add_member/out/crates/foo/src/main.rs b/tests/testsuite/cargo_init/workspace_add_member/out/crates/foo/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/testsuite/cargo_init/workspace_add_member/out/crates/foo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_init/workspace_add_member/stderr.log b/tests/testsuite/cargo_init/workspace_add_member/stderr.log new file mode 100644 index 00000000000..3847e4e4a26 --- /dev/null +++ b/tests/testsuite/cargo_init/workspace_add_member/stderr.log @@ -0,0 +1 @@ + Created binary (application) package diff --git a/tests/testsuite/cargo_init/workspace_add_member/stdout.log b/tests/testsuite/cargo_init/workspace_add_member/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/in/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/in/Cargo.toml new file mode 100644 index 00000000000..0614e21f76c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/in/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] +resolver = "2" + +[workspace.package] +authors = ["Rustaceans"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/mod.rs b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/mod.rs new file mode 100644 index 00000000000..9b964246846 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/mod.rs @@ -0,0 +1,22 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::curr_dir; +use cargo_test_support::CargoCommand; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project = Project::from_template(curr_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("new") + .args(["crates/foo"]) + .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_new/add_members_to_workspace_format_previous_items/out/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/Cargo.toml new file mode 100644 index 00000000000..8df793a4fec --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] +resolver = "2" +members = ["crates/foo"] + +[workspace.package] +authors = ["Rustaceans"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/crates/foo/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/crates/foo/Cargo.toml new file mode 100644 index 00000000000..d97480c7c21 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/crates/foo/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "foo" +version = "0.1.0" +edition = "2021" +authors.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/crates/foo/src/main.rs b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/crates/foo/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/out/crates/foo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/stderr.log b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/stderr.log new file mode 100644 index 00000000000..90150cdf570 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/stderr.log @@ -0,0 +1 @@ + Created binary (application) `crates/foo` package diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/stdout.log b/tests/testsuite/cargo_new/add_members_to_workspace_format_previous_items/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/Cargo.toml new file mode 100644 index 00000000000..1f3dfe4de6c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = ["crates/bar", "crates/qux"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/bar/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/bar/Cargo.toml new file mode 100644 index 00000000000..825efac3438 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/bar/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "bar" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/bar/src/main.rs b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/bar/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/bar/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/qux/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/qux/Cargo.toml new file mode 100644 index 00000000000..30a9d52b292 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/qux/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "qux" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/qux/src/main.rs b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/qux/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/in/crates/qux/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/mod.rs b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/mod.rs new file mode 100644 index 00000000000..9b964246846 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/mod.rs @@ -0,0 +1,22 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::curr_dir; +use cargo_test_support::CargoCommand; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project = Project::from_template(curr_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("new") + .args(["crates/foo"]) + .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_new/add_members_to_workspace_format_sorted/out/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/Cargo.toml new file mode 100644 index 00000000000..cf27071a900 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = ["crates/bar", "crates/foo", "crates/qux"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/crates/foo/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/crates/foo/Cargo.toml new file mode 100644 index 00000000000..1d9cfe3176c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/crates/foo/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "foo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/crates/foo/src/main.rs b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/crates/foo/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/out/crates/foo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/stderr.log b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/stderr.log new file mode 100644 index 00000000000..90150cdf570 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/stderr.log @@ -0,0 +1 @@ + Created binary (application) `crates/foo` package diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/stdout.log b/tests/testsuite/cargo_new/add_members_to_workspace_format_sorted/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/in/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/in/Cargo.toml new file mode 100644 index 00000000000..226fd80bc3c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/in/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = [] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/mod.rs b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/mod.rs new file mode 100644 index 00000000000..8bf91be454c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/mod.rs @@ -0,0 +1,23 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::curr_dir; +use cargo_test_support::CargoCommand; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project = Project::from_template(curr_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + let package_path = cwd.join("crates").join("foo"); + + snapbox::cmd::Command::cargo_ui() + .arg("new") + .args([package_path]) + .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_new/add_members_to_workspace_with_absolute_package_path/out/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/Cargo.toml new file mode 100644 index 00000000000..18a4e7cf286 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = ["crates/foo"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/crates/foo/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/crates/foo/Cargo.toml new file mode 100644 index 00000000000..1d9cfe3176c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/crates/foo/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "foo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/crates/foo/src/main.rs b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/crates/foo/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/out/crates/foo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/stderr.log b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/stderr.log new file mode 100644 index 00000000000..c93b25acb4a --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/stderr.log @@ -0,0 +1 @@ + Created binary (application) `[ROOT]/case/crates/foo` package diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/stdout.log b/tests/testsuite/cargo_new/add_members_to_workspace_with_absolute_package_path/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/in/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/in/Cargo.toml new file mode 100644 index 00000000000..226fd80bc3c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/in/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = [] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/mod.rs b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/mod.rs new file mode 100644 index 00000000000..9b964246846 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/mod.rs @@ -0,0 +1,22 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::curr_dir; +use cargo_test_support::CargoCommand; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project = Project::from_template(curr_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("new") + .args(["crates/foo"]) + .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_new/add_members_to_workspace_with_empty_members/out/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/Cargo.toml new file mode 100644 index 00000000000..18a4e7cf286 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = ["crates/foo"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/crates/foo/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/crates/foo/Cargo.toml new file mode 100644 index 00000000000..1d9cfe3176c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/crates/foo/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "foo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/crates/foo/src/main.rs b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/crates/foo/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/out/crates/foo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/stderr.log b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/stderr.log new file mode 100644 index 00000000000..90150cdf570 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/stderr.log @@ -0,0 +1 @@ + Created binary (application) `crates/foo` package diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/stdout.log b/tests/testsuite/cargo_new/add_members_to_workspace_with_empty_members/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/in/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/in/Cargo.toml new file mode 100644 index 00000000000..4790076a853 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/in/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +resolver = "2" +members = [] +exclude = ["crates/foo"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/in/src/lib.rs b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/in/src/lib.rs new file mode 100644 index 00000000000..7d12d9af819 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/in/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/mod.rs b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/mod.rs new file mode 100644 index 00000000000..9b964246846 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/mod.rs @@ -0,0 +1,22 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::curr_dir; +use cargo_test_support::CargoCommand; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project = Project::from_template(curr_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("new") + .args(["crates/foo"]) + .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_new/add_members_to_workspace_with_exclude_list/out/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/Cargo.toml new file mode 100644 index 00000000000..4790076a853 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +resolver = "2" +members = [] +exclude = ["crates/foo"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/crates/foo/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/crates/foo/Cargo.toml new file mode 100644 index 00000000000..1d9cfe3176c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/crates/foo/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "foo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/crates/foo/src/main.rs b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/crates/foo/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/out/crates/foo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/stderr.log b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/stderr.log new file mode 100644 index 00000000000..90150cdf570 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/stderr.log @@ -0,0 +1 @@ + Created binary (application) `crates/foo` package diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/stdout.log b/tests/testsuite/cargo_new/add_members_to_workspace_with_exclude_list/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/in/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/in/Cargo.toml new file mode 100644 index 00000000000..a848b85b435 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/in/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = ["crates/*"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/mod.rs b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/mod.rs new file mode 100644 index 00000000000..9b964246846 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/mod.rs @@ -0,0 +1,22 @@ +use cargo_test_support::compare::assert_ui; +use cargo_test_support::curr_dir; +use cargo_test_support::CargoCommand; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project = Project::from_template(curr_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("new") + .args(["crates/foo"]) + .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_new/add_members_to_workspace_with_members_glob/out/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/Cargo.toml new file mode 100644 index 00000000000..a848b85b435 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = ["crates/*"] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/crates/foo/Cargo.toml b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/crates/foo/Cargo.toml new file mode 100644 index 00000000000..1d9cfe3176c --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/crates/foo/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "foo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/crates/foo/src/main.rs b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/crates/foo/src/main.rs new file mode 100644 index 00000000000..e7a11a969c0 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/out/crates/foo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/stderr.log b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/stderr.log new file mode 100644 index 00000000000..90150cdf570 --- /dev/null +++ b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/stderr.log @@ -0,0 +1 @@ + Created binary (application) `crates/foo` package diff --git a/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/stdout.log b/tests/testsuite/cargo_new/add_members_to_workspace_with_members_glob/stdout.log new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/testsuite/cargo_new/mod.rs b/tests/testsuite/cargo_new/mod.rs index 969b09f4f0d..da030440975 100644 --- a/tests/testsuite/cargo_new/mod.rs +++ b/tests/testsuite/cargo_new/mod.rs @@ -1,3 +1,9 @@ +mod add_members_to_workspace_format_previous_items; +mod add_members_to_workspace_format_sorted; +mod add_members_to_workspace_with_absolute_package_path; +mod add_members_to_workspace_with_empty_members; +mod add_members_to_workspace_with_exclude_list; +mod add_members_to_workspace_with_members_glob; mod help; mod inherit_workspace_lints; mod inherit_workspace_package_table; diff --git a/tests/testsuite/cargo_new/not_inherit_workspace_package_table_if_not_members/out/Cargo.toml b/tests/testsuite/cargo_new/not_inherit_workspace_package_table_if_not_members/out/Cargo.toml index 2d204581cfc..8de6ed4bfe5 100644 --- a/tests/testsuite/cargo_new/not_inherit_workspace_package_table_if_not_members/out/Cargo.toml +++ b/tests/testsuite/cargo_new/not_inherit_workspace_package_table_if_not_members/out/Cargo.toml @@ -1,3 +1,5 @@ +[workspace] +members = ["foo"] [workspace.package] authors = ["Rustaceans"] description = "foo" diff --git a/tests/testsuite/cargo_new/not_inherit_workspace_package_table_if_not_members/stderr.log b/tests/testsuite/cargo_new/not_inherit_workspace_package_table_if_not_members/stderr.log index 03b1ff6dbda..9b501592449 100644 --- a/tests/testsuite/cargo_new/not_inherit_workspace_package_table_if_not_members/stderr.log +++ b/tests/testsuite/cargo_new/not_inherit_workspace_package_table_if_not_members/stderr.log @@ -1,9 +1 @@ -warning: compiling this new package may not work due to invalid workspace configuration - -current package believes it's in a workspace when it's not: -current: [ROOT]/case/foo/Cargo.toml -workspace: [ROOT]/case/Cargo.toml - -this may be fixable by adding `foo` to the `workspace.members` array of the manifest located at: [ROOT]/case/Cargo.toml -Alternatively, to keep it out of the workspace, add the package to the `workspace.exclude` array, or add an empty `[workspace]` table to the package's manifest. Created binary (application) `foo` package diff --git a/tests/testsuite/workspaces.rs b/tests/testsuite/workspaces.rs index 4f8997b384c..94b5142f4c2 100644 --- a/tests/testsuite/workspaces.rs +++ b/tests/testsuite/workspaces.rs @@ -1046,7 +1046,7 @@ fn members_include_path_deps() { } #[cargo_test] -fn new_warns_you_this_will_not_work() { +fn new_creates_members_list() { let p = project() .file( "Cargo.toml", @@ -1063,20 +1063,7 @@ fn new_warns_you_this_will_not_work() { let p = p.build(); p.cargo("new --lib bar") - .with_stderr( - "\ -warning: compiling this new package may not work due to invalid workspace configuration - -current package believes it's in a workspace when it's not: -current: [..] -workspace: [..] - -this may be fixable by ensuring that this crate is depended on by the workspace \ -root: [..] -[..] -[CREATED] library `bar` package -", - ) + .with_stderr(" Created library `bar` package") .run(); }