use super::{AsyncTestLifecycleHook, TestEnv, TestEnvConfig, TestLifecycleHook, TestServiceConfig};
pub struct TestEnvBuilder {
config: TestEnvConfig,
lifecycle_hooks: Vec<Box<dyn TestLifecycleHook>>,
async_lifecycle_hooks: Vec<Box<dyn AsyncTestLifecycleHook>>,
services: Vec<TestServiceConfig>,
}
impl TestEnvBuilder {
pub fn new() -> Self {
Self {
config: TestEnvConfig::default(),
lifecycle_hooks: Vec::new(),
async_lifecycle_hooks: Vec::new(),
services: Vec::new(),
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.config.name = name.into();
self
}
pub fn with_logging(mut self, enable: bool) -> Self {
self.config.enable_logging = enable;
self
}
pub fn with_tracing(mut self, enable: bool) -> Self {
self.config.enable_tracing = enable;
self
}
pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
self.config.default_timeout = timeout;
self
}
pub fn custom(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.config.custom.insert(key.into(), value.into());
self
}
pub fn with_lifecycle_hook<H>(mut self, hook: H) -> Self
where
H: TestLifecycleHook + 'static,
{
self.lifecycle_hooks.push(Box::new(hook));
self
}
pub fn with_async_lifecycle_hook<H>(mut self, hook: H) -> Self
where
H: AsyncTestLifecycleHook + 'static,
{
self.async_lifecycle_hooks.push(Box::new(hook));
self
}
pub fn with_service(mut self, service: TestServiceConfig) -> Self {
self.services.push(service);
self
}
pub fn with_services<I>(mut self, services: I) -> Self
where
I: IntoIterator<Item = TestServiceConfig>,
{
self.services.extend(services);
self
}
pub fn build(self) -> TestEnv {
let env = TestEnv::new(self.config);
for hook in self.lifecycle_hooks {
env.lifecycle_hooks.write().push(hook);
}
for hook in self.async_lifecycle_hooks {
env.async_lifecycle_hooks.write().push(hook);
}
for service in self.services {
env.add_service(service);
}
env
}
}
impl Default for TestEnvBuilder {
fn default() -> Self {
Self::new()
}
}