blob: a7df6cc9c46b855a31b0b9302b1293d33c58260e [file] [edit]
import groovy.transform.CompileStatic
import org.gradle.api.flow.*
import org.gradle.api.provider.ValueSource
import org.gradle.api.provider.ValueSourceParameters
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import org.gradle.api.tasks.compile.AbstractCompile
import org.gradle.api.tasks.testing.AbstractTestTask
import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.testing.TestFilter
import org.gradle.api.tasks.testing.TestResult
import org.gradle.api.tasks.testing.TestResult.ResultType
import org.gradle.build.event.BuildEventsListenerRegistry
import org.gradle.tooling.Failure
import org.gradle.tooling.events.FinishEvent
import org.gradle.tooling.events.OperationCompletionListener
import org.gradle.tooling.events.task.TaskFailureResult
import org.gradle.tooling.events.task.TaskFinishEvent
import javax.inject.Inject
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
import java.util.concurrent.atomic.AtomicLong
import java.util.regex.Pattern
initscript {
dependencies {
def initScriptClasspath = System.getenv("TEAMCITY_INIT_SCRIPT_CLASSPATH")
if (initScriptClasspath != null && !initScriptClasspath.trim().isEmpty()) {
logger.info "Init script classpath: ${initScriptClasspath}"
classpath files(initScriptClasspath.split(File.pathSeparator))
}
}
}
@CompileStatic
class Constants {
// Environment variable which points to the TeamCity build properties file. Is set by the agent's core logic.
public static final String BUILD_PROPERTIES_FILE_ENV_VAR = "TEAMCITY_BUILD_PROPERTIES_FILE"
// region Environment variables which are set in the Gradle runner's agent logic and are used internally by the runner's init scripts.
// Points to the TeamCity build temporary directory. Is set based on the system property provided by the agent's core logic.
public static final String BUILD_TEMP_DIR_ENV_VAR = "TEAMCITY_BUILD_TEMP_DIR"
// Points to the artifact with tests selected for parallel execution. Is set based on the system property provided by the agent's core logic.
public static final String PARALLEL_TESTS_ARTIFACT_PATH_ENV_VAR = "TEAMCITY_PARALLEL_TESTS_ARTIFACT_PATH"
// Points to the artifact with risk-based tests selected for execution. Is set based on the system property provided by the agent's core logic.
public static final String RISK_TESTS_ARTIFACT_PATH_ENV_VAR = "TEAMCITY_RISK_TESTS_ARTIFACT_PATH"
// Indicates whether test retry is supported on the build configuration level. Is set based on the build configuration's test retry setup.
public static final String SUPPORT_TEST_RETRY_ENV_VAR = "TEAMCITY_SUPPORT_TEST_RETRY"
// Supplies extra JVM arguments for Gradle test tasks. Is set by the agent's test coverage logic.
public static final String GRADLE_TEST_COVERAGE_JVM_ARGS_ENV_VAR = "TEAMCITY_GRADLE_TEST_COVERAGE_JVM_ARGS"
// Disables adding TeamCity build properties to Gradle project extra properties. Is set from the configuration parameters.
public static final String DO_NOT_POPULATE_GRADLE_PROPERTIES_ENV_VAR = "TEAMCITY_DO_NOT_POPULATE_GRADLE_PROPERTIES"
// Controls whether explicit test retry integration should inspect Gradle retry plugins. Is set from the configuration parameters.
public static final String USE_TEST_RETRY_PLUGIN_ENV_VAR = "TEAMCITY_USE_TEST_RETRY_PLUGIN"
// Selects the format used for TeamCity test names. Is set from the configuration parameters.
public static final String TEST_NAME_FORMAT_ENV_VAR = "TEAMCITY_TEST_NAME_FORMAT"
// Selects the format used for ignored TeamCity test suites. Is set from the configuration parameters.
public static final String IGNORED_SUITE_FORMAT_ENV_VAR = "TEAMCITY_IGNORE_SUITE_FORMAT_FORMAT"
// Points to the directory where TeamCity stores stacktrace logs. Is set based on a system property, the origin of which is unknown. Left for backwards compatibility for now.
public static final String STACKTRACE_LOG_DIR_ENV_VAR = "TEAMCITY_STACKTRACE_LOG_DIR"
// endregion
// region System properties not documented or used anywhere outside the runner's init scripts. Left for backwards compatibility.
// Selects the parser used for TeamCity service messages in Gradle output.
public static final String MESSAGE_PARSER_TYPE_SYSTEM_PROP = "teamcity.gradle.message.parser.type"
// Limits the size of test stdout/stderr service messages.
public static final String MESSAGE_MAX_LENGTH_SYSTEM_PROP = "teamcity.gradle.message.maxLength"
// Limits the size of stacktraces reported in test failure messages.
public static final String STACKTRACE_MAX_LENGTH_SYSTEM_PROP = "teamcity.gradle.stacktrace.maxLength"
// Controls when large test exception details are attached as artifacts instead of inline messages.
public static final String MIN_ATTACHED_TEST_EXCEPTION_SYSTEM_PROP = "teamcity.gradle.minAttachedTestException"
// Suppresses forwarding test stdout to TeamCity when enabled.
public static final String IGNORE_TEST_STDOUT_SYSTEM_PROP = "teamcity.ignoreTestStdOut"
// Suppresses forwarding test stderr to TeamCity when enabled.
public static final String IGNORE_TEST_STDERR_SYSTEM_PROP = "teamcity.ignoreTestStdErr"
// endregion
// Limits the size of internal String buffer that captures a compile task's stderr
public static final String COMPILE_ERROR_MAX_LENGTH_SYSTEM_PROP = "teamcity.gradle.compileError.maxLength"
}
// region Extension of the project's extra properties with TeamCity build properties
// This class extends the Gradle project properties (project.ext) with TeamCity build properties.
// This is quite complicated for setups that use the configuration cache, as it can lead unnecessary configuration cache invalidations.
// Possibly, a better approach would be to avoid extending project.ext altogether.
// The property extension can be kept for older Gradle versions and removed for newer versions.
@CompileStatic
class ProjectExtraPropertiesExtender {
static void extend(Project project, Logger logger) {
addTeamCityPropertiesToExtraProperties(project, logger)
}
static void logBuildPropertiesUsageWarningMessageIfNeeded(Gradle gradle, Logger logger) {
if (doNotPopulateGradleProperties() || !BuildFeatures.isConfigurationCacheRequested(gradle)) return
def warningMessage =
"\nGradle Configuration Cache is used in this build.\n" +
"Avoid accessing TeamCity build properties from the Gradle project " +
"('project.teamcity.buildProperty', 'project.buildProperty', 'teamcity.buildProperty', '\$buildProperty', etc.), " +
"as they can cause issues in Configuration Cache setups and will be removed for newer Gradle versions.\n" +
"If you experience unwarranted Configuration Cache invalidations, " +
"disable the population of the project properties with TeamCity build properties by setting " +
"the 'teamcity.internal.gradle.runner.doNotPopulateGradleProperties' configuration parameter to 'true'.\n"
logger.lifecycle(warningMessage)
}
private static void addTeamCityPropertiesToExtraProperties(Project project, Logger logger) {
if (doNotPopulateGradleProperties()) return
Properties properties = getPropertiesFromFile(logger)
if (!BuildFeatures.isConfigurationCacheRequested(project.gradle)) {
// When the configuration cache is not used,
// we can simply read the file and directly add the properties from it to the project's extra properties,
// as we did in the older init script.
for (String key : properties.stringPropertyNames()) {
project.getExtensions().getExtraProperties().set(key, properties.getProperty(key))
}
project.getExtensions().getExtraProperties().set("teamcity", properties)
} else {
// When the configuration cache is used, we add only the properties we consider non-volatile to 'project.ext'.
// All properties, including volatile ones, can be accessed through the 'project.ext.teamcity' map.
// This is done to ensure backwards compatibility with the previous integration logic (see init_since_8.gradle).
// These properties are configured as configuration inputs, so a change to any of their values will invalidate the configuration cache.
// In particular, a change to any of the non-volatile properties will invalidate the cache, even if the property is never accessed during the build.
Map<Object, Object> nonVolatileProperties = (Map<Object, Object>) project.getProviders().of(TeamCityNonVolatilePropertiesValueSource.class) {}.getOrNull()
if (nonVolatileProperties != null) {
for (Map.Entry<Object, Object> entry : nonVolatileProperties.entrySet()) {
project.getExtensions().getExtraProperties().set(entry.getKey().toString(), entry.getValue())
}
}
Map<String, String> tcExtraPropertiesMap = new TeamCityExtraPropertiesMap(project.getProviders(), properties)
project.getExtensions().getExtraProperties().set("teamcity", tcExtraPropertiesMap)
}
}
private static boolean doNotPopulateGradleProperties() {
return Boolean.valueOf(System.getenv(Constants.DO_NOT_POPULATE_GRADLE_PROPERTIES_ENV_VAR)) ||
Boolean.valueOf(System.getProperty("teamcity.internal.gradle.runner.doNotPopulateGradleProperties"))
}
private static Properties getPropertiesFromFile(Logger logger) {
try {
return PropertiesFileReader.loadPropertiesFromFile()
} catch (IOException e) {
logger.log(LogLevel.ERROR, e.getMessage(), e)
return new Properties()
}
}
}
@CompileStatic
class PropertiesFileReader {
// This method reads the file using RandomAccessFile, so that the file is not tracked as a configuration phase input.
// Thus, changes to the file do not invalidate the configuration cache.
// We need this because the properties file changes every build and we want to avoid unnecessary cache invalidations.
// This method is used in the non-configuration cache setup as well for uniformity.
static Properties loadPropertiesFromFile() throws IOException {
String filePath = System.getenv(Constants.BUILD_PROPERTIES_FILE_ENV_VAR)
if (filePath == null) return new Properties()
def debugServiceMessage = new ServiceMessage(name: "message", attributes: [text: "Reading the Teamcity build properties file '$filePath'", "tc:tags": "tc:internal"])
println(debugServiceMessage.asString())
Properties properties = new Properties()
RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "r")
try {
byte[] data = new byte[(int) randomAccessFile.length()]
randomAccessFile.readFully(data)
properties.load(new ByteArrayInputStream(data))
} finally {
randomAccessFile.close()
}
return properties
}
}
// This class filters the properties we consider "non-volatile", meaning their values do not change from build to build.
// The heuristics used are quite unreliable. They are kept only for backwards compatibility with the previous integration logic (see init_since_8.gradle).
@CompileStatic
class NonVolatileBuildPropertiesFilter {
static Map<Object, Object> filterNonVolatileProperties(Properties properties) {
return (Map<Object, Object>) properties.findAll { Object key, Object value ->
isANonVolatileProperty(key)
}
}
private static boolean isANonVolatileProperty(Object key) {
String propertyKey = key.toString()
// If the parameter is a dependency parameter, we must drop the dependency source id prefix before evaluating if the parameter is volatile.
// E.g. for 'dep.upstreamConfig.foo' we drop the ''dep.upstreamConfig' prefix before proceeding.
if (propertyKey.startsWith("dep.")) {
int propertyNameIndex = propertyKey.indexOf('.', "dep.".size()) + 1
propertyKey = propertyKey.substring(propertyNameIndex)
}
if (propertyKey.startsWith("build.")) return false
if (propertyKey.startsWith("teamcity.build.id")) return false
if (nonVolatileTeamCityPrefixes.any { String prefix -> propertyKey.startsWith(prefix) }) return true
return !propertyKey.startsWith("teamcity.")
}
private static final List<String> nonVolatileTeamCityPrefixes = [
"teamcity.build.",
"teamcity.buildConfName",
"teamcity.buildType.",
"teamcity.configuration.",
"teamcity.runner.",
"teamcity.projectName",
"teamcity.tests."
]
}
// This class acts as an unmodifiable map accessible through project.ext.teamcity.
// Because TeamCityPropertyValueSource is used to retrieve property values,
// accessing a property by get(), getOrDefault(), or containsKey() will make the configuration cache invalidate
// for a later build if the property value changes.
@CompileStatic
class TeamCityExtraPropertiesMap extends AbstractMap<String, String> {
private final ProviderFactory providerFactory
private final Properties properties
TeamCityExtraPropertiesMap(ProviderFactory providerFactory, Properties properties) {
this.providerFactory = providerFactory
this.properties = properties
}
@Override
String get(Object key) {
if (key == null) return null
return this.providerFactory.of(TeamCityPropertyValueSource.class) { ValueSourceSpec<TeamCityPropertyValueSource.Parameters> spec ->
spec.getParameters().getPropertyKey().set(key.toString())
}.getOrNull()
}
@Override
String getOrDefault(Object key, String defaultValue) {
String value = get(key)
if (value != null) {
return value
}
return defaultValue
}
@Override
boolean containsKey(Object key) {
return get(key) != null
}
@Override
Set<Entry<String, String>> entrySet() {
Set<Entry<String, String>> entries = new LinkedHashSet<>()
for (String key : properties.stringPropertyNames()) {
entries.add(new AbstractMap.SimpleImmutableEntry<String, String>(key, get(key)))
}
return Collections.unmodifiableSet(entries)
}
}
// ValueSource used to track accessed build properties as configuration phase inputs.
@CompileStatic
abstract class TeamCityPropertyValueSource implements ValueSource<String, Parameters>, Describable {
interface Parameters extends ValueSourceParameters {
Property<String> getPropertyKey()
}
@Override
String obtain() {
try {
Properties properties = PropertiesFileReader.loadPropertiesFromFile()
return properties.getProperty(parameters.propertyKey.get())
} catch (IOException ignored) {
return null
}
}
@Override
String getDisplayName() {
return "TeamCity build property '${parameters.propertyKey.get()}'".toString()
}
}
// ValueSource used to track all non-volatile build properties as a single configuration phase input.
// Used to populate 'project.ext' without re-reading the file for each property.
@CompileStatic
abstract class TeamCityNonVolatilePropertiesValueSource implements ValueSource<Map<Object, Object>, ValueSourceParameters.None>, Describable {
@Override
Map<Object, Object> obtain() {
try {
Properties properties = PropertiesFileReader.loadPropertiesFromFile()
return NonVolatileBuildPropertiesFilter.filterNonVolatileProperties(properties)
} catch (IOException ignored) {
return null
}
}
@Override
String getDisplayName() {
return "TeamCity build properties collection"
}
}
// endregion
@CompileStatic
class ServiceMessage {
static final Set<String> IGNORED_TAGS = new HashSet<String>(Arrays.asList('tc:tags', 'flowId', 'parent'))
static final String ESCAPE_CHARACTER = '|'
static final String START_MESSAGE = '##teamcity['
static final String END_MESSAGE = ']'
String name
Map<String, String> attributes
ServiceMessage() {
}
ServiceMessage(String name, Map<String, String> attributes) {
this.name = name
this.attributes = attributes
}
ServiceMessage withFlowId(String flowId) {
if (attributes == null) {
attributes = new LinkedHashMap<String, String>()
}
attributes.put('flowId', flowId)
return this
}
String asString() {
StringBuilder msg = new StringBuilder(START_MESSAGE).append(name)
if (attributes == null || attributes.isEmpty()) {
} else if (attributes.containsKey('')) {
msg = appendAttribute(msg, null, attributes.get(''))
} else {
String tags = attributes.get('tc:tags')
if (tags != null) msg = appendAttribute(msg, 'tc:tags', tags)
String flowId = attributes.get('flowId')
String parent = attributes.get('parent')
for (Map.Entry<String, String> attr : attributes.entrySet()) {
if (!IGNORED_TAGS.contains(attr.key)) msg = appendAttribute(msg, attr.key, attr.value)
}
if (flowId != null) msg = appendAttribute(msg, 'flowId', flowId)
if (parent != null) msg = appendAttribute(msg, 'parent', parent)
}
return msg.append(END_MESSAGE).toString()
}
private static StringBuilder appendAttribute(StringBuilder msg, String key, Object value) {
String strValue = null
if (value instanceof String) {
strValue = (String) value
} else {
strValue = value != null ? value.toString() : null
}
if (key == null) {
msg.append(' \'')
} else {
msg.append(' ').append(key).append('=\'')
}
if (strValue != null) {
for (int i = 0; i < strValue.length(); i++) {
char ch = strValue.charAt(i)
switch (ch) {
case '\n': msg.append(ESCAPE_CHARACTER).append('n'); break
case '\r': msg.append(ESCAPE_CHARACTER).append('r'); break
case '|': msg.append(ESCAPE_CHARACTER).append('|'); break
case '\'': msg.append(ESCAPE_CHARACTER).append('\''); break
case '[': msg.append(ESCAPE_CHARACTER).append('['); break
case ']': msg.append(ESCAPE_CHARACTER).append(']'); break
default:
if (ch < 128) {
msg.append(ch)
} else {
msg.append(ESCAPE_CHARACTER).append('0').append('x')
String hex = Integer.toHexString((int)ch)
for (int z = hex.length(); z < 4; z++) {
msg.append('0')
}
msg.append(hex)
}
break
}
}
}
return msg.append('\'')
}
}
@CompileStatic
class BuildFeatures {
static boolean isConfigurationCacheRequested(Gradle gradle) {
try {
org.gradle.api.configuration.BuildFeatures bf = ((org.gradle.api.internal.GradleInternal) gradle).getServices().get(org.gradle.api.configuration.BuildFeatures)
return bf.getConfigurationCache().getRequested().getOrElse(false)
} catch (Throwable ignored) {
return false
}
}
static boolean areIsolatedProjectsEnabled(Gradle gradle) {
try {
org.gradle.api.configuration.BuildFeatures bf = ((org.gradle.api.internal.GradleInternal) gradle).getServices().get(org.gradle.api.configuration.BuildFeatures)
return bf.getIsolatedProjects().getActive().getOrElse(false)
} catch (Throwable ignored) {
return false
}
}
}
@CompileStatic
class Util {
static String getFlowIdForTask(String taskPath) {
String flowIfPrefix = "%%teamcity%%-"
return flowIfPrefix + String.valueOf(taskPath.hashCode())
}
}
// region Setup of build log presentation for compile tasks stderr
/**
* Traverses failure exceptions from failed compile tasks and reports them as compilation error messages,
* creating a "compilation error" build problem in TeamCity without requiring task standard error listeners.
*/
@CompileStatic
abstract class CompileTaskErrorOutputService implements BuildService<BuildServiceParameters.None>, OperationCompletionListener, AutoCloseable {
private static final Logger logger = Logging.getLogger(CompileTaskErrorOutputService)
@Override
void onFinish(FinishEvent finishEvent) {
if (!(finishEvent instanceof TaskFinishEvent)) return
TaskFinishEvent taskFinishEvent = (TaskFinishEvent) finishEvent
if (!(taskFinishEvent.getResult() instanceof TaskFailureResult)) return
String taskPath = taskFinishEvent.getDescriptor().getTaskPath()
if (!isCompileTaskPath(taskPath)) return
TaskFailureResult failureResult = (TaskFailureResult) taskFinishEvent.getResult()
List<String> messages = extractDiagnosticMessages(failureResult.getFailures())
if (messages.isEmpty()) {
messages = Collections.singletonList("Compilation failed for task '" + taskPath + "'. See raw build log for details.")
}
String flowId = Util.getFlowIdForTask(taskPath)
logger.lifecycle(new ServiceMessage(name: "compilationStarted", attributes: [compiler: taskPath]).withFlowId(flowId).asString())
for (String msg : messages) {
logger.lifecycle(new ServiceMessage(name: "message", attributes: [text: msg, status: "ERROR"]).withFlowId(flowId).asString())
}
logger.lifecycle(new ServiceMessage(name: "compilationFinished", attributes: [compiler: taskPath]).withFlowId(flowId).asString())
}
private static boolean isCompileTaskPath(String taskPath) {
if (taskPath == null) return false
int lastColon = taskPath.lastIndexOf(":")
String taskName = lastColon >= 0 ? taskPath.substring(lastColon + 1) : taskPath
return taskName.startsWith("compile") || taskName.contains("Compile")
}
private static List<String> extractDiagnosticMessages(List<? extends Failure> failures) {
if (failures == null || failures.isEmpty()) return Collections.emptyList()
List<String> messages = new ArrayList<>()
Set<String> seen = new HashSet<>()
collectFailureMessages(failures, messages, seen)
return messages
}
private static void collectFailureMessages(List<? extends Failure> failures, List<String> messages, Set<String> seen) {
if (failures == null) return
for (Failure failure : failures) {
if (failure == null) continue
String msg = failure.getMessage()
if (msg != null && !msg.trim().isEmpty()) {
for (String line : msg.split("\r?\n")) {
String trimmed = line.trim()
if (!trimmed.isEmpty() && seen.add(trimmed)) {
messages.add(line)
}
}
}
String desc = failure.getDescription()
if (desc != null && !desc.trim().isEmpty()) {
for (String line : desc.split("\r?\n")) {
String trimmed = line.trim()
if (!trimmed.isEmpty() && isRelevantDiagnosticLine(trimmed) && seen.add(trimmed)) {
messages.add(line)
}
}
}
collectFailureMessages(failure.getCauses(), messages, seen)
}
}
private static boolean isRelevantDiagnosticLine(String line) {
if (line.startsWith("at ") || line.startsWith("\tat ")) return false
if (line.startsWith("Caused by:")) return false
if (line.startsWith("org.gradle.") || line.startsWith("java.lang.")) return false
if (line.startsWith("...") || line.startsWith("\t...")) return false
return true
}
@Override
void close() {
}
}
/**
* Registers {@link CompileTaskErrorOutputService} to receive task completion events.
*/
@CompileStatic
abstract class CompileTaskErrorOutputPlugin implements Plugin<Gradle> {
@Inject
abstract BuildEventsListenerRegistry getBuildEventsListenerRegistry()
@Override
void apply(Gradle gradle) {
if (gradle.parent != null) return
Provider<CompileTaskErrorOutputService> compileTaskErrorOutputServiceProvider =
gradle.getSharedServices().registerIfAbsent("compileTaskErrorOutputService", CompileTaskErrorOutputService) {}
buildEventsListenerRegistry.onTaskCompletion(compileTaskErrorOutputServiceProvider)
}
}
// endregion
// region Test reporting, test suite reporting, test retry
/**
* Converts Gradle test lifecycle and output callbacks into TeamCity service messages,
* including suite/test events, test stdout and stderr, and failure metadata.
*/
@CompileStatic
class TeamcityTestListener implements TestListener, TestOutputListener {
public boolean skipStdErr = false
public boolean skipStdOut = false
private final Logger logger
private final BuildDirectoriesResolver buildDirectoriesResolver
private final AtomicLong testCounter
private final TestNameDescriptor testNameDescriptor
private final Pattern ignoredSuiteFormat
private final FlowWrapper flowWrapper
private final ConcurrentMap<TestDescriptor, List<TestOutputEvent>> testOutputs = new ConcurrentHashMap<TestDescriptor, List<TestOutputEvent>>()
TeamcityTestListener(Logger logger, TestTaskWrapper testTaskWrapper, Project project, AtomicLong testCounter, TestNameDescriptor testNameDescriptor, String ignoredSuiteFormat) {
this.logger = logger
this.buildDirectoriesResolver = new BuildDirectoriesResolver(project)
this.testCounter = testCounter
this.testNameDescriptor = testNameDescriptor
this.ignoredSuiteFormat = ignoredSuiteFormat != null && ignoredSuiteFormat.trim().length() > 0 ? Pattern.compile(ignoredSuiteFormat) : null
this.flowWrapper = new FlowWrapper(Util.getFlowIdForTask(testTaskWrapper.getTaskPath()))
}
@Override
void beforeSuite(TestDescriptor testDescriptor) {
SuiteDescriptorWrapper suiteDescriptorWrapper = new SuiteDescriptorWrapper(testDescriptor, flowWrapper, ignoredSuiteFormat)
suiteDescriptorWrapper.start { ServiceMessage flowMessage, ServiceMessage suiteStartedMessage ->
if (flowMessage != null) this.logger.lifecycle(flowMessage.asString())
this.logger.lifecycle(suiteStartedMessage.asString())
}
}
@Override
void afterSuite(TestDescriptor testDescriptor, TestResult testResult) {
SuiteDescriptorWrapper suiteDescriptorWrapper = new SuiteDescriptorWrapper(testDescriptor, flowWrapper, ignoredSuiteFormat)
suiteDescriptorWrapper.finish { ServiceMessage flowMessage, ServiceMessage suiteFinishedMessage ->
TestDescriptorWrapper testDescriptorWrapper = new TestDescriptorWrapper(testDescriptor, testNameDescriptor, flowWrapper, buildDirectoriesResolver, ignoredSuiteFormat)
List<TestOutputEvent> outputs = testOutputs.remove(testDescriptor)
if (outputs != null) {
synchronized (outputs) {
testDescriptorWrapper.flush(outputs) { ServiceMessage testOutputServiceMessage -> this.logger.lifecycle(testOutputServiceMessage.asString()) }
outputs.clear()
}
}
this.logger.lifecycle(suiteFinishedMessage.asString())
if (flowMessage != null) this.logger.lifecycle(flowMessage.asString())
}
if (testDescriptor.getParent() == null) {
for (Map.Entry<TestDescriptor, List<TestOutputEvent>> entry : testOutputs.entrySet()) {
TestDescriptor descriptor = entry.getKey()
List<TestOutputEvent> outputs = entry.getValue()
synchronized (outputs) {
TestDescriptorWrapper testDescriptorWrapper = new TestDescriptorWrapper(descriptor, testNameDescriptor, flowWrapper, buildDirectoriesResolver, ignoredSuiteFormat)
testDescriptorWrapper.flush(outputs) { ServiceMessage testOutputServiceMessage -> this.logger.lifecycle(testOutputServiceMessage.asString()) }
outputs.clear()
}
}
testOutputs.clear()
}
}
@Override
void beforeTest(TestDescriptor testDescriptor) {
TestDescriptorWrapper testDescriptorWrapper = new TestDescriptorWrapper(testDescriptor, testNameDescriptor, flowWrapper, buildDirectoriesResolver, ignoredSuiteFormat)
testDescriptorWrapper.start { ServiceMessage flowMessage, ServiceMessage testStartedMessage ->
if (flowMessage != null) this.logger.lifecycle(flowMessage.asString())
this.logger.lifecycle(testStartedMessage.asString())
}
}
@Override
void afterTest(TestDescriptor testDescriptor, TestResult testResult) {
TestDescriptorWrapper testDescriptorWrapper = new TestDescriptorWrapper(testDescriptor, testNameDescriptor, flowWrapper, buildDirectoriesResolver, ignoredSuiteFormat)
testDescriptorWrapper.finish(testResult) { ServiceMessage flowMessage, ServiceMessage testFinished ->
List<TestOutputEvent> outputs = testOutputs.remove(testDescriptor)
if (outputs != null) {
synchronized (outputs) {
testDescriptorWrapper.flush(outputs) { ServiceMessage testOutputServiceMessage -> this.logger.lifecycle(testOutputServiceMessage.asString()) }
outputs.clear()
}
}
testDescriptorWrapper.result(testResult, testCounter) { List<ServiceMessage> messages ->
for (ServiceMessage message : messages) {
this.logger.lifecycle(message.asString())
}
}
this.logger.lifecycle(testFinished.asString())
if (flowMessage != null) this.logger.lifecycle(flowMessage.asString())
}
}
@Override
void onOutput(TestDescriptor testDescriptor, TestOutputEvent outputEvent) {
if (testDescriptor.getClassName() == null) {
return
}
switch (outputEvent.getDestination()) {
case TestOutputEvent.Destination.StdErr:
if (skipStdErr) return
break
case TestOutputEvent.Destination.StdOut:
if (skipStdOut) return
break
default: return
}
List<TestOutputEvent> outputs = testOutputs.get(testDescriptor)
if (outputs == null) {
outputs = new ArrayList<TestOutputEvent>()
List<TestOutputEvent> prev = testOutputs.putIfAbsent(testDescriptor, outputs)
if (prev != null) {
outputs = prev
}
}
synchronized (outputs) {
outputs.add(outputEvent)
if (outputs.size() > 100) {
TestDescriptorWrapper testDescriptorWrapper = new TestDescriptorWrapper(testDescriptor, testNameDescriptor, flowWrapper, buildDirectoriesResolver, ignoredSuiteFormat)
testDescriptorWrapper.flush(outputs) { ServiceMessage msg -> this.logger.lifecycle(msg.asString()) }
outputs.clear()
}
}
}
}
@CompileStatic
class BuildDirectoriesResolver implements Serializable {
private String buildStacktraceLogDir
private final String buildTempDir
private final File buildDir
BuildDirectoriesResolver(Project project) {
this(project.getLayout().getBuildDirectory().getAsFile().get())
}
BuildDirectoriesResolver(File buildDir) {
this.buildStacktraceLogDir = System.getenv(Constants.STACKTRACE_LOG_DIR_ENV_VAR)
this.buildTempDir = System.getenv(Constants.BUILD_TEMP_DIR_ENV_VAR)
this.buildDir = buildDir
}
String getBuildStacktraceLogDir() {
return buildStacktraceLogDir
}
void setBuildStacktraceLogDir(String buildStacktraceLogDir) {
this.buildStacktraceLogDir = buildStacktraceLogDir
}
String getBuildTempDir() {
return buildTempDir
}
File getBuildDir() {
return buildDir
}
}
public enum TestNameDescriptor {
DEFAULT {
String getTestName(TestDescriptorWrapper descriptor) {
return DISPLAY_NAME.getTestName(descriptor)
}
},
NAME {
String getTestName(TestDescriptorWrapper descriptor) {
return super.getTestName(descriptor)
}
},
DISPLAY_NAME {
String getTestName(TestDescriptorWrapper descriptor) {
String methodName = getMethodName(descriptor.name)
if (descriptor.displayName.startsWith(methodName)) {
methodName = descriptor.displayName
} else {
methodName = "$methodName(${descriptor.displayName})"
}
if (methodName == "${descriptor.name}()") {
methodName = descriptor.name
}
return "${descriptor.className}.$methodName"
}
private final List<Character> BRACKETS = [(char)'(', (char)'[', (char)'{', (char)'<']
private String getMethodName(String name) {
for (int i = 0; i < name.length(); i++) {
if (name.charAt(i) in BRACKETS) return name.substring(0, i)
}
return name
}
}
public String getTestName(TestDescriptorWrapper descriptor) {
"${descriptor.className}.${descriptor.name}"
}
static TestNameDescriptor of(String param) {
if (param == null) return DEFAULT
switch (param) {
case "name": return NAME
case "displayName": return DISPLAY_NAME
default: return DEFAULT
}
}
}
@CompileStatic
public class FlowWrapper {
private final String project
private Set<String> started = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>())
FlowWrapper(String projectFlowId) {
this.project = projectFlowId
}
ServiceMessage start(DescriptorWrapper descriptor, SuiteDescriptorWrapper suite) {
return started.add(descriptor.flowId) ? new ServiceMessage(name: "flowStarted",
attributes: [flowId: descriptor.flowId, parent: suite != null ? suite.flowId : id]) : null
}
ServiceMessage finish(DescriptorWrapper descriptor) {
return started.remove(descriptor.flowId) ? new ServiceMessage(name: "flowFinished",
attributes: [flowId: descriptor.flowId]) : null
}
String getId() {
return project
}
}
@CompileStatic
public class SuiteDescriptorWrapper extends DescriptorWrapper {
private static final Pattern partition = Pattern.compile("^Partition \\d+ .*")
SuiteDescriptorWrapper(TestDescriptor descriptor, FlowWrapper flow, Pattern ignore) {
super(descriptor, flow, ignore)
}
void start(Closure action) {
if (ignored) return
ServiceMessage suiteStartedMessage = new ServiceMessage(name: "testSuiteStarted", attributes: [name: descriptor.name, flowId: flowId])
action(flow.start(this, parent), suiteStartedMessage)
}
void finish(Closure action) {
if (ignored) return
ServiceMessage suiteFinishedMessage = new ServiceMessage(name: "testSuiteFinished", attributes: [name: descriptor.name, flowId: flowId])
action(flow.finish(this), suiteFinishedMessage)
}
boolean isIgnored() {
if (descriptor.parent == null) return true
if (descriptor.name.startsWith("Gradle Test Executor")) return true
if (descriptor.name.startsWith("Gradle Test Run")) return true
if (ignoreSuite != null) {
return ignoreSuite.matcher(descriptor.name).matches()
} else {
return descriptor.name.startsWith("Partition ") && partition.matcher(descriptor.name).matches()
}
}
}
@CompileStatic
public class TestDescriptorWrapper extends DescriptorWrapper {
private final TestNameDescriptor testNameDescriptor
private final BuildDirectoriesResolver buildDirectoriesResolver
private static final int MAX_MESSAGE_SIZE = Integer.getInteger(Constants.MESSAGE_MAX_LENGTH_SYSTEM_PROP, 18 * 1024)
private static final String OUTPUT_PARSER = System.getProperty(Constants.MESSAGE_PARSER_TYPE_SYSTEM_PROP, "ServiceMessage")
TestDescriptorWrapper(TestDescriptor descriptor, TestNameDescriptor testNameDescriptor, FlowWrapper flow, BuildDirectoriesResolver buildDirectoriesResolver, Pattern ignoreSuite) {
super(descriptor, flow, ignoreSuite)
this.testNameDescriptor = testNameDescriptor
this.buildDirectoriesResolver = buildDirectoriesResolver
}
void start(Closure action) {
ServiceMessage testStartedMessage = new ServiceMessage(name: "testStarted", attributes: [name: testName, flowId: flowId])
action(flow.start(this, parent), testStartedMessage)
}
void finish(TestResult result, Closure action) {
ServiceMessage testFinishedMessage = new ServiceMessage(name: "testFinished",
attributes: [name : testName,
duration: String.valueOf((int)(result.endTime - result.startTime)),
flowId : flowId])
action(flow.finish(this), testFinishedMessage)
}
boolean isIgnored() {
return className == null
}
void result(TestResult result, AtomicLong testCounter, Closure action) {
ResultType resultType = result != null ? result.getResultType() : ResultType.SUCCESS
switch (resultType) {
case ResultType.FAILURE:
WrappedException exception = new WrappedException(result.getException())
List<ServiceMessage> messages = attacheBigFailTestMessage(exception, testCounter)
ServiceMessage failure = exception.getServiceMessage(testName).withFlowId(flowId)
List<ServiceMessage> allMessages = new ArrayList<ServiceMessage>(messages)
allMessages.add(failure)
action(allMessages)
break
case ResultType.SKIPPED:
ServiceMessage skipped = new ServiceMessage(name: "testIgnored",
attributes: [name : testName,
message: "",
flowId : flowId])
action(Collections.singletonList(skipped))
break
}
}
void flush(List<TestOutputEvent> outputs, Closure action) {
if (outputs == null || outputs.isEmpty()) return
StringBuilder stdout = new StringBuilder()
StringBuilder stderr = new StringBuilder()
synchronized (outputs) {
for (TestOutputEvent oe : outputs) {
switch (oe.destination) {
case TestOutputEvent.Destination.StdErr:
stderr.append(oe.message)
break
case TestOutputEvent.Destination.StdOut:
stdout.append(oe.message)
break
}
}
if (stdout.length() > 0) {
flushOutputMessage("testStdOut", trimTrailingNewline(stdout.toString()), action)
}
if (stderr.length() > 0) {
flushOutputMessage("testStdErr", trimTrailingNewline(stderr.toString()), action)
}
}
}
private static String trimTrailingNewline(String text) {
if (text.endsWith("\r\n")) {
return text.substring(0, text.length() - 2)
} else if (text.endsWith("\n") || text.endsWith("\r")) {
return text.substring(0, text.length() - 1)
}
return text
}
private void flushOutputMessage(String type, String message, Closure action) {
int offset = 0
int length = message.length()
while (offset < length) {
String msg
switch (OUTPUT_PARSER) {
case "disabled":
msg = getMessageWithoutParsing(message, offset, MAX_MESSAGE_SIZE)
break
case "ServiceMessage":
default:
msg = getMessageWithParsing(message, offset, MAX_MESSAGE_SIZE)
break
}
offset += msg.length()
action(new ServiceMessage(name: type, attributes: [name : testName,
out : msg,
"tc:tags": (OUTPUT_PARSER == "disabled" ? null : "tc:parseServiceMessagesInside"),
flowId : flowId]))
}
}
private static String getMessageWithoutParsing(String message, int offset, int maxLength) {
int end = Math.min(offset + maxLength, message.length())
return message.substring(offset, end)
}
private static String getMessageWithParsing(String message, int offset, int maxLength) {
int remaining = message.length() - offset
if (remaining <= maxLength) return message.substring(offset)
int maxEnd = offset + maxLength
// Find inner service messages and try not to split each message
int start = message.indexOf(ServiceMessage.START_MESSAGE, offset)
if (start == -1 || start > maxEnd) return message.substring(offset, maxEnd)
int end = getEndMessage(message, start)
if (end == -1) return message.substring(offset, maxEnd)
if (end > maxEnd) return message.substring(offset, start > offset ? start : maxEnd)
while (end < maxEnd) {
int newStart = message.indexOf(ServiceMessage.START_MESSAGE, end)
if (newStart == -1 || newStart >= maxEnd) return message.substring(offset, maxEnd)
int newEnd = getEndMessage(message, newStart)
if (newEnd == -1) return message.substring(offset, maxEnd)
if (newEnd >= maxEnd) return message.substring(offset, newStart)
end = newEnd
}
return message.substring(offset, end)
}
private static int getEndMessage(String msg, int position) {
char lastChar = 0
while (position < msg.length()) {
char currentChar = msg.charAt(position)
if (lastChar != ServiceMessage.ESCAPE_CHARACTER.charAt(0) && currentChar == ServiceMessage.END_MESSAGE.charAt(0)) return position
else if (lastChar == ServiceMessage.ESCAPE_CHARACTER.charAt(0) && currentChar == ServiceMessage.ESCAPE_CHARACTER.charAt(0)) lastChar = 0
else lastChar = currentChar
position++
}
return -1
}
private String getTestName() {
testNameDescriptor != null ? testNameDescriptor.getTestName(this) : TestNameDescriptor.DEFAULT.getTestName(this)
}
String getClassName() {
return descriptor.getClassName()
}
String getName() {
return descriptor.getName()
}
String getClassDisplayName() {
return descriptor.getClassName()
}
String getDisplayName() {
return descriptor.getDisplayName() != null ? descriptor.getDisplayName() : descriptor.getName()
}
private List<ServiceMessage> attacheBigFailTestMessage(WrappedException exception, AtomicLong testCounter) {
if(exception.small) return []
File artifactDir = createBigStacktracesDir()
if (artifactDir == null) {
return []
}
String fileName = "${testCounter.incrementAndGet()}.log"
File artifactPath = new File(artifactDir, fileName)
PrintStream logFilePrint
try {
artifactPath.delete()
logFilePrint = new PrintStream(new FileOutputStream(artifactPath))
exception.th.printStackTrace(logFilePrint)
} finally {
logFilePrint?.close()
}
String artifactName = ".teamcity/gradle/${artifactDir.name}"
def artifact = new ServiceMessage(name: "publishArtifacts", attributes: ["": "${artifactPath.canonicalPath} => ${artifactName}" as String, flowId: flowId])
ServiceMessage metadata = new ServiceMessage(name: "testMetadata",
attributes: [testName: testName,
type : 'artifact',
value : "${artifactName}/${fileName}" as String,
name : 'teamcity.test.output',
flowId : flowId])
return [artifact, metadata]
}
synchronized private File createBigStacktracesDir() {
final String path = buildDirectoriesResolver.getBuildStacktraceLogDir()
if (path != null && !path.isEmpty()) return new File(path)
final String rootTmpDirectory = buildDirectoriesResolver.getBuildTempDir() ?: buildDirectoriesResolver.getBuildDir().path
File resultDir = new File("${rootTmpDirectory}${File.separator}.teamcity${File.separator}testFailMessages${File.separator}${System.nanoTime()}")
if (resultDir.mkdirs()) {
buildDirectoriesResolver.setBuildStacktraceLogDir(resultDir.path)
return resultDir
}
return null
}
@Override
boolean equals(Object obj) {
return obj != null &&
obj.class == TestDescriptorWrapper.class &&
descriptor.equals(((TestDescriptorWrapper)obj).descriptor)
}
@Override
int hashCode() {
return descriptor.hashCode()
}
}
@CompileStatic
class DescriptorWrapper {
protected final TestDescriptor descriptor
protected final FlowWrapper flow
protected final Pattern ignoreSuite
DescriptorWrapper(TestDescriptor descriptor, FlowWrapper flow, Pattern ignoreSuite) {
this.descriptor = descriptor
this.flow = flow
this.ignoreSuite = ignoreSuite
}
protected SuiteDescriptorWrapper getParent() {
TestDescriptor parent = descriptor.parent
if (parent != null) {
SuiteDescriptorWrapper suite = new SuiteDescriptorWrapper(parent, flow, ignoreSuite)
return suite.ignored ? null : suite
}
return null
}
protected String getFlowId() {
return (flow.id + "-" + descriptor.hashCode()).toString()
}
}
// endregion
// region Build problem reporting
@CompileStatic
abstract class TeamcityBuildProblemReportingPlugin implements Plugin<Gradle> {
@Inject
abstract FlowScope getFlowScope()
@Inject
abstract FlowProviders getFlowProviders()
void apply(Gradle gradle) {
if (gradle.parent != null) return
getFlowScope().always(TeamcityBuildProblemAction) { FlowActionSpec<TeamcityBuildProblemAction.Parameters> spec ->
spec.getParameters().getBuildResult().set(getFlowProviders().getBuildWorkResult())
}
}
}
/**
* Reports the build failure as a build problem, if eligible.
*/
@CompileStatic
abstract class TeamcityBuildProblemAction implements FlowAction<Parameters> {
interface Parameters extends FlowParameters {
@Input
Property<BuildWorkResult> getBuildResult()
}
@Override
void execute(Parameters parameters) {
BuildWorkResult buildWorkResult = parameters.getBuildResult().get()
if (!buildWorkResult.getFailure().isPresent()) return
Throwable failure = getRootCauseOfFailure(buildWorkResult)
String message = getMessageForFailure(failure)
if (message.contains("There were failing tests") || message.contains("Compilation failed") || message.contains("Compilation error")) {
// do not report tests and compilation failures as build problems, as they are logged separately
return
}
ServiceMessage serviceMessage = new ServiceMessage(
name: "buildProblem",
attributes: [type : "gradleBuildProblem",
identity : ("gradleBuildProblem " + message).hashCode().toString(),
description: message
])
println(serviceMessage.asString())
}
private static Throwable getRootCauseOfFailure(BuildWorkResult buildWorkResult) {
Throwable failure = buildWorkResult.getFailure().get()
while (!(failure instanceof TaskExecutionException) && failure != failure.getCause() && failure.getCause() != null) {
failure = failure.getCause()
}
return failure
}
private static String getMessageForFailure(Throwable failure) {
if (failure instanceof TaskExecutionException && failure.getCause() != null) {
return failure.getMessage() + " " + failure.getCause().getClass().getName() + ": " + failure.getCause().getMessage()
}
return failure.getClass().getName() + ": " + failure.getMessage()
}
}
// endregion
/**
* Use WrappedException if you need to make a error message shorter
*/
@CompileStatic
class WrappedException extends Exception {
private static final int MAX_MESSAGE_SIZE = Integer.getInteger(Constants.STACKTRACE_MAX_LENGTH_SYSTEM_PROP, 18 * 1024)
private static final int MIN_ATTACHED_MESSAGE_SIZE = Integer.getInteger(Constants.MIN_ATTACHED_TEST_EXCEPTION_SYSTEM_PROP, MAX_MESSAGE_SIZE)
final Throwable th
private final int maxLength
private final String DELIMITER
private final String message
private final String thMessage
/**
* @param th original exception
*/
WrappedException(Throwable th) {
this(th, MAX_MESSAGE_SIZE)
}
/**
* @param th original exception
* @param length max message length
*/
WrappedException(Throwable th, int maxLength) {
this.th = th != null ? th : new Exception() {
@Override void printStackTrace(PrintWriter printWriter) { }
@Override void printStackTrace(PrintStream ps) { }
@Override void printStackTrace() { }
@Override Throwable fillInStackTrace() { this }
@Override StackTraceElement[] getStackTrace() { new StackTraceElement[0] }
@Override String toString() { "" }
}
this.maxLength = maxLength
int size = (int) (maxLength / 1024)
DELIMITER = "***** A part of the stacktrace was cut by TeamCity build agent because the stacktrace size exceeded ${size} KB *****"
thMessage = this.th.toString()
message = trim(thMessage, maxLength)
}
private int getSize() {
return thMessage.length()
}
@Override
public String toString() {
return message
}
public boolean isSmall() {
if (MIN_ATTACHED_MESSAGE_SIZE == -1) return true
if (MIN_ATTACHED_MESSAGE_SIZE > getSize()) return true
if (getSize() <= maxLength) return true
return false
}
public ServiceMessage getServiceMessage(String testName) {
if (thMessage.length() == 0) return new ServiceMessage(name: "testFailed", attributes: [name: testName, message: message, details: getStacktrace()])
Map.Entry<String, String> cfd = extractComparisonFailure(th)
if (cfd != null) {
return new ServiceMessage(name: "testFailed", attributes: [name : testName,
message : message,
details : getStacktrace(),
actual : trim(cfd.key, maxLength),
expected: trim(cfd.value, maxLength)])
} else {
return new ServiceMessage(name: "testFailed", attributes: [name: testName, message: message, details: getStacktrace()])
}
}
private static String getFieldValue(Throwable th, String fieldName) {
try {
Field field = th.getClass().getDeclaredField(fieldName)
field.setAccessible(true)
Object val = field.get(th)
return val != null ? val.toString() : null
} catch (Throwable ignored) {
return null
}
}
private Map.Entry<String, String> extractComparisonFailure(Throwable th) {
Map.Entry<String, String> comparisonFailure = extractComparisonFailureWithComparisonFailureUtil(th)
if (comparisonFailure != null) return comparisonFailure
comparisonFailure = extractOpenTest4jFailure(th)
if (comparisonFailure != null) return comparisonFailure
String actual = getFieldValue(th, 'fActual')
String expected = getFieldValue(th, 'fExpected')
if (actual != null && expected != null) return new AbstractMap.SimpleEntry<String, String>(actual, expected)
String message = th.getLocalizedMessage()
if (message == null) return null
Map.Entry<String, String> res = parseComparison(message, "expected:<", "> but was:<", ">") // JUnit 4 (no spaces after ':' or 'was:')
if (res != null) return res
res = parseComparison(message, "expected: <", "> but was: <", ">") // JUnit 5 / OpenTest4j (spaces after ':' and 'was:')
if (res != null) return res
return parseComparison(message, "expected [", "] but found [", "]") // TestNG
}
private Map.Entry<String, String> extractOpenTest4jFailure(Throwable th) {
try {
Method getExpected = th.getClass().getMethod("getExpected")
Method getActual = th.getClass().getMethod("getActual")
Object expectedObj = getExpected.invoke(th)
Object actualObj = getActual.invoke(th)
if (expectedObj != null && actualObj != null) {
Method getValueExpected = expectedObj.getClass().getMethod("getValue")
Method getValueActual = actualObj.getClass().getMethod("getValue")
Object expectedVal = getValueExpected.invoke(expectedObj)
Object actualVal = getValueActual.invoke(actualObj)
if (expectedVal != null && actualVal != null) {
return new AbstractMap.SimpleEntry<String, String>(actualVal.toString(), expectedVal.toString())
}
}
} catch (Throwable ignored) { }
return null
}
private Map.Entry<String, String> extractComparisonFailureWithComparisonFailureUtil(Throwable th) {
try {
Class<?> utilClass = Class.forName('jetbrains.buildServer.ComparisonFailureUtil')
Method method = utilClass.getMethod("extractComparisonFailure", Throwable)
Object cfd = method.invoke(null, th)
if (cfd != null) {
Method getActual = cfd.getClass().getMethod("getActual")
Method getExpected = cfd.getClass().getMethod("getExpected")
Object actual = getActual.invoke(cfd)
Object expected = getExpected.invoke(cfd)
if (actual != null && expected != null) {
return new AbstractMap.SimpleEntry<String, String>(actual.toString(), expected.toString())
}
}
} catch (Throwable ignored) { }
return null
}
private Map.Entry<String, String> parseComparison(String msg, String start, String separator, String end) {
int expectedIdx = msg.indexOf(start)
if (expectedIdx >= 0) {
int butWasIdx = msg.indexOf(separator, expectedIdx + start.length())
if (butWasIdx >= 0) {
int endIdx = msg.lastIndexOf(end)
if (endIdx >= 0 && endIdx > butWasIdx) {
String expected = msg.substring(expectedIdx + start.length(), butWasIdx)
String actual = msg.substring(butWasIdx + separator.length(), endIdx)
return new AbstractMap.SimpleEntry<String, String>(actual, expected)
}
}
}
return null
}
private String getStacktrace() {
ByteArrayOutputStream out = new ByteArrayOutputStream()
th.printStackTrace(new PrintStream(out))
return out.toString()
}
/**
* Print StackTrace from the delegate object but a message from current current
*/
@Override
void printStackTrace(final PrintStream s) {
Throwable exc = this;
PrintStream stream = new PrintStream(s) {
@Override
void println(final Object obj) {
super.println(th.is(obj) ? exc : obj)
}
}
th.printStackTrace(stream)
}
/**
* Split the message into 2 parts, if its size is large
* @param str original message
* @param length max length
* @return original message if str.length < length or split message with delimiter
*/
private String trim(String str, int maxLength) {
if (str == null || str.length() <= maxLength) return str
return """${str.substring(0, (int)(maxLength / 2))} ...
${DELIMITER}
... ${str.substring(str.length() - (int)(maxLength / 2))}"""
}
}
@CompileStatic
class TestTaskWrapper {
private final AbstractTestTask testTask
/**
* Use {@link TestTaskWrapper#forAllTestTasks} to instantiate this object correctly.
*/
private TestTaskWrapper(AbstractTestTask testTask) { this.testTask = testTask }
void addTestListener(TestListener listener) { testTask.addTestListener(listener) }
void addTestOutputListener(TestOutputListener listener) { testTask.addTestOutputListener(listener) }
String getTaskPath() { testTask.getPath() }
TestTaskWrapper jvmArgs(String[] arguments) {
if (testTask instanceof Test && arguments != null && arguments.length > 0) {
((Test) testTask).jvmArgumentProviders.add(new TestTaskJvmArgumentsProvider(arguments))
}
return this
}
TestTaskWrapper setTmpDirectory(String path) {
if (testTask instanceof Test && path != null && path.length() > 0) {
((Test) testTask).systemProperty("java.io.tmpdir", path)
}
return this
}
TestFilterWrapper getFilter() {
if (testTask instanceof Test) {
return new TestFilterWrapper(((Test) testTask).getFilter())
}
return new TestFilterWrapper(null)
}
/**
* Creates an instance of the {@link TestTaskWrapper} for every instance of the Test in the TaskCollection.
* The Closure action is applied lazily via configureEach
*/
static void forAllTestTasks(TaskCollection<AbstractTestTask> taskCollection, Closure action) {
// Use lazy task configuration:
// https://docs.gradle.org/current/userguide/task_configuration_avoidance.html
taskCollection.configureEach { AbstractTestTask task ->
TestTaskWrapper wrapper = new TestTaskWrapper(task)
action.call(wrapper)
}
}
}
@CompileStatic
class TestTaskJvmArgumentsProvider implements org.gradle.process.CommandLineArgumentProvider {
private final List<String> arguments
TestTaskJvmArgumentsProvider(String[] arguments) {
this.arguments = Arrays.asList(arguments)
}
@Override
Iterable<String> asArguments() {
return arguments
}
}
@CompileStatic
class TestFilterWrapper {
final TestFilter filter
TestFilterWrapper(TestFilter filter) {
this.filter = filter
}
void setFailOnNoMatchingTests(boolean failOnNoMatchingTests) {
if (filter != null) {
filter.setFailOnNoMatchingTests(failOnNoMatchingTests)
}
}
void excludeTest(String className, String methodName) {
if (filter != null && className != null && className.trim().length() > 0) {
filter.excludeTest(className, methodName)
}
}
boolean isSupported() {
return filter != null
}
void updateFilters(String fileName, Logger logger) {
if (fileName == null || fileName.trim().length() == 0) {
return
}
File file = new File(fileName)
if (!file.exists()) {
return
}
if (!isSupported()) {
return
}
setFailOnNoMatchingTests(false)
file.eachLine { String line ->
if (!line.startsWith("#")) excludeTest(line, null)
}
}
}
apply plugin: CompileTaskErrorOutputPlugin
apply plugin: TeamcityBuildProblemReportingPlugin
ProjectExtraPropertiesExtender.logBuildPropertiesUsageWarningMessageIfNeeded(gradle, logger)
final AtomicLong testCounter = new AtomicLong(0)
gradle.lifecycle.beforeProject { Project project ->
ProjectExtraPropertiesExtender.extend(project, logger)
// init jvm args
final String jvmargs = System.getenv(Constants.GRADLE_TEST_COVERAGE_JVM_ARGS_ENV_VAR)
final String[] arguments = jvmargs == null || jvmargs.isEmpty() ? new String[0] : jvmargs.split("\n")
TestTaskWrapper.forAllTestTasks(project.tasks.withType(AbstractTestTask.class)) { TestTaskWrapper testTask ->
def buildTempDirectory = System.getenv(Constants.BUILD_TEMP_DIR_ENV_VAR)
testTask.setTmpDirectory(buildTempDirectory).jvmArgs(arguments)
def testNameFormat = System.getenv(Constants.TEST_NAME_FORMAT_ENV_VAR)
def testNameDescriptor = TestNameDescriptor.of(testNameFormat)
def ignoredSuiteFormat = System.getenv(Constants.IGNORED_SUITE_FORMAT_ENV_VAR)
def testListener = new TeamcityTestListener(logger, testTask, project, testCounter, testNameDescriptor, ignoredSuiteFormat)
testTask.addTestListener(testListener)
testTask.addTestOutputListener(testListener)
testListener.skipStdOut = Boolean.valueOf(System.getProperty(Constants.IGNORE_TEST_STDOUT_SYSTEM_PROP))
testListener.skipStdErr = Boolean.valueOf(System.getProperty(Constants.IGNORE_TEST_STDERR_SYSTEM_PROP))
// add executable tests
testTask.filter.updateFilters(System.getenv(Constants.PARALLEL_TESTS_ARTIFACT_PATH_ENV_VAR), logger)
// will be empty if parallel tests enabled
testTask.filter.updateFilters(System.getenv(Constants.RISK_TESTS_ARTIFACT_PATH_ENV_VAR), logger)
}
}