use std::path::{Path, PathBuf};
use super::model::{TestCase, TestStatus};
const PACK_DEPTH: usize = 4;
const DISCOVER_CAP: usize = 50_000;
pub fn declares_tests(text: &str) -> (bool, bool) {
let key = |k: &str| text.lines().any(|l| l.starts_with(k));
(key("tests:"), key("extractor:"))
}
fn walk(dir: &Path, max_depth: Option<usize>) -> impl Iterator<Item = PathBuf> {
ignore::WalkBuilder::new(dir)
.git_ignore(true)
.require_git(false)
.hidden(false)
.max_depth(max_depth)
.filter_entry(|e| {
e.depth() == 0
|| !e.file_type().is_some_and(|t| t.is_dir())
|| !(crate::file_index::is_noise_dir(e.file_name())
|| e.file_name().to_string_lossy().ends_with(".testproj")
|| e.file_name() == ".git")
})
.build()
.flatten()
.filter(|e| e.file_type().is_some_and(|t| t.is_file()))
.take(DISCOVER_CAP)
.map(|e| e.into_path())
}
fn is_test(path: &Path) -> bool {
matches!(
path.extension().and_then(|e| e.to_str()),
Some("ql" | "qlref")
) && path.with_extension("expected").is_file()
}
pub fn test_packs(root: &Path, markers: &[String]) -> Vec<PathBuf> {
let mut packs: Vec<PathBuf> = walk(root, Some(PACK_DEPTH))
.filter(|p| {
p.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| markers.iter().any(|m| m == n))
})
.filter_map(|file| {
let dir = file.parent()?.to_path_buf();
let text = std::fs::read_to_string(&file).ok()?;
match declares_tests(&text) {
(true, _) => Some(dir),
(false, true) if walk(&dir, None).any(|p| is_test(&p)) => Some(dir),
_ => None,
}
})
.collect();
packs.sort();
packs.dedup();
packs
}
pub fn test_id(root: &Path, path: &Path) -> String {
let file = path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
let dir = path.parent().unwrap_or(Path::new(""));
let rel = dir
.strip_prefix(root)
.ok()
.map(Path::to_path_buf)
.or_else(|| {
let canon = std::fs::canonicalize(root).ok()?;
dir.strip_prefix(canon).ok().map(Path::to_path_buf)
})
.unwrap_or_else(|| dir.to_path_buf());
let rel = rel
.components()
.map(|c| c.as_os_str().to_string_lossy().into_owned())
.collect::<Vec<_>>()
.join("/");
if rel.is_empty() {
file
} else {
format!("{rel}::{file}")
}
}
pub fn id_path(id: &str) -> String {
match id.rsplit_once("::") {
Some((dir, file)) => format!("{dir}/{file}"),
None => id.to_string(),
}
}
pub fn discover(root: &Path, packs: &[PathBuf]) -> Vec<String> {
let mut ids: Vec<String> = packs
.iter()
.flat_map(|pack| walk(pack, None))
.filter(|p| is_test(p))
.map(|p| test_id(root, &p))
.collect();
ids.sort();
ids.dedup();
ids
}
pub fn all_args(root: &Path, packs: &[PathBuf]) -> Option<Vec<String>> {
let rel: Vec<String> = packs
.iter()
.filter_map(|p| p.strip_prefix(root).ok())
.map(|p| {
let s = p.display().to_string();
if s.is_empty() { String::from(".") } else { s }
})
.collect();
if rel.is_empty() {
return None;
}
let mut args = vec![String::from("test"), String::from("run")];
args.extend(rel);
Some(args)
}
pub fn select_args(id: &str) -> Vec<String> {
vec![String::from("test"), String::from("run"), id_path(id)]
}
pub fn test_for_file(path: &Path) -> Option<PathBuf> {
let ext = path.extension()?.to_str()?;
if !matches!(ext, "ql" | "qlref" | "expected" | "actual") {
return None;
}
["ql", "qlref"]
.iter()
.map(|e| path.with_extension(e))
.find(|t| t.is_file() && is_test(t))
}
pub fn actual_output(test: &Path) -> PathBuf {
test.with_extension("actual")
}
pub fn accept_args(tests: &[PathBuf]) -> Vec<String> {
let mut args = vec![String::from("test"), String::from("accept")];
args.extend(tests.iter().map(|t| t.display().to_string()));
args
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Outcome {
pub id: String,
pub status: TestStatus,
pub stage: Option<String>,
pub message: Vec<String>,
}
impl Outcome {
pub fn case(&self) -> TestCase {
TestCase {
name: self.id.clone(),
status: self.status,
}
}
pub fn failure_summary(&self) -> Option<String> {
if self.status != TestStatus::Failed {
return None;
}
let stage = self.stage.as_deref().unwrap_or("FAILED");
let detail = self.message.iter().map(|l| l.trim()).find(|l| {
!l.is_empty()
&& !l.starts_with("--- ")
&& !l.starts_with("+++ ")
&& !l.starts_with("@@")
});
Some(match detail {
Some(d) => format!("{} failed ({stage}): {d}", self.id),
None => format!("{} failed ({stage})", self.id),
})
}
}
fn result_line(line: &str) -> Option<(TestStatus, Option<String>, &str)> {
let rest = line.trim_start().strip_prefix('[')?;
let (counter, rest) = rest.split_once(']')?;
let (done, total) = counter.split_once('/')?;
let total = total.split_whitespace().next()?;
if done.parse::<u32>().is_err() || total.parse::<u32>().is_err() {
return None;
}
let (verdict, path) = rest.trim_start().split_once(char::is_whitespace)?;
let path = path.trim();
if !(path.ends_with(".ql") || path.ends_with(".qlref")) {
return None;
}
let (status, stage) = if verdict == "PASSED" {
(TestStatus::Passed, None)
} else if let Some(rest) = verdict.strip_prefix("FAILED") {
let stage = rest
.strip_prefix('(')
.and_then(|s| s.strip_suffix(')'))
.map(str::to_string);
(TestStatus::Failed, stage)
} else if verdict.starts_with("SKIPPED") {
(TestStatus::Skipped, None)
} else {
return None;
};
Some((status, stage, path))
}
fn is_boundary(line: &str) -> bool {
let t = line.trim();
[
"Executing ",
"Extracting test database",
"Compiling queries",
"All ",
"FAILED: ",
]
.iter()
.any(|p| t.starts_with(p))
|| (t.contains(" tests passed;") && t.contains(" tests failed"))
}
fn is_diagnostic(line: &str) -> bool {
let t = line.trim_start();
t.starts_with("ERROR:") || t.starts_with("WARNING:") || t.starts_with("Error:")
}
fn names_other_query(line: &str, file: &str) -> bool {
(line.contains(".ql") || line.contains(".qlref")) && !line.contains(file)
}
pub struct RunStream {
root: PathBuf,
open: Option<Outcome>,
held: Vec<String>,
}
impl RunStream {
pub fn new(root: &Path) -> Self {
Self {
root: root.to_path_buf(),
open: None,
held: Vec::new(),
}
}
pub fn feed(&mut self, line: &str) -> Option<Outcome> {
if let Some((status, stage, path)) = result_line(line) {
let done = self.open.take();
let path = Path::new(path);
let abs = if path.is_absolute() {
path.to_path_buf()
} else {
self.root.join(path)
};
let held = std::mem::take(&mut self.held);
self.open = Some(Outcome {
id: test_id(&self.root, &abs),
status,
stage,
message: if status == TestStatus::Failed {
held
} else {
Vec::new()
},
});
return done;
}
if is_boundary(line) {
self.held.clear();
return self.open.take();
}
match &mut self.open {
Some(o) if o.status == TestStatus::Failed => {
let file = o.id.rsplit("::").next().unwrap_or(&o.id).to_string();
if is_diagnostic(line) && names_other_query(line, &file) {
self.held.push(line.to_string());
} else if !line.trim().is_empty() {
o.message.push(line.to_string());
}
}
_ if is_diagnostic(line) => self.held.push(line.to_string()),
_ => {}
}
None
}
pub fn finish(&mut self) -> Option<Outcome> {
self.open.take()
}
}
#[cfg(test)]
pub fn parse_output(root: &Path, text: &str) -> Vec<Outcome> {
let mut stream = RunStream::new(root);
let mut out: Vec<Outcome> = text.lines().filter_map(|l| stream.feed(l)).collect();
out.extend(stream.finish());
out
}
#[cfg(test)]
mod tests {
use super::*;
const ROOT: &str = "/work/queries";
const SAMPLE: &str = "\
Executing 3 tests in 2 directories.
Extracting test database in /work/queries/test/Sql.
Compiling queries in /work/queries/test/Sql.
Executing tests in /work/queries/test/Sql.
[1/3 comp 2.1s eval 412ms] PASSED /work/queries/test/Sql/SqlInjection.qlref
[2/3 comp 1.9s eval 388ms] FAILED(RESULT) /work/queries/test/Sql/Tainted.ql
--- expected
+++ actual
@@ -1,2 +1,3 @@
| app.js:3:5:3:9 | query | tainted |
+| app.js:9:5:9:9 | other | tainted |
Extracting test database in /work/queries/test/Broken.
Compiling queries in /work/queries/test/Broken.
ERROR: could not resolve type Foo (/work/queries/test/Broken/Broken.ql:4,8-11)
[3/3] FAILED(COMPILATION) /work/queries/test/Broken/Broken.ql
1 tests passed; 2 tests failed:
FAILED: /work/queries/test/Sql/Tainted.ql
FAILED: /work/queries/test/Broken/Broken.ql
";
#[test]
fn result_lines_give_status_stage_and_path() {
assert_eq!(
result_line("[1/3 comp 2.1s eval 412ms] PASSED /a/Foo.qlref"),
Some((TestStatus::Passed, None, "/a/Foo.qlref"))
);
assert_eq!(
result_line("[12/40] FAILED(RESULT) rel/Bar.ql"),
Some((
TestStatus::Failed,
Some(String::from("RESULT")),
"rel/Bar.ql"
))
);
assert_eq!(result_line(" FAILED: /a/Bar.ql"), None);
assert_eq!(result_line("+| [1/2] PASSED x.ql |"), None);
assert_eq!(result_line("[a/b] PASSED /a/Foo.ql"), None);
assert_eq!(result_line("[1/1] PASSED /a/notes.txt"), None);
}
#[test]
fn a_run_yields_passes_diffs_and_compilation_errors() {
let out = parse_output(Path::new(ROOT), SAMPLE);
assert_eq!(out.len(), 3, "{out:?}");
assert_eq!(out[0].id, "test/Sql::SqlInjection.qlref");
assert_eq!(out[0].status, TestStatus::Passed);
assert!(out[0].message.is_empty());
assert_eq!(out[1].id, "test/Sql::Tainted.ql");
assert_eq!(out[1].status, TestStatus::Failed);
assert_eq!(out[1].stage.as_deref(), Some("RESULT"));
assert_eq!(
out[1].message,
vec![
"--- expected",
"+++ actual",
"@@ -1,2 +1,3 @@",
" | app.js:3:5:3:9 | query | tainted |",
"+| app.js:9:5:9:9 | other | tainted |",
],
"the diff, and nothing past the next phase line"
);
assert_eq!(
out[1].failure_summary().as_deref(),
Some("test/Sql::Tainted.ql failed (RESULT): | app.js:3:5:3:9 | query | tainted |")
);
assert_eq!(out[2].id, "test/Broken::Broken.ql");
assert_eq!(out[2].stage.as_deref(), Some("COMPILATION"));
assert_eq!(
out[2].message,
vec!["ERROR: could not resolve type Foo (/work/queries/test/Broken/Broken.ql:4,8-11)"],
"an error printed before the result line belongs to it"
);
}
#[test]
fn an_error_after_its_result_line_and_the_last_test_are_kept() {
let text = "\
[1/2 comp 1s] FAILED(COMPILATION) /work/queries/A.ql
ERROR: could not resolve module Bar (/work/queries/A.ql:1,8-11)
ERROR: could not resolve type Baz (/work/queries/B.ql:2,1-4)
[2/2] FAILED(COMPILATION) /work/queries/B.ql
";
let out = parse_output(Path::new(ROOT), text);
assert_eq!(out.len(), 2);
assert_eq!(out[0].id, "A.ql", "a test at the root has no suite");
assert_eq!(
out[0].message,
vec!["ERROR: could not resolve module Bar (/work/queries/A.ql:1,8-11)"]
);
assert_eq!(
out[1].message,
vec!["ERROR: could not resolve type Baz (/work/queries/B.ql:2,1-4)"],
"the output ended with B open; finish() still returns it"
);
let all_passed =
"[1/1 comp 1s eval 2ms] PASSED /work/queries/t/A.ql\nAll 1 tests passed.\n";
let out = parse_output(Path::new(ROOT), all_passed);
assert_eq!(out.len(), 1);
assert_eq!(out[0].status, TestStatus::Passed);
assert_eq!(out[0].failure_summary(), None);
}
#[test]
fn ids_map_to_the_paths_codeql_takes() {
let root = Path::new(ROOT);
assert_eq!(
test_id(root, Path::new("/work/queries/a/b/X.qlref")),
"a/b::X.qlref"
);
assert_eq!(id_path("a/b::X.qlref"), "a/b/X.qlref");
assert_eq!(id_path("a/b"), "a/b", "a suite is its folder");
assert_eq!(select_args("X.ql"), vec!["test", "run", "X.ql"]);
assert_eq!(
all_args(root, &[root.join("test"), root.join("java/ql/test")]).unwrap(),
vec!["test", "run", "test", "java/ql/test"]
);
assert_eq!(
all_args(root, &[root.to_path_buf()]).unwrap(),
vec!["test", "run", "."]
);
assert_eq!(all_args(root, &[]), None, "no test pack, no full run");
}
fn write(root: &Path, rel: &str, text: &str) {
let p = root.join(rel);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, text).unwrap();
}
fn markers() -> Vec<String> {
vec![String::from("qlpack.yml"), String::from("codeql-pack.yml")]
}
#[test]
fn a_tests_files_lead_back_to_the_test_for_accepting() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
write(root, "t/Find/Find.qlref", "Find.ql");
write(root, "t/Find/Find.expected", "");
write(root, "t/Find/Find.actual", "");
write(root, "t/Loose/Loose.ql", "select 1");
let find = root.join("t/Find/Find.qlref");
for f in ["Find.qlref", "Find.expected", "Find.actual"] {
assert_eq!(
test_for_file(&root.join("t/Find").join(f)),
Some(find.clone()),
"{f}"
);
}
assert_eq!(
test_for_file(&root.join("t/Loose/Loose.ql")),
None,
"no .expected"
);
assert_eq!(actual_output(&find), root.join("t/Find/Find.actual"));
assert_eq!(
accept_args(std::slice::from_ref(&find)),
["test", "accept", find.to_str().unwrap()]
);
}
#[test]
fn discovery_finds_queries_with_expected_results() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
write(root, "src/qlpack.yml", "name: acme/queries\n");
write(root, "src/Find.ql", "select 1");
write(
root,
"test/qlpack.yml",
"name: acme/tests\nextractor: javascript\ntests: .\n",
);
write(root, "test/Find/Find.qlref", "Find.ql\n");
write(root, "test/Find/Find.expected", "");
write(root, "test/Find/test.js", "x()");
write(root, "test/Inline/Inline.ql", "select 1");
write(root, "test/Inline/Inline.expected", "");
write(root, "test/Draft/Draft.ql", "select 1");
write(root, "test/Find/Find.testproj/x/Y.ql", "");
write(root, "test/Find/Find.testproj/x/Y.expected", "");
let packs = test_packs(root, &markers());
assert_eq!(packs, vec![root.join("test")]);
assert_eq!(
discover(root, &packs),
vec!["test/Find::Find.qlref", "test/Inline::Inline.ql"]
);
write(root, "loose/Loose.ql", "select 1");
write(root, "loose/Loose.expected", "");
assert_eq!(discover(root, &packs).len(), 2);
assert!(discover(root, &[]).is_empty());
}
#[test]
fn only_packs_that_hold_tests_are_test_packs() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
write(root, "codeql-pack.yml", "name: acme/q\nextractor: go\n");
write(root, "Q.ql", "select 1");
assert!(test_packs(root, &markers()).is_empty());
write(root, "other/qlpack.yml", "name: x\nbuild:\n tests: yes\n");
assert!(test_packs(root, &markers()).is_empty());
write(root, "Q.expected", "");
assert_eq!(test_packs(root, &markers()), vec![root.to_path_buf()]);
assert_eq!(declares_tests("tests: .\n"), (true, false));
}
}