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

Run doc tests on README #383

Open
huonw opened this issue Aug 16, 2014 · 22 comments
Open

Run doc tests on README #383

huonw opened this issue Aug 16, 2014 · 22 comments
Labels
A-readme Area: README file issues C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-test S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@huonw
Copy link
Member

huonw commented Aug 16, 2014

cargo test could run (the equivalent of) rustdoc --test README.md, since people often have a basic example in it.

See also

Past efforts

@tomjakubowski
Copy link
Contributor

I imagine people might have many markdown files which are documentation and contain examples; it would be nice to be able to specify additional .md files to run rustdoc --test on in Cargo.toml.

@alexcrichton
Copy link
Member

Sounds like a good idea! We could at least start out with a README and expand it with a manifest key later on.

@alexcrichton
Copy link
Member

Some concerns were raised in #445 which are applicable to this as well.

@tomaka
Copy link
Contributor

tomaka commented Aug 27, 2014

The question is: how will README interact with the Cargo registry?

Currently the examples are in the README file mainly because it's easy to read when you discover a library on github.

But if the registry automatically provides the rustdoc output of each package, then I think that the basic examples should simply be in the crate root documentation, just like the standard regex and green crates do.

@skade
Copy link

skade commented Nov 22, 2014

I'd like to have this feature. Broken README code is worse then broken code in the crate docs, as this is the first place where people go to. Outdated READMEs are even worse of an issue, a thing tested READMEs elegantly circumvent.

Also, I prefer writing README code, as crate documentation is a bit cumbersume and hard to read in source, given the amount of comment characters it needs.

