2025-07-21 10:14:25 -07:00
|
|
|
//! The GUI test runner.
|
|
|
|
|
//!
|
|
|
|
|
//! This uses the browser-ui-test npm package to use a headless Chrome to
|
|
|
|
|
//! exercise the behavior of rendered books. See `CONTRIBUTING.md` for more
|
|
|
|
|
//! information.
|
|
|
|
|
|
2025-03-21 12:14:38 +01:00
|
|
|
use serde_json::Value;
|
2025-10-13 20:07:38 -07:00
|
|
|
use std::fs::read_to_string;
|
2025-10-15 14:05:23 -07:00
|
|
|
use std::path::Path;
|
2025-10-13 20:07:38 -07:00
|
|
|
use std::process::{Command, Output};
|
2024-11-08 15:15:45 +01:00
|
|
|
|
|
|
|
|
fn get_available_browser_ui_test_version_inner(global: bool) -> Option<String> {
|
|
|
|
|
let mut command = Command::new("npm");
|
|
|
|
|
command
|
|
|
|
|
.arg("list")
|
|
|
|
|
.arg("--parseable")
|
|
|
|
|
.arg("--long")
|
|
|
|
|
.arg("--depth=0");
|
|
|
|
|
if global {
|
|
|
|
|
command.arg("--global");
|
|
|
|
|
}
|
|
|
|
|
let stdout = command.output().expect("`npm` command not found").stdout;
|
|
|
|
|
let lines = String::from_utf8_lossy(&stdout);
|
|
|
|
|
lines
|
|
|
|
|
.lines()
|
|
|
|
|
.find_map(|l| l.split(':').nth(1)?.strip_prefix("browser-ui-test@"))
|
|
|
|
|
.map(std::borrow::ToOwned::to_owned)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_available_browser_ui_test_version() -> Option<String> {
|
|
|
|
|
get_available_browser_ui_test_version_inner(false)
|
|
|
|
|
.or_else(|| get_available_browser_ui_test_version_inner(true))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn expected_browser_ui_test_version() -> String {
|
2025-02-21 22:55:23 +01:00
|
|
|
let content = read_to_string("package.json").expect("failed to read `package.json`");
|
2025-03-21 12:14:38 +01:00
|
|
|
let v: Value = serde_json::from_str(&content).expect("failed to parse `package.json`");
|
|
|
|
|
let Some(dependencies) = v.get("dependencies") else {
|
|
|
|
|
panic!("Missing `dependencies` key in `package.json`");
|
|
|
|
|
};
|
|
|
|
|
let Some(browser_ui_test) = dependencies.get("browser-ui-test") else {
|
|
|
|
|
panic!("Missing `browser-ui-test` key in \"dependencies\" object in `package.json`");
|
|
|
|
|
};
|
|
|
|
|
let Value::String(version) = browser_ui_test else {
|
|
|
|
|
panic!("`browser-ui-test` version is not a string");
|
|
|
|
|
};
|
|
|
|
|
version.trim().to_string()
|
2024-11-08 15:15:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let browser_ui_test_version = expected_browser_ui_test_version();
|
|
|
|
|
match get_available_browser_ui_test_version() {
|
|
|
|
|
Some(version) => {
|
|
|
|
|
if version != browser_ui_test_version {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"⚠️ Installed version of browser-ui-test (`{version}`) is different than the \
|
|
|
|
|
one used in the CI (`{browser_ui_test_version}`) You can install this version \
|
|
|
|
|
using `npm update browser-ui-test` or by using `npm install browser-ui-test\
|
|
|
|
|
@{browser_ui_test_version}`",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
panic!(
|
|
|
|
|
"`browser-ui-test` is not installed. You can install this package using `npm \
|
|
|
|
|
update browser-ui-test` or by using `npm install browser-ui-test\
|
|
|
|
|
@{browser_ui_test_version}`",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 20:07:38 -07:00
|
|
|
let out_dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("gui");
|
|
|
|
|
build_books(&out_dir);
|
|
|
|
|
run_browser_ui_test(&out_dir);
|
|
|
|
|
}
|
2024-11-08 15:15:45 +01:00
|
|
|
|
2025-10-13 20:07:38 -07:00
|
|
|
fn build_books(out_dir: &Path) {
|
|
|
|
|
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
|
let books_dir = root.join("tests/gui/books");
|
|
|
|
|
for entry in books_dir.read_dir().unwrap() {
|
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
if !path.is_dir() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
println!("Building `{}`", path.display());
|
2025-10-15 14:05:23 -07:00
|
|
|
let mut cmd = Command::new(env!("CARGO_BIN_EXE_mdbook"));
|
2025-10-13 20:07:38 -07:00
|
|
|
let output = cmd
|
|
|
|
|
.arg("build")
|
|
|
|
|
.arg("--dest-dir")
|
|
|
|
|
.arg(out_dir.join(path.file_name().unwrap()))
|
|
|
|
|
.arg(&path)
|
|
|
|
|
.output()
|
|
|
|
|
.expect("mdbook should be built");
|
|
|
|
|
check_status(&cmd, &output);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-08 15:15:45 +01:00
|
|
|
|
2025-10-13 20:07:38 -07:00
|
|
|
fn check_status(cmd: &Command, output: &Output) {
|
|
|
|
|
if !output.status.success() {
|
|
|
|
|
eprintln!("error: `{cmd:?}` failed");
|
|
|
|
|
let stdout = std::str::from_utf8(&output.stdout).expect("stdout is not utf8");
|
|
|
|
|
let stderr = std::str::from_utf8(&output.stderr).expect("stderr is not utf8");
|
|
|
|
|
eprintln!("\n--- stdout\n{stdout}\n--- stderr\n{stderr}");
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-08 15:15:45 +01:00
|
|
|
|
2025-10-13 20:07:38 -07:00
|
|
|
fn run_browser_ui_test(out_dir: &Path) {
|
2025-08-26 20:38:24 -07:00
|
|
|
let mut command = Command::new("npx");
|
2025-10-13 20:07:38 -07:00
|
|
|
let mut doc_path = format!("file://{}", out_dir.display());
|
|
|
|
|
if !doc_path.ends_with('/') {
|
|
|
|
|
doc_path.push('/');
|
|
|
|
|
}
|
2025-08-26 20:38:24 -07:00
|
|
|
command
|
|
|
|
|
.arg("browser-ui-test")
|
2025-10-13 20:07:38 -07:00
|
|
|
.args(["--variable", "DOC_PATH", doc_path.as_str()])
|
2025-09-08 17:30:48 +02:00
|
|
|
.args(["--display-format", "compact"]);
|
2025-08-26 20:38:24 -07:00
|
|
|
|
2025-03-20 18:13:59 +02:00
|
|
|
for arg in std::env::args().skip(1) {
|
2025-03-10 13:31:40 +01:00
|
|
|
if arg == "--disable-headless-test" {
|
2025-08-26 20:38:24 -07:00
|
|
|
command.arg("--no-headless");
|
|
|
|
|
} else if arg.starts_with("--") {
|
|
|
|
|
command.arg(arg);
|
2025-03-10 13:31:40 +01:00
|
|
|
} else {
|
2025-09-08 17:30:48 +02:00
|
|
|
command.args(["--filter", arg.as_str()]);
|
2025-03-10 13:31:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let test_dir = "tests/gui";
|
2025-09-08 17:30:48 +02:00
|
|
|
command.args(["--test-folder", test_dir]);
|
2025-03-10 13:31:40 +01:00
|
|
|
|
2024-11-08 15:15:45 +01:00
|
|
|
// Then we run the GUI tests on it.
|
|
|
|
|
let status = command.status().expect("failed to get command output");
|
2024-12-16 23:45:29 +01:00
|
|
|
assert!(status.success(), "{status:?}");
|
2024-11-08 15:15:45 +01:00
|
|
|
}
|