Browse Source

Make public Setup interface async

master
Adrian Heine 4 years ago
parent
commit
d173198729
  1. 6
      src/setup/core.rs
  2. 40
      src/setup/setup.rs
  3. 61
      tests/setup.rs

6
src/setup/core.rs

@ -36,7 +36,7 @@ macro_rules! add_generic {
#[allow(unused)] #[allow(unused)]
async fn add_generic(&self, ($($name,)*): ($($name,)*)) -> Result<(($($name::Artifact,)*), bool), Box<dyn Error>> async fn add_generic(&self, ($($name,)*): ($($name,)*)) -> Result<(($($name::Artifact,)*), bool), Box<dyn Error>>
{ {
let x: Result<_, Box<dyn Error>> = try_join!($(self.add_async($name, false),)*);
let x: Result<_, Box<dyn Error>> = try_join!($(self.add_force($name, false),)*);
let ($($name,)*) = x?; let ($($name,)*) = x?;
Ok((($($name.0,)*), false $(|| $name.1)*)) Ok((($($name.0,)*), false $(|| $name.1)*))
} }
@ -66,7 +66,7 @@ where
async fn add_generic(&self, r: Option<T>) -> AddResult<Option<T>> { async fn add_generic(&self, r: Option<T>) -> AddResult<Option<T>> {
Ok(match r { Ok(match r {
Some(r) => { Some(r) => {
let (result, did_run) = self.add_async(r, false).await?;
let (result, did_run) = self.add_force(r, false).await?;
(Some(result), did_run) (Some(result), did_run)
} }
None => (None, false), None => (None, false),
@ -89,7 +89,7 @@ where
RegularSetupCore<SR, L, B>: 'static + SetupCore<T, Self>, RegularSetupCore<SR, L, B>: 'static + SetupCore<T, Self>,
{ {
async fn add_generic(&self, r: T) -> AddResult<T> { async fn add_generic(&self, r: T) -> AddResult<T> {
self.add_async(r, false).await
self.add_force(r, false).await
} }
} }

40
src/setup/setup.rs

@ -2,7 +2,6 @@ use super::core::{RegularSetupCore, SetupCore};
use super::runnable::Runnable; use super::runnable::Runnable;
use super::util::{AddResult, AddableResource}; use super::util::{AddResult, AddableResource};
use super::SymbolRunner; use super::SymbolRunner;
use crate::async_utils::run;
use crate::loggers::Logger; use crate::loggers::Logger;
use crate::resources::{DefaultArtifacts, DefaultResources, FromArtifact, FromResource}; use crate::resources::{DefaultArtifacts, DefaultResources, FromArtifact, FromResource};
use crate::{DefaultBuilder, DefaultLocator}; use crate::{DefaultBuilder, DefaultLocator};
@ -65,7 +64,7 @@ impl<
self.0.resources.borrow_mut() self.0.resources.borrow_mut()
} }
pub(super) async fn add_async<R: AddableResource>(
pub async fn add_force<R: AddableResource>(
&self, &self,
resource: R, resource: R,
force_run: bool, force_run: bool,
@ -117,37 +116,28 @@ impl<
// //
// Legacy // Legacy
// //
pub fn add<R: AddableResource>(&self, resource: R) -> AddResult<R>
pub async fn add<R: AddableResource>(&self, resource: R) -> AddResult<R>
where where
RegularSetupCore<SR, L, B>: SetupCore<R, Self>, RegularSetupCore<SR, L, B>: SetupCore<R, Self>,
Rs: FromResource<R>, Rs: FromResource<R>,
As: FromArtifact<R> + Clone, As: FromArtifact<R> + Clone,
R::Artifact: Clone, R::Artifact: Clone,
{ {
run(self.add_async(resource, false))
self.add_force(resource, false).await
} }
pub fn add_force<R: AddableResource>(&self, resource: R, force_run: bool) -> AddResult<R>
where
RegularSetupCore<SR, L, B>: SetupCore<R, Self>,
Rs: FromResource<R>,
As: FromArtifact<R> + Clone,
R::Artifact: Clone,
{
run(self.add_async(resource, force_run))
}
pub fn run_symbol<S: Runnable>(&self, symbol: S, force: bool) -> Result<bool, Box<dyn Error>>
pub async fn run_symbol<S: Runnable>(&self, symbol: S, force: bool) -> Result<bool, Box<dyn Error>>
where where
RegularSetupCore<SR, L, B>: SymbolRunner, RegularSetupCore<SR, L, B>: SymbolRunner,
{ {
run(symbol.run(&self.0.core, &self.0.logger, force))
symbol.run(&self.0.core, &self.0.logger, force).await
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::SymbolRunner; use super::SymbolRunner;
use crate::async_utils::run;
use crate::loggers::{Logger, StoringLogger}; use crate::loggers::{Logger, StoringLogger};
use crate::resources::{FromArtifact, FromResource, Resource}; use crate::resources::{FromArtifact, FromResource, Resource};
use crate::symbols::Symbol; use crate::symbols::Symbol;
@ -287,14 +277,16 @@ mod test {
#[test] #[test]
fn correctly_uses_force() { fn correctly_uses_force() {
let (count, setup) = get_setup();
setup.add(TestResource("A", "b")).unwrap();
assert_eq!(*count.borrow(), 2);
setup.add(TestResource("A", "b")).unwrap();
assert_eq!(*count.borrow(), 2);
run(async {
let (count, setup) = get_setup();
setup.add(TestResource("A", "b")).await.unwrap();
assert_eq!(*count.borrow(), 2);
setup.add(TestResource("A", "b")).await.unwrap();
assert_eq!(*count.borrow(), 2);
let (count, setup) = get_setup();
setup.add(TestResource("A", "B")).unwrap();
assert_eq!(*count.borrow(), 0);
let (count, setup) = get_setup();
setup.add(TestResource("A", "B")).await.unwrap();
assert_eq!(*count.borrow(), 0);
});
} }
} }

61
tests/setup.rs

@ -1,5 +1,5 @@
use async_trait::async_trait; use async_trait::async_trait;
use schematics::async_utils::sleep;
use schematics::async_utils::{run, sleep};
use schematics::loggers::{Logger, StoringLogger}; use schematics::loggers::{Logger, StoringLogger};
use schematics::resources::{AcmeUser, Cert, Csr, GitCheckout}; use schematics::resources::{AcmeUser, Cert, Csr, GitCheckout};
use schematics::symbols::Symbol; use schematics::symbols::Symbol;
@ -37,31 +37,33 @@ fn can_create_an_acme_user() {
count: Rc::clone(&count), count: Rc::clone(&count),
}; };
let setup = Setup::new(runner, StoringLogger::new()); let setup = Setup::new(runner, StoringLogger::new());
assert_eq!((setup.add(AcmeUser).unwrap().0).0, "acme");
assert_eq!((run(setup.add(AcmeUser)).unwrap().0).0, "acme");
} }
#[test] #[test]
fn runs_only_once() { fn runs_only_once() {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
let setup = Setup::new(runner, StoringLogger::new());
assert_eq!(
(setup.add(Csr("somehost")).unwrap().0)
.as_ref()
.to_str()
.unwrap(),
"/etc/ssl/local_certs/somehost.csr",
);
assert_eq!(
(setup.add(Csr("somehost")).unwrap().0)
.as_ref()
.to_str()
.unwrap(),
"/etc/ssl/local_certs/somehost.csr",
);
assert_eq!(*count.borrow(), 2 + 5); // Key and CSR + 5 dirs
run(async {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
let setup = Setup::new(runner, StoringLogger::new());
assert_eq!(
(setup.add(Csr("somehost")).await.unwrap().0)
.as_ref()
.to_str()
.unwrap(),
"/etc/ssl/local_certs/somehost.csr",
);
assert_eq!(
(setup.add(Csr("somehost")).await.unwrap().0)
.as_ref()
.to_str()
.unwrap(),
"/etc/ssl/local_certs/somehost.csr",
);
assert_eq!(*count.borrow(), 2 + 5); // Key and CSR + 5 dirs
});
} }
#[test] #[test]
@ -72,7 +74,7 @@ fn can_create_an_acme_cert() {
}; };
let setup = Setup::new(runner, StoringLogger::new()); let setup = Setup::new(runner, StoringLogger::new());
assert_eq!( assert_eq!(
(setup.add(Cert("somehost")).unwrap().0)
(run(setup.add(Cert("somehost"))).unwrap().0)
.as_ref() .as_ref()
.to_str() .to_str()
.unwrap(), .unwrap(),
@ -88,12 +90,11 @@ fn can_create_a_git_checkout() {
count: Rc::clone(&count), count: Rc::clone(&count),
}; };
let setup = Setup::new(runner, StoringLogger::new()); let setup = Setup::new(runner, StoringLogger::new());
setup
.add(GitCheckout(
"/tmp/somepath".into(),
"/tmp/some_src_repo",
"master",
))
.unwrap();
run(setup.add(GitCheckout(
"/tmp/somepath".into(),
"/tmp/some_src_repo",
"master",
)))
.unwrap();
assert_eq!(*count.borrow(), 3); assert_eq!(*count.borrow(), 3);
} }
Loading…
Cancel
Save