Rust code can be highlighted on github and ignored by using:

   ```{.rust .ignore}

(this currently is parsed correctly, but not properly picked up. {.ignore .rust} works. I'll file a bug on rustdoc after validating)

As a middleground, my suggestion is to allow listing additional documents to test with rustdoc in the Manifest and generate a Cargo.toml including "README.md" in that list per default. This raises awareness of the feature, but ultimately leaves the user in control.

@skade
Copy link

skade commented Feb 5, 2015

I'd like to push this one, I just ran into precisely the problem that my README examples were the only ones broken.

@pzol
Copy link

pzol commented Feb 5, 2015

+1

@pythonesque
Copy link

I would love this.

Concerns can be addressed by making the feature opt-in (and maybe also with attribute metadata, but I don't know markdown well enough to know if that's a thing).

@moonglum
Copy link

moonglum commented Feb 6, 2015

👍

@s-panferov
Copy link

Always forget to update README.md in my projects 👍

@huonw
Copy link
Member Author

huonw commented Jul 20, 2015

cc https://github.com/brson/rust-skeptic

@carols10cents
Copy link
Member

potentially relevant: rust-lang/rfcs#1990

@carols10cents carols10cents added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-test labels Sep 10, 2017
@abonander
Copy link

abonander commented Jan 23, 2019

The external docs include feature (RFC 1990) is definitely the best way to go about this, IMO. You could include your README.md as the docs for a hidden item and then rustdoc should be able to automatically test the examples. There's a bit of engineering effort left to make it stabilizable though.

@abonander
Copy link

abonander commented Jan 25, 2019

In case anyone missed it, I've got a proof-of-concept project for using the external_doc feature to test code examples in README.md: https://github.com/abonander/readme-doctest-poc

This can be done conditionally under a Cargo feature so it doesn't require nightly to test.

cc rust-lang/rust#44732 (comment)

rahulg added a commit to rahulg/treexml-rs that referenced this issue Feb 3, 2020
cargo test doesn't currently pick up doctests in README.md, tracking
issue: rust-lang/cargo#383
@ehuss ehuss added the A-readme Area: README file issues label Jul 1, 2020
@joshtriplett
Copy link
Member

I'd like to revive this. I've seen projects do CI on their README by manually running rustdoc test README.md and specifying their crate as an --extern.

@shepmaster
Copy link
Member

I've been happily using doc-comment to do this.

@ibraheemdev
Copy link
Member

ibraheemdev commented Nov 3, 2020

For anyone who doesn't want to add another dependency, this little macro works perfectly:

#[cfg(doctest)]
mod test_readme {
  macro_rules! external_doc_test {
    ($x:expr) => {
        #[doc = $x]
        extern {}
    };
  }

  external_doc_test!(include_str!("../README.md"));
}

bors bot added a commit to codyps/rust-systemd that referenced this issue Nov 10, 2020
158: tests: test readme r=jmesmon a=jmesmon

Using doc-comment. See rust-lang/cargo#383

Co-authored-by: Cody P Schafer <[email protected]>
codyps added a commit to codyps/logos that referenced this issue Nov 15, 2020
The code to include the readme as a doctest is from [this rust
ticket](rust-lang/cargo#383 (comment)).
codyps added a commit to codyps/ioctl that referenced this issue Nov 17, 2020
maciejhirsz pushed a commit to maciejhirsz/logos that referenced this issue Nov 23, 2020
The code to include the readme as a doctest is from [this rust
ticket](rust-lang/cargo#383 (comment)).
connec added a commit to connec/cloudformatious that referenced this issue Apr 25, 2021
The README now includes a code sample (and [this][1] technique is used
to test it).

[1]: rust-lang/cargo#383 (comment)
gadomski added a commit to stac-utils/stac-rs that referenced this issue Feb 15, 2022
gadomski added a commit to stac-utils/stac-rs that referenced this issue Feb 15, 2022
qmonnet added a commit to qmonnet/rbpf that referenced this issue Mar 10, 2023
We have several code snippets in the README.md file, some ignored
(because it's irrelevant to compile and run them), some not. They are
currently not tested by "cargo test". Let's include the file as part as
the documentation for the library, so that these examples are
automatically tested.

Note that the file name and lines indicated in the output for the tests
are not relevant to the README.md file.

Reference:
https://github.com/rust-lang/rust/blob/5f4e0677190b/src/doc/rustdoc/src/write-documentation/documentation-tests.md#include-items-only-when-collecting-doctests

Note that there's a related issue in cargo to add native support for
tests on README's examples:
rust-lang/cargo#383

Signed-off-by: Quentin Monnet <[email protected]>
qmonnet added a commit to qmonnet/rbpf that referenced this issue Mar 10, 2023
We have several code snippets in the README.md file, some ignored
(because it's irrelevant to compile and run them), some not. They are
currently not tested by "cargo test". Let's include the file as part as
the documentation for the library, so that these examples are
automatically tested.

Note that the file name and lines indicated in the output for the tests
are not relevant to the README.md file.

Reference:
https://github.com/rust-lang/rust/blob/5f4e0677190b/src/doc/rustdoc/src/write-documentation/documentation-tests.md#include-items-only-when-collecting-doctests

Note that there's a related issue in cargo to add native support for
tests on README's examples:
rust-lang/cargo#383

Signed-off-by: Quentin Monnet <[email protected]>
qmonnet added a commit to qmonnet/rbpf that referenced this issue Mar 10, 2023
We have several code snippets in the README.md file, some ignored
(because it's irrelevant to compile and run them), some not. They are
currently not tested by "cargo test". Let's include the file as part as
the documentation for the library, so that these examples are
automatically tested.

Note that the file name and lines indicated in the output for the tests
are not relevant to the README.md file.

Reference:
https://github.com/rust-lang/rust/blob/5f4e0677190b/src/doc/rustdoc/src/write-documentation/documentation-tests.md#include-items-only-when-collecting-doctests

Note that there's a related issue in cargo to add native support for
tests on README's examples:
rust-lang/cargo#383

Signed-off-by: Quentin Monnet <[email protected]>
qmonnet added a commit to qmonnet/rbpf that referenced this issue Mar 10, 2023
We have several code snippets in the README.md file, some ignored
(because it's irrelevant to compile and run them), some not. They are
currently not tested by "cargo test". Let's include the file as part as
the documentation for the library, so that these examples are
automatically tested.

Note that the file name and lines indicated in the output for the tests
are not relevant to the README.md file.

Reference:
https://github.com/rust-lang/rust/blob/5f4e0677190b/src/doc/rustdoc/src/write-documentation/documentation-tests.md#include-items-only-when-collecting-doctests

Note that there's a related issue in cargo to add native support for
tests on README's examples:
rust-lang/cargo#383

Signed-off-by: Quentin Monnet <[email protected]>
qmonnet added a commit to qmonnet/rbpf that referenced this issue Mar 10, 2023
We have several code snippets in the README.md file, some ignored
(because it's irrelevant to compile and run them), some not. They are
currently not tested by "cargo test". Let's include the file as part as
the documentation for the library, so that these examples are
automatically tested.

Note that the file name and lines indicated in the output for the tests
are not relevant to the README.md file.

Reference:
https://github.com/rust-lang/rust/blob/5f4e0677190b/src/doc/rustdoc/src/write-documentation/documentation-tests.md#include-items-only-when-collecting-doctests

Note that there's a related issue in cargo to add native support for
tests on README's examples:
rust-lang/cargo#383

Signed-off-by: Quentin Monnet <[email protected]>
@epage epage added the S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. label May 3, 2023
@tv42
Copy link

tv42 commented Nov 30, 2023

Three years later, here's an update to #383 (comment):

For anyone who doesn't want to add another dependency, this works:

#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadMe;

Note, it can't be in #[cfg(test)], it can't be in tests/.

Your IDE may gray it out as code not being used, because of the #[cfg(doctest)]. You may prefer

#[doc = include_str!("../README.md")]
struct _ReadMe;

I chose to put that in mod doctests { ... }, just to make the intent clearer.

Improvements welcome.

@tv42
Copy link

tv42 commented Nov 30, 2023

And here's the official documentation for it: https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#include-items-only-when-collecting-doctests

Considering that many README.md's likely contain broken code and not-even-intended-to-be-complete examples, is this amount of opt-in good enough; can this issue be closed?

@BilakshanP
Copy link

BilakshanP commented Apr 22, 2024

Hi, I didn't knew that this is an intended feature and by-chance found a bug. (unsure)

image

image

As here you can see, I ran cargo test instead of cargo test --lib. All of my unit tests passed, but I found that some doctest was failing. Which is strange as I couldn't recall writing any doctests. As suggested by the output, I noticed that my line 56 in lib.rs is error-prone. Which was quite strange for me as lib.rs is < 30 LOC. So, I ended up searching for that error-prone piece of code (as you can see in the right of the first image).

TL;DR + Conclusion: So, I think it is wrongly reporting that the issue is in lib.rs's line 56 when it is actually in README's line 56

Please, let me know if I am somehow mistaken and that this is indeed the expected behavior.

@epage
Copy link
Contributor

epage commented Apr 22, 2024

Its hard to tell whats going on without a reproduction case. I'm assuming you are including your README in the documentation for your lib. I have also seen line numbers be a bit strange in these scenarios. I believe rustdoc is what determines all of that.

@BilakshanP
Copy link

BilakshanP commented Apr 22, 2024

Its hard to tell whats going on without a reproduction case. I'm assuming you are including your README in the documentation for your lib. I have also seen line numbers be a bit strange in these scenarios. I believe rustdoc is what determines all of that.

If you don't mind may I share the link to the repository? So, that you can reproduce it yourselves.

Also, I myself think that wrong line numbers are an issue then. (Yes, README is included.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-readme Area: README file issues C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-test S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.