From 2bebaf2dfbf6e18d09e6bcdb9fcf59626908688a Mon Sep 17 00:00:00 2001 From: ghm Date: Fri, 11 Oct 2024 09:19:22 -0700 Subject: [PATCH] Don't quibble over trailing newlines. PiperOrigin-RevId: 684860035 --- .../bugpatterns/MisformattedTestData.java | 2 +- .../bugpatterns/MisformattedTestDataTest.java | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MisformattedTestData.java b/core/src/main/java/com/google/errorprone/bugpatterns/MisformattedTestData.java index a929c256dbd..403771a5d31 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MisformattedTestData.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MisformattedTestData.java @@ -75,7 +75,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState } catch (FormatterException exception) { return NO_MATCH; } - if (formattedSource.equals(sourceValue)) { + if (formattedSource.trim().equals(((String) sourceValue).trim())) { return NO_MATCH; } // This is a bit crude: but tokenize between the comma and the 2nd argument in order to work out diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/MisformattedTestDataTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/MisformattedTestDataTest.java index fd0a702173f..e11c1542aae 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/MisformattedTestDataTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/MisformattedTestDataTest.java @@ -61,6 +61,34 @@ void method() { .doTest(); } + @Test + public void onlyDiffersByFinalNewline_noFinding() { + assume().that(Runtime.version().feature()).isAtLeast(14); + + compilationHelper + .addSourceLines( + "Test.java", + """ + import com.google.errorprone.BugCheckerRefactoringTestHelper; + + class Test { + void method(BugCheckerRefactoringTestHelper h) { + h.addInputLines( + "Test.java", + \""" + package foo; + + class Test { + void method() { + int a = 1; + } + }\"""); + } + } + """) + .doTest(); + } + @Test public void misformatted_suggestsFix() { assume().that(Runtime.version().feature()).isAtLeast(14);