From 52df11566488631a36d517986fda38f50030e09b Mon Sep 17 00:00:00 2001 From: Bernard Fugas Date: Fri, 5 Aug 2022 08:38:11 +0200 Subject: [PATCH] Replace diff-utils with the latest actively maintained version 4.12 --- core/matchers/build.gradle | 2 +- .../com/dius/pact/core/matchers/DiffUtils.kt | 39 +++++++++++-------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/core/matchers/build.gradle b/core/matchers/build.gradle index 5c44310215..cbd10de732 100755 --- a/core/matchers/build.gradle +++ b/core/matchers/build.gradle @@ -2,7 +2,7 @@ dependencies { api project(path: ":core:model", configuration: 'default') api project(path: ":core:support", configuration: 'default') compile "org.apache.commons:commons-lang3:${project.commonsLang3Version}" - compile 'com.googlecode.java-diff-utils:diffutils:1.3.0' + compile 'io.github.java-diff-utils:java-diff-utils:4.12' implementation 'xerces:xercesImpl:2.12.0' implementation "org.slf4j:slf4j-api:${project.slf4jVersion}" implementation 'org.atteo:evo-inflector:1.2.2' diff --git a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/DiffUtils.kt b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/DiffUtils.kt index 7aaaeb5bd9..dcbfd79f33 100644 --- a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/DiffUtils.kt +++ b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/DiffUtils.kt @@ -1,33 +1,40 @@ package au.com.dius.pact.core.matchers import au.com.dius.pact.core.support.json.JsonValue +import com.github.difflib.DiffUtils +import com.github.difflib.patch.ChangeDelta private const val NEW_LINE = '\n' fun generateDiff(expectedBodyString: String, actualBodyString: String): List { val expectedLines = expectedBodyString.split(NEW_LINE) val actualLines = actualBodyString.split(NEW_LINE) - val patch = difflib.DiffUtils.diff(expectedLines, actualLines) + val patch = DiffUtils.diff(expectedLines, actualLines) val diff = mutableListOf() - + var line = 0 patch.deltas.forEach { delta -> - if (delta.original.position >= 1 && (diff.isEmpty() || expectedLines[delta.original.position - 1] != diff.last())) { - diff.add(expectedLines[delta.original.position - 1]) - } - - delta.original.lines.forEach { - diff.add("-$it") - } - delta.revised.lines.forEach { - diff.add("+$it") - } - - val pos = delta.original.position + delta.original.lines.size - if (pos < expectedLines.size) { - diff.add(expectedLines[pos]) + when (delta) { + is ChangeDelta<*> -> { + if (delta.source.position >= 1 && (diff.isEmpty() || + expectedLines[delta.source.position - 1] != diff.last())) { + diff.addAll(expectedLines.slice(line until delta.source.position)) + } + + delta.source.lines.forEach { + diff.add("-$it") + } + delta.target.lines.forEach { + diff.add("+$it") + } + + line = delta.source.position + delta.source.lines.size + } } } + if (line < expectedLines.size) { + diff.addAll(expectedLines.listIterator(line).asSequence()) + } return diff }