Skip to content

Commit 5a5bbc2

Browse files
committed
Auto merge of #5801 - ehuss:fix-doctestable, r=alexcrichton
Fix `test --doc` with incompatible lib types. When I recently changed the doctest handling, I forgot to check the lib type in the `test --doc` scenario. I also added the package name to a nearby error message, since it can be confusing in a workspace setting.
2 parents d195f28 + dd6c610 commit 5a5bbc2

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

src/cargo/core/manifest.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,12 @@ impl Target {
628628
pub fn benched(&self) -> bool {
629629
self.benched
630630
}
631-
632631
pub fn doctested(&self) -> bool {
633-
self.doctest && match self.kind {
632+
self.doctest
633+
}
634+
635+
pub fn doctestable(&self) -> bool {
636+
match self.kind {
634637
TargetKind::Lib(ref kinds) => kinds
635638
.iter()
636639
.any(|k| *k == LibKind::Rlib || *k == LibKind::Lib || *k == LibKind::ProcMacro),

src/cargo/ops/cargo_compile.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,11 @@ fn generate_targets<'a>(
545545
proposals.extend(default_units);
546546
if build_config.mode == CompileMode::Test {
547547
// Include doctest for lib.
548-
if let Some(t) = pkg.targets().iter().find(|t| t.is_lib() && t.doctested()) {
548+
if let Some(t) = pkg
549+
.targets()
550+
.iter()
551+
.find(|t| t.is_lib() && t.doctested() && t.doctestable())
552+
{
549553
proposals.push((new_unit(pkg, t, CompileMode::Doctest), false));
550554
}
551555
}
@@ -560,9 +564,16 @@ fn generate_targets<'a>(
560564
} => {
561565
if lib {
562566
if let Some(target) = pkg.targets().iter().find(|t| t.is_lib()) {
567+
if build_config.mode == CompileMode::Doctest && !target.doctestable() {
568+
bail!(
569+
"doc tests are not supported for crate type(s) `{}` in package `{}`",
570+
target.rustc_crate_types().join(", "),
571+
pkg.name()
572+
);
573+
}
563574
proposals.push((new_unit(pkg, target, build_config.mode), false));
564575
} else if !all_targets {
565-
bail!("no library targets found")
576+
bail!("no library targets found in package `{}`", pkg.name())
566577
}
567578
}
568579
// If --tests was specified, add all targets that would be

tests/testsuite/build_lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn build_with_no_lib() {
4545
p.cargo("build").arg("--lib"),
4646
execs()
4747
.with_status(101)
48-
.with_stderr("[ERROR] no library targets found"),
48+
.with_stderr("[ERROR] no library targets found in package `foo`"),
4949
);
5050
}
5151

tests/testsuite/test.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,3 +3547,45 @@ fn test_build_script_links() {
35473547
execs().with_status(0),
35483548
);
35493549
}
3550+
3551+
#[test]
3552+
fn doctest_skip_staticlib() {
3553+
let p = project()
3554+
.file(
3555+
"Cargo.toml",
3556+
r#"
3557+
[package]
3558+
name = "foo"
3559+
version = "0.0.1"
3560+
3561+
[lib]
3562+
crate-type = ["staticlib"]
3563+
"#,
3564+
)
3565+
.file(
3566+
"src/lib.rs",
3567+
r#"
3568+
//! ```
3569+
//! assert_eq!(1,2);
3570+
//! ```
3571+
"#,
3572+
)
3573+
.build();
3574+
3575+
assert_that(
3576+
p.cargo("test --doc"),
3577+
execs().with_status(101).with_stderr(
3578+
"[ERROR] doc tests are not supported for crate type(s) `staticlib` in package `foo`",
3579+
),
3580+
);
3581+
3582+
assert_that(
3583+
p.cargo("test"),
3584+
execs().with_status(0).with_stderr(
3585+
"\
3586+
[COMPILING] foo [..]
3587+
[FINISHED] dev [..]
3588+
[RUNNING] target[/]debug[/]deps[/]foo-[..]",
3589+
),
3590+
)
3591+
}

0 commit comments

Comments
 (0)