#![allow(dead_code)]
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use tabnas::{Tabnas, Value};
use tabnas_feed::{detect, FeedFormat, FeedOptions};
use tabnas_support::{find_spec_dir, Failure, Runner};
pub fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("rs/ has a parent")
.to_path_buf()
}
pub fn spec_dir() -> PathBuf {
find_spec_dir(Some(Path::new(env!("CARGO_MANIFEST_DIR"))))
.expect("a test/spec directory above rs/")
}
pub fn wellformed_dir() -> PathBuf {
repo_root().join("test").join("feedparser-wellformed")
}
pub fn require_corpus(dir: &str, script: &str) -> PathBuf {
let root = repo_root();
let full = root.join("test").join(dir);
if !full.exists() {
let fetch = Command::new("node")
.arg(root.join("scripts").join("fetch-corpus.mjs"))
.arg(dir)
.status();
if let Err(error) = fetch {
eprintln!("auto-fetch of the {dir} corpus failed: {error}");
}
}
assert!(
full.exists(),
"\n\nCONFORMANCE CORPUS MISSING: {}\n\
This suite cannot run without it, and must never be skipped.\n\
Fetch it (pinned commit, idempotent):\n\n ./scripts/{script}\n\n\
See scripts/{script} for the upstream URL and pinned SHA.\n",
full.display()
);
full
}
pub fn walk_xml(dir: &Path) -> Vec<PathBuf> {
let mut out = Vec::new();
let mut stack = vec![dir.to_path_buf()];
while let Some(next) = stack.pop() {
let entries =
fs::read_dir(&next).unwrap_or_else(|e| panic!("read {}: {e}", next.display()));
for entry in entries {
let entry = entry.expect("a directory entry");
let path = entry.path();
if path.is_dir() {
stack.push(path);
} else if path.extension().is_some_and(|ext| ext == "xml") {
out.push(path);
}
}
}
out.sort();
out
}
pub fn read_file(path: &Path) -> String {
let bytes = fs::read(path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
String::from_utf8_lossy(&bytes).into_owned()
}
pub fn to_value(value: &Value) -> tabnas_support::Value {
tabnas_support::Value::from(value.to_json())
}
pub fn to_failure(error: tabnas::TabnasError) -> Failure {
Failure::new(error.code.clone())
.at(error.row, error.col)
.with_message(error.to_string())
}
pub fn parser_with(options: &FeedOptions) -> Tabnas {
let mut parser = tabnas_jsonic::make();
parser
.use_plugin(tabnas_feed::plugin(), Some(options.to_value()))
.expect("the feed plugin installs on a jsonic instance");
parser
}
pub fn default_parser() -> Tabnas {
parser_with(&FeedOptions::default())
}
pub fn raw_parser() -> Tabnas {
parser_with(&FeedOptions {
format: FeedFormat::Raw,
..FeedOptions::default()
})
}
pub fn strip_ansi(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut chars = text.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '\u{1b}' && chars.peek() == Some(&'[') {
chars.next();
for next in chars.by_ref() {
if next == 'm' {
break;
}
}
continue;
}
out.push(ch);
}
out
}
pub fn json(value: &Value) -> String {
value.to_json().to_string()
}
pub fn spec_runner(mode: &str) -> Runner {
let detect_mode = mode == "detect";
Runner::new_with_row(move |input, row| {
let raw = row.named("opts");
let options = if detect_mode {
FeedOptions {
format: FeedFormat::Raw,
..FeedOptions::default()
}
} else if raw.trim().is_empty() {
FeedOptions::default()
} else {
let bag: serde_json::Value = serde_json::from_str(raw)
.map_err(|error| Failure::message(format!("opts column is not JSON: {error}")))?;
FeedOptions::from_value(&Value::from_json(&bag))
};
let parsed = parser_with(&options).parse(input).map_err(to_failure)?;
Ok(to_value(&if detect_mode {
detect(&parsed).to_value()
} else {
parsed
}))
})
.match_error(|failure, want, _row| strip_ansi(&failure.message).contains(want))
.input("input")
.expected(mode)
}