Skip to content

Commit

Permalink
feat: add an error message when trying to build library only crates
Browse files Browse the repository at this point in the history
Only binaries are supported, so users should have a message reminding
them that when trying to build a crate that do not contain a binary
package.

See #12
  • Loading branch information
ronnychevalier committed Jul 26, 2024
1 parent 5a0af1a commit 56b509c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ exclude = [
"tests/test-argv",
"tests/test-nothing",
"tests/test-workspace",
"tests/test-nobin",
]

[workspace.lints.rust]
Expand Down
9 changes: 9 additions & 0 deletions src/multivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ impl Multivers {
pub fn build(&self) -> anyhow::Result<()> {
let (selected_packages, _) = self.workspace.partition_packages(&self.metadata);

let has_bins = selected_packages
.iter()
.any(|&package| package.targets.iter().any(|target| target.is_bin()));
if !has_bins {
anyhow::bail!(
"No binary package detected. Only binaries can be built using cargo multivers."
);
}

let profile_dir = if self.profile == "dev" {
"debug"
} else {
Expand Down
7 changes: 7 additions & 0 deletions tests/test-nobin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/test-nobin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "test-nobin"
edition = "2021"
publish = false

[dependencies]
1 change: 1 addition & 0 deletions tests/test-nobin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

14 changes: 14 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ fn rebuild_std_env() {
)));
}

/// Checks that we can build using the dev profile
#[test]
fn profile_dev() {
let expected_args = ["z", "foo2", "''"];
Expand All @@ -193,3 +194,16 @@ fn profile_dev() {
expected_args.join(" ")
)));
}

/// Checks that users receive a message explaining that library only crates are not supported
///
/// See #12
#[test]
fn no_bin() {
build_crate("test-nobin", |_| ())
.0
.failure()
.stderr(predicate::eq(
"Error: No binary package detected. Only binaries can be built using cargo multivers.\n",
));
}

0 comments on commit 56b509c

Please sign in to comment.