Skip to content

Commit

Permalink
Support package selection in cargo publish
Browse files Browse the repository at this point in the history
  • Loading branch information
jneem committed Oct 10, 2024
1 parent 3ffcdee commit 9f992e3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 59 deletions.
32 changes: 31 additions & 1 deletion src/bin/cargo/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ pub fn cli() -> Command {
"Allow dirty working directories to be packaged",
))
.arg_silent_suggestion()
.arg_package("Package to publish")
.arg_package_spec_no_all(
"Package(s) to publish",
"Publish all packages in the workspace (unstable)",
"Don't publish specified packages (unstable)",
)
.arg_features()
.arg_parallel()
.arg_target_triple("Build for the target triple")
Expand All @@ -41,6 +45,32 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
.into());
}

if !gctx.cli_unstable().package_workspace {
if args.flag("workspace") {
return Err(anyhow::format_err!(
"--workspace requires the unstable -Zpackage-workspace option"
)
.into());
}

if args.get_raw_occurrences("exclude").is_some() {
return Err(anyhow::format_err!(
"--exclude requires the unstable -Zpackage-workspace option"
)
.into());
}

let multiple_packages = args
.get_raw_occurrences("package")
.map_or(false, |xs| xs.count() > 1);
if multiple_packages {
return Err(anyhow::format_err!(
"multiple occurrences of --package requires the unstable -Zpackage-workspace option"
)
.into());
}
}

ops::publish(
&ws,
&PublishOpts {
Expand Down
48 changes: 26 additions & 22 deletions tests/testsuite/cargo_publish/help/stdout.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/testsuite/git_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ Caused by:

#[expect(deprecated)]
#[cargo_test]
#[ignore]
fn net_err_suggests_fetch_with_cli() {
let p = project()
.file(
Expand Down
75 changes: 39 additions & 36 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3384,13 +3384,20 @@ fn package_selection() {
p.cargo("publish --no-verify --dry-run -Zpackage-workspace --workspace")
.replace_crates_io(registry.index_url())
.masquerade_as_nightly_cargo(&["package-workspace"])
.with_status(1)
.with_stderr_data(str![[r#"
[ERROR] unexpected argument '--workspace' found
Usage: cargo publish --no-verify --dry-run -Z <FLAG>
For more information, try '--help'.
[UPDATING] crates.io index
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[PACKAGING] a v0.1.0 ([ROOT]/foo/a)
[PACKAGED] 3 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[PACKAGING] b v0.1.0 ([ROOT]/foo/b)
[PACKAGED] 3 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[UPLOADING] a v0.1.0 ([ROOT]/foo/a)
[WARNING] aborting upload due to dry run
[UPLOADING] b v0.1.0 ([ROOT]/foo/b)
[WARNING] aborting upload due to dry run
"#]])
.with_stdout_data(str![[r#""#]])
Expand All @@ -3399,13 +3406,20 @@ For more information, try '--help'.
p.cargo("publish --no-verify --dry-run -Zpackage-workspace --package a --package b")
.replace_crates_io(registry.index_url())
.masquerade_as_nightly_cargo(&["package-workspace"])
.with_status(1)
.with_stderr_data(str![[r#"
[ERROR] the argument '--package [<SPEC>]' cannot be used multiple times
Usage: cargo publish [OPTIONS]
For more information, try '--help'.
[UPDATING] crates.io index
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[PACKAGING] a v0.1.0 ([ROOT]/foo/a)
[PACKAGED] 3 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[PACKAGING] b v0.1.0 ([ROOT]/foo/b)
[PACKAGED] 3 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[UPLOADING] a v0.1.0 ([ROOT]/foo/a)
[WARNING] aborting upload due to dry run
[UPLOADING] b v0.1.0 ([ROOT]/foo/b)
[WARNING] aborting upload due to dry run
"#]])
.with_stdout_data(str![[r#""#]])
Expand All @@ -3414,55 +3428,44 @@ For more information, try '--help'.
p.cargo("publish --no-verify --dry-run -Zpackage-workspace --workspace --exclude b")
.replace_crates_io(registry.index_url())
.masquerade_as_nightly_cargo(&["package-workspace"])
.with_status(1)
.with_stderr_data(str![[r#"
[ERROR] unexpected argument '--workspace' found
Usage: cargo publish --no-verify --dry-run -Z <FLAG>
For more information, try '--help'.
[UPDATING] crates.io index
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[PACKAGING] a v0.1.0 ([ROOT]/foo/a)
[PACKAGED] 3 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[UPLOADING] a v0.1.0 ([ROOT]/foo/a)
[WARNING] aborting upload due to dry run
"#]])
.with_stdout_data(str![[r#""#]])
.run();

p.cargo("publish --no-verify --dry-run --package a --package b")
.replace_crates_io(registry.index_url())
.with_status(1)
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] the argument '--package [<SPEC>]' cannot be used multiple times
Usage: cargo publish [OPTIONS]
For more information, try '--help'.
[ERROR] multiple occurrences of --package requires the unstable -Zpackage-workspace option
"#]])
.with_stdout_data(str![[r#""#]])
.run();

p.cargo("publish --no-verify --dry-run --workspace")
.replace_crates_io(registry.index_url())
.with_status(1)
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] unexpected argument '--workspace' found
Usage: cargo publish --no-verify --dry-run
For more information, try '--help'.
[ERROR] --workspace requires the unstable -Zpackage-workspace option
"#]])
.with_stdout_data(str![[r#""#]])
.run();

p.cargo("publish --no-verify --dry-run --exclude b")
.replace_crates_io(registry.index_url())
.with_status(1)
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] unexpected argument '--exclude' found
Usage: cargo publish --no-verify --dry-run
For more information, try '--help'.
[ERROR] --exclude requires the unstable -Zpackage-workspace option
"#]])
.with_stdout_data(str![[r#""#]])
Expand Down

0 comments on commit 9f992e3

Please sign in to comment.