Skip to content

Commit 3842281

Browse files
authored
Merge pull request #1774 from ehuss/xtask
Add test wrappers
2 parents efe2172 + 8f6d399 commit 3842281

File tree

7 files changed

+135
-1
lines changed

7 files changed

+135
-1
lines changed

.cargo/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[alias]
2+
xtask = "run --manifest-path=xtask/Cargo.toml --"

.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'

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 xtask 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 xtask linkcheck`.
66+
67+
### Running all tests
68+
69+
As a last step before opening a PR, it is recommended to run `cargo xtask 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).

xtask/.gitignore

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

xtask/Cargo.lock

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

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]

xtask/src/main.rs

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

0 commit comments

Comments
 (0)