Walk query files recursively in tree-sitter test.
We were only walking one level of depth into the `queries/` folder during invocations of `test`, which made us attempt to open folders rather than recurse into them. We have to pull in the `walkdir` crate, which is required for cross-platform walking of directories. Fixes #938.
This commit is contained in:
parent
225e15cb9f
commit
44010d69ea
4 changed files with 56 additions and 13 deletions
|
|
@ -2,6 +2,7 @@ use super::test_highlight;
|
|||
use std::fmt::Write;
|
||||
use std::io;
|
||||
use tree_sitter::{QueryError, QueryErrorKind};
|
||||
use walkdir;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error(pub Vec<String>);
|
||||
|
|
@ -121,3 +122,9 @@ impl From<String> for Error {
|
|||
Error::new(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<walkdir::Error> for Error {
|
||||
fn from(error: walkdir::Error) -> Self {
|
||||
Error::new(error.to_string())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,12 +6,14 @@ use lazy_static::lazy_static;
|
|||
use regex::bytes::{Regex as ByteRegex, RegexBuilder as ByteRegexBuilder};
|
||||
use regex::Regex;
|
||||
use std::char;
|
||||
use std::ffi::OsStr;
|
||||
use std::fmt::Write as FmtWrite;
|
||||
use std::fs;
|
||||
use std::io::{self, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str;
|
||||
use tree_sitter::{Language, LogType, Parser, Query};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
lazy_static! {
|
||||
static ref HEADER_REGEX: ByteRegex = ByteRegexBuilder::new(r"^===+\r?\n([^=]*)\r?\n===+\r?\n")
|
||||
|
|
@ -122,17 +124,20 @@ pub fn run_tests_at_path(
|
|||
|
||||
pub fn check_queries_at_path(language: Language, path: &Path) -> Result<()> {
|
||||
if path.exists() {
|
||||
for entry in fs::read_dir(path)? {
|
||||
let entry = entry?;
|
||||
let filepath = entry.file_name();
|
||||
let filepath = filepath.to_str().unwrap_or("");
|
||||
let hidden = filepath.starts_with(".");
|
||||
if !hidden {
|
||||
let content = fs::read_to_string(entry.path()).map_err(Error::wrap(|| {
|
||||
format!("Error reading query file {:?}", entry.file_name())
|
||||
}))?;
|
||||
Query::new(language, &content).map_err(|e| (filepath, e))?;
|
||||
}
|
||||
for entry in WalkDir::new(path)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| {
|
||||
e.file_type().is_file()
|
||||
&& e.path().extension().and_then(OsStr::to_str) == Some("scm")
|
||||
&& !e.path().starts_with(".")
|
||||
})
|
||||
{
|
||||
let filepath = entry.file_name().to_str().unwrap_or("");
|
||||
let content = fs::read_to_string(entry.path()).map_err(Error::wrap(|| {
|
||||
format!("Error reading query file {:?}", entry.file_name())
|
||||
}))?;
|
||||
Query::new(language, &content).map_err(|e| (filepath, e))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue