Let test.rs --exact also accept file path of a typ file (#2817)

This commit is contained in:
Leedehai 2023-12-06 05:02:27 -05:00 committed by GitHub
parent d1835b418f
commit 9a62b21a25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ use std::ffi::OsStr;
use std::fmt::{self, Display, Formatter, Write as _}; use std::fmt::{self, Display, Formatter, Write as _};
use std::io::{self, IsTerminal, Write}; use std::io::{self, IsTerminal, Write};
use std::ops::Range; use std::ops::Range;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf, MAIN_SEPARATOR_STR};
use std::{env, fs}; use std::{env, fs};
use clap::Parser; use clap::Parser;
@ -31,6 +31,7 @@ use typst::{Library, World, WorldExt};
use unscanny::Scanner; use unscanny::Scanner;
use walkdir::WalkDir; use walkdir::WalkDir;
// These directories are all relative to the tests/ directory.
const TYP_DIR: &str = "typ"; const TYP_DIR: &str = "typ";
const REF_DIR: &str = "ref"; const REF_DIR: &str = "ref";
const PNG_DIR: &str = "png"; const PNG_DIR: &str = "png";
@ -71,14 +72,19 @@ struct PrintConfig {
} }
impl Args { impl Args {
fn matches(&self, path: &Path) -> bool { fn matches(&self, canonicalized_path: &Path) -> bool {
if self.exact { let path = canonicalized_path.to_string_lossy();
let name = path.file_name().unwrap().to_string_lossy(); if !self.exact {
self.filter.iter().any(|v| v == &name) return self.filter.is_empty()
} else { || self.filter.iter().any(|v| path.contains(v));
let path = path.to_string_lossy();
self.filter.is_empty() || self.filter.iter().any(|v| path.contains(v))
} }
self.filter.iter().any(|v| match path.strip_suffix(v) {
None => false,
Some(residual) => {
residual.is_empty() || residual.ends_with(MAIN_SEPARATOR_STR)
}
})
} }
} }
@ -89,7 +95,7 @@ fn main() {
let world = TestWorld::new(args.print); let world = TestWorld::new(args.print);
println!("Running tests..."); println!("Running tests...");
let results = WalkDir::new("typ") let results = WalkDir::new(TYP_DIR)
.into_iter() .into_iter()
.par_bridge() .par_bridge()
.filter_map(|entry| { .filter_map(|entry| {
@ -102,12 +108,12 @@ fn main() {
return None; return None;
} }
let src_path = entry.into_path(); let src_path = entry.into_path(); // Relative to TYP_DIR.
if src_path.extension() != Some(OsStr::new("typ")) { if src_path.extension() != Some(OsStr::new("typ")) {
return None; return None;
} }
if args.matches(&src_path) { if args.matches(&src_path.canonicalize().unwrap()) {
Some(src_path) Some(src_path)
} else { } else {
None None