| 1 |
//! The GUI test runner. |
| 2 |
//! |
| 3 |
//! This uses the browser-ui-test npm package to use a headless Chrome to |
| 4 |
//! exercise the behavior of rendered books. See `CONTRIBUTING.md` for more |
| 5 |
//! information. |
| 6 |
|
| 7 |
use serde_json::Value; |
| 8 |
use std::fs::read_to_string; |
| 9 |
use std::path::Path; |
| 10 |
use std::process::{Command, Output}; |
| 11 |
|
| 12 |
fn get_available_browser_ui_test_version_inner(global: bool) -> Option<String> { |
| 13 |
let mut command = Command::new("npm"); |
| 14 |
command |
| 15 |
.arg("list") |
| 16 |
.arg("--parseable") |
| 17 |
.arg("--long") |
| 18 |
.arg("--depth=0"); |
| 19 |
if global { |
| 20 |
command.arg("--global"); |
| 21 |
} |
| 22 |
let stdout = command.output().expect("`npm` command not found").stdout; |
| 23 |
let lines = String::from_utf8_lossy(&stdout); |
| 24 |
lines |
| 25 |
.lines() |
| 26 |
.find_map(|l| l.split(':').nth(1)?.strip_prefix("browser-ui-test@")) |
| 27 |
.map(std::borrow::ToOwned::to_owned) |
| 28 |
} |
| 29 |
|
| 30 |
fn get_available_browser_ui_test_version() -> Option<String> { |
| 31 |
get_available_browser_ui_test_version_inner(false) |
| 32 |
.or_else(|| get_available_browser_ui_test_version_inner(true)) |
| 33 |
} |
| 34 |
|
| 35 |
fn expected_browser_ui_test_version() -> String { |
| 36 |
let content = read_to_string("package.json").expect("failed to read `package.json`"); |
| 37 |
let v: Value = serde_json::from_str(&content).expect("failed to parse `package.json`"); |
| 38 |
let Some(dependencies) = v.get("dependencies") else { |
| 39 |
panic!("Missing `dependencies` key in `package.json`"); |
| 40 |
}; |
| 41 |
let Some(browser_ui_test) = dependencies.get("browser-ui-test") else { |
| 42 |
panic!("Missing `browser-ui-test` key in \"dependencies\" object in `package.json`"); |
| 43 |
}; |
| 44 |
let Value::String(version) = browser_ui_test else { |
| 45 |
panic!("`browser-ui-test` version is not a string"); |
| 46 |
}; |
| 47 |
version.trim().to_string() |
| 48 |
} |
| 49 |
|
| 50 |
fn main() { |
| 51 |
let browser_ui_test_version = expected_browser_ui_test_version(); |
| 52 |
match get_available_browser_ui_test_version() { |
| 53 |
Some(version) => { |
| 54 |
if version != browser_ui_test_version { |
| 55 |
eprintln!( |
| 56 |
"⚠️ Installed version of browser-ui-test (`{version}`) is different than the \ |
| 57 |
one used in the CI (`{browser_ui_test_version}`) You can install this version \ |
| 58 |
using `npm update browser-ui-test` or by using `npm install browser-ui-test\ |
| 59 |
@{browser_ui_test_version}`", |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
None => { |
| 64 |
panic!( |
| 65 |
"`browser-ui-test` is not installed. You can install this package using `npm \ |
| 66 |
update browser-ui-test` or by using `npm install browser-ui-test\ |
| 67 |
@{browser_ui_test_version}`", |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
let out_dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("gui"); |
| 73 |
build_books(&out_dir); |
| 74 |
run_browser_ui_test(&out_dir); |
| 75 |
} |
| 76 |
|
| 77 |
fn build_books(out_dir: &Path) { |
| 78 |
let root = Path::new(env!("CARGO_MANIFEST_DIR")); |
| 79 |
let books_dir = root.join("tests/gui/books"); |
| 80 |
for entry in books_dir.read_dir().unwrap() { |
| 81 |
let entry = entry.unwrap(); |
| 82 |
let path = entry.path(); |
| 83 |
if !path.is_dir() { |
| 84 |
continue; |
| 85 |
} |
| 86 |
println!("Building `{}`", path.display()); |
| 87 |
let mut cmd = Command::new(env!("CARGO_BIN_EXE_mdbook")); |
| 88 |
let output = cmd |
| 89 |
.arg("build") |
| 90 |
.arg("--dest-dir") |
| 91 |
.arg(out_dir.join(path.file_name().unwrap())) |
| 92 |
.arg(&path) |
| 93 |
.output() |
| 94 |
.expect("mdbook should be built"); |
| 95 |
check_status(&cmd, &output); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
fn check_status(cmd: &Command, output: &Output) { |
| 100 |
if !output.status.success() { |
| 101 |
eprintln!("error: `{cmd:?}` failed"); |
| 102 |
let stdout = std::str::from_utf8(&output.stdout).expect("stdout is not utf8"); |
| 103 |
let stderr = std::str::from_utf8(&output.stderr).expect("stderr is not utf8"); |
| 104 |
eprintln!("\n--- stdout\n{stdout}\n--- stderr\n{stderr}"); |
| 105 |
std::process::exit(1); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
fn run_browser_ui_test(out_dir: &Path) { |
| 110 |
let mut command = Command::new("npx"); |
| 111 |
let mut doc_path = format!("file://{}", out_dir.display()); |
| 112 |
if !doc_path.ends_with('/') { |
| 113 |
doc_path.push('/'); |
| 114 |
} |
| 115 |
command |
| 116 |
.arg("browser-ui-test") |
| 117 |
.args(["--variable", "DOC_PATH", doc_path.as_str()]) |
| 118 |
.args(["--display-format", "compact"]); |
| 119 |
|
| 120 |
for arg in std::env::args().skip(1) { |
| 121 |
if arg == "--disable-headless-test" { |
| 122 |
command.arg("--no-headless"); |
| 123 |
} else if arg.starts_with("--") { |
| 124 |
command.arg(arg); |
| 125 |
} else { |
| 126 |
command.args(["--filter", arg.as_str()]); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
let test_dir = "tests/gui"; |
| 131 |
command.args(["--test-folder", test_dir]); |
| 132 |
|
| 133 |
// Then we run the GUI tests on it. |
| 134 |
let status = command.status().expect("failed to get command output"); |
| 135 |
assert!(status.success(), "{status:?}"); |
| 136 |
} |