A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
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());
}
|