organic/src/bin_foreign_document_test.rs

190 lines
4.9 KiB
Rust
Raw Normal View History

#![feature(round_char_boundary)]
#![feature(exact_size_is_empty)]
use std::io::Read;
2023-10-08 11:54:21 +00:00
use std::path::Path;
2023-10-11 23:41:32 +00:00
use std::path::PathBuf;
2023-10-11 23:47:30 +00:00
use futures::future::BoxFuture;
use futures::future::FutureExt;
use organic::compare::run_anonymous_compare;
use organic::compare::run_compare_on_file;
2023-10-13 15:41:49 +00:00
use tokio::sync::Semaphore;
2023-10-13 16:07:52 +00:00
use tokio::task::JoinError;
use walkdir::WalkDir;
#[cfg(feature = "tracing")]
use crate::init_tracing::init_telemetry;
#[cfg(feature = "tracing")]
use crate::init_tracing::shutdown_telemetry;
#[cfg(feature = "tracing")]
mod init_tracing;
#[cfg(not(feature = "tracing"))]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let rt = tokio::runtime::Runtime::new()?;
let result = rt.block_on(async {
2023-10-07 05:13:26 +00:00
let main_body_result = main_body().await;
main_body_result
});
result
}
#[cfg(feature = "tracing")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let rt = tokio::runtime::Runtime::new()?;
let result = rt.block_on(async {
init_telemetry()?;
2023-10-07 05:13:26 +00:00
let main_body_result = main_body().await;
shutdown_telemetry()?;
main_body_result
});
result
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
2023-10-07 05:13:26 +00:00
async fn main_body() -> Result<(), Box<dyn std::error::Error>> {
2023-10-13 17:20:21 +00:00
let layer = compare_group("org-mode", || {
compare_all_org_document("/foreign_documents/org-mode")
2023-10-08 21:17:32 +00:00
});
2023-10-13 17:20:21 +00:00
let layer = layer.chain(compare_group("emacs", || {
compare_all_org_document("/foreign_documents/emacs")
}));
let running_tests: Vec<_> = layer.map(|c| tokio::spawn(c.run_test())).collect();
for test in running_tests.into_iter() {
let test_result = test.await??;
println!("{:?}", test_result);
}
2023-10-08 21:17:32 +00:00
Ok(())
}
2023-10-07 05:13:26 +00:00
2023-10-13 17:20:21 +00:00
fn compare_group<N: Into<String>, F: Fn() -> I, I: Iterator<Item = TestConfig>>(
name: N,
inner: F,
) -> impl Iterator<Item = TestConfig> {
std::iter::once(TestConfig::TestLayer(TestLayer {
name: name.into(),
children: inner().collect(),
}))
}
fn compare_all_org_document<P: AsRef<Path>>(root_dir: P) -> impl Iterator<Item = TestConfig> {
let root_dir = root_dir.as_ref();
let test_files = WalkDir::new(root_dir)
.into_iter()
.filter(|e| match e {
Ok(dir_entry) => {
dir_entry.file_type().is_file()
&& Path::new(dir_entry.file_name())
.extension()
.map(|ext| ext.to_ascii_lowercase() == "org")
.unwrap_or(false)
}
Err(_) => true,
})
.collect::<Result<Vec<_>, _>>()
.unwrap();
let test_configs = test_files.into_iter().map(|test_file| {
let name = test_file.path().as_os_str().to_string_lossy().into_owned();
TestConfig::SingleFile(SingleFile {
name,
file_path: test_file.into_path(),
})
});
test_configs
}
2023-10-13 15:41:49 +00:00
static TEST_PERMITS: Semaphore = Semaphore::const_new(8);
2023-10-07 05:13:26 +00:00
#[derive(Debug)]
2023-10-11 23:41:32 +00:00
enum TestConfig {
TestLayer(TestLayer),
SingleFile(SingleFile),
2023-10-07 05:13:26 +00:00
}
#[derive(Debug)]
2023-10-11 23:41:32 +00:00
struct TestLayer {
name: String,
children: Vec<TestConfig>,
2023-10-07 05:13:26 +00:00
}
#[derive(Debug)]
2023-10-11 23:41:32 +00:00
struct SingleFile {
name: String,
2023-10-11 23:41:32 +00:00
file_path: PathBuf,
2023-10-08 11:54:21 +00:00
}
#[derive(Debug)]
2023-10-11 23:41:32 +00:00
enum TestResult {
ResultLayer(ResultLayer),
SingleFileResult(SingleFileResult),
2023-10-08 11:54:21 +00:00
}
#[derive(Debug)]
2023-10-11 23:41:32 +00:00
struct ResultLayer {
name: String,
children: Vec<TestResult>,
2023-10-08 11:54:21 +00:00
}
#[derive(Debug)]
2023-10-11 23:41:32 +00:00
struct SingleFileResult {
name: String,
2023-10-11 23:41:32 +00:00
file_path: PathBuf,
2023-10-08 21:17:32 +00:00
status: TestStatus,
2023-10-07 05:13:26 +00:00
}
#[derive(Debug)]
2023-10-08 21:17:32 +00:00
pub(crate) enum TestStatus {
2023-10-13 16:14:46 +00:00
Pass,
Fail,
2023-10-08 21:17:32 +00:00
}
2023-10-11 23:41:32 +00:00
impl TestConfig {
2023-10-13 16:07:52 +00:00
fn run_test(self) -> BoxFuture<'static, Result<TestResult, JoinError>> {
2023-10-11 23:47:30 +00:00
async move {
match self {
2023-10-13 16:07:52 +00:00
TestConfig::TestLayer(test) => Ok(TestResult::ResultLayer(test.run_test().await?)),
TestConfig::SingleFile(test) => {
Ok(TestResult::SingleFileResult(test.run_test().await?))
}
2023-10-11 23:47:30 +00:00
}
2023-10-08 21:17:32 +00:00
}
2023-10-11 23:47:30 +00:00
.boxed()
2023-10-08 21:17:32 +00:00
}
2023-10-07 05:13:26 +00:00
}
2023-10-08 11:54:21 +00:00
2023-10-11 23:41:32 +00:00
impl SingleFile {
2023-10-13 16:07:52 +00:00
async fn run_test(self) -> Result<SingleFileResult, JoinError> {
2023-10-13 15:41:49 +00:00
let _permit = TEST_PERMITS.acquire().await.unwrap();
2023-10-11 23:41:32 +00:00
let result = run_compare_on_file(&self.file_path);
2023-10-13 16:07:52 +00:00
Ok(SingleFileResult {
name: self.name,
2023-10-08 21:17:32 +00:00
file_path: self.file_path,
status: if result.is_ok() {
2023-10-13 16:14:46 +00:00
TestStatus::Pass
2023-10-08 21:17:32 +00:00
} else {
2023-10-13 16:14:46 +00:00
TestStatus::Fail
2023-10-08 21:17:32 +00:00
},
2023-10-13 16:07:52 +00:00
})
2023-10-08 21:17:32 +00:00
}
}
2023-10-11 23:41:32 +00:00
impl TestLayer {
2023-10-13 16:07:52 +00:00
async fn run_test(self) -> Result<ResultLayer, JoinError> {
let running_children: Vec<_> = self
.children
.into_iter()
.map(|c| tokio::spawn(c.run_test()))
.collect();
let mut children = Vec::with_capacity(running_children.len());
for c in running_children {
children.push(c.await??);
2023-10-11 23:41:32 +00:00
}
2023-10-13 16:07:52 +00:00
Ok(ResultLayer {
2023-10-11 23:41:32 +00:00
name: self.name,
children,
2023-10-13 16:07:52 +00:00
})
2023-10-08 11:54:21 +00:00
}
}