switch from prettydiff to diff

prettydiff has a weird chain of dependencies starting with
prettytable-rs and ending up with (among other things) argon2rs.
This commit is contained in:
Scott Lamb
2021-10-27 10:17:41 -07:00
parent 884c3333cc
commit 4f22cf66e3
4 changed files with 61 additions and 171 deletions

View File

@@ -18,6 +18,7 @@ base64 = "0.13.0"
blake3 = "1.0.0"
byteorder = "1.0"
cstr = "0.2.5"
diff = "0.1.12"
failure = "0.1.1"
fnv = "1.0"
futures = "0.3"
@@ -33,12 +34,12 @@ num-rational = { version = "0.4.0", default-features = false, features = ["std"]
odds = { version = "0.4.0", features = ["std-vec"] }
parking_lot = { version = "0.11.1", features = [] }
pretty-hex = "0.2.1"
prettydiff = { git = "https://github.com/scottlamb/prettydiff", branch = "pr-update-deps" }
protobuf = "3.0.0-alpha.1"
ring = "0.16.2"
rusqlite = "0.26.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
#similar = "2.1.0"
smallvec = "1.0"
tempfile = "3.2.0"
time = "0.1"

View File

@@ -7,7 +7,6 @@
//! and for tests of `moonfire-nvr upgrade`.
use failure::Error;
use prettydiff::diff_slice;
use rusqlite::params;
use std::fmt::Write;
@@ -55,6 +54,35 @@ impl std::fmt::Display for IndexColumn {
}
}
/// If `slice1` and `slice2` differ, return differences in roughly unified diff form.
fn diff_slices<T: std::fmt::Display + PartialEq>(
name1: &str,
slice1: &[T],
name2: &str,
slice2: &[T],
) -> Option<String> {
let mut diff = format!("--- {}\n+++ {}\n", name1, name2);
let mut changed = false;
for item in diff::slice(slice1, slice2) {
match item {
diff::Result::Left(i) => {
changed = true;
write!(&mut diff, "-{}\n", i)
}
diff::Result::Both(i, _) => write!(&mut diff, " {}\n", i),
diff::Result::Right(i) => {
changed = true;
write!(&mut diff, "+{}\n", i)
}
}
.unwrap();
}
if !changed {
return None;
}
Some(diff)
}
/// Returns a sorted vec of table names in the given connection.
fn get_tables(c: &rusqlite::Connection) -> Result<Vec<String>, rusqlite::Error> {
c.prepare(
@@ -139,13 +167,11 @@ pub fn get_diffs(
// Compare table list.
let tables1 = get_tables(c1)?;
let tables2 = get_tables(c2)?;
if tables1 != tables2 {
if let Some(diff) = diff_slices(n1, &tables1[..], n2, &tables2[..]) {
write!(
&mut diffs,
"table list mismatch, {} vs {}:\n{}",
n1,
n2,
diff_slice(&tables1, &tables2)
n1, n2, diff
)?;
}
@@ -153,14 +179,11 @@ pub fn get_diffs(
for t in &tables1 {
let columns1 = get_table_columns(c1, &t)?;
let columns2 = get_table_columns(c2, &t)?;
if columns1 != columns2 {
if let Some(diff) = diff_slices(n1, &columns1[..], n2, &columns2[..]) {
write!(
&mut diffs,
"table {:?} column, {} vs {}:\n{}",
t,
n1,
n2,
diff_slice(&columns1, &columns2)
t, n1, n2, diff
)?;
}
@@ -168,29 +191,22 @@ pub fn get_diffs(
let mut indices2 = get_indices(c2, &t)?;
indices1.sort_by(|a, b| a.name.cmp(&b.name));
indices2.sort_by(|a, b| a.name.cmp(&b.name));
if indices1 != indices2 {
if let Some(diff) = diff_slices(n1, &indices1[..], n2, &indices2[..]) {
write!(
&mut diffs,
"table {:?} indices, {} vs {}:\n{}",
t,
n1,
n2,
diff_slice(&indices1, &indices2)
t, n1, n2, diff
)?;
}
for i in &indices1 {
let ic1 = get_index_columns(c1, &i.name)?;
let ic2 = get_index_columns(c2, &i.name)?;
if ic1 != ic2 {
if let Some(diff) = diff_slices(n1, &ic1[..], n2, &ic2[..]) {
write!(
&mut diffs,
"table {:?} index {:?} columns {} vs {}:\n{}",
t,
i,
n1,
n2,
diff_slice(&ic1, &ic2)
t, i, n1, n2, diff
)?;
}
}

View File

@@ -190,7 +190,12 @@ mod tests {
fn compare(c: &rusqlite::Connection, ver: i32, fresh_sql: &str) -> Result<(), Error> {
let fresh = new_conn()?;
fresh.execute_batch(fresh_sql)?;
if let Some(diffs) = compare::get_diffs("upgraded", &c, "fresh", &fresh)? {
if let Some(diffs) = compare::get_diffs(
&format!("upgraded to version {}", ver),
&c,
&format!("fresh version {}", ver),
&fresh,
)? {
panic!("Version {}: differences found:\n{}", ver, diffs);
}
Ok(())