use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use crate::file::paths::{FileProvider, join_normalized, normalize_path};
use crate::include::config::CompilerConfig;
use crate::index::ProjectIndex;
use crate::index::store::SummaryStore;
use crate::preprocess::directive::IncludeForm;
use crate::summary::{FileSummary, IncludeFact};
const COMPILE_DATABASE: &str = "compile_commands.json";
const OVERWHELMING_BATCH: usize = 256;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventKind {
Created,
Modified,
Removed,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileEvent {
pub path: PathBuf,
pub kind: EventKind,
}
impl FileEvent {
pub fn created(path: impl Into<PathBuf>) -> Self {
FileEvent {
path: path.into(),
kind: EventKind::Created,
}
}
pub fn modified(path: impl Into<PathBuf>) -> Self {
FileEvent {
path: path.into(),
kind: EventKind::Modified,
}
}
pub fn removed(path: impl Into<PathBuf>) -> Self {
FileEvent {
path: path.into(),
kind: EventKind::Removed,
}
}
}
#[derive(Debug, Clone)]
pub struct WatchFilter {
root: PathBuf,
cache: PathBuf,
configuration: PathBuf,
ignored: Vec<PathBuf>,
ignored_patterns: Vec<PathPattern>,
}
impl WatchFilter {
pub fn new(root: impl AsRef<Path>) -> Self {
let root = root.as_ref();
WatchFilter {
root: root.to_path_buf(),
cache: root.join(crate::CACHE_DIRECTORY),
configuration: root.join(COMPILE_DATABASE),
ignored: Vec::new(),
ignored_patterns: Vec::new(),
}
}
pub fn ignore(mut self, directory: impl Into<PathBuf>) -> Self {
self.ignored.push(directory.into());
self
}
pub fn ignore_pattern(mut self, pattern: PathPattern) -> Self {
self.ignored_patterns.push(pattern);
self
}
pub fn with_configuration(mut self, path: impl Into<PathBuf>) -> Self {
self.configuration = path.into();
self
}
pub fn with_cache_directory(mut self, name: &str) -> Self {
if !name.is_empty() {
self.cache = self.root.join(name);
}
self
}
pub fn is_ignored(&self, path: &Path) -> bool {
let path = normalize(path);
if under(&path, &normalize(&self.cache)) {
return true;
}
if path.split('/').any(|segment| segment == ".git") {
return true;
}
if self
.ignored
.iter()
.any(|directory| under(&path, &normalize(directory)))
{
return true;
}
if self.ignored_patterns.is_empty() {
return false;
}
let root = normalize(&self.root);
let relative = path
.strip_prefix(&root)
.map(|rest| rest.trim_start_matches('/'));
self.ignored_patterns.iter().any(|pattern| {
pattern.matches(Path::new(&path))
|| relative.is_some_and(|relative| pattern.matches(Path::new(relative)))
})
}
pub fn is_configuration(&self, path: &Path) -> bool {
self.is_configuration_file(path)
}
pub fn is_configuration_file(&self, path: &Path) -> bool {
if normalize(path) == normalize(&self.configuration) {
return true;
}
path.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| {
crate::project::CONFIGURATION_FILE_NAMES
.iter()
.any(|known| known.eq_ignore_ascii_case(name))
})
}
}
const PATTERN_OPTIONS: glob::MatchOptions = glob::MatchOptions {
case_sensitive: !cfg!(windows),
require_literal_separator: true,
require_literal_leading_dot: false,
};
#[derive(Debug, Clone)]
pub struct PathPattern(glob::Pattern);
impl PathPattern {
pub fn new(pattern: &str) -> Result<PathPattern, String> {
glob::Pattern::new(pattern)
.map(PathPattern)
.map_err(|error| error.to_string())
}
pub fn matches(&self, path: &Path) -> bool {
let normalized = normalize(path);
self.0
.matches_path_with(Path::new(&normalized), PATTERN_OPTIONS)
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl std::str::FromStr for PathPattern {
type Err = String;
fn from_str(pattern: &str) -> Result<Self, Self::Err> {
PathPattern::new(pattern)
}
}
#[derive(Debug, Clone)]
pub struct ChangeBatch {
filter: WatchFilter,
changes: Vec<(PathBuf, EventKind)>,
position: HashMap<String, usize>,
configuration: bool,
}
impl ChangeBatch {
pub fn new(filter: WatchFilter) -> Self {
ChangeBatch {
filter,
changes: Vec::new(),
position: HashMap::new(),
configuration: false,
}
}
pub fn push(&mut self, event: FileEvent) -> bool {
if self.filter.is_configuration(&event.path) {
self.configuration = true;
return false;
}
if self.filter.is_ignored(&event.path) {
return false;
}
match self.position.get(&normalize(&event.path)) {
Some(index) => {
let (path, kind) = &mut self.changes[*index];
*kind = merge(*kind, event.kind);
*path = event.path;
}
None => {
self.position
.insert(normalize(&event.path), self.changes.len());
self.changes.push((event.path, event.kind));
}
}
true
}
pub fn extend(&mut self, events: impl IntoIterator<Item = FileEvent>) -> usize {
let mut kept = 0;
for event in events {
if self.push(event) {
kept += 1;
}
}
kept
}
pub fn changed(&self) -> impl Iterator<Item = (&Path, EventKind)> {
self.changes
.iter()
.map(|(path, kind)| (path.as_path(), *kind))
}
pub fn len(&self) -> usize {
self.changes.len()
}
pub fn is_empty(&self) -> bool {
self.changes.is_empty() && !self.configuration
}
pub fn configuration_changed(&self) -> bool {
self.configuration
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Response {
pub forgotten: Vec<PathBuf>,
pub reindex: Vec<PathBuf>,
pub everything: bool,
}
impl Response {
pub fn is_empty(&self) -> bool {
self.forgotten.is_empty() && self.reindex.is_empty() && !self.everything
}
}
impl<F: FileProvider> SummaryStore<F> {
pub fn respond(&mut self, batch: &ChangeBatch) -> Response {
if batch.configuration_changed() {
return Response {
everything: true,
..Response::default()
};
}
if batch.len() > OVERWHELMING_BATCH {
return Response {
everything: true,
..Response::default()
};
}
let mut forgotten = Vec::new();
let mut reindex: Vec<PathBuf> = Vec::new();
let mut queued: HashSet<String> = HashSet::new();
let created: HashSet<String> = batch
.changed()
.filter(|(_, kind)| *kind == EventKind::Created)
.map(|(path, _)| normalize(path))
.collect();
for (path, kind) in batch.changed() {
match kind {
EventKind::Removed => {
if self.forget(path) {
forgotten.push(path.to_path_buf());
}
for includer in self.index().includers_of(path) {
push_once(&mut reindex, &mut queued, includer);
}
}
EventKind::Modified => {
if self.index().summary(path).is_some() {
push_once(&mut reindex, &mut queued, path.to_path_buf());
}
}
EventKind::Created => {
if self.index().summary(path).is_some() {
push_once(&mut reindex, &mut queued, path.to_path_buf());
}
for file in naming_files(self.index(), self.config(), &created) {
push_once(&mut reindex, &mut queued, file);
}
}
}
}
if !forgotten.is_empty() {
let gone: std::collections::HashSet<String> =
forgotten.iter().map(|path| normalize(path)).collect();
reindex.retain(|path| !gone.contains(&normalize(path)));
}
Response {
forgotten,
reindex,
everything: false,
}
}
}
fn push_once(
work: &mut Vec<PathBuf>,
queued: &mut std::collections::HashSet<String>,
path: PathBuf,
) {
if queued.insert(normalize(&path)) {
work.push(path);
}
}
fn naming_files(
index: &ProjectIndex,
config: &CompilerConfig,
created: &std::collections::HashSet<String>,
) -> Vec<PathBuf> {
if created.is_empty() {
return Vec::new();
}
let mut found = Vec::new();
for summary in index.summaries() {
if names_one_of(summary, config, created) {
found.push(summary.path.clone());
}
}
found
}
fn names_one_of(
summary: &FileSummary,
config: &CompilerConfig,
created: &std::collections::HashSet<String>,
) -> bool {
let including = summary.path.parent().unwrap_or(Path::new("."));
summary.includes.iter().any(|include| {
candidates_of(include, including, config)
.into_iter()
.any(|(candidate, rank)| {
if !created.contains(&candidate) {
return false;
}
match &include.resolved {
None => true,
Some(resolved) => {
let held = resolved.parent().and_then(|directory| {
rank_of(directory, including, include.form, config)
});
held.is_none_or(|held| rank < held)
}
}
})
})
}
fn candidates_of(
include: &IncludeFact,
including: &Path,
config: &CompilerConfig,
) -> Vec<(String, usize)> {
let spelling = Path::new(&include.spelling);
if spelling.is_absolute() {
return vec![(
normalize(&join_normalized(Path::new(""), spelling, false)),
0,
)];
}
let mut candidates = Vec::new();
if include.form == IncludeForm::Quote {
candidates.push((
normalize(&join_normalized(including, spelling, cfg!(windows))),
0,
));
}
for (index, include_path) in config.include_paths.iter().enumerate() {
let directory = config.resolve_against_working_directory(&include_path.directory);
candidates.push((
normalize(&join_normalized(&directory, spelling, cfg!(windows))),
index + 1,
));
}
candidates
}
fn rank_of(
directory: &Path,
including: &Path,
form: IncludeForm,
config: &CompilerConfig,
) -> Option<usize> {
if form == IncludeForm::Quote && normalize(directory) == normalize(including) {
return Some(0);
}
config
.include_paths
.iter()
.position(|include_path| {
let searched = config.resolve_against_working_directory(&include_path.directory);
normalize(&searched) == normalize(directory)
})
.map(|index| index + 1)
}
fn merge(held: EventKind, arriving: EventKind) -> EventKind {
if arriving == EventKind::Removed {
return EventKind::Removed;
}
if held == EventKind::Created || arriving == EventKind::Created {
return EventKind::Created;
}
EventKind::Modified
}
fn under(path: &str, directory: &str) -> bool {
path == directory
|| path
.strip_prefix(directory)
.is_some_and(|rest| rest.starts_with('/'))
}
fn normalize(path: &Path) -> String {
normalize_path(path, cfg!(windows))
}
#[cfg(test)]
mod tests {
use super::{ChangeBatch, EventKind, FileEvent, PathPattern, WatchFilter};
use crate::file::paths::{DiskFiles, MemoryFiles};
use crate::include::config::CompilerConfig;
use crate::index::store::SummaryStore;
use std::path::{Path, PathBuf};
fn batch(root: &str) -> ChangeBatch {
ChangeBatch::new(WatchFilter::new(root))
}
fn pattern(text: &str) -> PathPattern {
PathPattern::new(text).expect("the test's own pattern is a pattern")
}
fn kinds(batch: &ChangeBatch) -> Vec<(String, EventKind)> {
batch
.changed()
.map(|(path, kind)| (path.to_string_lossy().replace('\\', "/"), kind))
.collect()
}
#[test]
fn the_cache_directory_is_ignored() {
let mut batch = batch("/p");
assert!(!batch.push(FileEvent::created("/p/.cppls/summaries/ab/abcdef.bin")));
assert!(!batch.push(FileEvent::modified("/p/.cppls/summaries/ab/abcdef.bin.tmp")));
assert!(batch.is_empty());
}
#[test]
fn a_path_that_merely_starts_like_the_cache_is_not_ignored() {
let mut batch = batch("/p");
assert!(batch.push(FileEvent::modified("/p/.cpplsfoo/widget.h")));
assert_eq!(batch.len(), 1);
}
#[test]
fn git_internals_are_ignored_wherever_they_are() {
let mut batch = batch("/p");
assert!(!batch.push(FileEvent::modified("/p/.git/index.lock")));
assert!(!batch.push(FileEvent::created("/p/sub/.git/objects/ab/cdef")));
assert!(!batch.push(FileEvent::modified("/p/.git")));
assert!(batch.is_empty());
}
#[test]
fn a_directory_the_caller_names_is_ignored() {
let mut batch = ChangeBatch::new(WatchFilter::new("/p").ignore("/p/build"));
assert!(!batch.push(FileEvent::modified("/p/build/generated.h")));
assert!(batch.push(FileEvent::modified("/p/src/generated.h")));
}
#[test]
fn a_pattern_the_caller_names_is_ignored() {
let mut batch =
ChangeBatch::new(WatchFilter::new("/p").ignore_pattern(pattern("**/build/**")));
assert!(!batch.push(FileEvent::modified("/p/build/generated.h")));
assert!(!batch.push(FileEvent::created("/p/sub/deep/build/generated.cpp")));
assert!(batch.push(FileEvent::modified("/p/src/generated.h")));
assert!(
batch.push(FileEvent::modified("/p/src/build.h")),
"a file that merely shares the name is not inside the ignored directory"
);
}
#[test]
fn a_star_stays_inside_one_directory_and_two_of_them_do_not() {
let mut batch = ChangeBatch::new(
WatchFilter::new("/p")
.ignore_pattern(pattern("build/*.h"))
.ignore_pattern(pattern("gen/**/*.cpp")),
);
assert!(!batch.push(FileEvent::modified("/p/build/one.h")));
assert!(batch.push(FileEvent::modified("/p/build/deep/two.h")));
assert!(!batch.push(FileEvent::modified("/p/gen/a/b/three.cpp")));
assert!(!batch.push(FileEvent::modified("/p/gen/four.cpp")));
}
#[test]
fn a_pattern_that_is_not_one_says_so() {
let error =
PathPattern::new("**/[unclosed").expect_err("an unclosed class is not a pattern");
assert!(
error.contains("Pattern syntax error"),
"the error should say what is wrong with the pattern, got {error:?}"
);
}
#[test]
fn a_file_with_no_extension_is_not_ignored() {
let mut batch = batch("/p");
assert!(batch.push(FileEvent::modified("/usr/include/vector")));
}
#[test]
fn the_compile_database_is_reported_rather_than_indexed() {
let mut batch = batch("/p");
assert!(
!batch.push(FileEvent::modified("/p/compile_commands.json")),
"it is not a source file and must not be queued as one"
);
assert!(batch.configuration_changed());
assert_eq!(batch.len(), 0);
assert!(
!batch.is_empty(),
"a configuration change is not nothing, even with no paths in the batch"
);
}
#[test]
fn several_events_on_one_path_become_one() {
let mut batch = batch("/p");
for _ in 0..5 {
batch.push(FileEvent::modified("/p/widget.h"));
}
assert_eq!(
kinds(&batch),
[("/p/widget.h".to_string(), EventKind::Modified)]
);
}
#[test]
fn a_save_that_replaces_a_file_is_a_creation() {
let mut batch = batch("/p");
batch.push(FileEvent::removed("/p/widget.h"));
batch.push(FileEvent::created("/p/widget.h"));
assert_eq!(
kinds(&batch),
[("/p/widget.h".to_string(), EventKind::Created)]
);
}
#[test]
fn a_temporary_file_that_comes_and_goes_is_gone() {
let mut batch = batch("/p");
batch.push(FileEvent::created("/p/widget.h"));
batch.push(FileEvent::removed("/p/widget.h"));
assert_eq!(
kinds(&batch),
[("/p/widget.h".to_string(), EventKind::Removed)]
);
}
#[test]
fn a_creation_anywhere_in_the_batch_upgrades_a_modification() {
let mut batch = batch("/p");
batch.push(FileEvent::created("/p/new.h"));
batch.push(FileEvent::modified("/p/new.h"));
assert_eq!(
kinds(&batch),
[("/p/new.h".to_string(), EventKind::Created)]
);
}
#[test]
fn a_batch_keeps_the_order_it_first_saw_paths_in() {
let mut batch = batch("/p");
batch.push(FileEvent::modified("/p/b.h"));
batch.push(FileEvent::modified("/p/a.h"));
batch.push(FileEvent::modified("/p/b.h"));
assert_eq!(
kinds(&batch),
[
("/p/b.h".to_string(), EventKind::Modified),
("/p/a.h".to_string(), EventKind::Modified),
]
);
}
fn memory_store(name: &str, files: &MemoryFiles) -> (SummaryStore<MemoryFiles>, PathBuf) {
let root = std::env::temp_dir().join("cppls-watch-tests").join(name);
let _ = std::fs::remove_dir_all(&root);
let store = SummaryStore::with_provider(&root, CompilerConfig::default(), files.clone());
(store, root)
}
fn shown(path: &Path) -> String {
super::normalize(path)
}
fn names(paths: &[PathBuf]) -> Vec<String> {
paths.iter().map(|path| shown(path)).collect()
}
#[test]
fn a_modified_header_is_read_again_and_nothing_else_is() {
let files = MemoryFiles::new()
.with_file("/p/widget.h", "struct Widget { int size; };\n")
.with_file("/p/a.cpp", "#include \"widget.h\"\n")
.with_file("/p/b.cpp", "#include \"widget.h\"\n")
.with_file("/p/c.cpp", "#include \"widget.h\"\n");
let (mut store, root) = memory_store("modified", &files);
for path in ["/p/widget.h", "/p/a.cpp", "/p/b.cpp", "/p/c.cpp"] {
store.get(Path::new(path)).expect("the file reads");
}
let mut events = batch("/p");
events.push(FileEvent::modified("/p/widget.h"));
let response = store.respond(&events);
assert_eq!(names(&response.reindex), ["/p/widget.h"]);
assert!(response.forgotten.is_empty());
assert!(!response.everything);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_modified_file_the_index_has_never_held_is_not_indexed() {
let files = MemoryFiles::new().with_file("/p/known.cpp", "int x;\n");
let (mut store, root) = memory_store("unknown-path", &files);
store
.get(Path::new("/p/known.cpp"))
.expect("the file reads");
let mut events = batch("/p");
events.push(FileEvent::modified("/p/README.md"));
events.push(FileEvent::modified("/p/new.cpp"));
let response = store.respond(&events);
assert!(response.is_empty(), "{response:?}");
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_removed_file_is_forgotten_and_its_includers_are_read_again() {
let files = MemoryFiles::new()
.with_file("/p/widget.h", "struct Widget { int size; };\n")
.with_file(
"/p/main.cpp",
"#include \"widget.h\"\nvoid f() { Widget w; }\n",
);
let (mut store, root) = memory_store("removed", &files);
for path in ["/p/widget.h", "/p/main.cpp"] {
store.get(Path::new(path)).expect("the file reads");
}
assert!(matches!(
store.index().definition("Widget", Path::new("/p/main.cpp")),
crate::Known::Yes(_)
));
let mut events = batch("/p");
events.push(FileEvent::removed("/p/widget.h"));
let response = store.respond(&events);
assert_eq!(names(&response.forgotten), ["/p/widget.h"]);
assert_eq!(
names(&response.reindex),
["/p/main.cpp"],
"the summary of the file that resolved to it records a search that would now fail"
);
assert!(
matches!(
store.index().definition("Widget", Path::new("/p/main.cpp")),
crate::Known::Unknown(_)
),
"and the declaration in the deleted file stops being found before anything is re-read"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_removed_file_nothing_included_is_only_forgotten() {
let files = MemoryFiles::new().with_file("/p/alone.cpp", "int x;\n");
let (mut store, root) = memory_store("removed-alone", &files);
store
.get(Path::new("/p/alone.cpp"))
.expect("the file reads");
let mut events = batch("/p");
events.push(FileEvent::removed("/p/alone.cpp"));
let response = store.respond(&events);
assert_eq!(names(&response.forgotten), ["/p/alone.cpp"]);
assert!(response.reindex.is_empty(), "nothing resolved to it");
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_created_file_that_nothing_searches_for_changes_nothing() {
let files = MemoryFiles::new()
.with_file("/p/main.cpp", "#include \"generated.h\"\n")
.with_file("/p/other.cpp", "#include \"other.h\"\n");
let (mut store, root) = memory_store("created-irrelevant", &files);
for path in ["/p/main.cpp", "/p/other.cpp"] {
store.get(Path::new(path)).expect("the file reads");
}
let mut events = batch("/p");
events.push(FileEvent::created("/p/unrelated.h"));
events.push(FileEvent::created("/p/deep/nested/generated.h"));
let response = store.respond(&events);
assert!(
response.is_empty(),
"neither creation is a candidate any include was searching: {response:?}"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_created_file_repairs_the_include_that_was_waiting_for_it() {
let files = MemoryFiles::new()
.with_file(
"/p/main.cpp",
"#include \"local/generated.h\"\nvoid f() { }\n",
)
.with_file("/p/local/other.h", "int other;\n");
let (mut store, root) = memory_store("created-relevant", &files);
store.get(Path::new("/p/main.cpp")).expect("the file reads");
assert_eq!(
store.stats().unstored,
1,
"the include did not resolve, so the summary was not stored"
);
let mut events = batch("/p");
events.push(FileEvent::created("/p/local/generated.h"));
let response = store.respond(&events);
assert_eq!(
names(&response.reindex),
["/p/main.cpp"],
"the file that wrote that include is the file whose summary is wrong"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_created_file_that_outranks_the_current_resolution_wins() {
let files = MemoryFiles::new()
.with_file("/inc/x.h", "struct FromInclude { int x; };\n")
.with_file("/p/a.cpp", "#include \"x.h\"\nvoid f() { }\n");
let config = CompilerConfig::default().with_include_path("/inc");
let root = std::env::temp_dir()
.join("cppls-watch-tests")
.join("outranks");
let _ = std::fs::remove_dir_all(&root);
let mut store = SummaryStore::with_provider(&root, config, files.clone());
let held = store
.get(Path::new("/p/a.cpp"))
.expect("the file reads")
.clone();
assert_eq!(
held.includes[0].resolved.as_deref(),
Some(Path::new("/inc/x.h"))
);
let mut events = batch("/p");
events.push(FileEvent::created("/p/x.h"));
let response = store.respond(&events);
assert_eq!(names(&response.reindex), ["/p/a.cpp"]);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_created_file_that_loses_to_the_current_resolution_changes_nothing() {
let files = MemoryFiles::new()
.with_file("/first/x.h", "struct First { int x; };\n")
.with_file("/p/a.cpp", "#include \"x.h\"\nvoid f() { }\n");
let config = CompilerConfig::default()
.with_include_path("/first")
.with_include_path("/second");
let root = std::env::temp_dir().join("cppls-watch-tests").join("loses");
let _ = std::fs::remove_dir_all(&root);
let mut store = SummaryStore::with_provider(&root, config, files.clone());
store.get(Path::new("/p/a.cpp")).expect("the file reads");
let mut events = batch("/p");
events.push(FileEvent::created("/second/x.h"));
let response = store.respond(&events);
assert!(
response.is_empty(),
"the search already stopped at /first: {response:?}"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_file_named_by_two_rules_at_once_is_read_once() {
let files = MemoryFiles::new()
.with_file("/p/a.h", "int a;\n")
.with_file("/p/main.cpp", "#include \"a.h\"\n#include \"b.h\"\n");
let (mut store, root) = memory_store("dedupe", &files);
store.get(Path::new("/p/a.h")).expect("the file reads");
store.get(Path::new("/p/main.cpp")).expect("the file reads");
let mut events = batch("/p");
events.push(FileEvent::removed("/p/a.h"));
events.push(FileEvent::created("/p/b.h"));
let response = store.respond(&events);
assert_eq!(names(&response.reindex), ["/p/main.cpp"]);
assert_eq!(names(&response.forgotten), ["/p/a.h"]);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_file_that_is_gone_is_not_read_again() {
let files = MemoryFiles::new()
.with_file("/p/widget.h", "struct Widget { int size; };\n")
.with_file("/p/main.cpp", "#include \"widget.h\"\n");
let (mut store, root) = memory_store("gone-includer", &files);
for path in ["/p/widget.h", "/p/main.cpp"] {
store.get(Path::new(path)).expect("the file reads");
}
let mut events = batch("/p");
events.push(FileEvent::removed("/p/widget.h"));
events.push(FileEvent::removed("/p/main.cpp"));
let response = store.respond(&events);
assert_eq!(names(&response.forgotten), ["/p/widget.h", "/p/main.cpp"]);
assert!(response.reindex.is_empty(), "{response:?}");
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn an_angle_include_does_not_search_the_files_own_directory() {
let files = MemoryFiles::new()
.with_file("/p/main.cpp", "#include <generated.h>\n")
.with_file("/p/other.cpp", "#include \"generated.h\"\n");
let (mut store, root) = memory_store("angle-form", &files);
for path in ["/p/main.cpp", "/p/other.cpp"] {
store.get(Path::new(path)).expect("the file reads");
}
let mut events = batch("/p");
events.push(FileEvent::created("/p/generated.h"));
let response = store.respond(&events);
assert_eq!(
names(&response.reindex),
["/p/other.cpp"],
"only the quoted include was looking beside itself"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_batch_of_creations_is_answered_in_one_pass() {
let files = MemoryFiles::new()
.with_file("/p/first.cpp", "#include \"one.h\"\n")
.with_file("/p/last.cpp", "#include \"two.h\"\n")
.with_file("/p/quiet.cpp", "#include \"three.h\"\n");
let (mut store, root) = memory_store("batch-of-creations", &files);
for path in ["/p/first.cpp", "/p/last.cpp", "/p/quiet.cpp"] {
store.get(Path::new(path)).expect("the file reads");
}
let mut events = batch("/p");
events.push(FileEvent::created("/p/one.h"));
for index in 0..200 {
events.push(FileEvent::created(format!("/p/unrelated{index}.h")));
}
events.push(FileEvent::created("/p/two.h"));
let response = store.respond(&events);
assert_eq!(
names(&response.reindex),
["/p/first.cpp", "/p/last.cpp"],
"both ends of the batch are considered, and three.h is still missing"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_configuration_change_is_everything() {
let files = MemoryFiles::new().with_file("/p/a.cpp", "int x;\n");
let (mut store, root) = memory_store("config-change", &files);
store.get(Path::new("/p/a.cpp")).expect("the file reads");
let mut events = batch("/p");
events.push(FileEvent::modified("/p/compile_commands.json"));
events.push(FileEvent::modified("/p/a.cpp"));
let response = store.respond(&events);
assert!(response.everything);
assert!(
response.reindex.is_empty(),
"the caller rebuilds from its file list: the paths in this batch are not the interesting set"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_batch_too_large_to_ask_about_one_path_at_a_time_is_everything() {
let files = MemoryFiles::new();
let (mut store, root) = memory_store("overwhelming", &files);
let mut events = batch("/p");
for index in 0..super::OVERWHELMING_BATCH + 1 {
events.push(FileEvent::modified(format!("/p/file{index}.cpp")));
}
let response = store.respond(&events);
assert!(response.everything);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_checkout_inside_git_does_nothing_at_all() {
let files = MemoryFiles::new();
let (mut store, root) = memory_store("git-churn", &files);
let mut events = batch("/p");
for path in [
"/p/.git/index.lock",
"/p/.git/HEAD",
"/p/.git/objects/ab/cdef0123",
"/p/.cppls/summaries/ab/abcdef.bin",
] {
events.push(FileEvent::modified(path));
}
let response = store.respond(&events);
assert!(events.is_empty(), "the batch kept nothing");
assert!(response.is_empty(), "{response:?}");
let _ = std::fs::remove_dir_all(&root);
}
struct Project {
root: PathBuf,
}
impl Project {
fn new(name: &str) -> Self {
let root = std::env::temp_dir().join("cppls-watch-disk").join(name);
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("the fixture directory");
Project { root }
}
fn path(&self, name: &str) -> PathBuf {
self.root.join(name)
}
fn write(&self, name: &str, text: &str) -> PathBuf {
let path = self.path(name);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).expect("the fixture directory");
}
std::fs::write(&path, text).expect("the fixture writes");
path
}
fn remove(&self, name: &str) {
std::fs::remove_file(self.path(name)).expect("the fixture is removed");
}
fn store(&self, config: CompilerConfig) -> SummaryStore<DiskFiles> {
SummaryStore::open(&self.root, config)
}
fn batch(&self) -> ChangeBatch {
ChangeBatch::new(WatchFilter::new(&self.root))
}
}
impl Drop for Project {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.root);
}
}
fn work(store: &mut SummaryStore<DiskFiles>, seeds: &[PathBuf]) {
let mut list = store.worklist(seeds.to_vec(), Vec::new());
while list.step().is_some() {}
}
#[test]
fn a_header_that_appears_is_found_by_the_file_that_was_waiting_for_it() {
let project = Project::new("appears");
let main = project.write("main.cpp", "#include \"generated.h\"\nvoid f() { }\n");
let mut store = project.store(CompilerConfig::default());
let held = store.get(&main).expect("the file reads").clone();
assert_eq!(held.includes[0].resolved, None);
assert_eq!(
store.stats().unstored,
1,
"and the summary was deliberately not stored, which is what makes this recoverable"
);
let generated = project.write("generated.h", "struct Generated { int x; };\n");
let mut events = project.batch();
events.push(FileEvent::created(&generated));
let response = store.respond(&events);
assert_eq!(names(&response.reindex), [shown(&main)]);
work(&mut store, &response.reindex);
let rebuilt = store.index().summary(&main).expect("held").clone();
assert_eq!(
rebuilt.includes[0].resolved.as_deref(),
Some(Path::new(&shown(&generated))),
"the include resolves now, and the summary says where to"
);
assert!(
store.index().summary(&generated).is_some(),
"and the file it resolves to was indexed by following the include"
);
assert!(
matches!(
store.index().definition("Generated", &main),
crate::Known::Yes(_)
),
"so the declaration in it is reachable"
);
}
#[test]
fn a_header_that_is_deleted_stops_being_found() {
let project = Project::new("deleted");
project.write("widget.h", "struct Widget { int size; };\n");
let main = project.write(
"main.cpp",
"#include \"widget.h\"\nvoid f() { Widget w; }\n",
);
let mut store = project.store(CompilerConfig::default());
store
.get(&project.path("widget.h"))
.expect("the file reads");
store.get(&main).expect("the file reads");
assert!(matches!(
store.index().definition("Widget", &main),
crate::Known::Yes(_)
));
project.remove("widget.h");
let mut events = project.batch();
events.push(FileEvent::removed(project.path("widget.h")));
let response = store.respond(&events);
assert_eq!(
names(&response.forgotten),
[shown(&project.path("widget.h"))]
);
assert_eq!(names(&response.reindex), [shown(&main)]);
work(&mut store, &response.reindex);
let rebuilt = store.index().summary(&main).expect("held").clone();
assert_eq!(
rebuilt.includes[0].resolved, None,
"the search fails now, and the summary records the attempt rather than the old answer"
);
assert!(
matches!(
store.index().definition("Widget", &main),
crate::Known::Unknown(_)
),
"so the declaration is no longer found"
);
}
#[test]
fn a_file_renamed_without_being_changed_is_a_cache_hit() {
let project = Project::new("rename");
let before = project.write("old_name.h", "struct Renamed { int x; };\n");
let mut store = project.store(CompilerConfig::default());
store.get(&before).expect("the file reads");
assert_eq!(store.stats().rebuilt, 1);
let after = project.write("new_name.h", "struct Renamed { int x; };\n");
project.remove("old_name.h");
let mut events = project.batch();
events.push(FileEvent::removed(&before));
events.push(FileEvent::created(&after));
let response = store.respond(&events);
assert_eq!(names(&response.forgotten), [shown(&before)]);
assert!(
response.reindex.is_empty(),
"nothing in the index names the new path, so nothing is read: {response:?}"
);
store.get(&after).expect("the file reads");
assert_eq!(
store.stats().rebuilt,
1,
"the same text under a new name was found, not parsed again"
);
assert_eq!(store.stats().reused, 1);
assert!(
matches!(
store.index().definition("Renamed", &after),
crate::Known::Yes(_)
),
"and the renamed file answers for itself"
);
}
}