Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doctest! seems to not work any more? #17

Closed
BurntSushi opened this issue Mar 7, 2020 · 3 comments · Fixed by #18
Closed

doctest! seems to not work any more? #17

BurntSushi opened this issue Mar 7, 2020 · 3 comments · Fixed by #18

Comments

@BurntSushi
Copy link

Consider the following:

$ tree -I target
.
├── Cargo.lock
├── Cargo.toml
├── README.md
└── src
    └── lib.rs

1 directory, 4 files

$ cat Cargo.toml
[package]
name = "doc-comment-fail"
version = "0.1.0"
authors = ["Andrew Gallant <jamslam@gmail.com>"]
edition = "2018"

[dependencies]
doc-comment = "0.3.1"

$ cat src/lib.rs
#[cfg(test)]
doc_comment::doctest!("../README.md");

pub fn something() {}

$ cat README.md
```rust
fn main() {
  this is an invalid program
}

Since my Rust program in my README is invalid, I expected cargo test to fail. But instead, it passes and does not seem to run any tests:

$ cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.01s
     Running target/debug/deps/doc_comment_fail-fe51e9186885559c

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests doc-comment-fail

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Did something break this functionality or am I perhaps just doing something wrong?

If I run cargo expand, then it shows the macro is still expanding seemingly correctly:

$ cargo expand --color never --lib --tests
   Compiling doc-comment-fail v0.1.0 (/tmp/doc-comment-fail)
    Finished test [unoptimized + debuginfo] target(s) in 0.04s

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
///```rust
///fn main() {
///  this is an invalid program
///}
///```
extern "C" {}
pub fn something() {}
#[main]
pub fn main() -> () {
    extern crate test;
    test::test_main_static(&[])
}

If I inline the relevant bits of cargo expand into src/lib.rs manually, then the test fails, as I expect:

$ cat src/lib.rs
///```rust
///fn main() {
///  this is an invalid program
///}
///```
extern "C" {}

pub fn something() {}

$ cargo test
   Compiling doc-comment-fail v0.1.0 (/tmp/doc-comment-fail)
    Finished test [unoptimized + debuginfo] target(s) in 0.19s
     Running target/debug/deps/doc_comment_fail-fe51e9186885559c

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests doc-comment-fail

running 1 test
test src/lib.rs -  (line 1) ... FAILED

failures:

---- src/lib.rs -  (line 1) stdout ----
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `is`
 --> src/lib.rs:3:8
  |
3 |   this is an invalid program
  |        ^^ expected one of 8 possible tokens

error: aborting due to previous error

Couldn't compile the test.

failures:
    src/lib.rs -  (line 1)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

error: test failed, to rerun pass '--doc'
@GuillaumeGomez
Copy link
Owner

It's not cfg(test) that you want but cfg(doctest). ;)

@BurntSushi
Copy link
Author

Ah right, completely forgot about that. Might be good to re-purpose this ticket into adding a quick note about that in the docs. Or even just use cfg(doctest) in the crate examples.

@GuillaumeGomez
Copy link
Owner

Oh good idea! I'll send a fix shortly.

BurntSushi added a commit to BurntSushi/rust-snappy that referenced this issue Mar 7, 2020
It needs `cfg(doctest)`, not `cfg(test)`.

See: GuillaumeGomez/doc-comment#17
@GuillaumeGomez GuillaumeGomez mentioned this issue Mar 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants