//! Building the index of a project on disk, incremental updates, and //! cancellation. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing, clippy::panic )] use std::fs; use std::path::Path; use std::sync::Mutex; use std::sync::mpsc::{Receiver, channel}; use std::time::Duration; use lutra_index::{CancelToken, Index, Job, Update, Updater, build, spawn_build}; /// Builds the index of `root` and applies every update. fn build_index(root: &Path, threads: usize) -> (Index, lutra_index::Summary, Vec<(usize, usize)>) { let updates = Mutex::new(Vec::new()); let summary = build(root, threads, &CancelToken::new(), &|u| { updates.lock().unwrap().push(u) }); let mut index = Index::new(); let mut progress = Vec::new(); for update in updates.into_inner().unwrap() { match update { Update::Files { files, live } => { assert!(!live); for file in files { assert!(index.insert(file)); } } Update::Progress { done, total } => progress.push((done, total)), other => panic!("unexpected {other:?}"), } } (index, summary, progress) } /// Applies updates from `rx` until one batch arrives and `rx` is quiet. fn apply_until_quiet(index: &mut Index, rx: &Receiver) { let first = rx.recv_timeout(Duration::from_secs(10)).expect("an update"); let mut pending = vec![first]; while let Ok(next) = rx.recv_timeout(Duration::from_millis(300)) { pending.push(next); } for update in pending { match update { Update::Files { files, live } => { assert!(live); for file in files { index.insert(file); } } Update::Removed { path } => { index.remove(&path); } other => panic!("unexpected {other:?}"), } } } fn write(root: &Path, rel: &str, text: &str) { let path = root.join(rel); fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path, text).unwrap(); } #[test] fn builds_with_the_project_rules() { let dir = tempfile::tempdir().unwrap(); let project = lutra_testkit::generate_php_project(dir.path(), 400); write( dir.path(), "src/Broken.php", " 0); } } #[test] fn spawned_build_ends_with_ready_and_cancels() { let dir = tempfile::tempdir().unwrap(); let project = lutra_testkit::generate_php_project(dir.path(), 300); let (tx, rx) = channel(); spawn_build(dir.path().to_path_buf(), 2, CancelToken::new(), tx).unwrap(); let mut files = 0; let summary = loop { match rx.recv_timeout(Duration::from_secs(30)).unwrap() { Update::Files { files: batch, .. } => files += batch.len(), Update::Ready(summary) => break summary, _ => {} } }; assert_eq!(files, project.indexed_files); assert_eq!(summary.files, files); assert!(rx.recv_timeout(Duration::from_millis(200)).is_err()); let cancel = CancelToken::new(); cancel.cancel(); let (tx, rx) = channel(); spawn_build(dir.path().to_path_buf(), 2, cancel, tx).unwrap(); let summary = loop { if let Update::Ready(summary) = rx.recv_timeout(Duration::from_secs(30)).unwrap() { break summary; } }; assert!(summary.cancelled); assert!(summary.files < project.indexed_files); } #[test] fn updater_follows_disk_and_buffers() { let dir = tempfile::tempdir().unwrap(); let root = dir.path(); write(root, ".gitignore", "*.log\n/ignored/\n"); write(root, "src/A.php", "