budziq / rust-skeptic

Test your Rust Markdown documentation via Cargo
Apache License 2.0
286 stars 43 forks source link

How to test the library crate? #29

Closed iddm closed 7 years ago

iddm commented 7 years ago

I see no way to test the crate inside itself. It seems that I can't add my own crate to the dev and build dependencies to make tests from README.md work, it needs to change the project hierarchy. More detailed, I have a simple library crate. It has a book in projectdir/book directory and a readme file in projectdir directory. What should I do to run tests in README.md? The code samples in my README.md have extern crate lines but the skeptic can't find them.

iddm commented 7 years ago

I have fixed this issue by myself. So, I have a usual directory structure:

projectdir ----src/ ----src/lib.rs ----Cargo.toml

To add skeptic here we must to make our library a workspace (this will reduce compile time a lot and is better by some other reasons) and then create the submodule in the workspace - create a directory, called, for example, external_markdown_tests, put a Cargo.toml there with contents:

[package]
name = "external_markdown_tests"
version = "0.1.0"
build = "build.rs"

[build-dependencies]
skeptic = "0.9"

[dev-dependencies]
skeptic = "0.9"
toornament = { path = "../" }

My library is called toornament so replace it with yours.

Then add add an empty library source there (src/lib.rs). Then create a build script build.rs:

extern crate skeptic;

fn main() {
    skeptic::generate_doc_tests(&["../README.md"]);
}

Then create a tests directory where you use the generated tests:

include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));

That's all. Simply go to this directory and run the tests via cargo test.