|
| 1 | +// Copyright 2018-2026 the Deno authors. MIT license. |
| 2 | + |
| 3 | +use std::hash::Hasher; |
| 4 | +use std::path::Path; |
| 5 | +use std::path::PathBuf; |
| 6 | +use std::time::SystemTime; |
| 7 | + |
| 8 | +use crate::eprintln; |
| 9 | + |
| 10 | +/// A fast hasher for computing a combined hash of files and directory mtimes. |
| 11 | +/// Uses xxhash64 for speed. |
| 12 | +pub struct InputHasher(twox_hash::XxHash64); |
| 13 | + |
| 14 | +impl InputHasher { |
| 15 | + pub fn new_with_cli_args() -> Self { |
| 16 | + let mut hasher = Self(Default::default()); |
| 17 | + for arg in std::env::args() { |
| 18 | + hasher.0.write(arg.as_bytes()); |
| 19 | + } |
| 20 | + hasher |
| 21 | + } |
| 22 | + |
| 23 | + /// Hash a single file's mtime. Skips if file doesn't exist. |
| 24 | + pub fn hash_file(&mut self, path: impl AsRef<Path>) -> &mut Self { |
| 25 | + if let Ok(meta) = std::fs::metadata(path.as_ref()) { |
| 26 | + self.hash_mtime(meta.modified().ok()); |
| 27 | + } |
| 28 | + self |
| 29 | + } |
| 30 | + |
| 31 | + /// Recursively hash all file mtimes in a directory (sorted for determinism). |
| 32 | + /// Skips if directory doesn't exist. |
| 33 | + pub fn hash_dir(&mut self, path: impl AsRef<Path>) -> &mut Self { |
| 34 | + let path = path.as_ref(); |
| 35 | + let mut entries = Vec::new(); |
| 36 | + collect_entries_recursive(path, &mut entries); |
| 37 | + entries.sort_by(|(a, _), (b, _)| a.cmp(b)); |
| 38 | + for (_, mtime) in &entries { |
| 39 | + self.hash_mtime(*mtime); |
| 40 | + } |
| 41 | + self |
| 42 | + } |
| 43 | + |
| 44 | + fn hash_mtime(&mut self, mtime: Option<SystemTime>) { |
| 45 | + if let Some(mtime) = mtime |
| 46 | + && let Ok(d) = mtime.duration_since(SystemTime::UNIX_EPOCH) |
| 47 | + { |
| 48 | + self.0.write_u64(d.as_secs()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + pub fn finish(&self) -> u64 { |
| 53 | + self.0.finish() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn collect_entries_recursive( |
| 58 | + dir: &Path, |
| 59 | + out: &mut Vec<(PathBuf, Option<SystemTime>)>, |
| 60 | +) { |
| 61 | + let entries = match std::fs::read_dir(dir) { |
| 62 | + Ok(entries) => entries, |
| 63 | + Err(_) => return, |
| 64 | + }; |
| 65 | + for entry in entries.flatten() { |
| 66 | + let file_type = match entry.file_type() { |
| 67 | + Ok(ft) => ft, |
| 68 | + Err(_) => continue, |
| 69 | + }; |
| 70 | + if file_type.is_dir() { |
| 71 | + collect_entries_recursive(&entry.path(), out); |
| 72 | + } else { |
| 73 | + let mtime = entry.metadata().ok().and_then(|m| m.modified().ok()); |
| 74 | + out.push((entry.path(), mtime)); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// Check if tests can be skipped on CI by comparing input hashes. |
| 80 | +/// |
| 81 | +/// `name` is used for the hash file name and log messages (e.g. "specs", |
| 82 | +/// "unit"). `configure` receives an `InputHasher` to add whatever files/dirs |
| 83 | +/// are relevant. |
| 84 | +/// |
| 85 | +/// Returns `true` if the hash is unchanged and tests should be skipped. |
| 86 | +/// |
| 87 | +/// ```ignore |
| 88 | +/// if test_util::hash::should_skip_on_ci("specs", |hasher| { |
| 89 | +/// hasher |
| 90 | +/// .hash_dir(tests.join("specs")) |
| 91 | +/// .hash_file(deno_exe_path()); |
| 92 | +/// }) { |
| 93 | +/// return; |
| 94 | +/// } |
| 95 | +/// ``` |
| 96 | +pub fn should_skip_on_ci( |
| 97 | + name: &str, |
| 98 | + configure: impl FnOnce(&mut InputHasher), |
| 99 | +) -> bool { |
| 100 | + if !*crate::IS_CI { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + let start = std::time::Instant::now(); |
| 105 | + let hash_path = crate::target_dir() |
| 106 | + .join(format!("{name}_input_hash")) |
| 107 | + .to_path_buf(); |
| 108 | + let mut hasher = InputHasher::new_with_cli_args(); |
| 109 | + configure(&mut hasher); |
| 110 | + let new_hash = hasher.finish().to_string(); |
| 111 | + |
| 112 | + eprintln!("ci hash took {}ms", start.elapsed().as_millis()); |
| 113 | + |
| 114 | + if let Ok(old_hash) = std::fs::read_to_string(&hash_path) |
| 115 | + && old_hash.trim() == new_hash |
| 116 | + { |
| 117 | + eprintln!("{name} input hash unchanged ({new_hash}), skipping"); |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + eprintln!("{name} input hash changed, writing new hash ({new_hash})"); |
| 122 | + std::fs::write(&hash_path, &new_hash).ok(); |
| 123 | + false |
| 124 | +} |
0 commit comments