blob: cb56f3b2be59700fd72e2fe7f677f63645d34279 [file] [log] [blame]
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
String[] ijTestIncludes = ${TEST_NAME_INCLUDES}
boolean ijTestFilteringSupported = GradleVersion.current().compareTo(GradleVersion.version("1.10"))>=0
boolean skipExecution
gradle.addBuildListener(new BuildListener() {
void buildFinished(BuildResult result) {
if(result.failure?.message?.startsWith("Task 'cleanTest' not found in") || result.failure?.message?.startsWith("Task 'test' not found in")) {
logConfigurationError(
"Unable to run unit tests",
"Unable to run unit tests, since there is no 'test' task configured in the project." +
"\n<br>You can add the following into the project gradle script: apply plugin: 'java'" +
"\n<br><br>See <a href='http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.testing.Test.html'>Gradle Test task</a> for details",
false
)
}
}
void buildStarted(Gradle gradle){}
void projectsEvaluated(Gradle gradle){}
void projectsLoaded(Gradle gradle){}
void settingsEvaluated(Settings settings){}
})
for (incl in ijTestIncludes) {
if(incl != '*' && !ijTestFilteringSupported) {
skipExecution = true
logConfigurationError(
"Unable to configure test filtering",
"Unable to configure test filtering for " + GradleVersion.current() +".<br>Please, use Gradle 1.10 or above or run all tests of a module.",
true)
break;
}
}
gradle.taskGraph.beforeTask { Task task ->
task.onlyIf { !skipExecution }
if (task instanceof Test) {
try {
logTestReportLocation(task.reports?.html?.entryPoint?.path)
task.beforeSuite { descriptor ->
logTestEvent("beforeSuite", descriptor, null, null)
}
task.afterSuite { descriptor, result ->
logTestEvent("afterSuite", descriptor, null, result)
}
task.beforeTest { descriptor ->
logTestEvent("beforeTest", descriptor, null, null)
}
task.onOutput { descriptor, event ->
logTestEvent("onOutput", descriptor, event, null)
}
task.afterTest { descriptor, result ->
logTestEvent("afterTest", descriptor, null, result)
}
if(ijTestFilteringSupported) {
task.filter {
ijTestIncludes.each(){ includeTestsMatching "${it}" }
}
}
}
catch (all) {
logger.error("", all)
}
}
}
Object logTestEvent(def testEventType, def testDescriptor, def testEvent, def testResult) {
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
xml.event(type: testEventType){
test(id: testDescriptor.id, parentId: testDescriptor.parent?.id ?: '') {
if(testDescriptor){
descriptor(name: testDescriptor.name ?: '', className: testDescriptor.className ?: '')
}
if(testEvent) {
def message = escapeCdata(testEvent.message)
event(destination: testEvent.destination){
xml.mkp.yieldUnescaped("$message")
}
}
if(testResult) {
def errorMsg = escapeCdata(testResult.exception?.message?:'')
def stackTrace = escapeCdata(getStackTrace(testResult.exception))
result(resultType: testResult.resultType ?: '', startTime: testResult.startTime, endTime: testResult.endTime){
if(testResult.exception?.message?.trim()) xml.mkp.yieldUnescaped("<errorMsg>$errorMsg</errorMsg>")
if(testResult.exception) xml.mkp.yieldUnescaped("<stackTrace>$stackTrace</stackTrace>")
if('junit.framework.ComparisonFailure'.equals(testResult.exception?.class?.name) ||
'org.junit.ComparisonFailure'.equals(testResult.exception?.class?.name)) {
def expected = escapeCdata(testResult.exception.fExpected)
def actual = escapeCdata(testResult.exception.fActual)
failureType('comparison')
xml.mkp.yieldUnescaped("<expected>$expected</expected>")
xml.mkp.yieldUnescaped("<actual>$actual</actual>")
} else if('junit.framework.AssertionFailedError'.equals(testResult.exception?.class?.name) ||
testResult.exception instanceof AssertionError) {
failureType('assertionFailed')
} else {
failureType('error')
}
}
}
}
}
writeLog(writer.toString())
}
def logTestReportLocation(def report) {
if(!report) return
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
xml.event(type: 'reportLocation', testReport: report)
writeLog(writer.toString());
}
def logConfigurationError(aTitle, aMessage, boolean openSettings) {
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
xml.event(type: 'configurationError', openSettings: openSettings) {
title(aTitle)
message(aMessage)
}
writeLog(writer.toString());
}
String escapeCdata(String s) {
return "<![CDATA[" + s?.replaceAll("]]>", "]]]]><![CDATA[>")+ "]]>";
}
def getStackTrace(Throwable t) {
if(!t) return ''
StringWriter sw = new StringWriter()
t.printStackTrace(new PrintWriter(sw))
sw.toString()
}
def wrap(String s) {
if(!s) return s;
s.replaceAll("\r\n|\n\r|\n|\r","<ijLogEol/>\n")
}
def writeLog(s) {
println String.format("\n<ijLog>%s</ijLog>", wrap(s))
}