use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
fn gd_files(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
if path
.file_name()
.is_some_and(|name| name == ".godot" || name == ".git")
{
continue;
}
gd_files(&path, out);
} else if path.extension().is_some_and(|ext| ext == "gd") {
out.push(path);
}
}
}
#[test]
fn corpus_round_trips_without_errors() {
let Some(roots) = std::env::var_os("GDSYNTAX_CORPUS") else {
eprintln!("GDSYNTAX_CORPUS is not set; skipping");
return;
};
let exclude: Vec<String> = std::env::var("GDSYNTAX_CORPUS_EXCLUDE")
.map(|v| v.split(',').map(|s| s.trim().replace('\\', "/")).collect())
.unwrap_or_default();
let mut files = Vec::new();
for root in std::env::split_paths(&roots) {
gd_files(&root, &mut files);
}
files.retain(|file| {
let shown = file.to_string_lossy().replace('\\', "/");
!exclude
.iter()
.any(|part| !part.is_empty() && shown.contains(part.as_str()))
});
assert!(!files.is_empty(), "no .gd files under GDSYNTAX_CORPUS");
let mut failures = Vec::new();
let (mut lines, mut elapsed) = (0, Duration::ZERO);
for file in &files {
let Ok(source) = std::fs::read_to_string(file) else {
continue; };
lines += source.lines().count();
let start = Instant::now();
let parse = gdsyntax::parser::parse(&source);
elapsed += start.elapsed();
let tree = gdsyntax::SyntaxNode::new_root(parse.green);
let errors = parse.errors;
if tree.to_string() != source {
failures.push(format!(
"{}: the tree does not reproduce the source",
file.display()
));
}
for error in errors {
let line = source[..error.start].matches('\n').count() + 1;
failures.push(format!("{}:{line}: {}", file.display(), error.message));
}
}
eprintln!(
"{} files, {lines} lines, parsed in {elapsed:.1?}, {} problems",
files.len(),
failures.len()
);
assert!(failures.is_empty(), "{}", failures.join("\n"));
}