use super::SummaryStore;
use crate::file::paths::MemoryFiles;
use crate::include::config::CompilerConfig;
use cpp_parser::Dialect;
use std::path::Path;
fn store(name: &str, files: &MemoryFiles) -> (SummaryStore<MemoryFiles>, std::path::PathBuf) {
let root = std::env::temp_dir().join("cppls-store-tests").join(name);
let _ = std::fs::remove_dir_all(&root);
let store = SummaryStore::with_provider(&root, CompilerConfig::default(), files.clone());
(store, root)
}
#[test]
fn a_summary_is_built_once_and_then_read_from_disk() {
let files = MemoryFiles::new().with_file("/p/widget.h", "struct Widget { int size; };\n");
let (mut store, root) = store("reuse", &files);
let first = store
.get(Path::new("/p/widget.h"))
.expect("the file reads")
.clone();
assert_eq!(first.declarations.len(), 2, "the class and its member");
assert_eq!(store.stats().rebuilt, 1);
assert_eq!(store.stats().reused, 0);
let mut reopened = SummaryStore::with_provider(&root, CompilerConfig::default(), files.clone());
let second = reopened
.get(Path::new("/p/widget.h"))
.expect("the file reads");
assert_eq!(second.declarations, first.declarations);
assert_eq!(
reopened.stats().rebuilt,
0,
"the parser was not asked: the disk had the answer"
);
assert_eq!(reopened.stats().reused, 1);
assert_eq!(reopened.stats().hit_rate(), Some(1.0));
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn preparing_files_together_gives_the_index_reading_them_one_by_one_gives() {
let mut files = MemoryFiles::new();
let mut paths: Vec<std::path::PathBuf> = Vec::new();
for number in 0..48 {
let text = match number % 8 {
0 => "struct Same { int v; };\n".to_string(),
1 => format!("#include \"missing{number}.h\"\nint one{number};\n"),
_ => format!(
"#include \"f{}.h\"\nstruct S{number} {{ int v; }};\n",
number.max(2) - 1
),
};
let path = format!("/p/f{number}.h");
files.insert(&path, &text);
paths.push(path.into());
}
paths.push("/p/nowhere.h".into());
let (mut one_by_one, root_one) = store("prepared-sequential", &files);
for path in &paths {
let _ = one_by_one.get(path);
}
let (mut together, root_together) = store("prepared-together", &files);
for (path, prepared) in paths.iter().zip(together.prepare_many(&paths)) {
let _ = together.commit(path, prepared);
}
let (a, b) = (together.stats(), one_by_one.stats());
assert_eq!(
(a.reused + a.rebuilt, a.unstored),
(b.reused + b.rebuilt, b.unstored)
);
assert_eq!(together.index().len(), one_by_one.index().len());
for summary in one_by_one.index().summaries() {
let other = together
.index()
.summary(&summary.path)
.expect("the same files are indexed");
assert_eq!(
other.declarations, summary.declarations,
"{:?}",
summary.path
);
assert_eq!(other.includes, summary.includes, "{:?}", summary.path);
}
let mut reopened =
SummaryStore::with_provider(&root_together, CompilerConfig::default(), files.clone());
for path in &paths {
let _ = reopened.get(path);
}
assert_eq!(
reopened.stats().reused + reopened.stats().rebuilt,
paths.len() - 1,
"every readable file was read"
);
assert_eq!(
reopened.stats().rebuilt,
together.stats().unstored,
"only the answers that were deliberately not stored are parsed again: {:?}",
reopened.stats()
);
let _ = std::fs::remove_dir_all(&root_one);
let _ = std::fs::remove_dir_all(&root_together);
}
#[test]
fn scanning_the_include_lines_gives_the_includes_the_parse_records() {
let files = MemoryFiles::new()
.with_file("/p/a.h", "#pragma once\n#include \"b.h\"\n#if 0\n#include \"c.h\"\n#endif\n#include \"gone.h\"\nint a;\n")
.with_file("/p/b.h", "int b;\n")
.with_file("/p/c.h", "int c;\n");
let (mut store, root) = store("include-scan", &files);
let config = CompilerConfig::default();
let source =
crate::FileProvider::read(&files, Path::new("/p/a.h")).expect("the fixture is there");
let scanned =
crate::FileIndexer::new(&files, &config).includes_of(Path::new("/p/a.h"), &source);
let recorded: Vec<std::path::PathBuf> = store
.get(Path::new("/p/a.h"))
.expect("reads")
.includes
.iter()
.filter_map(|include| include.resolved.clone())
.collect();
assert_eq!(scanned, recorded);
assert_eq!(
scanned.len(),
2,
"b.h and the one behind `#if 0`; the missing one is not resolved: {scanned:?}"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn preparing_a_closure_reaches_every_file_it_includes_within_its_limits() {
let mut files = MemoryFiles::new().with_file(
"/p/main.cpp",
"#include \"h0.h\"\n#include \"h1.h\"\nint main;\n",
);
for number in 0..30 {
files.insert(
format!("/p/h{number}.h"),
format!(
"#include \"h{}.h\"\n#include \"h{}.h\"\nstruct H{number} {{ int v; }};\n",
(number + 1) % 30,
(number + 2) % 30
),
);
}
let (store, root) = store("prepare-closure", &files);
let main = std::path::PathBuf::from("/p/main.cpp");
let everything = store.prepare_closure(std::slice::from_ref(&main), |_| true, 1000);
let mut reached: Vec<String> = everything
.iter()
.map(|(path, _)| path.to_string_lossy().into_owned())
.collect();
reached.sort();
reached.dedup();
assert_eq!(
reached.len(),
31,
"main and thirty headers, each once: {}",
everything.len()
);
assert_eq!(everything.len(), 31, "and no file was prepared twice");
let limited = store.prepare_closure(std::slice::from_ref(&main), |_| true, 10);
assert!(
limited.len() <= 10,
"the budget bounds the call: {}",
limited.len()
);
assert!(
limited.iter().any(|(path, _)| path == &main),
"the root is always prepared"
);
let refusing = store.prepare_closure(
std::slice::from_ref(&main),
|path| !path.ends_with("h1.h"),
1000,
);
assert!(
refusing
.iter()
.all(|(path, _)| !path.ends_with("h1.h") || path == &main)
);
let (mut together, root_together) = store_named("prepare-closure-commit", &files);
for (path, prepared) in everything {
let _ = together.commit(&path, prepared);
}
let (mut one_by_one, root_one) = store_named("prepare-closure-one", &files);
for path in &reached {
let _ = one_by_one.get(Path::new(path));
}
assert_eq!(together.index().len(), one_by_one.index().len());
for summary in one_by_one.index().summaries() {
let other = together.index().summary(&summary.path).expect("indexed");
assert_eq!(other.declarations, summary.declarations);
assert_eq!(other.includes, summary.includes);
}
for directory in [root, root_together, root_one] {
let _ = std::fs::remove_dir_all(&directory);
}
}
fn store_named(name: &str, files: &MemoryFiles) -> (SummaryStore<MemoryFiles>, std::path::PathBuf) {
store(name, files)
}
#[test]
fn coming_back_to_earlier_text_finds_the_entry_again() {
let root = std::env::temp_dir()
.join("cppls-store-tests")
.join("branch-switch");
let _ = std::fs::remove_dir_all(&root);
let one = MemoryFiles::new().with_file("/p/a.cpp", "int x;\n");
let two = MemoryFiles::new().with_file("/p/a.cpp", "int y;\n");
let mut store = SummaryStore::with_provider(&root, CompilerConfig::default(), one.clone());
store.get(Path::new("/p/a.cpp")).expect("the file reads");
let mut other = SummaryStore::with_provider(&root, CompilerConfig::default(), two.clone());
other.get(Path::new("/p/a.cpp")).expect("the file reads");
assert_eq!(other.stats().rebuilt, 1, "the second text is new");
let mut back = SummaryStore::with_provider(&root, CompilerConfig::default(), one.clone());
back.get(Path::new("/p/a.cpp")).expect("the file reads");
assert_eq!(
back.stats().reused,
1,
"and the first text's entry is still there"
);
assert_eq!(back.stats().rebuilt, 0);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn an_edited_file_is_rebuilt_rather_than_reused() {
let files = MemoryFiles::new().with_file("/p/widget.h", "struct Widget { int size; };\n");
let (mut store, root) = store("edited", &files);
store.get(Path::new("/p/widget.h")).expect("the file reads");
let edited = MemoryFiles::new().with_file("/p/widget.h", "struct Widget { int a; int b; };\n");
let mut second = SummaryStore::with_provider(&root, CompilerConfig::default(), edited.clone());
let summary = second
.get(Path::new("/p/widget.h"))
.expect("the file reads")
.clone();
assert_eq!(
summary.declarations.len(),
3,
"the class and both of its members — the edited text, not the stored one"
);
assert_eq!(
second.stats().reused,
0,
"the stored entry names other text"
);
assert_eq!(second.stats().rebuilt, 1);
assert_eq!(second.stats().unstored, 0, "nothing was declined");
assert_eq!(
summary.key.content_hash,
crate::cache::content_hash("struct Widget { int a; int b; };\n"),
"and the rebuilt summary is keyed by the text it was built from"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn every_file_gets_a_cache_entry_of_its_own() {
let files = MemoryFiles::new()
.with_file("/p/one.h", "struct One { int a; };\n")
.with_file("/p/two.h", "struct Two { int b; };\n");
let (mut store, root) = store("distinct-entries", &files);
let one = store
.get(Path::new("/p/one.h"))
.expect("the file reads")
.clone();
let two = store
.get(Path::new("/p/two.h"))
.expect("the file reads")
.clone();
assert_eq!(
one.key.content_hash,
crate::cache::content_hash("struct One { int a; };\n"),
"the summary is keyed by its own text"
);
assert_eq!(
two.key.content_hash,
crate::cache::content_hash("struct Two { int b; };\n")
);
assert_ne!(
one.key.path_under(store.cache_directory()),
two.key.path_under(store.cache_directory()),
"two files, two entries"
);
let stored_one = store.get(Path::new("/p/one.h")).expect("written").clone();
let stored_two = store.get(Path::new("/p/two.h")).expect("written").clone();
assert_eq!(stored_one.path, std::path::Path::new("/p/one.h"));
assert_eq!(stored_two.path, std::path::Path::new("/p/two.h"));
assert!(
stored_one
.declarations
.iter()
.any(|fact| fact.name == "One")
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_different_configuration_is_a_different_key() {
let files = MemoryFiles::new().with_file("/p/a.cpp", "int x;\n");
let (mut store, root) = store("config", &files);
store.get(Path::new("/p/a.cpp")).expect("the file reads");
let configured = CompilerConfig::default().with_standard("c++20");
let mut second = SummaryStore::with_provider(&root, configured, files.clone());
second.get(Path::new("/p/a.cpp")).expect("the file reads");
assert_eq!(
second.stats().reused,
0,
"the same text under a different configuration is a different summary"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn an_identical_configuration_gives_an_identical_key() {
let files = MemoryFiles::new();
let one = SummaryStore::with_provider("r", CompilerConfig::default(), files.clone());
let two = SummaryStore::with_provider("r", CompilerConfig::default(), files.clone());
assert_eq!(
one.context_hash(Path::new("/p/a.cpp")),
two.context_hash(Path::new("/p/a.cpp"))
);
let different = SummaryStore::with_provider(
"r",
CompilerConfig::default().with_define(crate::CommandLineMacro::defined("A")),
files.clone(),
);
assert_ne!(
one.context_hash(Path::new("/p/a.cpp")),
different.context_hash(Path::new("/p/a.cpp"))
);
}
#[test]
fn the_key_knows_which_compiler_the_file_is_read_for() {
let files = MemoryFiles::new();
let gnu = SummaryStore::with_provider(
"r",
CompilerConfig::default().with_dialect(Dialect::Gnu),
files.clone(),
);
let msvc = SummaryStore::with_provider(
"r",
CompilerConfig::default().with_dialect(Dialect::Msvc),
files.clone(),
);
assert_ne!(
gnu.context_hash(Path::new("/p/a.cpp")),
msvc.context_hash(Path::new("/p/a.cpp")),
"the same file read for two compilers is two different summaries"
);
}
#[test]
fn the_working_directory_counts_exactly_where_it_changes_what_is_found() {
let files = MemoryFiles::new();
let relative = |directory: &str| {
SummaryStore::with_provider(
"r",
CompilerConfig::default()
.with_include_path("inc")
.with_working_directory(directory),
files.clone(),
)
};
assert_ne!(
relative("build-one").context_hash(Path::new("/p/a.cpp")),
relative("build-two").context_hash(Path::new("/p/a.cpp")),
"a relative -I is a different directory in a different working directory"
);
let absolute = |directory: &str| {
SummaryStore::with_provider(
"r",
CompilerConfig::default()
.with_include_path("/opt/inc")
.with_working_directory(directory),
files.clone(),
)
};
assert_eq!(
absolute("build-one").context_hash(Path::new("/p/a.cpp")),
absolute("build-two").context_hash(Path::new("/p/a.cpp")),
"an absolute -I does not care where the compiler ran"
);
}
#[test]
fn a_file_whose_include_was_not_found_is_not_stored() {
let files = MemoryFiles::new().with_file("/p/main.cpp", "#include \"missing.h\"\nint x;\n");
let (mut store, root) = store("unresolved", &files);
let summary = store.get(Path::new("/p/main.cpp")).expect("the file reads");
assert_eq!(summary.declarations.len(), 1, "the summary is still built");
assert_eq!(store.stats().unstored, 1);
assert_eq!(store.stats().reused, 0);
assert_eq!(store.stats().rebuilt, 1);
assert_eq!(
store.summary_database_sizes().map(|sizes| sizes.summaries),
Some(0),
"no summary row: a search that failed must not be cached"
);
let with_header = MemoryFiles::new()
.with_file("/p/main.cpp", "#include \"missing.h\"\nint x;\n")
.with_file("/p/missing.h", "struct Now { int here; };\n");
let mut second =
SummaryStore::with_provider(&root, CompilerConfig::default(), with_header.clone());
let rebuilt = second
.get(Path::new("/p/main.cpp"))
.expect("the file reads")
.clone();
assert_eq!(second.stats().reused, 0);
assert_eq!(second.stats().rebuilt, 1);
assert_eq!(second.stats().unstored, 0, "now the search is answered");
assert_eq!(
rebuilt.includes[0].resolved.as_deref(),
Some(Path::new("/p/missing.h")),
"and the entry records where it was found"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_file_whose_include_was_never_indexed_is_still_stored() {
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) = store("unindexed-include", &files);
store.get(Path::new("/p/main.cpp")).expect("the file reads");
assert_eq!(store.stats().unstored, 0);
let mut reopened = SummaryStore::with_provider(&root, CompilerConfig::default(), files.clone());
reopened
.get(Path::new("/p/main.cpp"))
.expect("the file reads");
assert_eq!(reopened.stats().reused, 1, "the entry is usable");
assert_eq!(reopened.stats().rebuilt, 0);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_key_is_the_same_for_the_same_contents_in_the_same_directory() {
let files = MemoryFiles::new()
.with_file("/p/a.cpp", "int x;\n")
.with_file("/p/b.cpp", "int x;\n");
let (mut store, root) = store("content-addressed", &files);
let one = store
.get(Path::new("/p/a.cpp"))
.expect("the file reads")
.clone();
assert_eq!(one.path, Path::new("/p/a.cpp"));
let two = store
.get(Path::new("/p/b.cpp"))
.expect("the file reads")
.clone();
assert_eq!(two.key, one.key, "the same text is the same entry");
assert_eq!(store.stats().reused, 1, "so the second one is a hit");
assert_eq!(store.stats().rebuilt, 1);
assert_eq!(two.path, Path::new("/p/b.cpp"));
for path in ["/p/a.cpp", "/p/b.cpp"] {
assert_eq!(
store
.index()
.summary(Path::new(path))
.map(|held| held.path.clone()),
Some(std::path::PathBuf::from(path))
);
}
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_same_text_in_two_directories_does_not_share_an_entry() {
let files = MemoryFiles::new()
.with_file("/p/shared.h", "struct Outside { int x; };\n")
.with_file("/p/sub/shared.h", "struct Inside { int y; };\n")
.with_file("/p/a.h", "#include \"shared.h\"\n")
.with_file("/p/sub/b.h", "#include \"shared.h\"\n");
let (mut store, root) = store("directory-context", &files);
for path in ["/p/shared.h", "/p/sub/shared.h", "/p/a.h", "/p/sub/b.h"] {
assert!(
store.get(Path::new(path)).is_some(),
"the file reads: {path}"
);
}
let outside = store
.index()
.summary(Path::new("/p/a.h"))
.expect("held")
.clone();
let inside = store
.index()
.summary(Path::new("/p/sub/b.h"))
.expect("held")
.clone();
assert_eq!(
outside.key.content_hash, inside.key.content_hash,
"the two files really do say the same thing"
);
assert_ne!(
outside.key, inside.key,
"the same text compiled one directory down is a different compilation"
);
let resolution = |summary: &crate::FileSummary| {
summary.includes[0]
.resolved
.as_ref()
.map(|path| path.to_string_lossy().replace('\\', "/"))
};
assert_eq!(resolution(&outside).as_deref(), Some("/p/shared.h"));
assert_eq!(resolution(&inside).as_deref(), Some("/p/sub/shared.h"));
assert!(
matches!(
store.index().definition("Outside", Path::new("/p/a.h")),
crate::Known::Yes(_)
),
"`a.h` sees the header beside it"
);
assert!(
matches!(
store.index().definition("Outside", Path::new("/p/sub/b.h")),
crate::Known::Unknown(_)
),
"and `sub/b.h` does not"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_hit_re_checks_the_includes_without_parsing_them() {
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) = store("no-search-on-hit", &files);
store.get(Path::new("/p/main.cpp")).expect("the file reads");
let after_build = files.exists_of("/p/widget.h");
assert_eq!(after_build, 1, "one candidate, probed once while resolving");
let mut reopened = SummaryStore::with_provider(&root, CompilerConfig::default(), files.clone());
reopened
.get(Path::new("/p/main.cpp"))
.expect("the file reads");
assert_eq!(reopened.stats().reused, 1);
assert_eq!(reopened.stats().rebuilt, 0, "the parser was not asked");
assert_eq!(
files.reads_of("/p/main.cpp"),
2,
"the file was read once per get, and nothing else was read at all"
);
assert_eq!(
files.exists_of("/p/widget.h"),
after_build + 1,
"and the header was probed once more — the re-check, not a search"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_stored_summary_whose_include_no_longer_resolves_is_rebuilt() {
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) = store("stale-resolution", &files);
let built = store
.get(Path::new("/p/main.cpp"))
.expect("the file reads")
.clone();
assert_eq!(
built.includes[0].resolved.as_deref(),
Some(Path::new("/p/widget.h"))
);
let without = MemoryFiles::new().with_file(
"/p/main.cpp",
"#include \"widget.h\"\nvoid f() { Widget w; }\n",
);
let mut reopened =
SummaryStore::with_provider(&root, CompilerConfig::default(), without.clone());
let rebuilt = reopened
.get(Path::new("/p/main.cpp"))
.expect("the file reads")
.clone();
assert_eq!(
reopened.stats().reused,
0,
"the entry named a file that is not there, so it was not an answer"
);
assert_eq!(reopened.stats().rebuilt, 1);
assert_eq!(
rebuilt.includes[0].resolved, None,
"and the rebuilt summary records the search that now fails"
);
assert_eq!(
reopened.stats().unstored,
1,
"so it is not stored, which is what makes the change recoverable"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_file_that_includes_another_is_indexed_after_it() {
let files = MemoryFiles::new()
.with_file("/includes/widget.h", "struct Widget { int size; };\n")
.with_file("/includes/middle.h", "#include \"widget.h\"\n");
let (mut store, root) = store("include-after", &files);
assert!(
store.get(Path::new("/includes/widget.h")).is_some(),
"the included header is indexed first"
);
assert!(
store.get(Path::new("/includes/middle.h")).is_some(),
"and the file that includes it is indexed after, in either order"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_changed_header_invalidates_the_files_that_include_it() {
let files = MemoryFiles::new()
.with_file("/invalidate-a/widget.h", "struct Widget { int size; };\n")
.with_file("/invalidate-a/middle.h", "#include \"widget.h\"\n")
.with_file(
"/invalidate-a/main.cpp",
"#include \"middle.h\"\nvoid f() { Widget w; }\n",
)
.with_file("/invalidate-a/unrelated.cpp", "int x;\n");
let (mut store, root) = store("invalidate", &files);
for path in [
"/invalidate-a/widget.h",
"/invalidate-a/middle.h",
"/invalidate-a/main.cpp",
"/invalidate-a/unrelated.cpp",
] {
assert!(
store.get(Path::new(path)).is_some(),
"the file reads: {path}"
);
}
let mut work = store.invalidate(Path::new("/invalidate-a/widget.h"));
work.sort();
assert_eq!(
work,
[
Path::new("/invalidate-a/main.cpp"),
Path::new("/invalidate-a/middle.h"),
Path::new("/invalidate-a/widget.h"),
],
"the header and everything below it, transitively, and nothing else"
);
let mut alone = store.invalidate(Path::new("/invalidate-a/unrelated.cpp"));
alone.sort();
assert_eq!(alone, [Path::new("/invalidate-a/unrelated.cpp")]);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn invalidation_terminates_on_a_cycle_of_includes() {
let files = MemoryFiles::new()
.with_file("/p/a.h", "#include \"b.h\"\nstruct A { int x; };\n")
.with_file("/p/b.h", "#include \"a.h\"\nstruct B { int y; };\n");
let (mut store, root) = store("cycle", &files);
store.get(Path::new("/p/a.h")).expect("the file reads");
store.get(Path::new("/p/b.h")).expect("the file reads");
let mut work = store.invalidate(Path::new("/p/a.h"));
work.sort();
assert_eq!(work, [Path::new("/p/a.h"), Path::new("/p/b.h")]);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_file_that_cannot_be_read_is_not_an_error() {
let files = MemoryFiles::new();
let (mut store, root) = store("missing", &files);
assert!(store.get(Path::new("/p/nothing.cpp")).is_none());
assert_eq!(store.stats(), super::StoreStats::default());
assert_eq!(
store.stats().hit_rate(),
None,
"no lookups is not a zero hit rate"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_whole_store_survives_a_restart_with_its_facts_intact() {
let files = MemoryFiles::new()
.with_file("/p/config.h", "#define FEATURE 1\n")
.with_file("/p/widget.h", "struct Widget { int size; };\n")
.with_file(
"/p/main.cpp",
"#include \"widget.h\"\n#include \"config.h\"\nvoid f() { Widget w; }\n",
);
let (mut store, root) = store("restart", &files);
for path in ["/p/config.h", "/p/widget.h", "/p/main.cpp"] {
store.get(Path::new(path)).expect("the file reads");
}
assert_eq!(store.stats().rebuilt, 3);
let found = store.index().definition("Widget", Path::new("/p/main.cpp"));
assert!(
matches!(found, crate::Known::Yes(_)),
"the class is visible through the include: {found:?}"
);
let mut reopened = SummaryStore::with_provider(&root, CompilerConfig::default(), files.clone());
for path in ["/p/config.h", "/p/widget.h", "/p/main.cpp"] {
reopened.get(Path::new(path)).expect("the file reads");
}
assert_eq!(reopened.stats().rebuilt, 0, "nothing needed parsing");
assert_eq!(reopened.stats().reused, 3);
let after = reopened
.index()
.definition("Widget", Path::new("/p/main.cpp"));
assert!(
matches!(after, crate::Known::Yes(_)),
"and the answer survives the restart: {after:?}"
);
let _ = std::fs::remove_dir_all(&root);
}
use super::{IncludeBudget, NotIndexedReason, StoreStats};
fn chain() -> MemoryFiles {
MemoryFiles::new()
.with_case_insensitive(false)
.with_file(
"/p/main.cpp",
"#include \"middle.h\"\nint main() { return 0; }\n",
)
.with_file(
"/p/middle.h",
"#include \"deep.h\"\nstruct Middle { Deep d; };\n",
)
.with_file("/p/deep.h", "struct Deep { int x; };\n")
}
fn indexed(index: &super::IncludeIndex) -> Vec<String> {
index
.indexed
.iter()
.map(|path| path.to_string_lossy().replace('\\', "/"))
.collect()
}
#[test]
fn indexing_a_file_indexes_everything_it_includes() {
let files = chain();
let (mut store, root) = store("closure", &files);
let index = store.index_includes_from(Path::new("/p/main.cpp"), IncludeBudget::default());
assert_eq!(
indexed(&index),
["/p/main.cpp", "/p/middle.h", "/p/deep.h"],
"the entry first, then outwards"
);
assert!(index.not_indexed.is_empty(), "{:?}", index.not_indexed);
assert!(index.unresolved.is_empty());
assert_eq!(index.stats.rebuilt, 3, "three files, three parses");
assert_eq!(index.stats.reused, 0);
let found = store.index().definition("Deep", Path::new("/p/main.cpp"));
assert!(
matches!(found, crate::Known::Yes(_)),
"and a name two headers down is now visible from the file that includes them: {found:?}"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_header_reached_twice_is_indexed_once() {
let files = MemoryFiles::new()
.with_case_insensitive(false)
.with_file("/p/main.cpp", "#include \"a.h\"\n#include \"b.h\"\n")
.with_file("/p/a.h", "#include \"shared.h\"\nstruct A { int x; };\n")
.with_file("/p/b.h", "#include \"shared.h\"\nstruct B { int y; };\n")
.with_file("/p/shared.h", "struct Shared { int x; };\n");
let (mut store, root) = store("diamond", &files);
let index = store.index_includes_from(Path::new("/p/main.cpp"), IncludeBudget::default());
assert_eq!(indexed(&index).len(), 4, "{:?}", indexed(&index));
assert_eq!(
indexed(&index)
.iter()
.filter(|path| *path == "/p/shared.h")
.count(),
1
);
assert_eq!(
index.stats.rebuilt, 4,
"four files, four parses — the fifth visit is the one the visited set stopped, and it is why this \
number is not five"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn two_files_with_the_same_text_side_by_side_share_one_parse() {
let files = MemoryFiles::new()
.with_case_insensitive(false)
.with_file("/p/main.cpp", "#include \"one.h\"\n#include \"two.h\"\n")
.with_file("/p/one.h", "struct Same { int x; };\n")
.with_file("/p/two.h", "struct Same { int x; };\n");
let (mut store, root) = store("twin-headers", &files);
let index = store.index_includes_from(Path::new("/p/main.cpp"), IncludeBudget::default());
assert_eq!(indexed(&index).len(), 3, "three files");
assert_eq!(
index.stats.rebuilt, 2,
"and two parses, because two of them are the same text"
);
assert_eq!(
index.stats.reused, 1,
"the twin was answered by the entry the first one wrote"
);
let declared_in: Vec<String> = store
.index()
.files_declaring("Same", Path::new("/p/main.cpp"))
.iter()
.map(|found| found.file.to_string_lossy().replace('\\', "/"))
.collect();
assert_eq!(declared_in, ["/p/one.h", "/p/two.h"]);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_cycle_of_includes_terminates() {
let files = MemoryFiles::new()
.with_case_insensitive(false)
.with_file("/p/main.cpp", "#include \"a.h\"\n")
.with_file("/p/a.h", "#include \"b.h\"\nstruct A { int x; };\n")
.with_file("/p/b.h", "#include \"a.h\"\nstruct B { int y; };\n");
let (mut store, root) = store("cycle", &files);
let index = store.index_includes_from(Path::new("/p/main.cpp"), IncludeBudget::default());
assert_eq!(indexed(&index).len(), 3, "{:?}", indexed(&index));
assert!(index.not_indexed.is_empty());
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn an_include_that_resolved_to_nothing_is_reported_rather_than_dropped() {
let files = MemoryFiles::new()
.with_case_insensitive(false)
.with_file(
"/p/main.cpp",
"#include \"middle.h\"\n#include \"gone.h\"\nint main() { return 0; }\n",
)
.with_file("/p/middle.h", "#include \"deep.h\"\n")
.with_file("/p/deep.h", "struct Deep { int x; };\n");
let (mut store, root) = store("missing", &files);
let index = store.index_includes_from(Path::new("/p/main.cpp"), IncludeBudget::default());
assert_eq!(
indexed(&index).len(),
3,
"the rest of the closure was still indexed"
);
assert_eq!(index.unresolved.len(), 1, "{:?}", index.unresolved);
assert_eq!(index.unresolved[0].from, Path::new("/p/main.cpp"));
assert_eq!(index.unresolved[0].spelling, "gone.h");
assert!(
index.unresolved[0].range.start_offset > 0,
"and the range points at the directive rather than at the file"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_file_budget_stops_the_walk_and_says_so() {
let files = chain();
let (mut store, root) = store("budget", &files);
let index = store.index_includes_from(
Path::new("/p/main.cpp"),
IncludeBudget {
max_files: 2,
..IncludeBudget::default()
},
);
assert_eq!(indexed(&index).len(), 2);
assert_eq!(index.not_indexed.len(), 1, "{:?}", index.not_indexed);
assert_eq!(
index.not_indexed[0].reason,
NotIndexedReason::Budget,
"and the reason names the budget rather than the filesystem"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_depth_limit_stops_a_long_chain_and_says_so() {
let files = MemoryFiles::new()
.with_case_insensitive(false)
.with_file("/p/l0.h", "#include \"l1.h\"\n")
.with_file("/p/l1.h", "#include \"l2.h\"\n")
.with_file("/p/l2.h", "struct Deep { int x; };\n");
let (mut store, root) = store("depth", &files);
let index = store.index_includes_from(
Path::new("/p/l0.h"),
IncludeBudget {
max_depth: 1,
..IncludeBudget::default()
},
);
assert_eq!(indexed(&index), ["/p/l0.h", "/p/l1.h"]);
assert_eq!(
index
.not_indexed
.iter()
.map(|stopped| (stopped.reason, stopped.path.to_string_lossy().to_string()))
.collect::<Vec<_>>(),
[(NotIndexedReason::Depth, "/p/l2.h".to_string())]
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_file_that_cannot_be_read_is_reported_rather_than_being_an_error() {
let files = chain();
let (mut store, root) = store("missing-entry", &files);
let index = store.index_includes_from(Path::new("/p/nope.cpp"), IncludeBudget::default());
assert!(index.indexed.is_empty());
assert_eq!(
index.not_indexed,
[super::NotIndexed {
path: Path::new("/p/nope.cpp").to_path_buf(),
reason: NotIndexedReason::Unreadable,
}]
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_second_call_reads_the_whole_closure_from_disk() {
let files = chain();
let root = std::env::temp_dir()
.join("cppls-store-tests")
.join("closure-warm");
let _ = std::fs::remove_dir_all(&root);
let cold = {
let mut store =
SummaryStore::with_provider(&root, CompilerConfig::default(), files.clone());
store.index_includes_from(Path::new("/p/main.cpp"), IncludeBudget::default())
};
assert_eq!(cold.stats.rebuilt, 3);
let mut reopened = SummaryStore::with_provider(&root, CompilerConfig::default(), files.clone());
let warm = reopened.index_includes_from(Path::new("/p/main.cpp"), IncludeBudget::default());
assert_eq!(
indexed(&warm),
indexed(&cold),
"the same files, in the same order"
);
assert_eq!(warm.stats.rebuilt, 0, "the parser was not asked");
assert_eq!(warm.stats.reused, 3);
assert_eq!(warm.stats.hit_rate(), Some(1.0));
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_stats_of_one_call_are_a_delta_and_not_the_session() {
let files = chain();
let (mut store, root) = store("stats-delta", &files);
let first = store.index_includes_from(Path::new("/p/main.cpp"), IncludeBudget::default());
let second = store.index_includes_from(Path::new("/p/main.cpp"), IncludeBudget::default());
assert_eq!(first.stats.rebuilt, 3);
assert_eq!(
second.stats.rebuilt, 0,
"the second call found its own work on disk"
);
assert_eq!(second.stats.reused, 3);
assert_eq!(
store.stats().rebuilt,
3,
"while the session's total is still the whole story"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_default_budget_does_not_truncate_a_measured_real_closure() {
let budget = IncludeBudget::default();
assert!(
budget.max_files > 359,
"the default must not truncate the largest real closure anyone has measured: {budget:?}"
);
assert_eq!(budget.max_depth, crate::MAX_INCLUDE_DEPTH);
}
#[test]
fn a_delta_of_stats_saturates_rather_than_panicking() {
let later = StoreStats {
reused: 1,
rebuilt: 0,
unstored: 0,
stored_refinements_served: 0,
};
let earlier = StoreStats {
reused: 0,
rebuilt: 5,
unstored: 2,
stored_refinements_served: 0,
};
assert_eq!(
later.since(earlier),
StoreStats {
reused: 1,
rebuilt: 0,
unstored: 0,
stored_refinements_served: 0,
},
"a reading given out of order is a caller's mistake, and zero is a better failure than a panic"
);
}
#[test]
fn the_closure_is_the_roots_and_what_they_include() {
let files = MemoryFiles::new()
.with_file(
"/p/open.h",
"#pragma once\n#define _STD_BEGIN namespace std {\n",
)
.with_file(
"/p/widget.h",
"#include \"open.h\"\n_STD_BEGIN\nstruct Widget { int size; };\n}\n",
)
.with_file("/p/main.cpp", "#include \"widget.h\"\nint main;\n")
.with_file(
"/p/other.h",
"#define NULL 0\n#define MIN(a, b) ((a) < (b) ? (a) : (b))\n",
);
let (mut store, root) = store("closure", &files);
for path in ["/p/widget.h", "/p/main.cpp", "/p/other.h"] {
let _ = store.get(Path::new(path));
}
let widget = std::path::PathBuf::from("/p/widget.h");
let closure = store.the_closure_of(std::slice::from_ref(&widget));
let has = |path: &str| {
closure.contains(&crate::file::paths::normalize_path(
Path::new(path),
cfg!(windows),
))
};
assert!(has("/p/widget.h"), "the root is in its own closure");
assert!(
has("/p/open.h"),
"and the file it includes, which is where its macro body is"
);
assert!(
!has("/p/other.h"),
"a file nothing in the closure includes is not in it, however many names it defines: {closure:?}"
);
assert!(
!has("/p/main.cpp"),
"and neither is a file that includes the root — the closure follows `#include`, one way"
);
let main = std::path::PathBuf::from("/p/main.cpp");
let from_main = store.the_closure_of(&[main]);
assert_eq!(
from_main.len(),
3,
"main.cpp, widget.h and open.h, each once: {from_main:?}"
);
let _ = std::fs::remove_dir_all(&root);
}