use crate::file::paths::DiskFiles;
use crate::include::config::CompilerConfig;
use crate::index::watch::WatchFilter;
use crate::session::{OpenDocuments, Session, SessionFiles};
use super::*;
#[test]
pub(crate) fn a_project_configuration_narrows_the_scan_and_widens_the_source_list() {
let project = Project::new("config-scan");
project.write("main.cpp", "int main() { return 0; }\n");
project.write("vendor/third_party.cpp", "int vendored;\n");
project.write("kernels/vector.cuh", "struct FromCuda { int x; };\n");
project.write(
".cppls.toml",
"[workspace]\nexclude = [\"vendor/**\"]\nsource_extensions = [\"cuh\"]\n",
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::with_config(
&project.root,
providers,
WatchFilter::new(&project.root),
CompilerConfig::default(),
);
assert_eq!(
project.relative(session.project_files()),
["kernels/vector.cuh", "main.cpp"],
"the excluded tree is not scanned, and the project's own extension is"
);
assert!(
session.project_config().found(),
"the file was read: {:?}",
session.project_config()
);
assert!(session.project_config().problems.is_empty());
}
#[test]
pub(crate) fn the_projects_flags_win_over_the_databases_and_extra_args_add_to_the_winner() {
let project = Project::new("config-flags");
project.write("main.cpp", "int main() { return 0; }\n");
let root = project.root.to_string_lossy().replace('\\', "/");
project.write(
"compile_commands.json",
&format!(
"[\n {{\"directory\": \"{root}\", \"file\": \"{root}/main.cpp\", \
\"arguments\": [\"g++\", \"-DFROM_THE_DATABASE\", \"-std=c++17\", \"-Werror\", \
\"-c\", \"{root}/main.cpp\"]}}\n]\n"
),
);
project.write(
".cppls.toml",
"[compile]\nargs = [\"-DFROM_THE_CONFIG\", \"-std=c++20\"]\nextra_args = [\"-DEXTRA\"]\n",
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::open(&project.root, providers, WatchFilter::new(&project.root));
let defines: Vec<&str> = session
.config()
.defines
.iter()
.map(|define| define.name.as_ref())
.collect();
assert_eq!(
defines,
["FROM_THE_CONFIG", "EXTRA"],
"the file's flags are the compilation, and `extra_args` joins them"
);
assert_eq!(
session.config().standard.as_deref(),
Some("c++20"),
"the database's `-std=c++17` is not in effect"
);
}
#[test]
pub(crate) fn the_database_states_the_projects_standard_and_the_seed_carries_it() {
let project = Project::new("config-standard-seed");
project.write("main.cpp", "int main() { return 0; }\n");
let root = project.root.to_string_lossy().replace('\\', "/");
project.write(
"compile_commands.json",
&format!(
"[\n {{\"directory\": \"{root}\", \"file\": \"{root}/main.cpp\", \
\"arguments\": [\"cl.exe\", \"/std:c++latest\", \"/c\", \"{root}/main.cpp\"]}}\n]\n"
),
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::open(&project.root, providers, WatchFilter::new(&project.root));
assert_eq!(
session.config().standard.as_deref(),
Some("c++latest"),
"the session's own configuration — the one the seed is built from — states the database's standard"
);
assert_eq!(
session
.config_for(&project.root.join("some_header.hpp"))
.standard
.as_deref(),
Some("c++latest"),
"a file with no command inherits the project's one standard"
);
}
#[test]
pub(crate) fn removing_and_adding_a_flag_is_one_pass_in_that_order() {
let project = Project::new("config-remove");
project.write("main.cpp", "int main() { return 0; }\n");
let root = project.root.to_string_lossy().replace('\\', "/");
project.write(
"compile_commands.json",
&format!(
"[\n {{\"directory\": \"{root}\", \"file\": \"{root}/main.cpp\", \
\"arguments\": [\"g++\", \"-std=c++17\", \"-DKEEP\", \"-Werror\", \"-c\", \"{root}/main.cpp\"]}}\n]\n"
),
);
project.write(
".cppls.toml",
"[compile]\nremove_args = [\"-Werror\", \"-std=c++17\"]\nextra_args = [\"-std=c++20\"]\n",
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::open(&project.root, providers, WatchFilter::new(&project.root));
assert_eq!(
session
.config_for(&project.root.join("main.cpp"))
.standard
.as_deref(),
Some("c++20")
);
let per_file = session.config_for(&project.root.join("main.cpp"));
let defines: Vec<&str> = per_file
.defines
.iter()
.map(|define| define.name.as_ref())
.collect();
assert_eq!(defines, ["KEEP"], "a flag nobody removed still applies");
}
#[test]
pub(crate) fn the_database_can_be_somewhere_no_convention_predicts() {
let project = Project::new("config-database");
project.write("main.cpp", "int main() { return 0; }\n");
let root = project.root.to_string_lossy().replace('\\', "/");
project.write(
"out/compile_commands.json",
&format!(
"[\n {{\"directory\": \"{root}\", \"file\": \"{root}/main.cpp\", \
\"arguments\": [\"g++\", \"-DFROM_ELSEWHERE\", \"-c\", \"{root}/main.cpp\"]}}\n]\n"
),
);
project.write(
".cppls.toml",
"[compile]\ndatabase = \"out/compile_commands.json\"\n",
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::open(&project.root, providers, WatchFilter::new(&project.root));
assert_eq!(
session.compile_database().map(crate::CompileCommands::len),
Some(1)
);
let defines: Vec<&str> = session
.config()
.defines
.iter()
.map(|define| define.name.as_ref())
.collect();
assert_eq!(defines, ["FROM_ELSEWHERE"]);
assert_eq!(project.relative(session.project_files()), ["main.cpp"]);
}
#[test]
pub(crate) fn the_cache_goes_where_the_project_says_and_the_filter_follows_it() {
let project = Project::new("config-cache");
project.write("main.cpp", "int main() { return 0; }\n");
project.write(".cppls.toml", "[index]\ncache_dir = \".cppls-cache\"\n");
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let mut session = Session::with_config(
&project.root,
providers,
WatchFilter::new(&project.root),
CompilerConfig::default(),
);
session.index_everything();
assert_eq!(session.stats().rebuilt, 1, "the file was read");
assert!(
project.root.join(".cppls-cache").is_dir(),
"the cache is under the configured directory, not the default: {}",
project.root.join(".cppls-cache").display()
);
assert!(
!project.root.join(crate::CACHE_DIRECTORY).exists(),
"and the default directory was not created at all"
);
}
#[test]
pub(crate) fn the_sections_a_language_server_reads_come_from_the_same_parse() {
let project = Project::new("config-server-keys");
project.write("main.cpp", "int main() { return 0; }\n");
project.write(
".cppls.toml",
"[diagnostics]\non_change_ms = 250\n\n[hover]\nenable = false\n",
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::with_config(
&project.root,
providers,
WatchFilter::new(&project.root),
CompilerConfig::default(),
);
let config = &session.project_config().config;
assert_eq!(config.diagnostics.on_change_ms, Some(250));
assert_eq!(config.hover.enable, Some(false));
}
#[test]
pub(crate) fn a_configuration_file_with_a_typo_still_analyses_the_project() {
let project = Project::new("config-typo");
project.write("main.cpp", "int main() { return 0; }\n");
project.write("vendor/third_party.cpp", "int vendored;\n");
project.write(
".cppls.toml",
"[workspace]\nexclude = [\"vendor/**\"]\n\n[index]\ncache_dir = 5\n",
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::with_config(
&project.root,
providers,
WatchFilter::new(&project.root),
CompilerConfig::default(),
);
assert_eq!(
project.relative(session.project_files()),
["main.cpp"],
"the exclusions are in effect"
);
assert_eq!(session.project_config().problems.len(), 1);
assert!(
session.project_config().problems[0]
.message
.contains("[index]")
);
assert_eq!(session.project_config().errors().count(), 1);
}
#[test]
pub(crate) fn a_database_in_a_build_directory_is_found_and_its_flags_reach_the_session() {
let project = Project::new("build-directory-database");
project.write("main.cpp", "int main() { return 0; }\n");
let root = project.root.to_string_lossy().replace('\\', "/");
project.write(
"build/compile_commands.json",
&format!(
"[\n {{\"directory\": \"{root}\", \"file\": \"{root}/main.cpp\", \
\"arguments\": [\"g++\", \"-DFROM_THE_BUILD_DIRECTORY\", \"-std=c++20\", \
\"-c\", \"{root}/main.cpp\"]}}\n]\n"
),
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::open(&project.root, providers, WatchFilter::new(&project.root));
assert_eq!(
session.compile_database().map(crate::CompileCommands::len),
Some(1),
"the database in the build directory was read"
);
assert_eq!(
session
.discovery()
.database
.as_ref()
.map(|found| found.origin),
Some(crate::DatabaseOrigin::BuildDirectory),
"and the report says where it was found"
);
let per_file = session.config_for(&project.root.join("main.cpp"));
let defines: Vec<&str> = per_file
.defines
.iter()
.map(|define| define.name.as_ref())
.collect();
assert_eq!(defines, ["FROM_THE_BUILD_DIRECTORY"]);
assert_eq!(
session
.config_for(&project.root.join("main.cpp"))
.standard
.as_deref(),
Some("c++20")
);
assert!(
session
.discovery()
.problems
.iter()
.any(|problem| problem.message.contains("build directory")),
"and a user reading the report is told: {:?}",
session.discovery().problems
);
}
#[test]
pub(crate) fn a_project_without_a_database_is_analysed_against_the_compilers_defaults() {
let project = Project::new("without-a-database");
project.write("main.cpp", "int main() { return 0; }\n");
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::open(&project.root, providers, WatchFilter::new(&project.root));
assert!(session.compile_database().is_none());
assert!(
session
.discovery()
.problems
.iter()
.any(|problem| problem.message.contains("no compile database")),
"the way to do better is spelled out: {:?}",
session.discovery().problems
);
}
#[test]
pub(crate) fn a_compile_database_names_the_files_that_are_here_and_the_flags_they_are_built_with() {
let project = Project::new("database");
project.write("main.cpp", "int main() { return 0; }\n");
let root = project.root.to_string_lossy().replace('\\', "/");
project.write(
"compile_commands.json",
&format!(
"[\n {{\"directory\": \"{root}\", \"file\": \"{root}/main.cpp\", \
\"arguments\": [\"g++\", \"-DFROM_THE_DATABASE\", \"-I{root}/include\", \"-std=c++20\", \
\"-c\", \"{root}/main.cpp\"]}},\n \
{{\"directory\": \"{root}\", \"file\": \"{root}/elsewhere.cpp\", \
\"arguments\": [\"g++\", \"-c\", \"{root}/elsewhere.cpp\"]}}\n]\n"
),
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let session = Session::open(
&project.root,
providers.clone(),
WatchFilter::new(&project.root),
);
let database = session.compile_database().expect("the database is read");
assert_eq!(database.len(), 2);
let per_file = session.config_for(&project.root.join("main.cpp"));
let defines: Vec<&str> = per_file
.defines
.iter()
.map(|define| define.name.as_ref())
.collect();
assert_eq!(defines, ["FROM_THE_DATABASE"]);
assert_eq!(
session
.config_for(&project.root.join("main.cpp"))
.standard
.as_deref(),
Some("c++20")
);
assert_eq!(project.relative(session.project_files()), ["main.cpp"]);
assert_eq!(
session.pending(),
1,
"the entry for a file that is not here is not work"
);
}
#[test]
pub(crate) fn a_define_the_compile_database_carries_decides_a_condition_in_a_file() {
let project = Project::new("conditional-include");
let root = project.root.to_string_lossy().replace('\\', "/");
project.write("feature.h", "#define FEATURE_ONLY int\n");
project.write(
"main.cpp",
"#ifdef FROM_THE_DATABASE\n#include \"feature.h\"\n#endif\nFEATURE_ONLY x;\n",
);
project.write(
"compile_commands.json",
&format!(
"[{{\"directory\": \"{root}\", \"file\": \"{root}/main.cpp\", \
\"arguments\": [\"g++\", \"-DFROM_THE_DATABASE\", \"-c\", \"{root}/main.cpp\"]}}]\n"
),
);
let documents = OpenDocuments::new();
let providers = SessionFiles::new(documents, DiskFiles);
let mut session = Session::open(
&project.root,
providers.clone(),
WatchFilter::new(&project.root),
);
session.advance(64);
let path = project.root.join("main.cpp");
let _source = std::fs::read_to_string(&path).expect("the fixture reads");
assert!(
session.view(&path).is_none(),
"nothing has read this file as a compiler reads it, and that is what the ask is for"
);
session.index_everything();
let view = session
.view(&path)
.expect("the ask queued it and the pump cooked it");
assert_eq!(
view.source.as_ref(),
"int x;",
"the -D took the branch, the branch included feature.h, and FEATURE_ONLY became int"
);
}