#![allow(dead_code)]
use std::{
process::{Child, ExitStatus},
sync::Once,
};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
pub(super) struct ExampleRunner {
server_process: Child,
}
static INIT_TRACING_ONCE: Once = Once::new();
pub(super) fn init_tracing() {
INIT_TRACING_ONCE.call_once(|| {
let _ = tracing_subscriber::registry()
.with(fmt::layer())
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::TRACE.into())
.from_env_lossy(),
)
.try_init();
});
}
impl ExampleRunner {
pub(super) fn run_background(
example_name: impl AsRef<str>,
extra_features: Option<&'static str>,
) -> Self {
let child = escargot::CargoBuild::new()
.arg("-p")
.arg("rama-http-core")
.arg(format!("--features={}", extra_features.unwrap_or_default()))
.example(example_name.as_ref())
.manifest_path("Cargo.toml")
.target_dir("./target/")
.run()
.unwrap()
.command()
.env(
"RUST_LOG",
std::env::var("RUST_LOG").unwrap_or("info".into()),
)
.env("SSLKEYLOGFILE", "./target/test_ssl_key_log.txt")
.spawn()
.unwrap();
Self {
server_process: child,
}
}
}
impl ExampleRunner {
pub(super) fn run(example_name: impl AsRef<str>) -> ExitStatus {
let example_name = example_name.as_ref().to_owned();
escargot::CargoBuild::new()
.arg("-p")
.arg("rama-http-core")
.arg("--all-features")
.example(example_name)
.manifest_path("Cargo.toml")
.target_dir("./target/")
.run()
.unwrap()
.command()
.env(
"RUST_LOG",
std::env::var("RUST_LOG").unwrap_or("info".into()),
)
.status()
.unwrap()
}
}
impl std::ops::Drop for ExampleRunner {
fn drop(&mut self) {
self.server_process.kill().expect("kill server process");
}
}