Skip to content

Instantly share code, notes, and snippets.

@modille
Created October 23, 2018 17:55
Show Gist options
  • Save modille/d0d64fcf853eee9eb85b8862c28e5fb4 to your computer and use it in GitHub Desktop.
Save modille/d0d64fcf853eee9eb85b8862c28e5fb4 to your computer and use it in GitHub Desktop.
Log failures in an error format that vim and other tooling can use
test {
// Log failures in an error format that vim and other tooling can use
// Something like:
// FILENAME:LINE_NUMBER failed: MESSAGE
// For example:
// src/test/java/TestFoo.java:42 failed: junit.framework.ComparisonFailure: null expected:<[b]ar> but was:<[B]ar>
afterTest { desc, result ->
if (result.resultType == TestResult.ResultType.FAILURE) {
result.getException().each { e ->
e.getStackTrace().each { el ->
if (el.getFileName()) {
// Take the filename from the stacktrace line and look up the corresponding project file
def projectFiles = new FileNameFinder().getFileNames(rootProject.projectDir.getPath(), '**/' + el.getFileName())
projectFiles.each {
// Build up the path to the file relative to the project root
def relativePath = rootProject.projectDir.toPath().relativize(new File(it).toPath())
logger.lifecycle(relativePath.toString() + ":" + el.getLineNumber() + " failed: " + e.getMessage())
}
}
}
}
}
}
}
integrationTest {
// Log failures in an error format that vim and other tooling can use
// Something like:
// FILENAME:LINE_NUMBER failed: MESSAGE
// For example:
// src/test/java/TestFoo.java:42 failed: junit.framework.ComparisonFailure: null expected:<[b]ar> but was:<[B]ar>
afterTest { desc, result ->
if (result.resultType == TestResult.ResultType.FAILURE) {
result.getException().each { e ->
e.getStackTrace().each { el ->
if (el.getFileName()) {
// Take the filename from the stacktrace line and look up the corresponding project file
def projectFiles = new FileNameFinder().getFileNames(rootProject.projectDir.getPath(), '**/' + el.getFileName())
projectFiles.each {
// Build up the path to the file relative to the project root
def relativePath = rootProject.projectDir.toPath().relativize(new File(it).toPath())
logger.lifecycle(relativePath.toString() + ":" + el.getLineNumber() + " failed: " + e.getMessage())
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment