Skip to content

Commit f935cbd

Browse files
graememorganError Prone Team
authored andcommitted
Make ErrorProneJavaCompilerTest more verbose when it fails.
PiperOrigin-RevId: 830431937
1 parent 82b4f96 commit f935cbd

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

core/src/test/java/com/google/errorprone/ErrorProneJavaCompilerTest.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.errorprone;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.Truth.assertWithMessage;
2021
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
2122
import static com.google.errorprone.DiagnosticTestHelper.DIAGNOSTIC_CONTAINING;
2223
import static com.google.errorprone.FileObjects.forResources;
@@ -156,7 +157,7 @@ public void withDisabledCheck() {
156157
Arrays.asList("bugpatterns/testdata/SelfAssignmentPositiveCases1.java"),
157158
Arrays.asList("-Xep:SelfAssignment:OFF"),
158159
Collections.<Class<? extends BugChecker>>emptyList());
159-
assertThat(result.succeeded).isTrue();
160+
assertSucceeded(result);
160161
}
161162

162163
@Test
@@ -166,7 +167,7 @@ public void withCheckPromotedToError() {
166167
Arrays.asList("bugpatterns/testdata/WaitNotInLoopPositiveCases.java"),
167168
Collections.<String>emptyList(),
168169
Collections.<Class<? extends BugChecker>>emptyList());
169-
assertThat(result.succeeded).isTrue();
170+
assertSucceeded(result);
170171
assertThat(result.diagnosticHelper.getDiagnostics())
171172
.comparingElementsUsing(DIAGNOSTIC_CONTAINING)
172173
.contains("[WaitNotInLoop]");
@@ -199,7 +200,7 @@ public void withCheckDemotedToWarning() {
199200
Arrays.asList("bugpatterns/testdata/SelfAssignmentPositiveCases1.java"),
200201
Arrays.asList("-Xep:SelfAssignment:WARN"),
201202
Collections.<Class<? extends BugChecker>>emptyList());
202-
assertThat(result.succeeded).isTrue();
203+
assertSucceeded(result);
203204
assertThat(result.diagnosticHelper.getDiagnostics())
204205
.comparingElementsUsing(DIAGNOSTIC_CONTAINING)
205206
.contains("[SelfAssignment]");
@@ -212,7 +213,7 @@ public void withNonDefaultCheckOn() {
212213
Arrays.asList("bugpatterns/testdata/EmptyIfStatementPositiveCases.java"),
213214
Collections.<String>emptyList(),
214215
Collections.<Class<? extends BugChecker>>emptyList());
215-
assertThat(result.succeeded).isTrue();
216+
assertSucceeded(result);
216217
assertThat(result.diagnosticHelper.getDiagnostics()).isEmpty();
217218

218219
result =
@@ -282,7 +283,7 @@ public void withCustomCheckNegative() {
282283
Arrays.asList("bugpatterns/testdata/SelfAssignmentPositiveCases1.java"),
283284
Collections.<String>emptyList(),
284285
Arrays.<Class<? extends BugChecker>>asList(Finally.class));
285-
assertThat(result.succeeded).isTrue();
286+
assertSucceeded(result);
286287
assertThat(result.diagnosticHelper.getDiagnostics()).isEmpty();
287288
}
288289

@@ -406,7 +407,7 @@ public void withExcludedPaths() {
406407
Arrays.asList("bugpatterns/testdata/SelfAssignmentPositiveCases1.java"),
407408
Arrays.asList("-XepExcludedPaths:.*/bugpatterns/.*"),
408409
Collections.<Class<? extends BugChecker>>emptyList());
409-
assertThat(result.succeeded).isTrue();
410+
assertSucceeded(result);
410411

411412
// ensure regexp must match the full path
412413
result =
@@ -449,7 +450,7 @@ class StringConstantWrapper {
449450
Collections.singleton(fileObject),
450451
Arrays.asList("-XepPatchChecks:", "-XepPatchLocation:IN_PLACE"),
451452
ImmutableList.of(AssignmentUpdater.class));
452-
assertThat(result.succeeded).isTrue();
453+
assertSucceeded(result);
453454
assertThat(Files.readString(Path.of(fileObject.toUri())))
454455
.isEqualTo(
455456
"""
@@ -476,7 +477,7 @@ class StringConstantWrapper {
476477
Arrays.asList(
477478
"-XepPatchChecks:", "-XepPatchLocation:IN_PLACE", "-Xep:AssignmentUpdater:OFF"),
478479
ImmutableList.of(AssignmentUpdater.class));
479-
assertThat(result.succeeded).isTrue();
480+
assertSucceeded(result);
480481
assertThat(Files.readString(Path.of(fileObject.toUri())))
481482
.isEqualTo(
482483
"""
@@ -503,7 +504,7 @@ class StringConstantWrapper {
503504
ImmutableList.of(
504505
"-XepPatchChecks:", "-XepPatchLocation:IN_PLACE", "-XepDisableAllChecks"),
505506
ImmutableList.of(AssignmentUpdater.class));
506-
assertThat(result.succeeded).isTrue();
507+
assertSucceeded(result);
507508
assertThat(Files.readString(Path.of(fileObject.toUri())))
508509
.isEqualTo(
509510
"""
@@ -532,7 +533,7 @@ class StringConstantWrapper {
532533
"-XepPatchLocation:IN_PLACE",
533534
"-Xep:AssignmentUpdater:OFF"),
534535
ImmutableList.of(AssignmentUpdater.class));
535-
assertThat(result.succeeded).isTrue();
536+
assertSucceeded(result);
536537
assertThat(Files.readString(Path.of(fileObject.toUri())))
537538
.isEqualTo(
538539
"""
@@ -561,7 +562,7 @@ class StringConstantWrapper {
561562
"-XepPatchLocation:IN_PLACE",
562563
"-XepOpt:AssignmentUpdater:NewValue=new-value"),
563564
ImmutableList.of(AssignmentUpdater.class));
564-
assertThat(result.succeeded).isTrue();
565+
assertSucceeded(result);
565566
assertThat(Files.readString(Path.of(fileObject.toUri())))
566567
.isEqualTo(
567568
"""
@@ -588,15 +589,13 @@ public String getCharContent(boolean ignoreEncodingErrors) {
588589
};
589590
}
590591

591-
private static class CompilationResult {
592-
final boolean succeeded;
593-
final String output;
594-
final DiagnosticTestHelper diagnosticHelper;
595-
596-
CompilationResult(boolean succeeded, String output, DiagnosticTestHelper diagnosticHelper) {
597-
this.succeeded = succeeded;
598-
this.output = output;
599-
this.diagnosticHelper = diagnosticHelper;
592+
private static record CompilationResult(
593+
boolean succeeded, String output, DiagnosticTestHelper diagnosticHelper) {
594+
@Override
595+
public String toString() {
596+
return String.format(
597+
"CompilationResult{succeeded=%s, output=%s, diagnostics=%s}",
598+
succeeded, output, diagnosticHelper.getDiagnostics());
600599
}
601600
}
602601

@@ -631,4 +630,10 @@ private CompilationResult doCompile(
631630
return new CompilationResult(
632631
task.call(), new String(outputStream.toByteArray(), UTF_8), diagnosticHelper);
633632
}
633+
634+
private static void assertSucceeded(CompilationResult result) {
635+
assertWithMessage("Compilation should have succeeded, but had result %s", result)
636+
.that(result.succeeded)
637+
.isTrue();
638+
}
634639
}

0 commit comments

Comments
 (0)