diff --git a/README.md b/README.md index e245d5f..ed4ce52 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,10 @@ - Makes integration classpath extend test classpath and main classpath (in this order). - Makes sure IntelliJ idea treats `src/integration/*` as test sources. - Exposes kotlin internal scope (from main and test module) to integration tests. -- Integrates with [Jacoco](https://docs.gradle.org/current/userguide/jacoco_plugin.html) +- Integrates with test coverage tools like [Jacoco](https://docs.gradle.org/current/userguide/jacoco_plugin.html) and [Kover](https://github.com/Kotlin/kotlinx-kover). +- Integrates with test frameworks like [JUnit5](https://junit.org/junit5/), [Spock](https://spockframework.org/) and + [Kotest](https://kotest.io/). ## Using the plugin diff --git a/build.gradle.kts b/build.gradle.kts index e8f6dca..8d945e6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ plugins { kotlin("jvm") version "2.0.21" + id("com.coditory.integration-test") version "2.0.2" id("java-gradle-plugin") id("maven-publish") id("com.gradle.plugin-publish") version "1.3.0" diff --git a/src/integration/kotlin/com/coditory/gradle/integration/CommandLineTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/CommandLineTest.kt new file mode 100644 index 0000000..dd5ce22 --- /dev/null +++ b/src/integration/kotlin/com/coditory/gradle/integration/CommandLineTest.kt @@ -0,0 +1,174 @@ +package com.coditory.gradle.integration + +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.TestProject +import com.coditory.gradle.integration.base.TestProjectBuilder +import org.assertj.core.api.Assertions.assertThat +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AutoClose +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class CommandLineTest { + companion object { + @AutoClose + private val project = createProject() + + @AutoClose + private val failingProject = createProject(passingIntgTests = false) + + private fun createProject(passingIntgTests: Boolean = true): TestProject { + val name = listOf( + "project", + CommandLineTest::class.simpleName, + if (passingIntgTests) "passing" else "failing", + ).joinToString("-") + return TestProjectBuilder + .project(name) + .withBuildGradle( + """ + plugins { + id 'com.coditory.integration-test' + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation "org.junit.jupiter:junit-jupiter-api:${Versions.junit}" + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${Versions.junit}" + } + + tasks.withType(Test) { + useJUnitPlatform() + testLogging { + events("passed", "failed", "skipped") + setExceptionFormat("full") + } + } + """, + ).withFile( + "src/integration/java/TestIntgSpec.java", + """ + import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; + + public class TestIntgSpec { + @Test + public void shouldPass() { + assertEquals(true, $passingIntgTests); + } + } + """, + ).withFile( + "src/test/java/TestUnitSpec.java", + """ + import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; + + public class TestUnitSpec { + @Test + public void shouldPass() { + assertEquals(true, true); + } + } + """, + ) + .build() + } + } + + @ParameterizedTest(name = "should run unit tests and integration tests on check command for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("check"), gradleVersion) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } + + @ParameterizedTest(name = "should run integration tests on integrationTest command for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run integration tests on integrationTest command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("integrationTest"), gradleVersion) + // then + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } + + @ParameterizedTest(name = "should run integration tests on integration command for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run integration tests on integration command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("integration"), gradleVersion) + // then + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } + + @Test + fun `should not run integration tests during test task`() { + // when + val result = project.runGradle(listOf("test")) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":integration")?.outcome).isNull() + } + + @Test + fun `should run integration tests and unit tests during testAll task`() { + // when + val result = project.runGradle(listOf("testAll")) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } + + @Test + fun `should exclude integration tests on -x integrationTest`() { + // when + val result = project.runGradle(listOf("check", "-x", "integrationTest")) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED) + } + + @Test + fun `should fail check command when integration tests fail`() { + // when + val result = failingProject.runGradleAndFail(listOf("check")) + // then + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.FAILED) + assertThat(result.task(":check")?.outcome).isNull() + } + + @Test + fun `should skip integration tests -PskipIntegrationTest`() { + // when + val result = project.runGradle(listOf("check", "-PskipIntegrationTest")) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED) + } + + @Test + fun `should skip all tests on -PskipTest`() { + // when + val result = project.runGradle(listOf("check", "-PskipTest")) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SKIPPED) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED) + } + + @Test + fun `should skip unit tests on -PskipUnitTest`() { + // when + val result = project.runGradle(listOf("check", "-PskipUnitTest")) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SKIPPED) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } +} diff --git a/src/integration/kotlin/com/coditory/gradle/integration/JUnitBasicTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/JUnitBasicTest.kt new file mode 100644 index 0000000..1720669 --- /dev/null +++ b/src/integration/kotlin/com/coditory/gradle/integration/JUnitBasicTest.kt @@ -0,0 +1,101 @@ +package com.coditory.gradle.integration + +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.TestProject +import com.coditory.gradle.integration.base.TestProjectBuilder +import org.assertj.core.api.Assertions.assertThat +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AutoClose +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class JUnitBasicTest { + companion object { + @AutoClose + private val project = createProject() + + @AutoClose + private val failingProject = createProject(passingIntgTests = false) + + private fun createProject(passingIntgTests: Boolean = true): TestProject { + val name = listOf( + "project", + JUnitBasicTest::class.simpleName, + if (passingIntgTests) "passing" else "failing", + ).joinToString("-") + return TestProjectBuilder + .project(name) + .withBuildGradleKts( + """ + plugins { + id("com.coditory.integration-test") + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.junit}") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}") + } + + tasks.withType { + useJUnitPlatform() + testLogging { + events("passed", "failed", "skipped") + setExceptionFormat("full") + } + } + """, + ).withFile( + "src/integration/java/TestIntgSpec.java", + """ + import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; + + public class TestIntgSpec { + @Test + public void shouldPass() { + assertEquals(true, $passingIntgTests); + } + } + """, + ).withFile( + "src/test/java/TestUnitSpec.java", + """ + import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; + + public class TestUnitSpec { + @Test + public void shouldPass() { + assertEquals(true, true); + } + } + """, + ) + .build() + } + } + + @ParameterizedTest(name = "should pass unit tests and integration tests on check command for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("check"), gradleVersion) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } + + @ParameterizedTest(name = "should fail integration tests on test failure for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should fail integration tests on test failure`(gradleVersion: String?) { + // when + val result = failingProject.runGradleAndFail(listOf("integration"), gradleVersion) + // then + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.FAILED) + } +} diff --git a/src/integration/kotlin/com/coditory/gradle/integration/JUnitClasspathTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/JUnitClasspathTest.kt new file mode 100644 index 0000000..55b10d9 --- /dev/null +++ b/src/integration/kotlin/com/coditory/gradle/integration/JUnitClasspathTest.kt @@ -0,0 +1,207 @@ +package com.coditory.gradle.integration + +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.TestProject +import com.coditory.gradle.integration.base.TestProjectBuilder +import org.assertj.core.api.Assertions.assertThat +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AutoClose +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class JUnitClasspathTest { + companion object { + @AutoClose + private val project = createProject() + + private fun createProject(): TestProject { + val builder = TestProjectBuilder + .project("project-${JUnitClasspathTest::class.simpleName}") + .withBuildGradleKts( + """ + plugins { + id("com.coditory.integration-test") + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.junit}") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}") + // sample integration test dependency + integrationImplementation("com.google.code.gson:gson:${Versions.gson}") + } + + tasks.withType { + useJUnitPlatform() + testLogging { + events("passed", "failed", "skipped") + setExceptionFormat("full") + } + } + """, + ).withFile( + "src/test/java/sample/ClasspathFileReader.java", + """ + package sample; + + import java.io.IOException; + import java.net.URI; + import java.net.URISyntaxException; + import java.nio.file.Files; + import java.nio.file.Path; + import java.nio.file.Paths; + + public class ClasspathFileReader { + public static String readFile(String name) { + try { + URI uri = ClasspathFileReader.class.getClassLoader() + .getResource(name) + .toURI(); + Path path = Paths.get(uri); + return Files.readString(path); + } catch (IOException | URISyntaxException e) { + throw new RuntimeException("Could not read file from classpath: " + name, e); + } + } + } + """, + ).withFile( + "src/integration/java/sample/TestIntgSpec.java", + """ + package sample; + + import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; + import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + import static sample.ClasspathFileReader.readFile; + + public class TestIntgSpec { + @Test + public void shouldReadATxtFileFromMain() { + assertEquals("main-a", readFile("a.txt")); + } + + @Test + public void shouldReadBTxtFileFromTest() { + assertEquals("test-b", readFile("b.txt")); + } + + @Test + public void shouldReadCTxtFileFromIntegration() { + assertEquals("integration-c", readFile("c.txt")); + } + + @Test + public void shouldReadConstantValueAFromMain() { + assertEquals("main-a", ConstantValuesA.MODULE); + } + + @Test + public void shouldReadConstantValueBFromTest() { + assertEquals("test-b", ConstantValuesB.MODULE); + } + + @Test + public void shouldReadConstantValueCFromIntegration() { + assertEquals("integration-c", ConstantValuesC.MODULE); + } + + @Test + void shouldResolveIntegrationDependency() { + assertDoesNotThrow(() -> Class.forName("com.google.gson.Gson")); + } + } + """, + ).withFile( + "src/test/java/sample/TestUnitSpec.java", + """ + package sample; + + import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; + import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + import static sample.ClasspathFileReader.readFile; + + public class TestUnitSpec { + @Test + public void shouldReadATxtFromMain() { + assertEquals("main-a", readFile("a.txt")); + } + + @Test + public void shouldReadBTxtFromTest() { + assertEquals("test-b", readFile("b.txt")); + } + + @Test + public void shouldReadConstantValueAFromMain() { + assertEquals("main-a", ConstantValuesA.MODULE); + } + + @Test + public void shouldReadConstantValueBFromTest() { + assertEquals("test-b", ConstantValuesB.MODULE); + } + } + """, + ) + listOf("main").forEach { + builder + .withFile("src/$it/resources/a.txt", "$it-a") + .withFile( + "src/$it/java/sample/ConstantValuesA.java", + """ + package sample; + + public class ConstantValuesA { + public static final String MODULE = "$it-a"; + } + """, + ) + } + listOf("main", "test").forEach { + builder + .withFile("src/$it/resources/b.txt", "$it-b") + .withFile( + "src/$it/java/sample/ConstantValuesB.java", + """ + package sample; + + public class ConstantValuesB { + public static final String MODULE = "$it-b"; + } + """, + ) + } + listOf("main", "test", "integration").forEach { + builder + .withFile("src/$it/resources/c.txt", "$it-c") + .withFile( + "src/$it/java/sample/ConstantValuesC.java", + """ + package sample; + + public class ConstantValuesC { + public static final String MODULE = "$it-c"; + } + """, + ) + } + return builder.build() + } + } + + @ParameterizedTest(name = "should read files from classpath for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("check"), gradleVersion) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } +} diff --git a/src/test/kotlin/com/coditory/gradle/integration/acceptance/JacocoBasedAcceptanceTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/JacocoBasedTest.kt similarity index 74% rename from src/test/kotlin/com/coditory/gradle/integration/acceptance/JacocoBasedAcceptanceTest.kt rename to src/integration/kotlin/com/coditory/gradle/integration/JacocoBasedTest.kt index 677e550..0c223f3 100644 --- a/src/test/kotlin/com/coditory/gradle/integration/acceptance/JacocoBasedAcceptanceTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/JacocoBasedTest.kt @@ -1,26 +1,19 @@ -package com.coditory.gradle.integration.acceptance +package com.coditory.gradle.integration import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION -import com.coditory.gradle.integration.base.TestProjectBuilder.Companion.project -import com.coditory.gradle.integration.base.TestProjectRunner.runGradle -import com.coditory.gradle.integration.base.readFileFromBuildDir +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat -import org.gradle.api.Project import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource -class JacocoBasedAcceptanceTest { - private val project = createProject() - - private fun createProject(): Project { - val commonImports = - """ - import org.junit.jupiter.api.Test; - - import static org.junit.jupiter.api.Assertions.assertEquals; - """.trimIndent() - return project("sample-project") +class JacocoBasedTest { + companion object { + @AutoClose + private val project = TestProjectBuilder + .project("project-" + JacocoBasedTest::class.simpleName) .withBuildGradle( """ plugins { @@ -33,8 +26,8 @@ class JacocoBasedAcceptanceTest { } dependencies { - testImplementation "org.junit.jupiter:junit-jupiter-api:5.11.0" - testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.11.0" + testImplementation "org.junit.jupiter:junit-jupiter-api:${Versions.junit}" + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${Versions.junit}" } tasks.withType(Test) { @@ -67,7 +60,8 @@ class JacocoBasedAcceptanceTest { ).withFile( "src/integration/java/TestIntgSpec.java", """ - $commonImports + import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; public class TestIntgSpec { @Test @@ -79,7 +73,8 @@ class JacocoBasedAcceptanceTest { ).withFile( "src/test/java/TestUnitSpec.java", """ - $commonImports + import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; public class TestUnitSpec { @Test @@ -93,12 +88,10 @@ class JacocoBasedAcceptanceTest { } @ParameterizedTest(name = "should aggregate coverage from unit and integration tests when using Jacoco {0}") - // @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) - @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION]) + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should aggregate coverage from unit and integration tests when using Jacoco`(gradleVersion: String?) { // when - val result = - runGradle(project, listOf("check", "jacocoTestReport"), gradleVersion) + val result = project.runGradle(listOf("check", "jacocoTestReport"), gradleVersion) // then assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(result.task(":integrationTest")?.outcome).isEqualTo(TaskOutcome.SUCCESS) diff --git a/src/integration/kotlin/com/coditory/gradle/integration/KotestBasicTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/KotestBasicTest.kt new file mode 100644 index 0000000..143b280 --- /dev/null +++ b/src/integration/kotlin/com/coditory/gradle/integration/KotestBasicTest.kt @@ -0,0 +1,101 @@ +package com.coditory.gradle.integration + +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.TestProject +import com.coditory.gradle.integration.base.TestProjectBuilder +import org.assertj.core.api.Assertions.assertThat +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AutoClose +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class KotestBasicTest { + companion object { + @AutoClose + private val project = createProject() + + @AutoClose + private val failingProject = createProject(passingIntgTests = false) + + private fun createProject(passingIntgTests: Boolean = true): TestProject { + val name = listOf( + "project", + KotestBasicTest::class.simpleName, + if (passingIntgTests) "passing" else "failing", + ).joinToString("-") + return TestProjectBuilder + .project(name) + .withBuildGradleKts( + """ + plugins { + kotlin("jvm") version "${Versions.kotlin}" + id("com.coditory.integration-test") + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.junit}") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}") + testImplementation("io.kotest:kotest-runner-junit5:${Versions.kotest}") + } + + tasks.withType { + useJUnitPlatform() + testLogging { + events("passed", "failed", "skipped") + setExceptionFormat("full") + } + } + """, + ).withFile( + "src/integration/kotlin/TestIntgSpec.kt", + """ + import io.kotest.core.spec.style.FreeSpec + import org.junit.jupiter.api.Assertions.assertEquals + + class TestIntgSpec : FreeSpec({ + "should pass" { + assertEquals(true, $passingIntgTests) + } + }) + """, + ).withFile( + "src/test/kotlin/TestUnitSpec.kt", + """ + import io.kotest.core.spec.style.FreeSpec + import org.junit.jupiter.api.Assertions.assertEquals + + class TestUnitSpec : FreeSpec({ + "should pass" { + assertEquals(4, 2 + 2) + } + }) + """, + ) + .build() + } + } + + @ParameterizedTest(name = "should pass unit tests and integration tests on check command for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("check"), gradleVersion) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } + + @ParameterizedTest(name = "should fail integration tests on test failure for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should fail integration tests on test failure`(gradleVersion: String?) { + // when + val result = failingProject.runGradleAndFail(listOf("integration"), gradleVersion) + // then + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.FAILED) + } +} diff --git a/src/integration/kotlin/com/coditory/gradle/integration/KotestClasspathTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/KotestClasspathTest.kt new file mode 100644 index 0000000..dc43fd8 --- /dev/null +++ b/src/integration/kotlin/com/coditory/gradle/integration/KotestClasspathTest.kt @@ -0,0 +1,177 @@ +package com.coditory.gradle.integration + +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.TestProject +import com.coditory.gradle.integration.base.TestProjectBuilder +import org.assertj.core.api.Assertions.assertThat +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AutoClose +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class KotestClasspathTest { + companion object { + @AutoClose + private val project = createProject() + + private fun createProject(): TestProject { + val builder = TestProjectBuilder + .project("project-${KotestClasspathTest::class.simpleName}") + .withBuildGradleKts( + """ + plugins { + kotlin("jvm") version "${Versions.kotlin}" + id("com.coditory.integration-test") + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.junit}") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}") + testImplementation("io.kotest:kotest-runner-junit5:${Versions.kotest}") + // sample integration test dependency + integrationImplementation("com.google.code.gson:gson:${Versions.gson}") + } + + tasks.withType { + useJUnitPlatform() + testLogging { + events("passed", "failed", "skipped") + setExceptionFormat("full") + } + } + """, + ).withFile( + "src/test/kotlin/ClasspathFileReader.kt", + """ + import java.net.URI + import java.nio.file.Files + import java.nio.file.Paths + + object ClasspathFileReader { + fun readFile(name: String): String { + val uri: URI = ClasspathFileReader::class.java.classLoader + ?.getResource(name) + ?.toURI()!! + return Files.readString(Paths.get(uri)) + } + } + """, + ).withFile( + "src/integration/kotlin/TestIntgSpec.kt", + """ + import io.kotest.core.spec.style.FreeSpec + import org.junit.jupiter.api.Assertions.assertEquals + import org.junit.jupiter.api.Assertions.assertDoesNotThrow + import ClasspathFileReader.readFile + + class TestIntgSpec : FreeSpec({ + "should read a.txt from main" { + assertEquals("main-a", readFile("a.txt")) + } + + "should read b.txt from test" { + assertEquals("test-b", readFile("b.txt")) + } + + "should read c.txt from integration" { + assertEquals("integration-c", readFile("c.txt")) + } + + "should read constant value A from main" { + assertEquals("main-a", ConstantValuesA.MODULE) + } + + "should read constant value B from test" { + assertEquals("test-b", ConstantValuesB.MODULE) + } + + "should read constant value C from integration" { + assertEquals("integration-c", ConstantValuesC.MODULE) + } + + "should resolve integration dependency" { + assertDoesNotThrow { Class.forName("com.google.gson.Gson") } + } + }) + """, + ).withFile( + "src/test/kotlin/TestUnitSpec.kt", + """ + import io.kotest.core.spec.style.FreeSpec + import org.junit.jupiter.api.Assertions.assertEquals + import ClasspathFileReader.readFile + + class TestUnitSpec : FreeSpec({ + "should read a.txt from main" { + assertEquals("main-a", readFile("a.txt")) + } + + "should read b.txt from test" { + assertEquals("test-b", readFile("b.txt")) + } + + "should read constant value A from main" { + assertEquals("main-a", ConstantValuesA.MODULE) + } + + "should read constant value B from test" { + assertEquals("test-b", ConstantValuesB.MODULE) + } + }) + """, + ) + listOf("main").forEach { + builder + .withFile("src/$it/resources/a.txt", "$it-a") + .withFile( + "src/$it/kotlin/ConstantValuesA.kt", + """ + object ConstantValuesA { + const val MODULE: String = "$it-a"; + } + """, + ) + } + listOf("main", "test").forEach { + builder + .withFile("src/$it/resources/b.txt", "$it-b") + .withFile( + "src/$it/kotlin/ConstantValuesB.kt", + """ + object ConstantValuesB { + const val MODULE: String = "$it-b"; + } + """, + ) + } + listOf("main", "test", "integration").forEach { + builder + .withFile("src/$it/resources/c.txt", "$it-c") + .withFile( + "src/$it/kotlin/ConstantValuesC.kt", + """ + object ConstantValuesC { + const val MODULE: String = "$it-c"; + } + """, + ) + } + return builder.build() + } + } + + @ParameterizedTest(name = "should read files from classpath for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("check"), gradleVersion) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } +} diff --git a/src/test/kotlin/com/coditory/gradle/integration/acceptance/KotlinInternalScopeAcceptanceTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/KotlinInternalScopeTest.kt similarity index 73% rename from src/test/kotlin/com/coditory/gradle/integration/acceptance/KotlinInternalScopeAcceptanceTest.kt rename to src/integration/kotlin/com/coditory/gradle/integration/KotlinInternalScopeTest.kt index 76217dd..347cfd0 100644 --- a/src/test/kotlin/com/coditory/gradle/integration/acceptance/KotlinInternalScopeAcceptanceTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/KotlinInternalScopeTest.kt @@ -1,31 +1,23 @@ -package com.coditory.gradle.integration.acceptance +package com.coditory.gradle.integration import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION -import com.coditory.gradle.integration.base.TestProjectBuilder.Companion.project -import com.coditory.gradle.integration.base.TestProjectRunner.runGradle +import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat -import org.gradle.api.Project import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource -class KotlinInternalScopeAcceptanceTest { - private val project = createProject() - - private fun createProject(): Project { - val commonImports = - """ - import org.junit.jupiter.api.Test - import org.junit.jupiter.api.Assertions.assertEquals - import InternalObject - import PublicObject - """.trimIndent() - return project("sample-project") - .withKtsBuildGradle( +class KotlinInternalScopeTest { + companion object { + @AutoClose + private val project = TestProjectBuilder + .project("project-${KotlinInternalScopeTest::class.simpleName}") + .withBuildGradleKts( """ plugins { - kotlin("jvm") version "2.0.20" + kotlin("jvm") version "${Versions.kotlin}" id("com.coditory.integration-test") } @@ -34,10 +26,8 @@ class KotlinInternalScopeAcceptanceTest { } dependencies { - testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.0") - testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.11.0") - // sample integration test dependency - integrationImplementation("org.slf4j:slf4j-api:2.0.16") + testImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.junit}") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}") } tasks.withType { @@ -65,7 +55,10 @@ class KotlinInternalScopeAcceptanceTest { ).withFile( "src/integration/kotlin/TestIntgSpec.kt", """ - $commonImports + import org.junit.jupiter.api.Test + import org.junit.jupiter.api.Assertions.assertEquals + import InternalObject + import PublicObject class TestIntgSpec { @Test @@ -82,7 +75,10 @@ class KotlinInternalScopeAcceptanceTest { ).withFile( "src/test/kotlin/TestUnitSpec.kt", """ - $commonImports + import org.junit.jupiter.api.Test + import org.junit.jupiter.api.Assertions.assertEquals + import InternalObject + import PublicObject class TestUnitSpec { @Test @@ -96,14 +92,15 @@ class KotlinInternalScopeAcceptanceTest { } } """, - ).build() + ) + .build() } - @ParameterizedTest(name = "should make internal scope visible in integration tests {0}") + @ParameterizedTest(name = "should make internal scope visible in integration tests for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { // when - val result = runGradle(project, listOf("check"), gradleVersion) + val result = project.runGradle(listOf("check"), gradleVersion) // then assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(result.task(":integrationTest")?.outcome).isEqualTo(TaskOutcome.SUCCESS) diff --git a/src/test/kotlin/com/coditory/gradle/integration/acceptance/LombokAcceptanceTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/LombokTest.kt similarity index 80% rename from src/test/kotlin/com/coditory/gradle/integration/acceptance/LombokAcceptanceTest.kt rename to src/integration/kotlin/com/coditory/gradle/integration/LombokTest.kt index 8260ead..8ee3d00 100644 --- a/src/test/kotlin/com/coditory/gradle/integration/acceptance/LombokAcceptanceTest.kt +++ b/src/integration/kotlin/com/coditory/gradle/integration/LombokTest.kt @@ -1,27 +1,19 @@ -package com.coditory.gradle.integration.acceptance +package com.coditory.gradle.integration import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION -import com.coditory.gradle.integration.base.TestProjectBuilder.Companion.project -import com.coditory.gradle.integration.base.TestProjectRunner.runGradle +import com.coditory.gradle.integration.base.TestProjectBuilder import org.assertj.core.api.Assertions.assertThat -import org.gradle.api.Project import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AutoClose import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource -class LombokAcceptanceTest { - private val project = createProject() - - private fun createProject(): Project { - val commonImports = - """ - import org.junit.jupiter.api.Test; - - import static org.junit.jupiter.api.Assertions.assertEquals; - import static org.junit.jupiter.api.Assertions.assertNotEquals; - """.trimIndent() - return project("sample-lombok-project") +class LombokTest { + companion object { + @AutoClose + private val project = TestProjectBuilder + .project("project-" + LombokTest::class.simpleName) .withBuildGradle( """ plugins { @@ -33,12 +25,12 @@ class LombokAcceptanceTest { } dependencies { - compileOnly "org.projectlombok:lombok:1.18.34" - annotationProcessor "org.projectlombok:lombok:1.18.34" - testCompileOnly "org.projectlombok:lombok:1.18.34" - testAnnotationProcessor "org.projectlombok:lombok:1.18.34" - testImplementation "org.junit.jupiter:junit-jupiter-api:5.11.0" - testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.11.0" + compileOnly "org.projectlombok:lombok:${Versions.lombok}" + annotationProcessor "org.projectlombok:lombok:${Versions.lombok}" + testCompileOnly "org.projectlombok:lombok:${Versions.lombok}" + testAnnotationProcessor "org.projectlombok:lombok:${Versions.lombok}" + testImplementation "org.junit.jupiter:junit-jupiter-api:${Versions.junit}" + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${Versions.junit}" } tasks.withType(Test) { @@ -82,7 +74,10 @@ class LombokAcceptanceTest { ).withFile( "src/integration/java/TestIntgSpec.java", """ - $commonImports + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.assertEquals; + import static org.junit.jupiter.api.Assertions.assertNotEquals; public class TestIntgSpec { @Test @@ -107,7 +102,10 @@ class LombokAcceptanceTest { ).withFile( "src/test/java/TestUnitSpec.java", """ - $commonImports + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.assertEquals; + import static org.junit.jupiter.api.Assertions.assertNotEquals; public class TestUnitSpec { @Test @@ -123,14 +121,15 @@ class LombokAcceptanceTest { } } """, - ).build() + ) + .build() } @ParameterizedTest(name = "should run unit tests and integration tests on check command for gradle {0}") @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) fun `should run unit tests and integration tests with lombok`(gradleVersion: String?) { // when - val result = runGradle(project, listOf("check"), gradleVersion) + val result = project.runGradle(listOf("check"), gradleVersion) // then assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(result.task(":integrationTest")?.outcome).isEqualTo(TaskOutcome.SUCCESS) diff --git a/src/integration/kotlin/com/coditory/gradle/integration/PlatformDependencyTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/PlatformDependencyTest.kt new file mode 100644 index 0000000..3349cfd --- /dev/null +++ b/src/integration/kotlin/com/coditory/gradle/integration/PlatformDependencyTest.kt @@ -0,0 +1,67 @@ +package com.coditory.gradle.integration + +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.TestProjectBuilder +import org.assertj.core.api.Assertions.assertThat +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.AutoClose +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class PlatformDependencyTest { + companion object { + @AutoClose + private val project = TestProjectBuilder + .project("project-${PlatformDependencyTest::class.simpleName}") + .withBuildGradle( + """ + plugins { + id 'com.coditory.integration-test' + } + + repositories { + mavenCentral() + } + + dependencies { + implementation platform("org.springframework.boot:spring-boot-dependencies:${Versions.spring}") + testImplementation "org.junit.jupiter:junit-jupiter-api:${Versions.junit}" + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${Versions.junit}" + integrationImplementation "org.springframework.boot:spring-boot-starter-test" + } + + tasks.withType(Test) { + useJUnitPlatform() + testLogging { + events("passed", "failed", "skipped") + setExceptionFormat("full") + } + } + """, + ).withFile( + "src/integration/java/TestIntgSpec.java", + """ + import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + + public class TestIntgSpec { + @Test + void shouldResolveDependencyFromBom() { + assertDoesNotThrow(() -> Class.forName("org.springframework.test.context.ContextConfiguration")); + } + } + """, + ) + .build() + } + + @ParameterizedTest(name = "should use dependency version from platform dependency for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("check"), gradleVersion) + // then + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } +} diff --git a/src/integration/kotlin/com/coditory/gradle/integration/SpockBasicTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/SpockBasicTest.kt new file mode 100644 index 0000000..2560b5d --- /dev/null +++ b/src/integration/kotlin/com/coditory/gradle/integration/SpockBasicTest.kt @@ -0,0 +1,98 @@ +package com.coditory.gradle.integration + +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.TestProject +import com.coditory.gradle.integration.base.TestProjectBuilder +import org.assertj.core.api.Assertions.assertThat +import org.gradle.testkit.runner.TaskOutcome +import org.gradle.testkit.runner.TaskOutcome.SUCCESS +import org.junit.jupiter.api.AutoClose +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class SpockBasicTest { + companion object { + @AutoClose + private val project = createProject() + + @AutoClose + private val failingProject = createProject(passingIntgTests = false) + + private fun createProject(passingIntgTests: Boolean = true): TestProject { + val name = listOf( + "project", + SpockBasicTest::class.simpleName, + if (passingIntgTests) "passing" else "failing", + ).joinToString("-") + return TestProjectBuilder + .project(name) + .withBuildGradle( + """ + plugins { + id 'groovy' + id 'com.coditory.integration-test' + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation "org.spockframework:spock-core:${Versions.spock}" + } + + tasks.withType(Test) { + testLogging { + events("passed", "failed", "skipped") + setExceptionFormat("full") + } + } + """, + ).withFile( + "src/integration/groovy/TestIntgSpec.groovy", + """ + import spock.lang.Specification + + class TestIntgSpec extends Specification { + def "should pass"() { + expect: + $passingIntgTests + } + } + """, + ).withFile( + "src/test/groovy/TestUnitSpec.groovy", + """ + import spock.lang.Specification + + class TestUnitSpec extends Specification { + def "should pass"() { + expect: + 2 + 2 == 4 + } + } + """, + ).build() + } + } + + @ParameterizedTest(name = "should pass unit tests and integration tests on check command for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("check"), gradleVersion) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(SUCCESS) + assertThat(result.task(":integrationTest")?.outcome).isEqualTo(SUCCESS) + } + + @ParameterizedTest(name = "should fail integration tests on test failure for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should fail integration tests on test failure`(gradleVersion: String?) { + // when + val result = failingProject.runGradleAndFail(listOf("integration"), gradleVersion) + // then + assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.FAILED) + } +} diff --git a/src/integration/kotlin/com/coditory/gradle/integration/SpockClasspathTest.kt b/src/integration/kotlin/com/coditory/gradle/integration/SpockClasspathTest.kt new file mode 100644 index 0000000..a649332 --- /dev/null +++ b/src/integration/kotlin/com/coditory/gradle/integration/SpockClasspathTest.kt @@ -0,0 +1,178 @@ +package com.coditory.gradle.integration + +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION +import com.coditory.gradle.integration.base.TestProject +import com.coditory.gradle.integration.base.TestProjectBuilder +import org.assertj.core.api.Assertions.assertThat +import org.gradle.testkit.runner.TaskOutcome.SUCCESS +import org.junit.jupiter.api.AutoClose +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class SpockClasspathTest { + companion object { + @AutoClose + private val project = createProject() + + private fun createProject(): TestProject { + val builder = TestProjectBuilder + .project("project-${SpockClasspathTest::class.simpleName}") + .withBuildGradle( + """ + plugins { + id 'groovy' + id 'com.coditory.integration-test' + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation "org.spockframework:spock-core:${Versions.spock}" + // sample integration test dependency + integrationImplementation "com.google.code.gson:gson:${Versions.gson}" + } + + tasks.withType(Test) { + testLogging { + events("passed", "failed", "skipped") + setExceptionFormat("full") + } + } + """, + ).withFile( + "src/test/groovy/ClasspathFileReader.groovy", + """ + class ClasspathFileReader { + static String readFile(String name) throws Exception { + return ClasspathFileReader.class.getResource("/" + name).getText() + } + } + """, + ).withFile( + "src/integration/groovy/TestIntgSpec.groovy", + """ + import spock.lang.Specification + import static ClasspathFileReader.readFile; + + class TestIntgSpec extends Specification { + def "should read a.txt from main"() { + expect: + readFile("a.txt") == "main-a"; + } + + def "should read b.txt from test"() { + expect: + readFile('b.txt') == 'test-b' + } + + def "should read c.txt from integration"() { + expect: + readFile('c.txt') == 'integration-c' + } + + def "should read constant value A from main"() { + expect: + ConstantValuesA.MODULE == 'main-a' + } + + def "should read constant value B from test"() { + expect: + ConstantValuesB.MODULE == 'test-b' + } + + def "should read constant value C from integration"() { + expect: + ConstantValuesC.MODULE == 'integration-c' + } + + def "should resolve integration dependency"() { + when: + Class.forName("com.google.gson.Gson") + then: + noExceptionThrown() + } + } + """, + ).withFile( + "src/test/groovy/TestUnitSpec.groovy", + """ + import spock.lang.Specification + import static ClasspathFileReader.readFile + + class TestUnitSpec extends Specification { + def "should read a.txt from main"() { + expect: + readFile('a.txt') == 'main-a' + } + + def "should read b.txt from test"() { + expect: + readFile('b.txt') == 'test-b' + } + + def "should read constant value A from main"() { + expect: + ConstantValuesA.MODULE == 'main' + } + + def "should read constant value B from test"() { + expect: + ConstantValuesB.MODULE == 'test' + } + } + """, + ) + val lang = { module: String -> if (module == "main") "java" else "groovy" } + listOf("main").forEach { + builder + .withFile("src/$it/resources/a.txt", "$it-a") + .withFile( + "src/$it/${lang(it)}/ConstantValuesA.${lang(it)}", + """ + public class ConstantValuesA { + public static final String MODULE = "$it-a"; + } + """, + ) + } + listOf("main", "test").forEach { + builder + .withFile("src/$it/resources/b.txt", "$it-b") + .withFile( + "src/$it/${lang(it)}/ConstantValuesB.${lang(it)}", + """ + public class ConstantValuesB { + public static final String MODULE = "$it-b"; + } + """, + ) + } + listOf("main", "test", "integration").forEach { + builder + .withFile("src/$it/resources/c.txt", "$it-c") + .withFile( + "src/$it/${lang(it)}/ConstantValuesC.${lang(it)}", + """ + public class ConstantValuesC { + public static final String MODULE = "$it-c"; + } + """, + ) + } + return builder.build() + } + } + + @ParameterizedTest(name = "should read files from classpath for gradle {0}") + @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) + fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { + // when + val result = project.runGradle(listOf("check"), gradleVersion) + // then + assertThat(result.task(":test")?.outcome).isEqualTo(SUCCESS) + assertThat(result.task(":integrationTest")?.outcome).isEqualTo(SUCCESS) + } +} diff --git a/src/integration/kotlin/com/coditory/gradle/integration/Versions.kt b/src/integration/kotlin/com/coditory/gradle/integration/Versions.kt new file mode 100644 index 0000000..9e9cc1e --- /dev/null +++ b/src/integration/kotlin/com/coditory/gradle/integration/Versions.kt @@ -0,0 +1,11 @@ +package com.coditory.gradle.integration + +object Versions { + val kotlin = "2.0.20" + val junit = "5.11.0" + val kotest = "5.9.1" + val spock = "2.4-M4-groovy-4.0" + val gson = "2.11.0" + val lombok = "1.18.34" + val spring = "3.3.4" +} diff --git a/src/test/kotlin/com/coditory/gradle/integration/JacocoConfigurationTest.kt b/src/test/kotlin/com/coditory/gradle/integration/JacocoConfigurationTest.kt index d0918a5..38451a8 100644 --- a/src/test/kotlin/com/coditory/gradle/integration/JacocoConfigurationTest.kt +++ b/src/test/kotlin/com/coditory/gradle/integration/JacocoConfigurationTest.kt @@ -1,16 +1,15 @@ package com.coditory.gradle.integration +import com.coditory.gradle.integration.base.TestProject import com.coditory.gradle.integration.base.TestProjectBuilder.Companion.project -import com.coditory.gradle.integration.base.toBuildPath import org.assertj.core.api.Assertions.assertThat -import org.gradle.api.Project import org.gradle.api.plugins.JavaPlugin import org.gradle.testing.jacoco.plugins.JacocoPlugin import org.gradle.testing.jacoco.tasks.JacocoReport import org.junit.jupiter.api.Test class JacocoConfigurationTest { - private val project: Project = project() + private val project: TestProject = project() .withPlugins(JavaPlugin::class, JacocoPlugin::class, IntegrationTestPlugin::class) .build() diff --git a/src/test/kotlin/com/coditory/gradle/integration/TestTaskConfigurationTest.kt b/src/test/kotlin/com/coditory/gradle/integration/TestTaskConfigurationTest.kt index 43b88aa..b2dd3a4 100644 --- a/src/test/kotlin/com/coditory/gradle/integration/TestTaskConfigurationTest.kt +++ b/src/test/kotlin/com/coditory/gradle/integration/TestTaskConfigurationTest.kt @@ -3,7 +3,6 @@ package com.coditory.gradle.integration import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.INTEGRATION import com.coditory.gradle.integration.IntegrationTestPlugin.Companion.INTEGRATION_TEST import com.coditory.gradle.integration.base.TestProjectBuilder.Companion.createProject -import com.coditory.gradle.integration.base.toBuildPath import org.assertj.core.api.Assertions.assertThat import org.gradle.api.Project import org.gradle.api.plugins.JavaBasePlugin @@ -15,28 +14,7 @@ import org.junit.jupiter.api.Test import org.gradle.api.tasks.testing.Test as TestTask class TestTaskConfigurationTest { - private val project: Project = createProject() - - @Test - fun `should configure integration source sets`() { - val sourceSet = getSourceSet() - assertThat(sourceSet).isNotNull - assertThat(sourceSet.output.classesDirs.asPath).isEqualTo(project.toBuildPath("classes/java/integration")) - assertThat(sourceSet.output.resourcesDir.toString()).isEqualTo(project.toBuildPath("resources/integration")) - // TODO: Fix it. Tried it all. It's failing with Could not find org.gradle.internal.impldep.org.junit.jupiter:junit-jupiter:5.8.2 - // Tried: adding repositories to test project, defining tests to use junit platform etc - did not help... - // assertThat(sourceSet.runtimeClasspath.asPath) - // .isEqualTo( - // project.toBuildPath( - // "classes/java/integrationTest", - // "resources/integrationTest", - // "classes/java/test", - // "resources/test", - // "classes/java/main", - // "resources/main", - // ), - // ) - } + private val project = createProject() @Test fun `should configure integrationTest task`() { diff --git a/src/test/kotlin/com/coditory/gradle/integration/acceptance/CommandLineAcceptanceTest.kt b/src/test/kotlin/com/coditory/gradle/integration/acceptance/CommandLineAcceptanceTest.kt deleted file mode 100644 index 95e7530..0000000 --- a/src/test/kotlin/com/coditory/gradle/integration/acceptance/CommandLineAcceptanceTest.kt +++ /dev/null @@ -1,152 +0,0 @@ -package com.coditory.gradle.integration.acceptance - -import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION -import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION -import com.coditory.gradle.integration.base.TestProjectBuilder -import com.coditory.gradle.integration.base.TestProjectRunner.runGradle -import org.assertj.core.api.Assertions.assertThat -import org.gradle.api.Project -import org.gradle.testkit.runner.TaskOutcome -import org.junit.jupiter.api.Test -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.ValueSource - -class CommandLineAcceptanceTest { - private val project = createProject() - - private fun createProject(): Project { - val commonImports = - """ - import static org.junit.jupiter.api.Assertions.assertEquals; - import org.junit.jupiter.api.Test; - """.trimIndent() - return TestProjectBuilder - .project() - .withBuildGradle( - """ - plugins { - id 'com.coditory.integration-test' - } - - repositories { - mavenCentral() - } - - dependencies { - testImplementation "org.junit.jupiter:junit-jupiter-api:5.11.0" - testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.11.0" - } - - tasks.withType(Test) { - useJUnitPlatform() - testLogging { - events("passed", "failed", "skipped") - setExceptionFormat("full") - } - } - """, - ).withFile( - "src/integration/java/TestIntgSpec.java", - """ - $commonImports - - public class TestIntgSpec { - @Test - public void shouldPassSampleIntegrationSpec() { - assertEquals(true, true); - } - - @Test - public void shouldPassSecondSampleIntegrationSpec() { - assertEquals(true, true); - } - } - """, - ).withFile( - "src/test/java/TestUnitSpec.java", - """ - $commonImports - - public class TestUnitSpec { - @Test - public void shouldPassSampleUnitSpec() { - assertEquals(true, true); - } - } - """, - ).build() - } - - @ParameterizedTest(name = "should run unit tests and integration tests on check command for gradle {0}") - @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) - fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { - // when - val result = runGradle(project, listOf("check"), gradleVersion) - // then - assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - } - - @ParameterizedTest(name = "should run integration tests on integrationTest command for gradle {0}") - @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) - fun `should run integration tests on integrationTest command`(gradleVersion: String?) { - // when - val result = runGradle(project, listOf("integrationTest"), gradleVersion) - // then - assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - } - - @Test - fun `should not run integration tests during test task`() { - // when - val result = runGradle(project, listOf("test")) - // then - assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - assertThat(result.task(":integration")?.outcome).isNull() - } - - @Test - fun `should run integration tests and unit tests during testAll task`() { - // when - val result = runGradle(project, listOf("testAll")) - // then - assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - } - - @Test - fun `should exclude integration tests`() { - // when - val result = runGradle(project, listOf("check", "-x", "integrationTest")) - // then - assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED) - } - - @Test - fun `should skip integration tests`() { - // when - val result = runGradle(project, listOf("check", "-PskipIntegrationTest")) - // then - assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED) - } - - @Test - fun `should skip all tests`() { - // when - val result = runGradle(project, listOf("check", "-PskipTest")) - // then - assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SKIPPED) - assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED) - } - - @Test - fun `should skip unit tests`() { - // when - val result = runGradle(project, listOf("check", "-PskipUnitTest")) - // then - assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SKIPPED) - assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - } -} diff --git a/src/test/kotlin/com/coditory/gradle/integration/acceptance/Junit5BasedAcceptanceTest.kt b/src/test/kotlin/com/coditory/gradle/integration/acceptance/Junit5BasedAcceptanceTest.kt deleted file mode 100644 index 5741dc5..0000000 --- a/src/test/kotlin/com/coditory/gradle/integration/acceptance/Junit5BasedAcceptanceTest.kt +++ /dev/null @@ -1,189 +0,0 @@ -package com.coditory.gradle.integration.acceptance - -import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION -import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION -import com.coditory.gradle.integration.base.TestProjectBuilder.Companion.project -import com.coditory.gradle.integration.base.TestProjectRunner.runGradle -import org.assertj.core.api.Assertions.assertThat -import org.gradle.api.Project -import org.gradle.testkit.runner.TaskOutcome -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.ValueSource - -class Junit5BasedAcceptanceTest { - private val project = createProject() - - private fun createProject(): Project { - val commonImports = - """ - import org.junit.jupiter.api.Test; - - import static org.junit.jupiter.api.Assertions.assertEquals; - import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - import static base.ClasspathFileReader.readFile; - """.trimIndent() - return project("sample-project") - .withBuildGradle( - """ - plugins { - id 'com.coditory.integration-test' - } - - repositories { - mavenCentral() - } - - dependencies { - implementation platform("org.springframework.boot:spring-boot-dependencies:3.3.3") - testImplementation "org.junit.jupiter:junit-jupiter-api:5.11.0" - testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.11.0" - // sample integration test dependency - integrationImplementation "org.slf4j:slf4j-api:2.0.16" - integrationImplementation "org.springframework.boot:spring-boot-starter-test" - } - - tasks.withType(Test) { - useJUnitPlatform() - testLogging { - events("passed", "failed", "skipped") - setExceptionFormat("full") - } - } - """, - ).withFile( - "src/test/java/base/ClasspathFileReader.java", - """ - package base; - - import java.net.URI; - import java.nio.file.Files; - import java.nio.file.Path; - import java.nio.file.Paths; - - public class ClasspathFileReader { - public static String readFile(String name) { - try { - URI uri = ClasspathFileReader.class.getClassLoader() - .getResource(name) - .toURI(); - Path path = Paths.get(uri); - return new String(Files.readAllBytes(path)); - } catch (Exception e) { - throw new RuntimeException("Could not read file from classpath: " + name, e); - } - } - } - """, - ).withFile( - "src/test/java/ConstantValues.java", - """ - public class ConstantValues { - public static final String MODULE = "test"; - } - """, - ).withFile( - "src/integration/java/ConstantValues.java", - """ - public class ConstantValues { - public static final String MODULE = "integration"; - } - """, - ).withFile( - "src/main/java/ConstantValues.java", - """ - public class ConstantValues { - public static final String MODULE = "main"; - } - """, - ).withFile( - "src/main/java/MainConstantValues.java", - """ - public class MainConstantValues { - public static final String MODULE = "main"; - } - """, - ).withFile( - "src/integration/java/TestIntgSpec.java", - """ - $commonImports - - public class TestIntgSpec { - @Test - public void shouldReadATxtFileFromMain() { - assertEquals("main-a", readFile("a.txt")); - } - - @Test - public void shouldReadBTxtFileFromTest() { - assertEquals("test-b", readFile("b.txt")); - } - - @Test - public void shouldReadCTxtFileFromIntegration() { - assertEquals("integration-c", readFile("c.txt")); - } - - @Test - public void shouldReadConstantValueFromIntModule() { - assertEquals("integration", ConstantValues.MODULE); - } - - @Test - public void shouldReadConstantValueFromMainModule() { - assertEquals("main", MainConstantValues.MODULE); - } - - @Test - void shouldResolveDependencyFromBom() { - assertDoesNotThrow( - () -> Class.forName("org.springframework.test.context.ContextConfiguration") - ); - } - } - """, - ).withFile( - "src/test/java/TestUnitSpec.java", - """ - $commonImports - - public class TestUnitSpec { - @Test - public void shouldReadATxtFromMain() { - assertEquals("main-a", readFile("a.txt")); - } - - @Test - public void shouldReadBTxtFromTest() { - assertEquals("test-b", readFile("b.txt")); - } - - @Test - public void shouldReadConstantValueFromTestModule() { - assertEquals("test", ConstantValues.MODULE); - } - - @Test - public void shouldReadConstantValueFromMainModule() { - assertEquals("main", MainConstantValues.MODULE); - } - } - """, - ).withFile("src/main/resources/a.txt", "main-a") - .withFile("src/main/resources/b.txt", "main-b") - .withFile("src/main/resources/c.txt", "main-c") - .withFile("src/test/resources/b.txt", "test-b") - .withFile("src/test/resources/c.txt", "test-c") - .withFile("src/integration/resources/c.txt", "integration-c") - .build() - } - - @ParameterizedTest(name = "should run unit tests and integration tests on check command for gradle {0}") - @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) - fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { - // when - val result = runGradle(project, listOf("check"), gradleVersion) - // then - assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS) - } -} diff --git a/src/test/kotlin/com/coditory/gradle/integration/acceptance/SpockBasedAcceptanceTest.kt b/src/test/kotlin/com/coditory/gradle/integration/acceptance/SpockBasedAcceptanceTest.kt deleted file mode 100644 index bb62ce7..0000000 --- a/src/test/kotlin/com/coditory/gradle/integration/acceptance/SpockBasedAcceptanceTest.kt +++ /dev/null @@ -1,149 +0,0 @@ -package com.coditory.gradle.integration.acceptance - -import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION -import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION -import com.coditory.gradle.integration.base.TestProjectBuilder.Companion.project -import com.coditory.gradle.integration.base.TestProjectRunner.runGradle -import org.assertj.core.api.Assertions.assertThat -import org.gradle.testkit.runner.TaskOutcome.SUCCESS -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.ValueSource - -class SpockBasedAcceptanceTest { - private val project = - project("sample-project") - .withBuildGradle( - """ - plugins { - id 'groovy' - id 'com.coditory.integration-test' - } - - repositories { - mavenCentral() - } - - dependencies { - testImplementation "org.spockframework:spock-core:2.4-M4-groovy-4.0" - // sample integration test dependency - integrationImplementation "org.slf4j:slf4j-api:2.0.16" - } - - tasks.withType(Test) { - testLogging { - events("passed", "failed", "skipped") - setExceptionFormat("full") - } - } - """, - ).withFile( - "src/test/groovy/ClasspathFileReader.groovy", - """ - class ClasspathFileReader { - static String readFile(String name) throws Exception { - return ClasspathFileReader.class.getResource("/" + name).getText() - } - } - """, - ).withFile( - "src/test/groovy/ConstantValues.groovy", - """ - class ConstantValues { - static final String MODULE = "test" - } - """, - ).withFile( - "src/integration/groovy/ConstantValues.groovy", - """ - class ConstantValues { - static final String MODULE = "integration" - } - """, - ).withFile( - "src/main/java/ConstantValues.java", - """ - public class ConstantValues { - public static final String MODULE = "main"; - } - """, - ).withFile( - "src/main/java/MainConstantValues.java", - """ - public class MainConstantValues { - public static final String MODULE = "main"; - } - """, - ).withFile( - "src/integration/groovy/TestIntgSpec.groovy", - """ - import spock.lang.Specification - import static ClasspathFileReader.readFile - - class TestIntgSpec extends Specification { - def "should read a.txt from main"() { - expect: - readFile('a.txt') == 'main-a' - } - - def "should read b.txt from test"() { - expect: - readFile('b.txt') == 'test-b' - } - - def "should read c.txt from test"() { - expect: - readFile('c.txt') == 'integration-c' - } - - def "should read constant value from integration module"() { - expect: - ConstantValues.MODULE == 'integration' - } - - def "should read main constant value from main module"() { - expect: - MainConstantValues.MODULE == 'main' - } - } - """, - ).withFile( - "src/test/groovy/TestUnitSpec.groovy", - """ - import spock.lang.Specification - import static ClasspathFileReader.readFile - - class TestUnitSpec extends Specification { - def "should read a.txt from main"() { - expect: - readFile('a.txt') == 'main-a' - } - - def "should read b.txt from test"() { - expect: - readFile('b.txt') == 'test-b' - } - - def "should read constant value from test module"() { - expect: - ConstantValues.MODULE == 'test' - } - } - """, - ).withFile("src/main/resources/a.txt", "main-a") - .withFile("src/main/resources/b.txt", "main-b") - .withFile("src/main/resources/c.txt", "main-c") - .withFile("src/test/resources/b.txt", "test-b") - .withFile("src/test/resources/c.txt", "test-c") - .withFile("src/integration/resources/c.txt", "integration-c") - .build() - - @ParameterizedTest(name = "should run unit tests and integration tests on check command for gradle {0}") - @ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION]) - fun `should run unit tests and integration tests on check command`(gradleVersion: String?) { - // when - val result = runGradle(project, listOf("check"), gradleVersion) - // then - assertThat(result.task(":test")?.outcome).isEqualTo(SUCCESS) - assertThat(result.task(":integrationTest")?.outcome).isEqualTo(SUCCESS) - } -} diff --git a/src/test/kotlin/com/coditory/gradle/integration/base/GradleTestVersions.kt b/src/test/kotlin/com/coditory/gradle/integration/base/GradleTestVersions.kt index 8950e0e..fba89a1 100644 --- a/src/test/kotlin/com/coditory/gradle/integration/base/GradleTestVersions.kt +++ b/src/test/kotlin/com/coditory/gradle/integration/base/GradleTestVersions.kt @@ -2,5 +2,5 @@ package com.coditory.gradle.integration.base object GradleTestVersions { const val GRADLE_MAX_SUPPORTED_VERSION = "current" - const val GRADLE_MIN_SUPPORTED_VERSION = "7.5" + const val GRADLE_MIN_SUPPORTED_VERSION = "7.6.4" } diff --git a/src/test/kotlin/com/coditory/gradle/integration/base/TestProject.kt b/src/test/kotlin/com/coditory/gradle/integration/base/TestProject.kt new file mode 100644 index 0000000..3bf2fa0 --- /dev/null +++ b/src/test/kotlin/com/coditory/gradle/integration/base/TestProject.kt @@ -0,0 +1,44 @@ +package com.coditory.gradle.integration.base + +import org.gradle.api.Project +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import java.io.File + +class TestProject(private val project: Project) : Project by project { + fun toBuildPath(vararg paths: String): String { + return paths.joinToString(File.pathSeparator) { + "${this.layout.buildDirectory.get()}${File.separator}${it.replace("/", File.separator)}" + } + } + + fun readFileFromBuildDir(path: String): String { + return this.layout.buildDirectory.file(path).get().asFile.readText() + } + + fun runGradle(arguments: List, gradleVersion: String? = null): BuildResult { + return gradleRunner(this, arguments, gradleVersion).build() + } + + fun runGradleAndFail(arguments: List, gradleVersion: String? = null): BuildResult { + return gradleRunner(this, arguments, gradleVersion).buildAndFail() + } + + // Used by @AutoClose test annotation + fun close() { + this.projectDir.deleteRecursively() + } + + private fun gradleRunner(project: Project, arguments: List, gradleVersion: String? = null): GradleRunner { + val builder = GradleRunner.create() + .withProjectDir(project.projectDir) + // clean is required so tasks are not cached + .withArguments(listOf("clean") + arguments) + .withPluginClasspath() + .forwardOutput() + if (!gradleVersion.isNullOrBlank() && gradleVersion != "current") { + builder.withGradleVersion(gradleVersion) + } + return builder + } +} diff --git a/src/test/kotlin/com/coditory/gradle/integration/base/TestProjectBuilder.kt b/src/test/kotlin/com/coditory/gradle/integration/base/TestProjectBuilder.kt index fa9f0eb..f9e5a95 100644 --- a/src/test/kotlin/com/coditory/gradle/integration/base/TestProjectBuilder.kt +++ b/src/test/kotlin/com/coditory/gradle/integration/base/TestProjectBuilder.kt @@ -2,7 +2,6 @@ package com.coditory.gradle.integration.base import com.coditory.gradle.integration.IntegrationTestPlugin import org.gradle.api.Plugin -import org.gradle.api.Project import org.gradle.api.internal.project.DefaultProject import org.gradle.api.plugins.JavaPlugin import org.gradle.testfixtures.ProjectBuilder @@ -39,7 +38,7 @@ class TestProjectBuilder private constructor(projectDir: File, name: String) { return this } - fun withKtsBuildGradle(content: String): TestProjectBuilder { + fun withBuildGradleKts(content: String): TestProjectBuilder { val buildFile = project.rootDir.resolve("build.gradle.kts") buildFile.writeText(content.trimIndent().trim()) return this @@ -65,15 +64,13 @@ class TestProjectBuilder private constructor(projectDir: File, name: String) { return this } - fun build(): Project { + fun build(): TestProject { project.evaluate() - return project + return TestProject(project) } companion object { - private var projectDirs = mutableListOf() - - fun createProject(): Project { + fun createProject(): TestProject { return projectWithPlugins().build() } @@ -88,28 +85,10 @@ class TestProjectBuilder private constructor(projectDir: File, name: String) { @Suppress("EXPERIMENTAL_API_USAGE_ERROR") private fun createProjectDir(directory: String): File { - removeProjectDirs() val projectParentDir = createTempDirectory().toFile() val projectDir = projectParentDir.resolve(directory) projectDir.mkdir() - projectDirs.add(projectParentDir) return projectDir } - - private fun removeProjectDirs() { - projectDirs.forEach { - it.deleteRecursively() - } - } - } -} - -fun Project.toBuildPath(vararg paths: String): String { - return paths.joinToString(File.pathSeparator) { - "${this.layout.buildDirectory.get()}${File.separator}${it.replace("/", File.separator)}" } } - -fun Project.readFileFromBuildDir(path: String): String? { - return this.layout.buildDirectory.file(path).get().asFile.readText() -} diff --git a/src/test/kotlin/com/coditory/gradle/integration/base/TestProjectRunner.kt b/src/test/kotlin/com/coditory/gradle/integration/base/TestProjectRunner.kt deleted file mode 100644 index b6c2312..0000000 --- a/src/test/kotlin/com/coditory/gradle/integration/base/TestProjectRunner.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.coditory.gradle.integration.base - -import org.gradle.api.Project -import org.gradle.testkit.runner.BuildResult -import org.gradle.testkit.runner.GradleRunner - -object TestProjectRunner { - fun runGradle(project: Project, arguments: List, gradleVersion: String? = null): BuildResult { - val builder = GradleRunner.create() - .withProjectDir(project.projectDir) - .withArguments(arguments) - .withPluginClasspath() - .forwardOutput() - if (!gradleVersion.isNullOrBlank() && gradleVersion != "current") { - builder.withGradleVersion(gradleVersion) - } - return builder.build() - } -}