readme-sync

CI Latest Version Documentation GitHub license Rust Version

The readme-sync crate makes it easy to add an integration test that checks that your readme and crate documentation are synchronized.

About

This crate provides several abstractions for readme and documentation front page content as well as multiple readme and documentation parsing and transformation functions. With them, readme and documentation can be converted to a set of markup nodes that are expected to be the same. Their equality can be checked with the assert_sync function, which also provides useful diagnostic messages about the differences found.

Documentation parser accepts not only inner doc-comments (//!) but also inner doc-attributes (#[!cfg(...)] and #[!cfg_attr(...)]). This is useful when some doc-tests require certain features to compile and run.

Usage

First, add the following to your Cargo.toml:

toml [dev-dependencies] readme-sync = "0.1.0"

Then add an integration test using the necessary readme and docs modifiers, and check their synchronization using the assert_sync function.

The example below is used to test the synchronization of the readme and documentation of this crate. You can copy it and follow the diagnostic messages to adjust the modifiers used and to correct your readme and documentation.

```rust

[cfg(test)]

[test]

fn readmesynctest() { use readmesync::{assertsync, CMarkDocs, CMarkReadme, Config, Package};

let package = Package::from_path(env!("CARGO_MANIFEST_DIR").into()).unwrap();
let config = Config::from_package_docs_rs_features(&package);
let readme = CMarkReadme::from_package(&package).unwrap();
let docs = CMarkDocs::from_package_and_config(&package, &config).unwrap();

let readme = readme
    .remove_badges_paragraph()
    .remove_documentation_section()
    .remove_codeblock_tag("no_sync")
    .disallow_absolute_repository_blob_links()
    .unwrap()
    .use_absolute_repository_blob_urls()
    .unwrap();

let docs = docs
    .increment_heading_levels()
    .add_package_title()
    .remove_codeblock_rust_test_tags()
    .use_default_codeblock_rust_tag()
    .remove_hidden_rust_code()
    .disallow_absolute_package_docs_links()
    .unwrap()
    .use_absolute_package_docs_urls()
    .unwrap();

assert_sync(&readme, &docs);

} ```

Note that both cargo build and cargo test enable features from dev-dependencies, so if you want to test your crate without them (for example in no_std environment) you can use readme-sync with default-features = false. See this FAQ section for more details.

Documentation

[API Documentation]

Feature Flags

Other crates

FAQ

Why is the example integration test so long and there is no function that would do it all at once?

Readme and documentation transformations are very different between different crates and the API of this crate is not yet stabilized.

At the moment, however, it supports extensive customization. You can specify the paths to readme and docs, their contents, the features and transformations used, and use your own transformations.

So any feedback is welcome!

Why use syn instead of just parsing documentation comments?

Because of cfg and cfg_attr that are useful for documentation tests that require some specific features and can only be compiled with them.

Why Markdown instead of text comparison?

It simplifies the Markdown transformations. Transformations are necessary, because of some differences between readme content and documentation front page including: the presence of a crate name, different heading levels, the presence of badges, different relative url root, etc.

Why are all dependencies optional?

By default, Rust compiler enables features from dev-dependencies for normal dependencies for commands like cargo test and cargo build. As a result, the features used by dev-dependencies are implicitly enabled during testing. Because all readme-sync dependencies are optional, you can easily protect your crate from implicitly enabled common features when testing.

See rust-lang/cargo#7916 for more details.

How to prevent readme-sync dependency features enabled for dependencies of my crate.

If you use nightly Rust you can simply use -Z features=dev_dep flags.

Or, in any Rust release, you can disable all readme-sync dependencies with: toml [dev-dependencies.readme-sync] version = "0.1.0" default-features = false

This will help you avoid feature injection from dev-dependencies.

In order to use readme-sync functionality in this case, you need to add a feature that reenables readme-sync default features and can be used to run readme synchronization integration tests: toml,no_sync [features] test-readme-sync = ["readme-sync/default"]

Then you need to add test-readme-sync conditional check to your readme sync integration test: ```rust

[cfg(all(test, feature = "test-readme-sync"))]

// ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[test]

fn readmesynctest() { // ... } ```

And run it with bash cargo test --features "test-readme-sync"

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.