Browse Source

Fix tests, remove unused IfAlreadyPresent

master
Adrian Heine 7 years ago
parent
commit
823db2e17e
  1. 20
      src/schema.rs
  2. 14
      src/symbols/hook.rs
  3. 144
      src/symbols/if_already_present.rs
  4. 9
      src/symbols/mod.rs
  5. 2
      src/symbols/user.rs

20
src/schema.rs

@ -180,7 +180,7 @@ mod test {
use std::fmt; use std::fmt;
use loggers::Logger; use loggers::Logger;
use symbols::Symbol;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction};
use schema::SymbolRunner; use schema::SymbolRunner;
use schema::InitializingSymbolRunner; use schema::InitializingSymbolRunner;
@ -206,13 +206,21 @@ mod test {
_target_reached: &'a Fn() -> Result<bool, Box<Error>> _target_reached: &'a Fn() -> Result<bool, Box<Error>>
} }
impl<'a> Symbol for DummySymbol<'a> {
impl<'b> Symbol for DummySymbol<'b> {
fn target_reached(&self) -> Result<bool, Box<Error>> { fn target_reached(&self) -> Result<bool, Box<Error>> {
(self._target_reached)() (self._target_reached)()
} }
fn execute(&self) -> Result<(), Box<Error>> { fn execute(&self) -> Result<(), Box<Error>> {
(self._execute)() (self._execute)()
} }
fn as_action<'a>(&'a self, runner: &'a SymbolRunner) -> Box<Action + 'a> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'a>(self: Box<Self>, runner: &'a SymbolRunner) -> Box<Action + 'a> where Self: 'a {
Box::new(OwnedSymbolAction::new(runner, *self))
}
} }
impl<'a> fmt::Display for DummySymbol<'a> { impl<'a> fmt::Display for DummySymbol<'a> {
@ -248,14 +256,14 @@ mod test {
#[test] #[test]
fn nothing_needed_to_be_done() { fn nothing_needed_to_be_done() {
let result = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(&|| Ok(true), &|| Ok(())) );
let result = InitializingSymbolRunner::new(DummyLogger::new()).run_symbol(&DummySymbol::new(&|| Ok(true), &|| Ok(())));
assert!(result.is_ok()); assert!(result.is_ok());
} }
#[test] #[test]
fn everything_is_ok() { fn everything_is_ok() {
let first = RefCell::new(true); let first = RefCell::new(true);
let result = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(&|| {
let result = InitializingSymbolRunner::new(DummyLogger::new()).run_symbol(&DummySymbol::new(&|| {
let mut _first = first.borrow_mut(); let mut _first = first.borrow_mut();
Ok(if *_first { Ok(if *_first {
*_first = false; *_first = false;
@ -269,7 +277,7 @@ mod test {
#[test] #[test]
fn executing_did_not_change_state() { fn executing_did_not_change_state() {
let result = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(&|| Ok(false), &|| Ok(())));
let result = InitializingSymbolRunner::new(DummyLogger::new()).run_symbol(&DummySymbol::new(&|| Ok(false), &|| Ok(())));
assert_eq!( assert_eq!(
result.unwrap_err().description(), result.unwrap_err().description(),
"Target not reached after executing symbol" "Target not reached after executing symbol"
@ -278,7 +286,7 @@ mod test {
#[test] #[test]
fn executing_did_not_work() { fn executing_did_not_work() {
let err = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(&|| Ok(false), &|| Err(Box::new(DummySymbolError::Error(()))) )).unwrap_err();
let err = InitializingSymbolRunner::new(DummyLogger::new()).run_symbol(&DummySymbol::new(&|| Ok(false), &|| Err(Box::new(DummySymbolError::Error(()))))).unwrap_err();
assert_eq!(err.description(), "Description"); assert_eq!(err.description(), "Description");
} }
} }

14
src/symbols/hook.rs

@ -70,6 +70,13 @@ mod test {
impl Symbol for ErrSymbol { impl Symbol for ErrSymbol {
fn target_reached(&self) -> Result<bool, Box<Error>> { Err(self.0.clone().into()) } fn target_reached(&self) -> Result<bool, Box<Error>> { Err(self.0.clone().into()) }
fn execute(&self) -> Result<(), Box<Error>> { Err(self.0.clone().into()) } fn execute(&self) -> Result<(), Box<Error>> { Err(self.0.clone().into()) }
fn as_action<'a>(&'a self, runner: &'a SymbolRunner) -> Box<Action + 'a> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'a>(self: Box<Self>, runner: &'a SymbolRunner) -> Box<Action + 'a> where Self: 'a {
Box::new(OwnedSymbolAction::new(runner, *self))
}
} }
impl fmt::Display for ErrSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } } impl fmt::Display for ErrSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
@ -77,6 +84,13 @@ mod test {
impl Symbol for OkSymbol { impl Symbol for OkSymbol {
fn target_reached(&self) -> Result<bool, Box<Error>> { Ok(self.0) } fn target_reached(&self) -> Result<bool, Box<Error>> { Ok(self.0) }
fn execute(&self) -> Result<(), Box<Error>> { Ok(()) } fn execute(&self) -> Result<(), Box<Error>> { Ok(()) }
fn as_action<'a>(&'a self, runner: &'a SymbolRunner) -> Box<Action + 'a> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'a>(self: Box<Self>, runner: &'a SymbolRunner) -> Box<Action + 'a> where Self: 'a {
Box::new(OwnedSymbolAction::new(runner, *self))
}
} }
impl fmt::Display for OkSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } } impl fmt::Display for OkSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }

144
src/symbols/if_already_present.rs

@ -1,144 +0,0 @@
use std::error::Error;
use std::fmt;
use resources::Resource;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
pub struct IfAlreadyPresent<A, B> where A: Symbol, B: Symbol {
a: A,
b: B
}
impl<A, B> IfAlreadyPresent<A, B> where A: Symbol, B: Symbol {
pub fn new(a: A, b: B) -> Self {
IfAlreadyPresent { a: a, b: b }
}
}
impl<A, B> Symbol for IfAlreadyPresent<A, B> where A: Symbol, B: Symbol {
fn target_reached(&self) -> Result<bool, Box<Error>> {
self.a.target_reached().and_then(|reached| if reached { self.b.target_reached() } else { Ok(reached) })
}
fn execute(&self) -> Result<(), Box<Error>> {
// Execute a & b if a is not reached
// Execute b if a was reached and b isn't
if !try!(self.a.target_reached()) {
try!(self.a.execute());
self.b.execute()
} else if !try!(self.b.target_reached()) {
self.b.execute()
} else {
Ok(())
}
}
fn get_prerequisites(&self) -> Vec<Resource> {
let mut r = vec![];
r.extend(self.a.get_prerequisites().into_iter());
r.extend(self.b.get_prerequisites().into_iter());
r
}
fn provides(&self) -> Option<Vec<Resource>> {
let mut r = vec![];
if let Some(provides) = self.a.provides() {
r.extend(provides.into_iter());
}
if let Some(provides) = self.b.provides() {
r.extend(provides.into_iter());
}
if r.len() > 0 { Some(r) } else { None }
}
fn as_action<'a>(&'a self, runner: &'a SymbolRunner) -> Box<Action + 'a> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'a>(self: Box<Self>, runner: &'a SymbolRunner) -> Box<Action + 'a> where Self: 'a {
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
impl<A, B> fmt::Display for IfAlreadyPresent<A, B> where A: Symbol, B: Symbol {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
write!(f, "IfAlreadyPresent {} and then {}", self.a, self.b)
}
}
#[cfg(test)]
mod test {
use std::error::Error;
use std::fmt;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
use symbols::if_already_present::IfAlreadyPresent;
struct ErrSymbol(String);
impl Symbol for ErrSymbol {
fn target_reached(&self) -> Result<bool, Box<Error>> { Err(self.0.clone().into()) }
fn execute(&self) -> Result<(), Box<Error>> { Err(self.0.clone().into()) }
}
impl fmt::Display for ErrSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
struct OkSymbol(bool);
impl Symbol for OkSymbol {
fn target_reached(&self) -> Result<bool, Box<Error>> { Ok(self.0) }
fn execute(&self) -> Result<(), Box<Error>> { Ok(()) }
}
impl fmt::Display for OkSymbol { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "") } }
#[test]
fn first_target_reached_fails() {
let res = IfAlreadyPresent::new(ErrSymbol("first".into()), ErrSymbol("second".into())).target_reached();
assert_eq!(res.unwrap_err().description(), "first");
}
#[test]
fn first_target_not_reached() {
let res = IfAlreadyPresent::new(OkSymbol(false), ErrSymbol("second".into())).target_reached();
assert_eq!(res.unwrap(), false);
}
#[test]
fn second_target_reached_fails() {
let res = IfAlreadyPresent::new(OkSymbol(true), ErrSymbol("second".into())).target_reached();
assert_eq!(res.unwrap_err().description(), "second");
}
#[test]
fn second_target_not_reached() {
let res = IfAlreadyPresent::new(OkSymbol(true), OkSymbol(false)).target_reached();
assert_eq!(res.unwrap(), false);
}
#[test]
fn everything_reached() {
let res = IfAlreadyPresent::new(OkSymbol(true), OkSymbol(true)).target_reached();
assert_eq!(res.unwrap(), true);
}
#[test]
fn first_execute_fails() {
let res = IfAlreadyPresent::new(ErrSymbol("first".into()), ErrSymbol("second".into())).execute();
assert_eq!(res.unwrap_err().description(), "first");
}
#[test]
fn second_execute_fails_but_isnt_run() {
let res = IfAlreadyPresent::new(OkSymbol(false), ErrSymbol("second".into())).execute();
assert_eq!(res.unwrap(), ());
}
#[test]
fn second_execute_fails() {
let res = IfAlreadyPresent::new(OkSymbol(true), ErrSymbol("second".into())).execute();
assert_eq!(res.unwrap_err().description(), "second");
}
#[test]
fn everything_executes() {
let res = IfAlreadyPresent::new(OkSymbol(true), OkSymbol(true)).execute();
assert_eq!(res.unwrap(), ());
}
}

9
src/symbols/mod.rs

@ -31,13 +31,13 @@ pub trait Symbol: Display {
} }
// SymbolAction // SymbolAction
struct SymbolAction<'a, S: Symbol + 'a> {
pub struct SymbolAction<'a, S: Symbol + 'a> {
runner: &'a SymbolRunner, runner: &'a SymbolRunner,
symbol: &'a S symbol: &'a S
} }
impl<'a, S: Symbol> SymbolAction<'a, S> { impl<'a, S: Symbol> SymbolAction<'a, S> {
fn new(runner: &'a SymbolRunner, symbol: &'a S) -> Self {
pub fn new(runner: &'a SymbolRunner, symbol: &'a S) -> Self {
Self { runner: runner, symbol: symbol } Self { runner: runner, symbol: symbol }
} }
} }
@ -48,13 +48,13 @@ impl<'a, S: Symbol> Action for SymbolAction<'a, S> {
} }
} }
struct OwnedSymbolAction<'a, S: Symbol + 'a> {
pub struct OwnedSymbolAction<'a, S: Symbol + 'a> {
runner: &'a SymbolRunner, runner: &'a SymbolRunner,
symbol: S symbol: S
} }
impl<'a, S: Symbol + 'a> OwnedSymbolAction<'a, S> { impl<'a, S: Symbol + 'a> OwnedSymbolAction<'a, S> {
fn new(runner: &'a SymbolRunner, symbol: S) -> Self {
pub fn new(runner: &'a SymbolRunner, symbol: S) -> Self {
Self { runner: runner, symbol: symbol } Self { runner: runner, symbol: symbol }
} }
} }
@ -71,7 +71,6 @@ pub mod factory;
pub mod file; pub mod file;
pub mod git; pub mod git;
pub mod hook; pub mod hook;
pub mod if_already_present;
pub mod list; pub mod list;
pub mod mariadb; pub mod mariadb;
pub mod nginx; pub mod nginx;

2
src/symbols/user.rs

@ -165,7 +165,7 @@ mod test {
use std::fmt; use std::fmt;
use command_runner::StdCommandRunner; use command_runner::StdCommandRunner;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
use symbols::Symbol;
use symbols::user::User; use symbols::user::User;
use symbols::user::UserAdder; use symbols::user::UserAdder;
use symbols::user::UserAdderError; use symbols::user::UserAdderError;

Loading…
Cancel
Save