Skip to content

Commit d5c1abd

Browse files
committed
Add test wrappers
This adds a simple xtask-like system to make it easier to run tests. I expect this to evolve more in the future, this is just intended as an initial experiment to try to make it easier to do things locally. * `cargo style-check` — Runs style checks * `cargo linkcheck` — Checks links * `cargo test-all` — Runs all tests, like mdbook, linkcheck, etc.
1 parent 49a9c8c commit d5c1abd

File tree

7 files changed

+136
-1
lines changed

7 files changed

+136
-1
lines changed

Diff for: .cargo/config.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[alias]
2+
style-check = "run --manifest-path=style-check/Cargo.toml -- src"
3+
linkcheck = "run --manifest-path=xtask/Cargo.toml -- linkcheck"
4+
test-all = "run --manifest-path=xtask/Cargo.toml -- test-all"

Diff for: .github/workflows/main.yml

+3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ jobs:
103103
- name: Rustfmt check
104104
working-directory: ./mdbook-spec
105105
run: cargo fmt --check
106+
- name: Xtask rustfmt check
107+
working-directory: ./xtask
108+
run: cargo fmt --check
106109

107110
preview:
108111
if: github.event_name == 'pull_request'

Diff for: docs/authoring.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This document serves as a guide for editors and reviewers. Some conventions and
2828
```
2929
* See the [Conventions] section for formatting callouts such as notes, edition differences, and warnings.
3030

31-
There are automated checks for some of these rules. Run `cargo run --manifest-path style-check/Cargo.toml -- src` to run them locally.
31+
There are automated checks for some of these rules. Run `cargo style-check` to run them locally.
3232

3333
[atx]: https://spec.commonmark.org/0.31.2/#atx-headings
3434
[conventions]: ../src/introduction.md#conventions
@@ -58,6 +58,16 @@ See the [rustdoc documentation] for more detail.
5858

5959
[rustdoc documentation]: https://doc.rust-lang.org/rustdoc/documentation-tests.html
6060

61+
You can verify the samples pass by running `mdbook test`.
62+
63+
### Linkcheck
64+
65+
To verify that links are not broken, run `cargo linkcheck`.
66+
67+
### Running all tests
68+
69+
As a last step before opening a PR, it is recommended to run `cargo test-all`. This will go through and run most of the tests that are required for CI to pass. See `xtask/src/main.rs` for what all this does.
70+
6171
## Special markdown constructs
6272

6373
The following are extensions provided by [`mdbook-spec`](https://github.com/rust-lang/spec/tree/main/mdbook-spec).

Diff for: xtask/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Diff for: xtask/Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: xtask/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "xtask"
3+
edition = "2024"
4+
5+
[dependencies]

Diff for: xtask/src/main.rs

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use std::error::Error;
2+
use std::process::Command;
3+
use std::process::exit;
4+
5+
type Result<T> = std::result::Result<T, Box<dyn Error>>;
6+
7+
fn main() -> Result<()> {
8+
let mut args = std::env::args().skip(1);
9+
let cmd = args.next();
10+
match cmd.as_deref() {
11+
Some("test-all") => {
12+
mdbook_test()?;
13+
style_check()?;
14+
fmt()?;
15+
linkcheck(args)?;
16+
cargo_test()?;
17+
eprintln!("all tests passed!");
18+
}
19+
Some("linkcheck") => linkcheck(args)?,
20+
Some(x) => {
21+
eprintln!("error: unknown command `{x}` (valid options: linkcheck)");
22+
exit(1);
23+
}
24+
None => {
25+
eprintln!("error: specify a command (valid options: linkcheck)");
26+
exit(1);
27+
}
28+
}
29+
Ok(())
30+
}
31+
32+
fn mdbook_test() -> Result<()> {
33+
eprintln!("Testing inline code tests...");
34+
let status = Command::new("mdbook")
35+
.arg("test")
36+
.status()
37+
.expect("mdbook should be installed");
38+
if !status.success() {
39+
return Err("inline code tests failed".into());
40+
}
41+
Ok(())
42+
}
43+
44+
fn style_check() -> Result<()> {
45+
eprintln!("Running style checks...");
46+
let status = Command::new("cargo")
47+
.args(["run", "--manifest-path=style-check/Cargo.toml", "--", "src"])
48+
.status()
49+
.expect("cargo should be installed");
50+
if !status.success() {
51+
return Err("style check failed".into());
52+
}
53+
Ok(())
54+
}
55+
56+
fn fmt() -> Result<()> {
57+
eprintln!("Checking code formatting...");
58+
for dir in ["style-check", "mdbook-spec", "xtask"] {
59+
let status = Command::new("cargo")
60+
.args(["fmt", "--check"])
61+
.current_dir(dir)
62+
.status()
63+
.expect("cargo should be installed");
64+
if !status.success() {
65+
return Err(format!("fmt check failed for {dir}").into());
66+
}
67+
}
68+
Ok(())
69+
}
70+
71+
fn cargo_test() -> Result<()> {
72+
eprintln!("Running cargo tests...");
73+
let status = Command::new("cargo")
74+
.arg("test")
75+
.current_dir("mdbook-spec")
76+
.status()
77+
.expect("cargo should be installed");
78+
if !status.success() {
79+
return Err("mdbook-spec test failed".into());
80+
}
81+
82+
Ok(())
83+
}
84+
85+
fn linkcheck(args: impl Iterator<Item = String>) -> Result<()> {
86+
eprintln!("Running linkcheck...");
87+
let status = Command::new("curl")
88+
.args(["-sSLo", "linkcheck.sh", "https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/linkchecker/linkcheck.sh"])
89+
.status()
90+
.expect("curl should be installed");
91+
if !status.success() {
92+
return Err("failed to fetch script from GitHub".into());
93+
}
94+
95+
let status = Command::new("sh")
96+
.args(["linkcheck.sh", "--all", "reference"])
97+
.args(args)
98+
.status()
99+
.expect("sh should be installed");
100+
if !status.success() {
101+
return Err("linkcheck failed".into());
102+
}
103+
104+
Ok(())
105+
}

0 commit comments

Comments
 (0)