|
| 1 | +// This test ensures that the clippy lints page is working as expected. |
| 2 | + |
| 3 | +use std::ffi::OsStr; |
| 4 | +use std::path::Path; |
| 5 | +use std::process::Command; |
| 6 | +use std::time::SystemTime; |
| 7 | + |
| 8 | +const BROWSER_UI_TEST_VERSION: &str = "0.18.2"; |
| 9 | + |
| 10 | +fn get_browser_ui_test_version_inner(global: bool) -> Option<String> { |
| 11 | + let mut command = Command::new("npm"); |
| 12 | + command.arg("list").arg("--parseable").arg("--long").arg("--depth=0"); |
| 13 | + if global { |
| 14 | + command.arg("--global"); |
| 15 | + } |
| 16 | + let stdout = command.output().expect("`npm` command not found").stdout; |
| 17 | + let lines = String::from_utf8_lossy(&stdout); |
| 18 | + lines |
| 19 | + .lines() |
| 20 | + .find_map(|l| l.split(':').nth(1)?.strip_prefix("browser-ui-test@")) |
| 21 | + .map(|v| v.to_owned()) |
| 22 | +} |
| 23 | + |
| 24 | +fn get_browser_ui_test_version() -> Option<String> { |
| 25 | + get_browser_ui_test_version_inner(false).or_else(|| get_browser_ui_test_version_inner(true)) |
| 26 | +} |
| 27 | + |
| 28 | +fn mtime(path: impl AsRef<Path>) -> SystemTime { |
| 29 | + let path = path.as_ref(); |
| 30 | + if path.is_dir() { |
| 31 | + path.read_dir() |
| 32 | + .into_iter() |
| 33 | + .flatten() |
| 34 | + .flatten() |
| 35 | + .map(|entry| mtime(entry.path())) |
| 36 | + .max() |
| 37 | + .unwrap_or(SystemTime::UNIX_EPOCH) |
| 38 | + } else { |
| 39 | + path.metadata() |
| 40 | + .and_then(|metadata| metadata.modified()) |
| 41 | + .unwrap_or(SystemTime::UNIX_EPOCH) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[test] |
| 46 | +fn check_clippy_lints_page() { |
| 47 | + // do not run this test inside the upstream rustc repo. |
| 48 | + if option_env!("RUSTC_TEST_SUITE").is_some() { |
| 49 | + return; |
| 50 | + } |
| 51 | + match get_browser_ui_test_version() { |
| 52 | + Some(version) => { |
| 53 | + if version != BROWSER_UI_TEST_VERSION { |
| 54 | + eprintln!( |
| 55 | + "⚠️ Installed version of browser-ui-test (`{version}`) is different than the \ |
| 56 | + one used in the CI (`{BROWSER_UI_TEST_VERSION}`) You can install this version \ |
| 57 | + using `npm update browser-ui-test` or by using `npm install browser-ui-test\ |
| 58 | + @{BROWSER_UI_TEST_VERSION}`", |
| 59 | + ); |
| 60 | + } |
| 61 | + }, |
| 62 | + None => { |
| 63 | + panic!( |
| 64 | + "`browser-ui-test` is not installed. You can install this package using `npm \ |
| 65 | + update browser-ui-test` or by using `npm install browser-ui-test@{}`", |
| 66 | + BROWSER_UI_TEST_VERSION, |
| 67 | + ); |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + // We build the lints page only if needed. |
| 72 | + let index_time = mtime("util/gh-pages/index.html"); |
| 73 | + |
| 74 | + if index_time < mtime("clippy_lints/src") || index_time < mtime("util/gh-pages/index_template.html") { |
| 75 | + if !Command::new("cargo") |
| 76 | + .arg("collect-metadata") |
| 77 | + .status() |
| 78 | + .is_ok_and(|status| status.success()) |
| 79 | + { |
| 80 | + panic!("failed to run `cargo collect-metadata`"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + let current_dir = std::env::current_dir() |
| 85 | + .expect("failed to retrieve current directory") |
| 86 | + .join("util/gh-pages/index.html"); |
| 87 | + let current_dir = format!("file://{}", current_dir.display()); |
| 88 | + let mut command = Command::new("npx"); |
| 89 | + command |
| 90 | + .arg("browser-ui-test") |
| 91 | + .args(&["--variable", "DOC_PATH", current_dir.as_str()]) |
| 92 | + .args(&["--test-folder", "tests/gui"]); |
| 93 | + if std::env::var_os("DISABLE_HEADLESS_TEST").is_some_and(|value| value == OsStr::new("1")) { |
| 94 | + command.arg("--no-headless"); |
| 95 | + } |
| 96 | + |
| 97 | + // Then we run the GUI tests on it. |
| 98 | + assert!(command.status().is_ok_and(|status| status.success())); |
| 99 | +} |
0 commit comments