diff options
author | mvicsokolova <82594708+mvicsokolova@users.noreply.github.com> | 2022-02-07 16:15:19 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-07 16:15:19 +0300 |
commit | 25bf2042a04f9681c39a3d2a504e82f640672b64 (patch) | |
tree | 7fe9a2991f87e3db89d57dbcdeddb922b640d9bb | |
parent | fd1131433f120b024c0e8e95b3d6fcc96863dbfe (diff) | |
download | kotlinx.atomicfu-25bf2042a04f9681c39a3d2a504e82f640672b64.tar.gz |
New atomicfu-gradle-plugin tests (#217)
24 files changed, 560 insertions, 470 deletions
diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/BaseKotlinGradleTest.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/BaseKotlinGradleTest.kt deleted file mode 100644 index b81a0c9..0000000 --- a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/BaseKotlinGradleTest.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.atomicfu.plugin.gradle - -import org.gradle.internal.impldep.com.google.common.io.Files -import org.junit.After -import org.junit.Before -import java.io.File - -abstract class BaseKotlinGradleTest { - private lateinit var workingDir: File - - fun project(name: String, suffix: String = "", fn: Project.() -> Unit) { - workingDir = File("build${File.separator}test-$name$suffix").absoluteFile - workingDir.deleteRecursively() - workingDir.mkdirs() - val testResources = File("src/test/resources") - val originalProjectDir = testResources.resolve("projects/$name").apply { checkExists() } - val projectDir = workingDir.resolve(name).apply { mkdirs() } - originalProjectDir.listFiles().forEach { it.copyRecursively(projectDir.resolve(it.name)) } - - // Add an empty setting.gradle - projectDir.resolve("settings.gradle").writeText("// this file is intentionally left empty") - - Project(projectDir = projectDir).fn() - } -}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/EmptyProjectTest.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/EmptyProjectTest.kt deleted file mode 100644 index b542f2b..0000000 --- a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/EmptyProjectTest.kt +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.atomicfu.plugin.gradle - -import org.junit.Test - -class EmptyProjectTest : BaseKotlinGradleTest() { - @Test - fun testEmpty() = project("empty") { - build("build") {} - } -} diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/JsProjectTest.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/JsProjectTest.kt deleted file mode 100644 index 72c1157..0000000 --- a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/JsProjectTest.kt +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.atomicfu.plugin.gradle - -import org.gradle.testkit.runner.TaskOutcome -import org.junit.* -import java.io.File - -class JsProjectTest : BaseKotlinGradleTest() { - @Ignore - @Test - fun testKotlin2JsPlugin() = project("js-simple") { - val tasksToCheck = arrayOf( - ":compileKotlin2Js", - ":compileTestKotlin2Js", - ":transformAtomicfuJsFiles", - ":transformTestAtomicfuJsFiles" - ) - - build("build") { - checkOutcomes(TaskOutcome.SUCCESS, *tasksToCheck) - - val testCompileClasspathFiles = projectDir.resolve("build/test_compile_classpath.txt") - .readLines().asSequence().flatMap { File(it).walk().filter { it.isFile } }.toHashSet() - - projectDir.resolve("build/classes/kotlin/main/js-simple.js").let { - it.checkExists() - check(it in testCompileClasspathFiles) { "Original '$it' is missing from test compile classpath" } - // todo: check test runtime classpath when js test tasks are supported in plugin - } - - projectDir.resolve("build/classes/atomicfu/main/js-simple.js").let { - it.checkExists() - check(it !in testCompileClasspathFiles) { "Transformed '$it' is present in test compile classpath" } - } - } - - build("build") { - checkOutcomes(TaskOutcome.UP_TO_DATE, *tasksToCheck) - } - } -} diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/JvmProjectTest.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/JvmProjectTest.kt deleted file mode 100644 index e017f05..0000000 --- a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/JvmProjectTest.kt +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.atomicfu.plugin.gradle - -import org.gradle.testkit.runner.TaskOutcome -import org.junit.Test -import java.io.File - -class JvmProjectTest : BaseKotlinGradleTest() { - @Test - fun testKotlinPlugin() = - project("jvm-simple") { - doSimpleTest() - } - - @Test - fun testKotlinPlatformJvmPlugin() = - project("jvm-simple", "-platform") { - projectDir.resolve("build.gradle").modify { - it.checkedReplace("apply plugin: 'kotlin'", "apply plugin: 'kotlin-platform-jvm'") - } - doSimpleTest() - } - - private fun Project.doSimpleTest() { - val tasksToCheck = arrayOf( - ":compileKotlin", - ":compileTestKotlin", - ":transformAtomicfuClasses", - ":transformTestAtomicfuClasses" - ) - - build("build") { - checkOutcomes(TaskOutcome.SUCCESS, *tasksToCheck) - - val testCompileClasspathFiles = filesFrom("build/test_compile_classpath.txt") - val testRuntimeClasspathFiles = filesFrom("build/test_runtime_classpath.txt") - - projectDir.resolve("build/classes/kotlin/main/IntArithmetic.class").let { - it.checkExists() - check(it in testCompileClasspathFiles) { "Original '$it' is missing from test compile classpath" } - check(it in testRuntimeClasspathFiles) { "Original '$it' is missing from test runtime classpath" } - } - - projectDir.resolve("build/classes/atomicfu/main/IntArithmetic.class").let { - it.checkExists() - check(it !in testCompileClasspathFiles) { "Transformed '$it' is present in test compile classpath" } - check(it !in testRuntimeClasspathFiles) { "Transformed '$it' is present in test runtime classpath" } - } - } - - build("build") { - checkOutcomes(TaskOutcome.UP_TO_DATE, *tasksToCheck) - } - } - - private fun Project.filesFrom(name: String) = projectDir.resolve(name) - .readLines().asSequence().flatMap { listFiles(it) }.toHashSet() - - private fun listFiles(dir: String): Sequence<File> = File(dir).walk().filter { it.isFile } -} diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/MppProjectTest.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/MppProjectTest.kt deleted file mode 100644 index 1e87645..0000000 --- a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/MppProjectTest.kt +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.atomicfu.plugin.gradle - -import org.gradle.testkit.runner.TaskOutcome -import org.junit.* -import java.io.File - -class MppProjectTest : BaseKotlinGradleTest() { - @Ignore - @Test - fun testKotlinMultiplatformPlugin() = project("mpp-simple") { - val tasksToCheck = arrayOf( - ":compileKotlinJvm", - ":compileTestKotlinJvm", - ":transformJvmMainAtomicfu", - ":transformJvmTestAtomicfu", - ":compileKotlinJs", - ":transformJsMainAtomicfu" - ) - - build("build") { - checkOutcomes(TaskOutcome.SUCCESS, *tasksToCheck) - - fun checkPlatform(platform: String, fileInMainName: String) { - val isJs = platform == "js" - val testCompileClasspathFiles = projectDir.resolve("build/classpath/$platform/test_compile.txt") - .readLines().asSequence().flatMapTo(HashSet()) { File(it).walk().filter { it.isFile } } - val testRuntimeClasspathFiles = if (isJs) emptySet<File>() else projectDir.resolve("build/classpath/$platform/test_runtime.txt") - .readLines().asSequence().flatMapTo(HashSet()) { File(it).walk().filter { it.isFile } } - - projectDir.resolve("build/classes/kotlin/$platform/main/$fileInMainName").let { - it.checkExists() - check(it in testCompileClasspathFiles) { "Original '$it' is missing from $platform test compile classpath" } - if (!isJs) check(it in testRuntimeClasspathFiles) { "Original '$it' is missing from $platform test runtime classpath" } - } - - projectDir.resolve("build/classes/atomicfu/jvm/main/IntArithmetic.class").let { - it.checkExists() - check(it !in testCompileClasspathFiles) { "Transformed '$it' is present in $platform test compile classpath" } - if (!isJs) check(it !in testRuntimeClasspathFiles) { "Transformed '$it' is present in $platform test runtime classpath" } - } - - } - - checkPlatform("jvm", "IntArithmetic.class") - checkPlatform("js", "mpp-simple.js") - } - - build("build") { - checkOutcomes(TaskOutcome.UP_TO_DATE, *tasksToCheck) - } - } -} diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/Project.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/Project.kt deleted file mode 100644 index f2e06a9..0000000 --- a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/Project.kt +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.atomicfu.plugin.gradle - -import org.gradle.testkit.runner.BuildResult -import org.gradle.testkit.runner.GradleRunner -import java.io.File - -class Project(val projectDir: File) { - init { - projectDir.resolve("build.gradle").modify { - buildScript + "\n\n" + it - } - } - - private var isDebug = false - private var printStdout = false - - @Deprecated("Should be used for debug only!") - @Suppress("unused") - fun debug() { - isDebug = true - } - - /** - * Redirects Gradle runner output to stdout. Useful for debugging. - */ - @Deprecated("Should be used for debug only!") - @Suppress("unused") - fun printStdout() { - printStdout = true - } - - fun gradle(vararg tasks: String): GradleRunner = - GradleRunner.create() - .withDebug(isDebug) - .withProjectDir(projectDir) - .withArguments(*(defaultArguments() + tasks)) - .run { - if (printStdout) { - forwardStdOutput(System.out.bufferedWriter()) - } else { - this - } - } - - fun build(vararg tasks: String, fn: BuildResult.() -> Unit = {}) { - val gradle = gradle(*tasks) - val buildResult = gradle.build() - buildResult.fn() - } - - @Suppress("unused") - fun buildAndFail(vararg tasks: String, fn: BuildResult.() -> Unit = {}) { - val gradle = gradle(*tasks) - val buildResult = gradle.buildAndFail() - buildResult.fn() - } - - private fun defaultArguments(): Array<String> = - arrayOf("--stacktrace") - - companion object { - private fun readFileList(fileName: String): String { - val resource = Project::class.java.classLoader.getResource(fileName) - ?: throw IllegalStateException("Could not find resource '$fileName'") - val files = File(resource.toURI()) - .readLines() - .map { File(it).absolutePath.replace("\\", "\\\\") } // escape backslashes in Windows paths - return files.joinToString(", ") { "'$it'" } - } - - private val buildScript = run { - """ - buildscript { - dependencies { - classpath files(${readFileList("plugin-classpath.txt")}) - } - } - - repositories { - jcenter() - mavenCentral() - maven { url 'https://cache-redirector.jetbrains.com/maven.pkg.jetbrains.space/kotlin/p/kotlin/dev' } - maven { url 'https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev' } - maven { url "https://oss.sonatype.org/content/repositories/snapshots" } - } - - def atomicfuJvm = files(${readFileList("atomicfu-jvm.txt")}) - def atomicfuJs = files(${readFileList("atomicfu-js.txt")}) - def atomicfuMetadata = files(${readFileList("atomicfu-metadata.txt")}) - """.trimIndent() - } - } -} diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/internal/Assert.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/internal/Assert.kt new file mode 100644 index 0000000..f55e38a --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/internal/Assert.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2016-2022 JetBrains s.r.o. + * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. + */ + +package kotlinx.atomicfu.plugin.gradle.internal + +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.TaskOutcome +import kotlin.test.assertEquals + +/** + * Helper `fun` for asserting a [TaskOutcome] to be equal to [TaskOutcome.SUCCESS] + */ +internal fun BuildResult.assertTaskSuccess(task: String) { + assertTaskOutcome(TaskOutcome.SUCCESS, task) +} + +/** + * Helper `fun` for asserting a [TaskOutcome] to be equal to [TaskOutcome.FAILED] + */ +internal fun BuildResult.assertTaskFailure(task: String) { + assertTaskOutcome(TaskOutcome.FAILED, task) +} + +internal fun BuildResult.assertTaskUpToDate(task: String) { + assertTaskOutcome(TaskOutcome.UP_TO_DATE, task) +} + +private fun BuildResult.assertTaskOutcome(taskOutcome: TaskOutcome, taskName: String) { + assertEquals(taskOutcome, task(taskName)?.outcome) +} diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/internal/TestDsl.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/internal/TestDsl.kt new file mode 100644 index 0000000..2a51f1b --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/internal/TestDsl.kt @@ -0,0 +1,131 @@ +/* + * Copyright 2016-2022 JetBrains s.r.o. + * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. + */ + +package kotlinx.atomicfu.plugin.gradle.internal + +import kotlinx.atomicfu.plugin.gradle.test.* +import org.gradle.testkit.runner.* +import java.io.* + +internal fun BaseKotlinGradleTest.test(fn: BaseKotlinScope.() -> Unit): GradleRunner { + val baseKotlinScope = BaseKotlinScope() + fn(baseKotlinScope) + + baseKotlinScope.files.forEach { scope -> + val fileWriteTo = rootProjectDir.resolve(scope.filePath) + .apply { + parentFile.mkdirs() + createNewFile() + } + + scope.files.forEach { + val fileContent = readFileList(it) + fileWriteTo.appendText(fileContent) + } + } + + return GradleRunner.create() + .withProjectDir(rootProjectDir) + .withArguments(baseKotlinScope.runner.arguments) + .withPluginClasspath() + .addPluginTestRuntimeClasspath() +} + +/** + * same as [file][FileContainer.file], but prepends "src/${sourceSet}/kotlin" before given `classFileName` + */ +internal fun FileContainer.kotlin(classFileName: String, sourceSet: String = "main", fn: AppendableScope.() -> Unit) { + require(classFileName.endsWith(".kt")) { + "ClassFileName must end with '.kt'" + } + + val fileName = "src/${sourceSet}/kotlin/$classFileName" + file(fileName, fn) +} + +/** + * Shortcut for creating a `build.gradle.kts` by using [file][FileContainer.file] + */ +internal fun FileContainer.buildGradleKts(fn: AppendableScope.() -> Unit) { + val fileName = "build.gradle.kts" + file(fileName, fn) +} + +/** + * Shortcut for creating a `settings.gradle.kts` by using [file][FileContainer.file] + */ +internal fun FileContainer.settingsGradleKts(fn: AppendableScope.() -> Unit) { + val fileName = "settings.gradle.kts" + file(fileName, fn) +} + +/** + * Declares a directory with the given [dirName] inside the current container. + * All calls creating files within this scope will create the files nested in this directory. + * + * Note that it is valid to call this method multiple times at the same level with the same [dirName]. + * Files declared within 2 independent calls to [dir] will be added to the same directory. + */ +internal fun FileContainer.dir(dirName: String, fn: DirectoryScope.() -> Unit) { + DirectoryScope(dirName, this).fn() +} + +internal fun BaseKotlinScope.runner(fn: Runner.() -> Unit) { + val runner = Runner() + fn(runner) + + this.runner = runner +} + +internal fun AppendableScope.resolve(fileName: String) { + this.files.add(fileName) +} + +internal interface FileContainer { + fun file(fileName: String, fn: AppendableScope.() -> Unit) +} + +internal class BaseKotlinScope : FileContainer { + var files: MutableList<AppendableScope> = mutableListOf() + var runner: Runner = Runner() + + override fun file(fileName: String, fn: AppendableScope.() -> Unit) { + val appendableScope = AppendableScope(fileName) + fn(appendableScope) + files.add(appendableScope) + } +} + +internal class DirectoryScope( + val dirPath: String, + val parent: FileContainer +): FileContainer { + + override fun file(fileName: String, fn: AppendableScope.() -> Unit) { + parent.file("$dirPath/$fileName", fn) + } +} + +internal class AppendableScope(val filePath: String) { + val files: MutableList<String> = mutableListOf() +} + +internal class Runner { + val arguments: MutableList<String> = mutableListOf() +} + +internal fun readFileList(fileName: String): String = + getFile(fileName).readText() + +internal fun getFile(fileName: String): File { + val resource = BaseKotlinGradleTest::class.java.classLoader.getResource(fileName) + ?: throw IllegalStateException("Could not find resource '$fileName'") + return File(resource.toURI()) +} + +internal fun GradleRunner.addPluginTestRuntimeClasspath() = apply { + val pluginClasspath = getFile("plugin-classpath.txt").readLines().map { File(it) } + withPluginClasspath(pluginClasspath) +}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/internal/utils.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/internal/utils.kt new file mode 100644 index 0000000..49856f2 --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/internal/utils.kt @@ -0,0 +1,13 @@ +package kotlinx.atomicfu.plugin.gradle.internal + +import java.io.* +import kotlin.test.* + +fun File.checkExists() { + assertTrue(exists(), "File does not exist: $canonicalPath") +} + +fun File.filesFrom(relative: String) = resolve(relative) + .readLines().asSequence().flatMap { listFiles(it) }.toHashSet() + +fun listFiles(dir: String): Sequence<File> = File(dir).walk().filter { it.isFile }
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/BaseKotlinGradleTest.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/BaseKotlinGradleTest.kt new file mode 100644 index 0000000..f6ec8ef --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/BaseKotlinGradleTest.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.atomicfu.plugin.gradle.test + +import kotlinx.atomicfu.plugin.gradle.internal.* +import java.io.File + +open class BaseKotlinGradleTest(private val projectName: String) { + internal val rootProjectDir: File + + init { + rootProjectDir = File("build${File.separator}test-$projectName").absoluteFile + rootProjectDir.deleteRecursively() + rootProjectDir.mkdirs() + } + + fun checkJvmCompilationClasspath(originalClassFile: String, transformedClassFile: String) { + // check that test compilation depends on transformed main sources + val testCompileClasspathFiles = rootProjectDir.filesFrom("build/test_compile_jvm_classpath.txt") + val testRuntimeClasspathFiles = rootProjectDir.filesFrom("build/test_runtime_jvm_classpath.txt") + + rootProjectDir.resolve(transformedClassFile).let { + it.checkExists() + check(it in testCompileClasspathFiles) { "Original '$it' is missing from test compile classpath" } + check(it in testRuntimeClasspathFiles) { "Original '$it' is missing from test runtime classpath" } + } + + rootProjectDir.resolve(originalClassFile).let { + it.checkExists() + check(it !in testCompileClasspathFiles) { "Transformed '$it' is present in test compile classpath" } + check(it !in testRuntimeClasspathFiles) { "Transformed '$it' is present in test runtime classpath" } + } + } + + fun checkJsCompilationClasspath() { + // check that test compilation depends on transformed main sources + val testCompileClasspathFiles = rootProjectDir.filesFrom("build/test_compile_js_classpath.txt") + + rootProjectDir.resolve("build/classes/atomicfu/js/main/$projectName.js").let { + it.checkExists() + check(it in testCompileClasspathFiles) { "Original '$it' is missing from test compile classpath" } + } + } +}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/JsProjectTest.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/JsProjectTest.kt new file mode 100644 index 0000000..8630f70 --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/JsProjectTest.kt @@ -0,0 +1,52 @@ +package kotlinx.atomicfu.plugin.gradle.test + +import kotlinx.atomicfu.plugin.gradle.internal.* +import kotlinx.atomicfu.plugin.gradle.internal.BaseKotlinScope +import org.junit.Test + +class JsProjectTest : BaseKotlinGradleTest("js-simple") { + private fun BaseKotlinScope.createProject() { + buildGradleKts { + resolve("projects/js-simple/js-simple.gradle.kts") + } + settingsGradleKts { + resolve("projects/js-simple/settings.gradle.kts") + } + dir("src/main/kotlin") {} + kotlin("IntArithmetic.kt", "main") { + resolve("projects/js-simple/src/main/kotlin/IntArithmetic.kt") + } + dir("src/test/kotlin") {} + kotlin("ArithmeticTest.kt", "test") { + resolve("projects/js-simple/src/test/kotlin/ArithmeticTest.kt") + } + } + + @Test + fun testPlugin() { + val runner = test { + createProject() + runner { + arguments.add(":build") + } + } + val tasksToCheck = arrayOf( + ":compileKotlinJs", + ":transformJsMainAtomicfu", + ":compileTestKotlinJs", + ":transformJsTestAtomicfu" + ) + runner.build().apply { + tasksToCheck.forEach { + assertTaskSuccess(it) + } + } + // check that task outcomes are cached for the second build + runner.build().apply { + tasksToCheck.forEach { + assertTaskUpToDate(it) + } + } + checkJsCompilationClasspath() + } +}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/JvmProjectTest.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/JvmProjectTest.kt new file mode 100644 index 0000000..838d091 --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/JvmProjectTest.kt @@ -0,0 +1,56 @@ +package kotlinx.atomicfu.plugin.gradle.test + +import kotlinx.atomicfu.plugin.gradle.internal.* +import kotlinx.atomicfu.plugin.gradle.internal.BaseKotlinScope +import org.junit.Test + +class JvmProjectTest : BaseKotlinGradleTest("jvm-simple") { + + private fun BaseKotlinScope.createProject() { + buildGradleKts { + resolve("projects/jvm-simple/jvm-simple.gradle.kts") + } + settingsGradleKts { + resolve("projects/jvm-simple/settings.gradle.kts") + } + dir("src/main/kotlin") {} + kotlin("IntArithmetic.kt", "main") { + resolve("projects/jvm-simple/src/main/kotlin/IntArithmetic.kt") + } + dir("src/test/kotlin") {} + kotlin("ArithmeticTest.kt", "test") { + resolve("projects/jvm-simple/src/test/kotlin/ArithmeticTest.kt") + } + } + + @Test + fun testPlugin() { + val runner = test { + createProject() + runner { + arguments.add(":build") + } + } + val tasksToCheck = arrayOf( + ":compileKotlin", + ":transformAtomicfuClasses", + ":compileTestKotlin", + ":transformTestAtomicfuClasses" + ) + runner.build().apply { + tasksToCheck.forEach { + assertTaskSuccess(it) + } + } + // check that task outcomes are cached for the second build + runner.build().apply { + tasksToCheck.forEach { + assertTaskUpToDate(it) + } + } + checkJvmCompilationClasspath( + originalClassFile = "build/classes/kotlin/main/IntArithmetic.class", + transformedClassFile = "build/classes/atomicfu/main/IntArithmetic.class" + ) + } +}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/MppProjectTest.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/MppProjectTest.kt new file mode 100644 index 0000000..cbeefa2 --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/test/MppProjectTest.kt @@ -0,0 +1,58 @@ +package kotlinx.atomicfu.plugin.gradle.test + +import kotlinx.atomicfu.plugin.gradle.internal.* +import org.junit.* + +class MppProjectTest : BaseKotlinGradleTest("mpp-simple") { + private fun BaseKotlinScope.createProject() { + buildGradleKts { + resolve("projects/mpp-simple/mpp-simple.gradle.kts") + } + settingsGradleKts { + resolve("projects/mpp-simple/settings.gradle.kts") + } + dir("src/commonMain/kotlin") {} + kotlin("IntArithmetic.kt", "commonMain") { + resolve("projects/mpp-simple/src/commonMain/kotlin/IntArithmetic.kt") + } + dir("src/commonTest/kotlin") {} + kotlin("ArithmeticTest.kt", "commonTest") { + resolve("projects/mpp-simple/src/commonTest/kotlin/ArithmeticTest.kt") + } + } + + @Test + fun testPlugin() { + val runner = test { + createProject() + runner { + arguments.add(":build") + } + } + val tasksToCheck = arrayOf( + ":compileKotlinJvm", + ":compileTestKotlinJvm", + ":transformJvmMainAtomicfu", + ":transformJvmTestAtomicfu", + ":compileKotlinJs", + ":transformJsMainAtomicfu" + ) + runner.build().apply { + tasksToCheck.forEach { + assertTaskSuccess(it) + } + } + // check that task outcomes are cached for the second build + runner.build().apply { + tasksToCheck.forEach { + assertTaskUpToDate(it) + } + } + + checkJvmCompilationClasspath( + originalClassFile = "build/classes/kotlin/jvm/main/IntArithmetic.class", + transformedClassFile = "build/classes/atomicfu/jvm/main/IntArithmetic.class" + ) + checkJsCompilationClasspath() + } +}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/utils.kt b/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/utils.kt deleted file mode 100644 index 2a8d0f7..0000000 --- a/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/utils.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.atomicfu.plugin.gradle - -import org.gradle.testkit.runner.BuildResult -import org.gradle.testkit.runner.TaskOutcome -import java.io.File -import kotlin.test.assertTrue - -fun BuildResult.checkOutcomes(expected: TaskOutcome, vararg tasks: String) { - val unexpectedOutcomes = tasks - .map { it to task(it)?.outcome } - .filter { (_, outcome) -> outcome != expected } - if (unexpectedOutcomes.isNotEmpty()) { - throw AssertionError("Unexpected outcomes for tasks." + - "\nExpected: $expected." + - "\nGot:" + - "\n${unexpectedOutcomes.joinToString("\n") { (task, outcome) -> "* $task -> $outcome" }}") - - } -} - -fun File.checkExists() { - assertTrue(exists(), "File does not exist: $canonicalPath") -} - -fun File.modify(fn: (String) -> String) { - writeText(fn(readText())) -} - -fun String.checkedReplace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String { - check(contains(oldValue, ignoreCase)) { "String must contain '$oldValue'" } - return replace(oldValue, newValue, ignoreCase) -}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/empty/build.gradle b/atomicfu-gradle-plugin/src/test/resources/projects/empty/build.gradle deleted file mode 100644 index f4add41..0000000 --- a/atomicfu-gradle-plugin/src/test/resources/projects/empty/build.gradle +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -apply plugin: 'kotlinx-atomicfu' -apply plugin: 'base' - -repositories { - maven { url "https://oss.sonatype.org/content/repositories/snapshots" } -}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/js-simple/build.gradle b/atomicfu-gradle-plugin/src/test/resources/projects/js-simple/build.gradle deleted file mode 100644 index 31dbec6..0000000 --- a/atomicfu-gradle-plugin/src/test/resources/projects/js-simple/build.gradle +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -apply plugin: 'kotlinx-atomicfu' -apply plugin: 'kotlin2js' - -dependencies { - compileOnly atomicfuJs - testRuntime atomicfuJs - - compile 'org.jetbrains.kotlin:kotlin-stdlib-js' - testCompile 'org.jetbrains.kotlin:kotlin-test-js' -} - -compileTestKotlin2Js.doLast { - file("$buildDir/test_compile_classpath.txt").text = classpath.join("\n") -}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/js-simple/js-simple.gradle.kts b/atomicfu-gradle-plugin/src/test/resources/projects/js-simple/js-simple.gradle.kts new file mode 100644 index 0000000..a284a87 --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/resources/projects/js-simple/js-simple.gradle.kts @@ -0,0 +1,38 @@ +import kotlinx.atomicfu.plugin.gradle.* + +buildscript { + dependencies { + classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.17.0") + } +} + +plugins { + kotlin("js") version "1.6.0" +} + +apply(plugin = "kotlinx-atomicfu") + +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + implementation(kotlin("stdlib-js")) + implementation(kotlin("test-junit")) + implementation("org.jetbrains.kotlin:kotlin-test-js") +} + +kotlin { + js { + nodejs() + } + + tasks.named("compileTestKotlinJs") { + doLast { + file("$buildDir/test_compile_js_classpath.txt").writeText( + target.compilations["test"].compileDependencyFiles.joinToString("\n") + ) + } + } +}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/js-simple/settings.gradle.kts b/atomicfu-gradle-plugin/src/test/resources/projects/js-simple/settings.gradle.kts new file mode 100644 index 0000000..bd39e74 --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/resources/projects/js-simple/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "js-simple"
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/build.gradle b/atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/build.gradle deleted file mode 100644 index 7e21215..0000000 --- a/atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/build.gradle +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -apply plugin: 'kotlinx-atomicfu' -apply plugin: 'kotlin' - -// This flag is enabled to be able using JVM IR compiled dependencies (when build is ran with -Penable_jvm_ir) -kotlin.target.compilations.all { - kotlinOptions.freeCompilerArgs += '-Xallow-jvm-ir-dependencies' -} - -dependencies { - compileOnly atomicfuJvm - testRuntime atomicfuJvm - - compile 'org.jetbrains.kotlin:kotlin-stdlib' - - testCompile 'org.jetbrains.kotlin:kotlin-test' - testCompile 'org.jetbrains.kotlin:kotlin-test-junit' - testCompile 'junit:junit:4.12' -} - -compileTestKotlin.doLast { - file("$buildDir/test_compile_classpath.txt").text = classpath.join("\n") -} - -test.doLast { - file("$buildDir/test_runtime_classpath.txt").text = classpath.join("\n") -}
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/jvm-simple.gradle.kts b/atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/jvm-simple.gradle.kts new file mode 100644 index 0000000..fa1f4d8 --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/jvm-simple.gradle.kts @@ -0,0 +1,41 @@ +import org.gradle.api.tasks.compile.* +import org.jetbrains.kotlin.gradle.plugin.* + +buildscript { + dependencies { + classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.17.0") + } +} + +plugins { + kotlin("jvm") version "1.6.0" +} + +apply(plugin = "kotlinx-atomicfu") + +repositories { + mavenCentral() +} + +dependencies { + implementation(kotlin("stdlib")) + implementation(kotlin("test-junit")) +} + +kotlin { + tasks.compileTestKotlin { + doLast { + file("$buildDir/test_compile_jvm_classpath.txt").writeText( + target.compilations["test"].compileDependencyFiles.joinToString("\n") + ) + } + } + + tasks.test { + doLast { + file("$buildDir/test_runtime_jvm_classpath.txt").writeText( + (target.compilations["test"] as KotlinCompilationToRunnableFiles<*>).runtimeDependencyFiles.joinToString("\n") + ) + } + } +} diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/settings.gradle.kts b/atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/settings.gradle.kts new file mode 100644 index 0000000..2f5327f --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "jvm-simple"
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/build.gradle b/atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/build.gradle deleted file mode 100644 index fc95366..0000000 --- a/atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/build.gradle +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -apply plugin: 'kotlinx-atomicfu' -apply plugin: 'kotlin-multiplatform' - -kotlin { - // This flag is enabled to be able using JVM IR compiled dependencies (when build is ran with -Penable_jvm_ir) - jvm() { - compilations.all { - kotlinOptions.freeCompilerArgs += '-Xallow-jvm-ir-dependencies' - } - } - js() - - sourceSets { - commonMain.dependencies { - implementation 'org.jetbrains.kotlin:kotlin-stdlib-common' - compileOnly atomicfuMetadata - - } - commonTest.dependencies { - implementation 'org.jetbrains.kotlin:kotlin-test-common' - implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common' - runtimeOnly atomicfuMetadata - - } - jsMain.dependencies { - implementation 'org.jetbrains.kotlin:kotlin-stdlib-js' - compileOnly atomicfuJs - - } - jsTest.dependencies { - implementation 'org.jetbrains.kotlin:kotlin-test-js' - runtimeOnly atomicfuJs - } - jvmMain.dependencies { - implementation 'org.jetbrains.kotlin:kotlin-stdlib' - compileOnly atomicfuJvm - } - jvmTest.dependencies { - implementation 'org.jetbrains.kotlin:kotlin-test' - implementation 'org.jetbrains.kotlin:kotlin-test-junit' - implementation "junit:junit:4.12" - runtimeOnly atomicfuJvm - } - } -} - -def File classpathFile(String platform, String fileName) { - def dir = file("$buildDir/classpath/$platform") - dir.mkdirs() - return file("$dir/$fileName") -} - - -compileTestKotlinJvm.doLast { - classpathFile("jvm", "test_compile.txt").text = classpath.files.join("\n") -} - -jvmTest.doLast { - classpathFile("jvm", "test_runtime.txt").text = classpath.files.join("\n") -} - - -compileTestKotlinJs.doLast { - classpathFile("js", "test_compile.txt").text = classpath.files.join("\n") -} - -jsTest.dependsOn(":compileTestKotlinJs") -jsTest.dependsOn(":transformJsTestAtomicfu") -check.dependsOn(":jsTest")
\ No newline at end of file diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/mpp-simple.gradle.kts b/atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/mpp-simple.gradle.kts new file mode 100644 index 0000000..f289821 --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/mpp-simple.gradle.kts @@ -0,0 +1,90 @@ +import org.jetbrains.kotlin.gradle.plugin.* + +buildscript { + dependencies { + classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.17.0") + } +} + +plugins { + kotlin("multiplatform") version "1.6.0" +} + +apply(plugin = "kotlinx-atomicfu") + +repositories { + mavenCentral() +} + +kotlin { + targets { + jvm { + compilations.all { + kotlinOptions.jvmTarget = "1.8" + } + testRuns["test"].executionTask.configure { + useJUnit() + } + } + js { + nodejs() + } + } + sourceSets { + val commonMain by getting { + dependencies { + implementation("org.jetbrains.kotlin:kotlin-stdlib-common") + } + } + val commonTest by getting { + dependencies { + implementation(kotlin("test-common")) + implementation(kotlin("test-annotations-common")) + } + } + val jvmMain by getting { + dependencies { + implementation(kotlin("stdlib")) + } + } + val jvmTest by getting { + dependencies { + implementation(kotlin("test-junit")) + } + } + val jsMain by getting { + dependencies { + implementation("org.jetbrains.kotlin:kotlin-stdlib-js") + } + } + val jsTest by getting { + dependencies { + implementation("org.jetbrains.kotlin:kotlin-test-js") + } + } + } + + tasks.named("compileTestKotlinJvm") { + doLast { + file("$buildDir/test_compile_jvm_classpath.txt").writeText( + targets["jvm"].compilations["test"].compileDependencyFiles.joinToString("\n") + ) + } + } + + tasks.named("jvmTest") { + doLast { + file("$buildDir/test_runtime_jvm_classpath.txt").writeText( + (targets["jvm"].compilations["test"] as KotlinCompilationToRunnableFiles).runtimeDependencyFiles.joinToString("\n") + ) + } + } + + tasks.named("compileTestKotlinJs") { + doLast { + file("$buildDir/test_compile_js_classpath.txt").writeText( + targets["js"].compilations["test"].compileDependencyFiles.joinToString("\n") + ) + } + } +} diff --git a/atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/settings.gradle.kts b/atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/settings.gradle.kts new file mode 100644 index 0000000..5a4e5ab --- /dev/null +++ b/atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "mpp-simple"
\ No newline at end of file |