Skip to content

Commit 3dc85f1

Browse files
Merge pull request #677 from antiguru/test_mdbook
Test mdbook using regular doc tests
2 parents be56290 + f029325 commit 3dc85f1

File tree

5 files changed

+65
-16
lines changed

5 files changed

+65
-16
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ jobs:
2626
components: clippy
2727
- name: Cargo test
2828
run: cargo test --workspace --all-targets
29+
- name: Cargo doc test
30+
run: cargo test --doc
2931

3032
# Check for clippy warnings
3133
clippy:
@@ -40,19 +42,3 @@ jobs:
4042
run: cargo clippy --workspace --all-targets
4143
env:
4244
RUSTFLAGS: "" # Don't make test fail on clippy
43-
44-
# Check mdbook files for errors
45-
mdbook:
46-
name: test mdBook
47-
runs-on: ubuntu-latest
48-
steps:
49-
- uses: actions/checkout@v4
50-
- uses: actions-rust-lang/setup-rust-toolchain@v1
51-
# rustdoc doesn't build dependencies, so it needs to run after `cargo build`,
52-
# but its dependency search gets confused if there are multiple copies of any
53-
# dependency in target/debug/deps, so it needs to run before `cargo test` et al.
54-
# clutter target/debug/deps with multiple copies of things.
55-
- run: cargo clean
56-
- run: cargo build
57-
- name: test mdBook
58-
run: for file in $(find mdbook -name '*.md' | sort); do rustdoc --test $file -L ./target/debug/deps; done

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ members = [
44
"communication",
55
"container",
66
"logging",
7+
"mdbook",
78
"timely",
89
]
910

mdbook/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "mdbook"
3+
version = "0.0.0"
4+
edition.workspace = true
5+
publish = false
6+
7+
[lints]
8+
workspace = true
9+
10+
[dependencies]
11+
timely = { path = "../timely" }
12+
timely_bytes = { path = "../bytes" }
13+
timely_communication = { path = "../communication" }
14+
serde = "1.0"

mdbook/build.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! Infrastructure to test the mdbook documentation.
2+
//!
3+
//! Generates a module for each Markdown file in the `src/` directory, and includes
4+
//! the contents of each file as a doc comment for that module.
5+
6+
use std::env;
7+
use std::fs;
8+
use std::io;
9+
use std::path::{Path, PathBuf};
10+
11+
/// Recursively finds all Markdown files in the given path and collects their paths into `mds`.
12+
fn find_mds(dir: impl AsRef<Path>, mds: &mut Vec<PathBuf>) -> io::Result<()> {
13+
for entry in fs::read_dir(dir)? {
14+
let path = entry?.path();
15+
if path.is_dir() {
16+
find_mds(path, mds)?;
17+
} else if path.extension().and_then(|s| s.to_str()) == Some("md") {
18+
mds.push(path);
19+
}
20+
}
21+
Ok(())
22+
}
23+
24+
fn main() -> io::Result<()> {
25+
let mut mds = Vec::new();
26+
find_mds("src", &mut mds)?;
27+
28+
let mut lib = String::new();
29+
30+
for md in mds {
31+
let md_path = md.to_str().unwrap();
32+
println!("cargo::rerun-if-changed={md_path}");
33+
let mod_name = md_path.replace(['/', '\\', '-', '.'], "_");
34+
use std::fmt::Write;
35+
writeln!(
36+
&mut lib,
37+
"#[allow(non_snake_case)] #[doc = include_str!(concat!(env!(\"CARGO_MANIFEST_DIR\"), r\"{}{md_path}\"))] mod {mod_name} {{}}",
38+
std::path::MAIN_SEPARATOR,
39+
).unwrap();
40+
}
41+
42+
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("mdbook.rs");
43+
fs::write(&dest_path, lib)?;
44+
println!("cargo::rerun-if-changed=build.rs");
45+
Ok(())
46+
}

mdbook/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//! Dummy library file to allow testing the mdbook documentation.
2+
include!(concat!(env!("OUT_DIR"), "/mdbook.rs"));

0 commit comments

Comments
 (0)