test helpers
parent
b5b794817c
commit
bcbcd6fbb7
|
|
@ -104,7 +104,7 @@ mod test_flags {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_flags_files_unhidden_only() {
|
fn test_flags_files_unhidden_only() {
|
||||||
let d = tempdir::TempDir::new("").unwrap();
|
tests::with_dir(|d| {
|
||||||
std::fs::File::create(d.path().join("plain")).unwrap();
|
std::fs::File::create(d.path().join("plain")).unwrap();
|
||||||
std::fs::File::create(d.path().join(".hidden")).unwrap();
|
std::fs::File::create(d.path().join(".hidden")).unwrap();
|
||||||
|
|
||||||
|
|
@ -116,6 +116,7 @@ mod test_flags {
|
||||||
};
|
};
|
||||||
let files = flags.files().expect("failed to files from dir");
|
let files = flags.files().expect("failed to files from dir");
|
||||||
assert_eq!(1, files.files.len());
|
assert_eq!(1, files.files.len());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -233,12 +234,28 @@ mod test_file {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_file() {
|
fn test_file_empty() {
|
||||||
let d = tempdir::TempDir::new("").unwrap();
|
tests::with_dir(|d| {
|
||||||
let mut f = std::fs::File::create(d.path().join("plain")).unwrap();
|
tests::write_file(&d, "plain", "[]");
|
||||||
writeln!(f, "hello world").unwrap();
|
|
||||||
|
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");
|
assert!(false, "not impl");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -360,9 +377,9 @@ mod test_events {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_events_oplog_to_snapshot_one() {
|
fn test_events_oplog_to_snapshot_one() {
|
||||||
let d = tempdir::TempDir::new("").unwrap();
|
tests::with_dir(|d| {
|
||||||
test_file(&d, "plain", "- persisted\n- stage only");
|
tests::write_file(&d, "plain", "- persisted\n- stage only");
|
||||||
test_file(
|
tests::write_file(
|
||||||
&d,
|
&d,
|
||||||
".plain.some_host",
|
".plain.some_host",
|
||||||
r#"
|
r#"
|
||||||
|
|
@ -370,7 +387,8 @@ mod test_events {
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
|
||||||
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();
|
||||||
assert_eq!(1, events.0.len(), "events: {:?}", events);
|
assert_eq!(1, events.0.len(), "events: {:?}", events);
|
||||||
|
|
||||||
let snapshot = events.snapshot().unwrap();
|
let snapshot = events.snapshot().unwrap();
|
||||||
|
|
@ -379,27 +397,35 @@ mod test_events {
|
||||||
serde_yaml::Value::String("persisted".to_string()),
|
serde_yaml::Value::String("persisted".to_string()),
|
||||||
snapshot[0].0
|
snapshot[0].0
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_events_oplog_to_snapshot_complex() {
|
fn test_events_oplog_to_snapshot_complex() {
|
||||||
let d = tempdir::TempDir::new("").unwrap();
|
tests::with_dir(|d| {
|
||||||
test_file(&d, "plain", "- ignored");
|
tests::write_file(&d, "plain", "- ignored");
|
||||||
test_file(
|
tests::write_file(
|
||||||
&d,
|
&d,
|
||||||
".plain.some_host",
|
".plain.host_a",
|
||||||
r#"
|
r#"
|
||||||
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
|
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
|
||||||
{"ts":3, "patch":{"op":"add", "path":"/-", "value":"persisted 3"}}
|
{"ts":3, "patch":{"op":"add", "path":"/-", "value":"persisted 3"}}
|
||||||
{"ts":2, "patch":{"op":"add", "path":"/-", "value":"persisted 2"}}
|
{"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":6, "patch":{"op":"replace", "path":"/4", "value":"persisted 5'"}}
|
||||||
{"ts":7, "patch":{"op":"remove", "path":"/3"}}
|
{"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();
|
let snapshot = events.snapshot().unwrap();
|
||||||
assert_eq!(4, snapshot.len());
|
assert_eq!(4, snapshot.len());
|
||||||
|
|
@ -419,11 +445,26 @@ mod test_events {
|
||||||
serde_yaml::Value::String("persisted 5'".to_string()),
|
serde_yaml::Value::String("persisted 5'".to_string()),
|
||||||
snapshot[3].0
|
snapshot[3].0
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_file(d: &tempdir::TempDir, fname: &str, content: &str) {
|
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();
|
let mut f = std::fs::File::create(d.path().join(&fname)).unwrap();
|
||||||
writeln!(f, "{}", &content).unwrap();
|
writeln!(f, "{}", &content).unwrap();
|
||||||
f.sync_all().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