Skip to content

Commit 72108c1

Browse files
committed
add source information to diagnostic messages reported for failures
The rationale is to help simplify troubleshooting the errors reported by compile-testing.
1 parent 1ea11bc commit 72108c1

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/main/java/com/google/testing/compile/CompilationSubject.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,18 @@ private void checkDiagnosticCount(
183183
}
184184

185185
private static String messageListing(
186-
Iterable<? extends Diagnostic<?>> diagnostics, String headingFormat, Object... formatArgs) {
186+
Iterable<? extends Diagnostic<? extends JavaFileObject>> diagnostics,
187+
String headingFormat,
188+
Object... formatArgs) {
187189
StringBuilder listing =
188190
new StringBuilder(String.format(headingFormat, formatArgs)).append('\n');
189-
for (Diagnostic<?> diagnostic : diagnostics) {
190-
listing.append(diagnostic.getMessage(null)).append('\n');
191+
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
192+
listing.append(
193+
String.format(
194+
"%s:%d - %s\n",
195+
sourceFileName(diagnostic),
196+
diagnostic.getLineNumber(),
197+
diagnostic.getMessage(null)));
191198
}
192199
return listing.toString();
193200
}
@@ -416,15 +423,17 @@ private ImmutableList<Diagnostic<? extends JavaFileObject>> findDiagnosticsInFil
416423
}
417424

418425
private ImmutableSet<String> sourceFilesWithDiagnostics() {
419-
return mapDiagnostics(
420-
diagnostic ->
421-
diagnostic.getSource() == null
422-
? "(no associated file)"
423-
: diagnostic.getSource().getName())
426+
return mapDiagnostics(CompilationSubject::sourceFileName)
424427
.collect(toImmutableSet());
425428
}
426429
}
427430

431+
private static String sourceFileName(Diagnostic<? extends JavaFileObject> diagnostic) {
432+
return diagnostic.getSource() == null
433+
? "(no associated file)"
434+
: diagnostic.getSource().getName();
435+
}
436+
428437
/** An object that can list the lines in a file. */
429438
static final class LinesInFile {
430439
private final JavaFileObject file;

src/test/java/com/google/testing/compile/CompilationSubjectTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,21 @@ public void hadWarningCount() {
415415
assertThat(compilerWithWarning().compile(sourceFile)).hadWarningCount(2);
416416
}
417417

418+
@Test
419+
public void hadWarningCountReportsLineNumbers() {
420+
expectFailure
421+
.whenTesting()
422+
.about(compilations())
423+
.that(compilerWithWarning().compile(sourceFile))
424+
.hadWarningCount(0);
425+
AssertionError expected = expectFailure.getFailure();
426+
427+
assertThat(expected.getMessage())
428+
.contains(String.format("%s:6 - this is a message", sourceFile.getName()));
429+
assertThat(expected.getMessage())
430+
.contains(String.format("%s:7 - this is a message", sourceFile.getName()));
431+
}
432+
418433
@Test
419434
public void hadWarningCount_wrongCount() {
420435
expectFailure

0 commit comments

Comments
 (0)