From bc1b464446a692d0eae69ef01fd4f3ffd2c572b8 Mon Sep 17 00:00:00 2001 From: XuJiang Date: Sat, 31 Aug 2024 19:32:33 -0400 Subject: [PATCH] Issue: #3139 part 2 Tasks completed: 1. Locate ConsoleLauncher output 2. Check if actual and expected of AssertionFailedError are both type of CharSequence 3. Add diff function dependency 4. Using diff to generate the output desired Issue: #3139 --- .../release-notes-5.12.0-M1.adoc | 1 + gradle/libs.versions.toml | 1 + .../junit-platform-console.gradle.kts | 6 ++++- .../console/tasks/ConsoleTestExecutor.java | 24 +++++++++++++------ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc b/documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc index 89a1753a5280..b54c48686446 100644 --- a/documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc +++ b/documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc @@ -4,6 +4,7 @@ *Date of Release:* ❓ *Scope:* ❓ +* `ConsoleLauncher` output shows extra diff message for failed assertions on two `CharSequence` objects For a complete list of all _closed_ issues and pull requests for this release, consult the link:{junit5-repo}+/milestone/75?closed=1+[5.12.0-M1] milestone page in the diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6ec9b4616f49..23955a16a02c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -41,6 +41,7 @@ jfrunit = { module = "org.moditect.jfrunit:jfrunit-core", version = "1.0.0.Alpha jimfs = { module = "com.google.jimfs:jimfs", version = "1.3.0" } jmh-core = { module = "org.openjdk.jmh:jmh-core", version.ref = "jmh" } jmh-generator-annprocess = { module = "org.openjdk.jmh:jmh-generator-annprocess", version.ref = "jmh" } +java-diff-utils = { module = "io.github.java-diff-utils:java-diff-utils", version = "4.12" } joox = { module = "org.jooq:joox", version = "2.0.1" } jte = { module = "gg.jte:jte", version = "3.1.12" } junit4 = { module = "junit:junit", version = { require = "[4.12,)", prefer = "4.13.2" } } diff --git a/junit-platform-console/junit-platform-console.gradle.kts b/junit-platform-console/junit-platform-console.gradle.kts index be24d568d804..591450399a18 100644 --- a/junit-platform-console/junit-platform-console.gradle.kts +++ b/junit-platform-console/junit-platform-console.gradle.kts @@ -16,6 +16,8 @@ dependencies { compileOnly(libs.openTestReporting.events) + implementation(libs.java.diff.utils) + shadowed(libs.picocli) osgiVerification(projects.junitJupiterEngine) @@ -28,7 +30,9 @@ tasks { "--add-modules", "org.opentest4j.reporting.events", "--add-reads", "${project.projects.junitPlatformReporting.dependencyProject.javaModuleName}=org.opentest4j.reporting.events", "--add-modules", "info.picocli", - "--add-reads", "${javaModuleName}=info.picocli" + "--add-reads", "${javaModuleName}=info.picocli", + "--add-modules", "io.github.javadiffutils", + "--add-reads", "${javaModuleName}=io.github.javadiffutils" )) } shadowJar { diff --git a/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java b/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java index e38a27865856..471df66e53ae 100644 --- a/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java +++ b/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java @@ -16,11 +16,15 @@ import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Path; +import java.util.Arrays; import java.util.EnumSet; import java.util.List; import java.util.Optional; import java.util.function.Supplier; +import com.github.difflib.text.DiffRow; +import com.github.difflib.text.DiffRowGenerator; + import org.apiguardian.api.API; import org.junit.platform.commons.JUnitException; import org.junit.platform.commons.util.ClassLoaderUtils; @@ -36,7 +40,6 @@ import org.junit.platform.launcher.listeners.SummaryGeneratingListener; import org.junit.platform.launcher.listeners.TestExecutionSummary; import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener; -//for console output of diff import org.opentest4j.AssertionFailedError; import org.opentest4j.ValueWrapper; @@ -186,15 +189,23 @@ private void printSummary(TestExecutionSummary summary, PrintWriter out) { //adding diff code here summary.getFailures().forEach(failure -> { //get AssertionFailedError - if(failure.getException() instanceof AssertionFailedError){ - AssertionFailedError assertionFailedError = (AssertionFailedError)failure.getException(); + if (failure.getException() instanceof AssertionFailedError) { + AssertionFailedError assertionFailedError = (AssertionFailedError) failure.getException(); ValueWrapper expected = assertionFailedError.getExpected(); ValueWrapper actual = assertionFailedError.getActual(); //apply diff function if (isCharSequence(expected) && isCharSequence(actual)) { - out.printf("Expected %s\n", expected.getStringRepresentation()); - out.printf("Actual %s\n", actual.getStringRepresentation()); - //out.printf("Diff %s", calculateDiff(expected, actual)); + DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).inlineDiffByWord( + true).oldTag(f -> "~").newTag(f -> "**").build(); + List rows = generator.generateDiffRows( + Arrays.asList(expected.getStringRepresentation()), + Arrays.asList(actual.getStringRepresentation())); + + System.out.println( + "\nPlease put the diff result below into a onli../ne markdown editor to see markdown effect: "); + for (DiffRow row : rows) { + System.out.println(" | " + row.getOldLine() + " | " + row.getNewLine() + " | "); + } } } @@ -210,7 +221,6 @@ private boolean isCharSequence(ValueWrapper value) { return value != null && CharSequence.class.isAssignableFrom(value.getType()); } - @FunctionalInterface public interface Factory { ConsoleTestExecutor create(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions);