From fb0819d490346118bf0a8287836200b9c76ba639 Mon Sep 17 00:00:00 2001 From: Alexander Dudinsky Date: Mon, 21 Mar 2022 15:35:05 +0000 Subject: [PATCH] Implement `OsCondition` test annotation With this annotation we can configure specific tests for running on different platforms on TC and locally. `supportedOn` shows the platform where the test should pass. `enabledOnCI` configures on which platform test should be run on CI. --- .../kotlin/gradle/testbase/GradleTest.java | 1 + .../kotlin/gradle/testbase/osConditions.kt | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/osConditions.kt diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/java/org/jetbrains/kotlin/gradle/testbase/GradleTest.java b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/java/org/jetbrains/kotlin/gradle/testbase/GradleTest.java index 9cf42674f87..f44dd266c59 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/java/org/jetbrains/kotlin/gradle/testbase/GradleTest.java +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/java/org/jetbrains/kotlin/gradle/testbase/GradleTest.java @@ -17,6 +17,7 @@ import java.lang.annotation.Target; // Workaround for https://youtrack.jetbrains.com/issue/IDEA-265284 @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) +@OsCondition @GradleTestVersions @ParameterizedTest(name = "{0}: {displayName}") @ArgumentsSource(GradleArgumentsProvider.class) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/osConditions.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/osConditions.kt new file mode 100644 index 00000000000..d168b811b2b --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/osConditions.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.gradle.testbase + +import org.junit.jupiter.api.condition.OS +import org.junit.jupiter.api.extension.ConditionEvaluationResult +import org.junit.jupiter.api.extension.ExecutionCondition +import org.junit.jupiter.api.extension.ExtendWith +import org.junit.jupiter.api.extension.ExtensionContext +import org.junit.platform.commons.util.AnnotationUtils + +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@ExtendWith(ExecutionOnOsCondition::class) +annotation class OsCondition( + val supportedOn: Array = [OS.LINUX, OS.MAC, OS.WINDOWS], + val enabledOnCI: Array = [OS.LINUX, OS.WINDOWS] +) + +internal class ExecutionOnOsCondition : ExecutionCondition { + private val isUnderTeamcity = System.getenv("TEAMCITY_VERSION") != null + private val disabledByDefault = ConditionEvaluationResult.disabled("@OsCondition is not present") + + private val enabledOnCurrentOs = "Enabled on operating system: " + System.getProperty("os.name") + private val notSupportedOnCurrentOs = "Test is not supported on operating system: " + System.getProperty("os.name") + private val disabledForCI = "Disabled for operating system: " + System.getProperty("os.name") + " on CI" + + override fun evaluateExecutionCondition(context: ExtensionContext): ConditionEvaluationResult { + val annotation = AnnotationUtils.findAnnotation(context.element, OsCondition::class.java) + if (annotation.isPresent) { + val supportedOn = annotation.get().supportedOn + val enabledOnCI = annotation.get().enabledOnCI + + return if (!supportedOn.any { it.isCurrentOs }) + ConditionEvaluationResult.disabled(notSupportedOnCurrentOs) + else if (isUnderTeamcity && !enabledOnCI.any { it.isCurrentOs }) + ConditionEvaluationResult.disabled(disabledForCI) + else ConditionEvaluationResult.enabled(enabledOnCurrentOs) + } + + return disabledByDefault + } +} \ No newline at end of file