Skip to content

Commit d04e8dd

Browse files
authored
Skip crates without lib targets; exit non-zero if nothing to check. (#477)
* Skip crates without lib targets; exit non-zero if nothing to check. * Delint.
1 parent acf7e4a commit d04e8dd

File tree

7 files changed

+43
-6
lines changed

7 files changed

+43
-6
lines changed

src/lib.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ impl Scope {
216216

217217
meta.packages
218218
.iter()
219-
.filter(|&p| base_ids.contains(&p.id))
219+
.filter(|&p| {
220+
// The package has to not have been explicitly excluded,
221+
// and also has to have a library target (an API we can check).
222+
base_ids.contains(&p.id) && p.targets.iter().any(|target| target.is_lib())
223+
})
220224
.collect()
221225
}
222226
}
@@ -437,6 +441,12 @@ impl Check {
437441
RustdocSource::Root(project_root) => {
438442
let metadata = manifest_metadata(project_root)?;
439443
let selected = self.scope.selected_packages(&metadata);
444+
if selected.is_empty() {
445+
anyhow::bail!(
446+
"no crates with library targets selected, nothing to semver-check"
447+
);
448+
}
449+
440450
selected
441451
.iter()
442452
.map(|selected| {
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["crate1"]
2+
members = ["lib_crate", "non_lib_crate"]
33

44
[workspace.package]
55
version = "0.1.0"
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
publish = false
3-
name = "crate_in_workspace_crate1"
3+
name = "lib_crate"
44
version = { workspace = true }
55

66
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "non_lib_crate"
3+
edition = "2021"
4+
version.workspace = true
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

tests/rustdoc_edge_cases.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ fn lib_target_with_dashes() {
2424
.success();
2525
}
2626

27-
/// Ensure that proc macro crates can be semver-checked correctly.
27+
/// Ensure that proc macro crates without a lib target produce the correct error message
28+
/// since they have no library API and therefore nothing we can semver-check.
2829
#[test]
2930
fn proc_macro_target() {
3031
let mut cmd = Command::cargo_bin("cargo-semver-checks").unwrap();
3132
cmd.current_dir("test_crates/proc_macro_crate")
3233
.args(["semver-checks", "check-release", "--baseline-root=."])
3334
.assert()
34-
.success();
35+
.stderr("Error: no crates with library targets selected, nothing to semver-check\n")
36+
.failure();
3537
}
3638

3739
/// Ensure that crates whose lib targets have a different name can be semver-checked correctly.
@@ -69,11 +71,27 @@ fn crate_in_workspace() {
6971
"check-release",
7072
"--manifest-path=./Cargo.toml",
7173
"-p",
72-
"crate1",
74+
"lib_crate",
7375
"--baseline-root=.",
7476
])
7577
.assert()
7678
.success();
79+
80+
// Run at workspace level then point out a crate without a lib target.
81+
// This should produce an error and a non-zero exit code.
82+
let mut cmd = Command::cargo_bin("cargo-semver-checks").unwrap();
83+
cmd.current_dir("test_crates/crate_in_workspace")
84+
.args([
85+
"semver-checks",
86+
"check-release",
87+
"--manifest-path=./Cargo.toml",
88+
"-p",
89+
"non_lib_crate",
90+
"--baseline-root=.",
91+
])
92+
.assert()
93+
.stderr("Error: no crates with library targets selected, nothing to semver-check\n")
94+
.failure();
7795
}
7896

7997
/// This test ensures that the `--release-type` flag works correctly,

0 commit comments

Comments
 (0)