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