test helpers
parent
b5b794817c
commit
bcbcd6fbb7
|
|
@ -104,18 +104,19 @@ mod test_flags {
|
|||
|
||||
#[test]
|
||||
fn test_flags_files_unhidden_only() {
|
||||
let d = tempdir::TempDir::new("").unwrap();
|
||||
std::fs::File::create(d.path().join("plain")).unwrap();
|
||||
std::fs::File::create(d.path().join(".hidden")).unwrap();
|
||||
tests::with_dir(|d| {
|
||||
std::fs::File::create(d.path().join("plain")).unwrap();
|
||||
std::fs::File::create(d.path().join(".hidden")).unwrap();
|
||||
|
||||
let flags = Flags {
|
||||
path: d.path().to_str().unwrap().to_string(),
|
||||
add: None,
|
||||
edit: false,
|
||||
dry_run: true,
|
||||
};
|
||||
let files = flags.files().expect("failed to files from dir");
|
||||
assert_eq!(1, files.files.len());
|
||||
let flags = Flags {
|
||||
path: d.path().to_str().unwrap().to_string(),
|
||||
add: None,
|
||||
edit: false,
|
||||
dry_run: true,
|
||||
};
|
||||
let files = flags.files().expect("failed to files from dir");
|
||||
assert_eq!(1, files.files.len());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -233,12 +234,28 @@ mod test_file {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_file() {
|
||||
let d = tempdir::TempDir::new("").unwrap();
|
||||
let mut f = std::fs::File::create(d.path().join("plain")).unwrap();
|
||||
writeln!(f, "hello world").unwrap();
|
||||
fn test_file_empty() {
|
||||
tests::with_dir(|d| {
|
||||
tests::write_file(&d, "plain", "[]");
|
||||
|
||||
assert!(false, "not impl");
|
||||
let f = File::new(&d.path().join("plain").to_str().unwrap().to_string());
|
||||
|
||||
assert_eq!(0, f.events().unwrap().0.len());
|
||||
assert_eq!(0, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "[]");
|
||||
|
||||
f.stage_persisted().unwrap();
|
||||
assert_eq!(0, f.events().unwrap().0.len());
|
||||
assert_eq!(0, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "[]");
|
||||
|
||||
f.persist_stage().unwrap();
|
||||
assert_eq!(0, f.events().unwrap().0.len());
|
||||
assert_eq!(0, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "[]");
|
||||
|
||||
assert!(false, "not impl");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -360,70 +377,94 @@ mod test_events {
|
|||
|
||||
#[test]
|
||||
fn test_events_oplog_to_snapshot_one() {
|
||||
let d = tempdir::TempDir::new("").unwrap();
|
||||
test_file(&d, "plain", "- persisted\n- stage only");
|
||||
test_file(
|
||||
&d,
|
||||
".plain.some_host",
|
||||
r#"
|
||||
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
|
||||
"#,
|
||||
);
|
||||
tests::with_dir(|d| {
|
||||
tests::write_file(&d, "plain", "- persisted\n- stage only");
|
||||
tests::write_file(
|
||||
&d,
|
||||
".plain.some_host",
|
||||
r#"
|
||||
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
|
||||
"#,
|
||||
);
|
||||
|
||||
let events = Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
|
||||
assert_eq!(1, events.0.len(), "events: {:?}", events);
|
||||
let events =
|
||||
Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
|
||||
assert_eq!(1, events.0.len(), "events: {:?}", events);
|
||||
|
||||
let snapshot = events.snapshot().unwrap();
|
||||
assert_eq!(1, snapshot.len());
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted".to_string()),
|
||||
snapshot[0].0
|
||||
);
|
||||
let snapshot = events.snapshot().unwrap();
|
||||
assert_eq!(1, snapshot.len());
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted".to_string()),
|
||||
snapshot[0].0
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_events_oplog_to_snapshot_complex() {
|
||||
let d = tempdir::TempDir::new("").unwrap();
|
||||
test_file(&d, "plain", "- ignored");
|
||||
test_file(
|
||||
&d,
|
||||
".plain.some_host",
|
||||
r#"
|
||||
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
|
||||
{"ts":3, "patch":{"op":"add", "path":"/-", "value":"persisted 3"}}
|
||||
{"ts":2, "patch":{"op":"add", "path":"/-", "value":"persisted 2"}}
|
||||
{"ts":4, "patch":{"op":"add", "path":"/-", "value":"persisted 4"}}
|
||||
{"ts":5, "patch":{"op":"add", "path":"/-", "value":"persisted 5"}}
|
||||
{"ts":6, "patch":{"op":"replace", "path":"/4", "value":"persisted 5'"}}
|
||||
{"ts":7, "patch":{"op":"remove", "path":"/3"}}
|
||||
"#,
|
||||
);
|
||||
tests::with_dir(|d| {
|
||||
tests::write_file(&d, "plain", "- ignored");
|
||||
tests::write_file(
|
||||
&d,
|
||||
".plain.host_a",
|
||||
r#"
|
||||
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
|
||||
{"ts":3, "patch":{"op":"add", "path":"/-", "value":"persisted 3"}}
|
||||
{"ts":2, "patch":{"op":"add", "path":"/-", "value":"persisted 2"}}
|
||||
{"ts":6, "patch":{"op":"replace", "path":"/4", "value":"persisted 5'"}}
|
||||
{"ts":7, "patch":{"op":"remove", "path":"/3"}}
|
||||
"#,
|
||||
);
|
||||
tests::write_file(
|
||||
&d,
|
||||
".plain.host_b",
|
||||
r#"
|
||||
{"ts":4, "patch":{"op":"add", "path":"/-", "value":"persisted 4"}}
|
||||
{"ts":5, "patch":{"op":"add", "path":"/-", "value":"persisted 5"}}
|
||||
"#,
|
||||
);
|
||||
|
||||
let events = Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
|
||||
let events =
|
||||
Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
|
||||
|
||||
let snapshot = events.snapshot().unwrap();
|
||||
assert_eq!(4, snapshot.len());
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted".to_string()),
|
||||
snapshot[0].0
|
||||
);
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted 2".to_string()),
|
||||
snapshot[1].0
|
||||
);
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted 3".to_string()),
|
||||
snapshot[2].0
|
||||
);
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted 5'".to_string()),
|
||||
snapshot[3].0
|
||||
);
|
||||
let snapshot = events.snapshot().unwrap();
|
||||
assert_eq!(4, snapshot.len());
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted".to_string()),
|
||||
snapshot[0].0
|
||||
);
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted 2".to_string()),
|
||||
snapshot[1].0
|
||||
);
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted 3".to_string()),
|
||||
snapshot[2].0
|
||||
);
|
||||
assert_eq!(
|
||||
serde_yaml::Value::String("persisted 5'".to_string()),
|
||||
snapshot[3].0
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn test_file(d: &tempdir::TempDir, fname: &str, content: &str) {
|
||||
let mut f = std::fs::File::create(d.path().join(&fname)).unwrap();
|
||||
writeln!(f, "{}", &content).unwrap();
|
||||
f.sync_all().unwrap();
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
pub fn with_dir(mut foo: impl FnMut(tempdir::TempDir)) {
|
||||
foo(tempdir::TempDir::new("").unwrap());
|
||||
}
|
||||
|
||||
pub fn write_file(d: &tempdir::TempDir, fname: &str, content: &str) {
|
||||
let mut f = std::fs::File::create(d.path().join(&fname)).unwrap();
|
||||
writeln!(f, "{}", &content).unwrap();
|
||||
f.sync_all().unwrap();
|
||||
}
|
||||
|
||||
pub fn file_contains(d: &tempdir::TempDir, fname: &str, content: &str) {
|
||||
let mut f = std::fs::File::create(d.path().join(&fname)).unwrap();
|
||||
writeln!(f, "{}", &content).unwrap();
|
||||
f.sync_all().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue