Repository: mitsuhiko/insta
Pull request #173 (2021-02-27 14:24:37)

Update similar, switch to patience diff and add timeout


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c712ef0..6476294 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@ All notable changes to insta and cargo-insta are documented here.
 
 * Added support for u128/i128.  (#169)
 * Normalize newlines to unix before before asserting.  (#172)
+* Switch diffing to patience.  (#173)
 
 ## 1.6.3
 
diff --git a/Cargo.toml b/Cargo.toml
index ca6511b..a283460 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -48,3 +48,6 @@ globset = { version = "0.4.6", optional = true }
 walkdir = { version = "2.3.1", optional = true }
 uuid = "0.8.1"
 similar = { version = "1.3.0", features = ["inline"] }
+
+[dev-dependencies]
+similar-asserts = "1.1.0"
diff --git a/src/redaction.rs b/src/redaction.rs
index 67f0213..4f00f6a 100644
--- a/src/redaction.rs
+++ b/src/redaction.rs
@@ -469,6 +469,7 @@ impl<'a> Selector<'a> {
 
 #[test]
 fn test_range_checks() {
+    use similar_asserts::assert_eq;
     assert_eq!(PathItem::Index(0, 10).range_check(None, Some(-1)), true);
     assert_eq!(PathItem::Index(9, 10).range_check(None, Some(-1)), false);
     assert_eq!(PathItem::Index(0, 10).range_check(Some(1), Some(-1)), false);
diff --git a/src/runtime.rs b/src/runtime.rs
index 6e43a93..4753a60 100644
--- a/src/runtime.rs
+++ b/src/runtime.rs
@@ -9,9 +9,10 @@ use std::process::{Command, Stdio};
 use std::str;
 use std::sync::Mutex;
 use std::thread;
+use std::time::Duration;
 
 use lazy_static::lazy_static;
-use similar::{ChangeTag, TextDiff};
+use similar::{Algorithm, ChangeTag, TextDiff};
 
 use serde::Deserialize;
 
@@ -208,7 +209,10 @@ pub fn get_cargo_workspace(manifest_dir: &str) -> &Path {
 
 fn print_changeset(old: &str, new: &str, expr: Option<&str>) {
     let width = term_width();
-    let diff = TextDiff::from_lines(old, new);
+    let diff = TextDiff::configure()
+        .algorithm(Algorithm::Patience)
+        .timeout(Duration::from_millis(500))
+        .diff_lines(old, new);
 
     if let Some(expr) = expr {
         println!("{:─^1$}", "", width,);
@@ -642,6 +646,7 @@ fn min_indentation(snapshot: &str) -> usize {
 
 #[test]
 fn test_min_indentation() {
+    use similar_asserts::assert_eq;
     let t = r#"
    1
    2
@@ -709,6 +714,7 @@ fn normalize_inline_snapshot(snapshot: &str) -> String {
 
 #[test]
 fn test_normalize_inline_snapshot() {
+    use similar_asserts::assert_eq;
     // here we do exact matching (rather than `assert_snapshot`)
     // to ensure we're not incorporating the modifications this library makes
     let t = r#"
diff --git a/src/snapshot.rs b/src/snapshot.rs
index b6a9e05..d1c5d7b 100644
--- a/src/snapshot.rs
+++ b/src/snapshot.rs
@@ -333,6 +333,7 @@ impl PartialEq for SnapshotContents {
 
 #[test]
 fn test_snapshot_contents() {
+    use similar_asserts::assert_eq;
     let snapshot_contents = SnapshotContents("testing".to_string());
     assert_eq!(snapshot_contents.to_inline(0), r#""testing""#);
 


Test patch:
diff --git a/tests/test_clash_detection.rs b/tests/test_clash_detection.rs
index 71ea588..fd8035a 100644
--- a/tests/test_clash_detection.rs
+++ b/tests/test_clash_detection.rs
@@ -1,6 +1,8 @@
 use std::env;
 use std::thread;
 
+use similar_asserts::assert_eq;
+
 #[test]
 fn test_clash_detection() {
     let old_update_value = env::var("INSTA_UPDATE");
@@ -42,6 +44,6 @@ fn test_clash_detection() {
     values.sort();
     assert_eq!(&values[..], &vec![
         "Insta snapshot name clash detected between \'foo_always_missing\' and \'test_foo_always_missing\' in \'test_clash_detection\'. Rename one function.",
-        "snapshot assertion for \'foo_always_missing\' failed in line 14",
+        "snapshot assertion for \'foo_always_missing\' failed in line 16",
     ][..]);
 }
diff --git a/tests/test_redaction.rs b/tests/test_redaction.rs
index 0a5736c..f375749 100644
--- a/tests/test_redaction.rs
+++ b/tests/test_redaction.rs
@@ -6,6 +6,8 @@ use insta::{
 };
 use serde::Serialize;
 
+use similar_asserts::assert_eq;
+
 #[test]
 fn test_selector_parser() {
     macro_rules! assert_selector_snapshot {