//! Find in files over real directories: matching, rules, caps, cancel. #![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::time::{Duration, Instant}; use lutra_search::{ CancelToken, FileMatches, MAX_FILE_SIZE, Overlay, Query, Search, SearchError, SearchEvent, Summary, run, spawn, }; use proptest::prelude::*; /// Runs `query` over `root` and returns the files sorted by path. fn find(root: &Path, query: &Query, overlay: &Overlay) -> (Vec, Summary) { let search = Search::new(query).unwrap(); let found = Mutex::new(Vec::new()); let summary = run(root, &search, overlay, &CancelToken::new(), &|f| { found.lock().unwrap().push(f) }); let mut files = found.into_inner().unwrap(); files.sort_by(|a, b| a.path.cmp(&b.path)); (files, summary) } fn paths(files: &[FileMatches]) -> Vec<&str> { files.iter().map(|f| f.path.as_str()).collect() } fn put(root: &Path, rel: &str, text: &[u8]) { let path = root.join(rel); fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path, text).unwrap(); } #[test] fn literal_case_and_whole_word() { let dir = tempfile::tempdir().unwrap(); put( dir.path(), "a.php", b" 0); assert_eq!(summary.files, tree.needle_files + 1); assert!(!summary.truncated && !summary.cancelled); } #[test] fn skips_large_and_binary_files() { let dir = tempfile::tempdir().unwrap(); let mut big = b"needle\n".to_vec(); big.resize(usize::try_from(MAX_FILE_SIZE).unwrap() + 1, b'a'); put(dir.path(), "big.txt", &big); let mut late_nul = b"needle\n".to_vec(); late_nul.extend(b"filler\n".repeat(50_000)); late_nul.push(0); put(dir.path(), "late.bin", &late_nul); put(dir.path(), "ok.txt", b"needle\n"); let (files, _) = find(dir.path(), &Query::literal("needle"), &Overlay::new()); assert_eq!(paths(&files), ["ok.txt"]); } #[test] fn overlay_replaces_the_disk_text() { let dir = tempfile::tempdir().unwrap(); put(dir.path(), "a.php", b"old text\n"); put(dir.path(), "b.php", b"needle on disk\n"); let mut overlay = Overlay::new(); overlay.insert("a.php".into(), "line\n needle in buffer\n".into()); overlay.insert("b.php".into(), "gone\n".into()); overlay.insert("missing.php".into(), "needle\n".into()); let (files, _) = find(dir.path(), &Query::literal("needle"), &overlay); assert_eq!(paths(&files), ["a.php"]); assert_eq!(files[0].matches[0].line, 1); assert_eq!(files[0].matches[0].column, 2); } #[test] fn caps_the_number_of_matches() { let dir = tempfile::tempdir().unwrap(); for i in 0..20 { put(dir.path(), &format!("f{i}.txt"), b"x x x x x\n"); } let mut q = Query::literal("x"); q.max_results = 7; let (files, summary) = find(dir.path(), &q, &Overlay::new()); assert_eq!(summary.matches, 7); assert!(summary.truncated); assert_eq!(files.iter().map(|f| f.matches.len()).sum::(), 7); assert_eq!(summary.files, files.len()); q.max_results = 100; let (_, summary) = find(dir.path(), &q, &Overlay::new()); assert_eq!(summary.matches, 100); assert!( !summary.truncated, "exactly at the cap with nothing left out" ); q.max_results = 99; assert!(find(dir.path(), &q, &Overlay::new()).1.truncated); } #[test] fn spawn_streams_and_cancels() { let dir = tempfile::tempdir().unwrap(); let tree = lutra_testkit::generate_search_tree(dir.path(), 300); let search = Search::new(&Query::literal(lutra_testkit::NEEDLE)).unwrap(); let rx = spawn( dir.path().into(), search.clone(), Overlay::new(), CancelToken::new(), ) .unwrap(); let events: Vec = rx.iter().collect(); let files = events .iter() .filter(|e| matches!(e, SearchEvent::File(_))) .count(); assert_eq!(files, tree.needle_files); let Some(SearchEvent::Done(summary)) = events.last() else { panic!("no summary last: {events:?}"); }; assert_eq!(summary.files, files); let cancel = CancelToken::new(); cancel.cancel(); let rx = spawn(dir.path().into(), search, Overlay::new(), cancel).unwrap(); let events: Vec = rx.iter().collect(); assert_eq!(events.len(), 1); let SearchEvent::Done(summary) = &events[0] else { panic!("{events:?}"); }; assert!(summary.cancelled); assert_eq!(summary.matches, 0); } /// Measures the first result and the total on a generated tree of 20,000 /// files. Run in release: `cargo test -p lutra-search --release --test /// search -- --ignored --nocapture measure`. #[test] #[ignore = "measurement; run explicitly in release"] fn measure_20k_tree() { let dir = tempfile::tempdir().unwrap(); let made = Instant::now(); let tree = lutra_testkit::generate_search_tree(dir.path(), 20_000); println!("generated {} files in {:?}", tree.files, made.elapsed()); for (label, pattern) in [ ("common literal", "function"), ("rare literal", lutra_testkit::NEEDLE), ] { for round in 0..3 { let search = Search::new(&Query::literal(pattern)).unwrap(); let started = Instant::now(); let rx = spawn( dir.path().into(), search, Overlay::new(), CancelToken::new(), ) .unwrap(); let mut first: Option = None; let mut summary = None; for event in rx { match event { SearchEvent::File(_) => { first.get_or_insert_with(|| started.elapsed()); } SearchEvent::Done(s) => summary = Some(s), } } let s = summary.unwrap(); println!( "{label} round {round}: first {:?}, total {:?}, files {}, matches {}, truncated {}", first.unwrap(), s.elapsed, s.files, s.matches, s.truncated ); } } let mut q = Query::literal("function"); q.max_results = lutra_search::MAX_RESULTS_LIMIT; let search = Search::new(&q).unwrap(); let s = run( dir.path(), &search, &Overlay::new(), &CancelToken::new(), &|_| {}, ); println!( "common literal, cap 100k: total {:?}, files {}, matches {}, truncated {}", s.elapsed, s.files, s.matches, s.truncated ); } proptest! { #![proptest_config(ProptestConfig::with_cases(64))] /// Random patterns, flags and file contents never panic, and every /// reported match lies inside its preview. #[test] fn random_queries_never_panic( pattern in "\\PC{0,12}", regex in any::(), case_sensitive in any::(), whole_word in any::(), content in proptest::collection::vec(any::(), 0..400), text in "\\PC{0,200}", ) { let dir = tempfile::tempdir().unwrap(); fs::write(dir.path().join("bytes.txt"), &content).unwrap(); fs::write(dir.path().join("text.php"), &text).unwrap(); let query = Query { pattern, regex, case_sensitive, whole_word, include: vec![], exclude: vec![], max_results: 50, }; let Ok(search) = Search::new(&query) else { return Ok(()); }; let found = Mutex::new(Vec::new()); let summary = run(dir.path(), &search, &Overlay::new(), &CancelToken::new(), &|f| { found.lock().unwrap().push(f) }); let found = found.into_inner().unwrap(); prop_assert!(summary.matches <= 50); for file in found { for m in file.matches { prop_assert!(m.length > 0); let units = m.preview.encode_utf16().count(); prop_assert!(m.preview_column + m.length <= units || m.preview_column <= units); } } } }