Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test wrappers #1774

Merged
merged 4 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --manifest-path=xtask/Cargo.toml --"
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ jobs:
- name: Rustfmt check
working-directory: ./mdbook-spec
run: cargo fmt --check
- name: Xtask rustfmt check
working-directory: ./xtask
run: cargo fmt --check

preview:
if: github.event_name == 'pull_request'
Expand Down
12 changes: 11 additions & 1 deletion docs/authoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This document serves as a guide for editors and reviewers. Some conventions and
```
* See the [Conventions] section for formatting callouts such as notes, edition differences, and warnings.

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

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

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

You can verify the samples pass by running `mdbook test`.

### Linkcheck

To verify that links are not broken, run `cargo xtask linkcheck`.

### Running all tests

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.

## Special markdown constructs

The following are extensions provided by [`mdbook-spec`](https://github.com/rust-lang/spec/tree/main/mdbook-spec).
Expand Down
1 change: 1 addition & 0 deletions xtask/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions xtask/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "xtask"
edition = "2024"

[dependencies]
106 changes: 106 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use std::error::Error;
use std::process::Command;
use std::process::exit;

type Result<T> = std::result::Result<T, Box<dyn Error>>;

fn main() -> Result<()> {
let mut args = std::env::args().skip(1);
let cmd = args.next();
const OPTIONS: &str = "linkcheck, style-check, test-all";
match cmd.as_deref() {
Some("test-all") => {
mdbook_test()?;
style_check()?;
fmt()?;
linkcheck(args)?;
cargo_test()?;
eprintln!("all tests passed!");
}
Some("linkcheck") => linkcheck(args)?,
Some("style-check") => style_check()?,
Some("-h" | "--help") => eprintln!("valid options: {OPTIONS}"),
Some(x) => {
eprintln!("error: unknown command `{x}` (valid options: {OPTIONS})");
exit(1);
}
None => {
eprintln!("error: specify a command (valid options: {OPTIONS})");
exit(1);
}
}
Ok(())
}

fn mdbook_test() -> Result<()> {
eprintln!("Testing inline code tests...");
let status = Command::new("mdbook")
.arg("test")
.status()
.expect("mdbook should be installed");
if !status.success() {
return Err("inline code tests failed".into());
}
Ok(())
}

fn style_check() -> Result<()> {
eprintln!("Running style checks...");
let status = Command::new("cargo")
.args(["run", "--manifest-path=style-check/Cargo.toml", "--", "src"])
.status()
.expect("cargo should be installed");
if !status.success() {
return Err("style check failed".into());
}
Ok(())
}

fn fmt() -> Result<()> {
eprintln!("Checking code formatting...");
for dir in ["style-check", "mdbook-spec", "xtask"] {
let status = Command::new("cargo")
.args(["fmt", "--check"])
.current_dir(dir)
.status()
.expect("cargo should be installed");
if !status.success() {
return Err(format!("fmt check failed for {dir}").into());
}
}
Ok(())
}

fn cargo_test() -> Result<()> {
eprintln!("Running cargo tests...");
let status = Command::new("cargo")
.arg("test")
.current_dir("mdbook-spec")
.status()
.expect("cargo should be installed");
if !status.success() {
return Err("mdbook-spec test failed".into());
}
Ok(())
}

fn linkcheck(args: impl Iterator<Item = String>) -> Result<()> {
eprintln!("Running linkcheck...");
let status = Command::new("curl")
.args(["-sSLo", "linkcheck.sh", "https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/linkchecker/linkcheck.sh"])
.status()
.expect("curl should be installed");
if !status.success() {
return Err("failed to fetch script from GitHub".into());
}

let status = Command::new("sh")
.args(["linkcheck.sh", "--all", "reference"])
.args(args)
.status()
.expect("sh should be installed");
if !status.success() {
return Err("linkcheck failed".into());
}
Ok(())
}