|
| 1 | +use std::env::current_dir; |
| 2 | +use std::fs::{read_to_string, remove_dir_all}; |
| 3 | +use std::process::Command; |
| 4 | + |
| 5 | +fn get_available_browser_ui_test_version_inner(global: bool) -> Option<String> { |
| 6 | + let mut command = Command::new("npm"); |
| 7 | + command |
| 8 | + .arg("list") |
| 9 | + .arg("--parseable") |
| 10 | + .arg("--long") |
| 11 | + .arg("--depth=0"); |
| 12 | + if global { |
| 13 | + command.arg("--global"); |
| 14 | + } |
| 15 | + let stdout = command.output().expect("`npm` command not found").stdout; |
| 16 | + let lines = String::from_utf8_lossy(&stdout); |
| 17 | + lines |
| 18 | + .lines() |
| 19 | + .find_map(|l| l.split(':').nth(1)?.strip_prefix("browser-ui-test@")) |
| 20 | + .map(std::borrow::ToOwned::to_owned) |
| 21 | +} |
| 22 | + |
| 23 | +fn get_available_browser_ui_test_version() -> Option<String> { |
| 24 | + get_available_browser_ui_test_version_inner(false) |
| 25 | + .or_else(|| get_available_browser_ui_test_version_inner(true)) |
| 26 | +} |
| 27 | + |
| 28 | +fn expected_browser_ui_test_version() -> String { |
| 29 | + let content = read_to_string(".github/workflows/main.yml") |
| 30 | + .expect("failed to read `.github/workflows/main.yml`"); |
| 31 | + for line in content.lines() { |
| 32 | + let line = line.trim(); |
| 33 | + if let Some(version) = line.strip_prefix("BROWSER_UI_TEST_VERSION:") { |
| 34 | + return version.trim().replace('\'', ""); |
| 35 | + } |
| 36 | + } |
| 37 | + panic!("failed to retrieved `browser-ui-test` version"); |
| 38 | +} |
| 39 | + |
| 40 | +fn main() { |
| 41 | + let browser_ui_test_version = expected_browser_ui_test_version(); |
| 42 | + match get_available_browser_ui_test_version() { |
| 43 | + Some(version) => { |
| 44 | + if version != browser_ui_test_version { |
| 45 | + eprintln!( |
| 46 | + "⚠️ Installed version of browser-ui-test (`{version}`) is different than the \ |
| 47 | + one used in the CI (`{browser_ui_test_version}`) You can install this version \ |
| 48 | + using `npm update browser-ui-test` or by using `npm install browser-ui-test\ |
| 49 | + @{browser_ui_test_version}`", |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + None => { |
| 54 | + panic!( |
| 55 | + "`browser-ui-test` is not installed. You can install this package using `npm \ |
| 56 | + update browser-ui-test` or by using `npm install browser-ui-test\ |
| 57 | + @{browser_ui_test_version}`", |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + let current_dir = current_dir().expect("failed to retrieve current directory"); |
| 63 | + let test_book = current_dir.join("test_book"); |
| 64 | + |
| 65 | + // Result doesn't matter. |
| 66 | + let _ = remove_dir_all(test_book.join("book")); |
| 67 | + |
| 68 | + let mut cmd = Command::new("cargo"); |
| 69 | + cmd.arg("run").arg("build").arg(&test_book); |
| 70 | + // Then we run the GUI tests on it. |
| 71 | + assert!(cmd.status().is_ok_and(|status| status.success())); |
| 72 | + |
| 73 | + let book_dir = format!("file://{}", current_dir.join("test_book/book/").display()); |
| 74 | + |
| 75 | + let mut command = Command::new("npx"); |
| 76 | + command |
| 77 | + .arg("browser-ui-test") |
| 78 | + .args(["--variable", "DOC_PATH", book_dir.as_str()]) |
| 79 | + .args(["--test-folder", "tests/gui"]); |
| 80 | + if std::env::args().any(|arg| arg == "--disable-headless-test") { |
| 81 | + command.arg("--no-headless"); |
| 82 | + } |
| 83 | + |
| 84 | + // Then we run the GUI tests on it. |
| 85 | + let status = command.status().expect("failed to get command status()"); |
| 86 | + assert!(status.success()); |
| 87 | +} |
0 commit comments