Browse Source

Update File Symbol, add tests

master
Adrian Heine 8 years ago
parent
commit
55f2cfae95
  1. 3
      Cargo.toml
  2. 64
      src/symbols/file.rs
  3. 11
      src/symbols/nginx/server.rs
  4. 103
      tests/file.rs

3
Cargo.toml

@ -2,3 +2,6 @@
name = "schematics" name = "schematics"
version = "0.1.0" version = "0.1.0"
authors = ["Adrian Heine <mail@adrianheine.de>"] authors = ["Adrian Heine <mail@adrianheine.de>"]
[dev-dependencies]
tempdir = "0.3"

64
src/symbols/file.rs

@ -1,45 +1,13 @@
use std::error::Error;
use std::borrow::Cow;
use std::fmt; use std::fmt;
use std::fs::File as FsFile;
use std::io; use std::io;
use std::io::{Read, Write};
use std::ops::Deref; use std::ops::Deref;
use std::path::Path;
use symbols::Symbol; use symbols::Symbol;
#[derive(Debug)]
pub enum FileError<E: Error> {
ExecError(E),
GenericError
}
impl From<io::Error> for FileError<io::Error> {
fn from(err: io::Error) -> FileError<io::Error> {
FileError::ExecError(err)
}
}
impl<E: Error> Error for FileError<E> {
fn description(&self) -> &str {
match self {
&FileError::ExecError(ref e) => e.description(),
&FileError::GenericError => "Generic error"
}
}
fn cause(&self) -> Option<&Error> {
match self {
&FileError::ExecError(ref e) => Some(e),
_ => None
}
}
}
impl<E: Error> fmt::Display for FileError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.description())
}
}
use std::convert::AsRef;
pub struct File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display { pub struct File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
path: D, path: D,
content: C content: C
@ -54,11 +22,9 @@ impl<C, D> File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
} }
} }
use std::fs::File as FsFile;
use std::io::{Read, Write};
impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display { impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
type Error = FileError<io::Error>;
type Error = io::Error;
fn target_reached(&self) -> Result<bool, Self::Error> { fn target_reached(&self) -> Result<bool, Self::Error> {
let file = FsFile::open(self.path.as_ref()); let file = FsFile::open(self.path.as_ref());
// Check if file exists // Check if file exists
@ -66,16 +32,20 @@ impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt
return if e.kind() == io::ErrorKind::NotFound { return if e.kind() == io::ErrorKind::NotFound {
Ok(false) Ok(false)
} else { } else {
Err(e.into())
Err(e)
}; };
} }
// Check if content is the same // Check if content is the same
let file_content = file.unwrap().bytes();
let content_equal = try!(self.content.bytes().zip(file_content).fold(
Ok(true),
|state, (target_byte, file_byte_option)| state.and_then(|s| file_byte_option.map(|file_byte| s && file_byte == target_byte))
));
return Ok(content_equal)
let mut file_content = file.unwrap().bytes();
let mut target_content = self.content.bytes();
loop {
match (file_content.next(), target_content.next()) {
(None, None) => return Ok(true),
(Some(Ok(a)), Some(b)) if a == b => {},
(Some(Err(e)), _) => return Err(e),
(_, _) => return Ok(false)
}
}
} }
fn execute(&self) -> Result<(), Self::Error> { fn execute(&self) -> Result<(), Self::Error> {

11
src/symbols/nginx/server.rs

@ -6,7 +6,6 @@ use std::ops::Deref;
use command_runner::CommandRunner; use command_runner::CommandRunner;
use symbols::Symbol; use symbols::Symbol;
use symbols::file::File as FileSymbol; use symbols::file::File as FileSymbol;
use symbols::file::FileError;
#[derive(Debug)] #[derive(Debug)]
pub enum NginxServerError<E: Error> { pub enum NginxServerError<E: Error> {
@ -14,18 +13,12 @@ pub enum NginxServerError<E: Error> {
GenericError GenericError
} }
impl<E: Error> From<FileError<E>> for NginxServerError<FileError<E>> {
fn from(err: FileError<E>) -> NginxServerError<FileError<E>> {
impl From<io::Error> for NginxServerError<io::Error> {
fn from(err: io::Error) -> NginxServerError<io::Error> {
NginxServerError::ExecError(err) NginxServerError::ExecError(err)
} }
} }
impl<E: Error> From<io::Error> for NginxServerError<FileError<E>> {
fn from(err: io::Error) -> NginxServerError<FileError<E>> {
NginxServerError::GenericError
}
}
impl<E: Error> Error for NginxServerError<E> { impl<E: Error> Error for NginxServerError<E> {
fn description(&self) -> &str { fn description(&self) -> &str {
match self { match self {

103
tests/file.rs

@ -0,0 +1,103 @@
extern crate schematics;
extern crate tempdir;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use tempdir::TempDir;
use schematics::symbols::Symbol;
use schematics::symbols::file::File as FileSymbol;
fn get_dir(content: Option<&str>) -> TempDir {
let tmp_dir = TempDir::new("unittest").expect("create temp dir");
if content.is_none() {
return tmp_dir;
}
let file_path = tmp_dir.path().join("filename");
let mut tmp_file = File::create(file_path).expect("create temp file");
tmp_file.write_all(content.unwrap().as_bytes()).expect("write temp file");
tmp_dir
}
fn get_symbol(path: &Path) -> FileSymbol<&str, String> {
FileSymbol::new(String::from(path.join("filename").to_str().unwrap()), "target content")
}
// Normal cases
// Good
#[test]
fn already_reached() {
let dir = get_dir(Some("target content"));
let symbol = get_symbol(dir.path());
assert_eq!(symbol.target_reached().unwrap(), true);
}
// Bad
#[test]
fn wrong_prefix() {
let dir = get_dir(Some("not target content"));
let symbol = get_symbol(dir.path());
assert_eq!(symbol.target_reached().unwrap(), false);
symbol.execute().unwrap();
assert_eq!(symbol.target_reached().unwrap(), true);
}
#[test]
fn wrong_postfix() {
let dir = get_dir(Some("target content not"));
let symbol = get_symbol(dir.path());
assert_eq!(symbol.target_reached().unwrap(), false);
symbol.execute().unwrap();
assert_eq!(symbol.target_reached().unwrap(), true);
}
#[test]
fn empty_file() {
let dir = get_dir(Some(""));
let symbol = get_symbol(dir.path());
assert_eq!(symbol.target_reached().unwrap(), false);
symbol.execute().unwrap();
assert_eq!(symbol.target_reached().unwrap(), true);
}
#[test]
fn no_file() {
let dir = get_dir(None);
let symbol = get_symbol(dir.path());
assert_eq!(symbol.target_reached().unwrap(), false);
symbol.execute().unwrap();
assert_eq!(symbol.target_reached().unwrap(), true);
}
// Exceptional cases
#[test]
fn may_not_read_file() {
let symbol = get_symbol(&Path::new("/etc/passwd"));
assert_eq!(symbol.target_reached().is_err(), true);
}
#[test]
fn may_not_create_file() {
let symbol = get_symbol(&Path::new("/proc/somefile"));
// Could also return an error
assert_eq!(symbol.target_reached().unwrap(), false);
assert!(symbol.execute().is_err());
}
#[test]
fn directory_missing() {
let symbol = get_symbol(&Path::new("/nonexisting"));
// Could also return an error
assert_eq!(symbol.target_reached().unwrap(), false);
assert!(symbol.execute().is_err());
}
Loading…
Cancel
Save