organic/src/bin_foreign_document_test.rs

133 lines
3.2 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;
use organic::compare::run_anonymous_compare;
use organic::compare::run_compare_on_file;
#[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-11 23:41:32 +00:00
let single_file = TestConfig::SingleFile(SingleFile {
file_path: PathBuf::from("/tmp/test.org"),
2023-10-08 21:17:32 +00:00
});
2023-10-11 23:41:32 +00:00
// let result = single_file.run_test().await;
let result = tokio::spawn(single_file.run_test());
println!("{:?}", result);
// let test_config = TestConfig::TestLayer(TestLayer {
// name: "foo",
// children: vec![TestConfig::SingleFile(SingleFile {
// file_path: Path::new("/tmp/test.org"),
// })],
// });
2023-10-08 21:17:32 +00:00
Ok(())
}
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 {
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 {
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 {
Good,
Bad,
}
2023-10-11 23:41:32 +00:00
impl TestConfig {
async fn run_test(self) -> TestResult {
2023-10-08 21:17:32 +00:00
match self {
TestConfig::TestLayer(test) => TestResult::ResultLayer(test.run_test().await),
TestConfig::SingleFile(test) => TestResult::SingleFileResult(test.run_test().await),
}
}
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 {
async fn run_test(self) -> SingleFileResult {
let result = run_compare_on_file(&self.file_path);
2023-10-08 21:17:32 +00:00
SingleFileResult {
file_path: self.file_path,
status: if result.is_ok() {
TestStatus::Good
} else {
TestStatus::Bad
},
}
}
}
2023-10-11 23:41:32 +00:00
impl TestLayer {
async fn run_test(self) -> ResultLayer {
let mut children = Vec::with_capacity(self.children.len());
for config in self.children {
let result = config.run_test().await;
children.push(result);
}
ResultLayer {
name: self.name,
children,
}
2023-10-08 11:54:21 +00:00
}
}