]> git.proxmox.com Git - cargo.git/commitdiff
refactor logic
authorlikzn <1020193211@qq.com>
Mon, 23 May 2022 13:01:55 +0000 (21:01 +0800)
committerlikzn <1020193211@qq.com>
Mon, 23 May 2022 13:01:55 +0000 (21:01 +0800)
src/cargo/ops/registry.rs
tests/testsuite/publish.rs

index 21e7bbf67d25b24d95632675e366b460a8353d0b..034835c40cf15c47ee6e60bd875df2e7fd7c20ee 100644 (file)
@@ -12,7 +12,6 @@ use anyhow::{bail, format_err, Context as _};
 use cargo_util::paths;
 use crates_io::{self, NewCrate, NewCrateDependency, Registry};
 use curl::easy::{Easy, InfoType, SslOpt, SslVersion};
-use itertools::Itertools;
 use log::{log, Level};
 use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
 use termcolor::Color::Green;
@@ -93,35 +92,21 @@ pub struct PublishOpts<'cfg> {
 pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
     let specs = opts.to_publish.to_package_id_specs(ws)?;
 
-    let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?;
-
-    if let Packages(opt_in) = &opts.to_publish {
-        if ws.is_virtual() {
-            bail!("can't use \"--package <SPEC>\" in virtual manifest")
-        }
-        let matched_packages = ws
-            .members()
-            .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
-            .collect::<Vec<_>>();
-        if matched_packages.is_empty() {
-            bail!(
-                "not found `{}` in manifest members. Check in manifest path `{}`",
-                opt_in.get(0).unwrap(),
-                ws.root().display()
-            )
+    if let Packages(_) = &opts.to_publish {
+        if specs.len() > 1 {
+            bail!("the `-p` argument must be specified to select a single package to publish");
         }
-        if matched_packages.len() > 1 {
-            bail!(
-                "found multiple `{}` in manifest members. Check in manifest path `{}`",
-                opt_in.get(0).unwrap(),
-                ws.root().display()
-            )
+    } else {
+        if ws.is_virtual() {
+            bail!("must use `-p` argument in virtual manifest")
         }
-        pkgs = pkgs
-            .into_iter()
-            .filter(|(m, _)| specs.iter().any(|spec| spec.matches(m.package_id())))
-            .collect_vec();
     }
+    let member_ids = ws.members().map(|p| p.package_id());
+    // Check that the spec matches exactly one member.
+    specs[0].query(member_ids)?;
+    let mut pkgs = ws.members_with_features(&specs, &opts.cli_features)?;
+    // Double check
+    assert_eq!(pkgs.len(), 1);
 
     let (pkg, cli_features) = pkgs.pop().unwrap();
 
index f5ec3fc3fe994eb000cf1a9a0feeda6f3c720b29..e68b54a38c890ff514b63b398020ca4cd248b603 100644 (file)
@@ -1675,7 +1675,7 @@ fn in_package_workspace() {
                 [package]
                 name = "foo"
                 version = "0.1.0"
-                edition = "2018"
+                edition = "2021"
                 [workspace]
                 members = ["li"]
             "#,
@@ -1735,9 +1735,9 @@ fn in_virtual_workspace() {
         .file("foo/src/main.rs", "fn main() {}")
         .build();
 
-    p.cargo("publish --no-verify --token sekrit -p foo")
+    p.cargo("publish --no-verify --token sekrit")
         .with_status(101)
-        .with_stderr("error: can't use \"--package <SPEC>\" in virtual manifest")
+        .with_stderr("error: must use `-p` argument in virtual manifest")
         .run();
 }
 
@@ -1752,7 +1752,7 @@ fn in_package_workspace_not_found() {
                 [package]
                 name = "foo"
                 version = "0.1.0"
-                edition = "2018"
+                edition = "2021"
                 [workspace]
             "#,
         )
@@ -1776,14 +1776,16 @@ fn in_package_workspace_not_found() {
         .with_status(101)
         .with_stderr(
             "\
-error: not found `li` in manifest members. Check in manifest path `[CWD]`
+error: package ID specification `li` did not match any packages
+
+<tab>Did you mean `foo`?
 ",
         )
         .run();
 }
 
 #[cargo_test]
-fn in_package_workspace_found_mutilate() {
+fn in_package_workspace_found_multiple() {
     registry::init();
 
     let p = project()
@@ -1793,7 +1795,7 @@ fn in_package_workspace_found_mutilate() {
                 [package]
                 name = "foo"
                 version = "0.1.0"
-                edition = "2018"
+                edition = "2021"
                 [workspace]
                 members = ["li","lii"]
             "#,
@@ -1831,8 +1833,55 @@ fn in_package_workspace_found_mutilate() {
         .with_status(101)
         .with_stderr(
             "\
-error: found multiple `li*` in manifest members. Check in manifest path `[CWD]`
+error: the `-p` argument must be specified to select a single package to publish
+",
+        )
+        .run();
+}
+
+
+#[cargo_test]
+// https://github.com/rust-lang/cargo/issues/10536
+fn publish_path_dependency_without_workspace() {
+    registry::init();
+
+    let p = project()
+        .file(
+            "Cargo.toml",
+            r#"
+                [package]
+                name = "foo"
+                version = "0.1.0"
+                edition = "2021"
+                [dependencies.bar]
+                path = "bar"
+            "#,
+        )
+        .file("src/main.rs", "fn main() {}")
+        .file(
+            "bar/Cargo.toml",
+            r#"
+                [package]
+                name = "bar"
+                version = "0.0.1"
+                edition = "2021"
+                authors = []
+                license = "MIT"
+                description = "bar"
+            "#,
+        )
+        .file("bar/src/main.rs", "fn main() {}")
+        .build();
+
+    p.cargo("publish -p bar --no-verify --token sekrit ")
+        .with_status(101)
+        .with_stderr(
+            "\
+error: package ID specification `bar` did not match any packages
+
+<tab>Did you mean `foo`?
 ",
         )
         .run();
 }
+