Skip to content

Commit

Permalink
fix(OpenAPITools#9328): Gradle Plugin ValidateTask does not work unde…
Browse files Browse the repository at this point in the history
…r Gradle 7.0 (OpenAPITools#9453)

* Allow to run ValidateTask tests against Gradle 7.0 as well

* Drop setters for ValidateTask properties of mutable types

This is applicable to the following properties:
- inputSpec
- recommend

Otherwise, Gradle 7.0 now returns an error when attempting
to configure this task.

See https://docs.gradle.org/7.0/userguide/validation_problems.html#mutable_type_with_setter

* Be more specific about the versions of Gradle tested in ValidateTaskDslTest

Test against the latests Gradle 5.x and 6.x versions
  • Loading branch information
rm3l authored Jun 3, 2021
1 parent 972ad56 commit a8c4cbd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ import org.openapitools.codegen.validations.oas.RuleConfiguration
open class ValidateTask : DefaultTask() {
@get:InputFile
@PathSensitive(PathSensitivity.RELATIVE)
var inputSpec = project.objects.property<String>()
val inputSpec = project.objects.property<String>()

@Optional
@Input
var recommend = project.objects.property<Boolean?>()
val recommend = project.objects.property<Boolean?>()

@Suppress("unused")
@get:Internal
Expand Down Expand Up @@ -128,4 +128,4 @@ open class ValidateTask : DefaultTask() {
out.println("Spec is valid.")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.openapitools.generator.gradle.plugin
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome.FAILED
import org.gradle.testkit.runner.TaskOutcome.SUCCESS
import org.gradle.util.GradleVersion
import org.testng.annotations.DataProvider
import org.testng.annotations.Test
import java.io.File
import kotlin.test.assertEquals
Expand All @@ -11,8 +13,25 @@ import kotlin.test.assertTrue
class ValidateTaskDslTest : TestBase() {
override var temp: File = createTempDir(javaClass.simpleName)

@Test
fun `openApiValidate should fail on non-file spec`() {
@DataProvider(name = "gradle_version_provider")
fun gradleVersionProvider(): Array<Array<String?>> = arrayOf(
arrayOf(null), // uses the version of Gradle used to build the plugin itself
arrayOf("5.6.4"),
arrayOf("6.9"),
arrayOf("7.0"))

private fun getGradleRunner(gradleVersion: String?): GradleRunner {
val gradleRunner = GradleRunner.create()
return if (gradleVersion.isNullOrBlank()) {
//Use the current version of Gradle
gradleRunner
} else {
gradleRunner.withGradleVersion(gradleVersion)
}
}

@Test(dataProvider = "gradle_version_provider")
fun `openApiValidate should fail on non-file spec`(gradleVersion: String?) {
// Arrange
withProject("""
| plugins {
Expand All @@ -25,20 +44,28 @@ class ValidateTaskDslTest : TestBase() {
""".trimMargin())

// Act
val result = GradleRunner.create()
val result = getGradleRunner(gradleVersion)
.withProjectDir(temp)
.withArguments("openApiValidate")
.withPluginClasspath()
.buildAndFail()

// Assert
assertTrue(result.output.contains("some_location' specified for property 'inputSpec' does not exist"), "Unexpected/no message presented to the user for a spec pointing to an invalid URI.")
val gradleActualVersion = gradleVersion ?: GradleVersion.current().version
val gradleVersionParts = gradleActualVersion.split(".")
val isBeforeGradle7 = (gradleVersionParts.isEmpty() || gradleVersionParts[0].toInt() < 7)
val expectedMessage = if (isBeforeGradle7) {
"some_location' specified for property 'inputSpec' does not exist"
} else {
"An input file was expected to be present but it doesn't exist."
}
assertTrue(result.output.contains(expectedMessage), "Unexpected/no message presented to the user for a spec pointing to an invalid URI.")
assertEquals(FAILED, result.task(":openApiValidate")?.outcome,
"Expected a failed run, but found ${result.task(":openApiValidate")?.outcome}")
}

@Test
fun `openApiValidate should succeed on valid spec`() {
@Test(dataProvider = "gradle_version_provider")
fun `openApiValidate should succeed on valid spec`(gradleVersion: String?) {
// Arrange
val projectFiles = mapOf(
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml")
Expand All @@ -55,7 +82,7 @@ class ValidateTaskDslTest : TestBase() {
""".trimMargin(), projectFiles)

// Act
val result = GradleRunner.create()
val result = getGradleRunner(gradleVersion)
.withProjectDir(temp)
.withArguments("openApiValidate")
.withPluginClasspath()
Expand All @@ -67,8 +94,8 @@ class ValidateTaskDslTest : TestBase() {
"Expected a successful run, but found ${result.task(":openApiValidate")?.outcome}")
}

@Test
fun `openApiValidate should fail on invalid spec`() {
@Test(dataProvider = "gradle_version_provider")
fun `openApiValidate should fail on invalid spec`(gradleVersion: String?) {
// Arrange
val projectFiles = mapOf(
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid.yaml")
Expand All @@ -84,7 +111,7 @@ class ValidateTaskDslTest : TestBase() {
""".trimMargin(), projectFiles)

// Act
val result = GradleRunner.create()
val result = getGradleRunner(gradleVersion)
.withProjectDir(temp)
.withArguments("openApiValidate")
.withPluginClasspath()
Expand All @@ -96,4 +123,4 @@ class ValidateTaskDslTest : TestBase() {
"Expected a failed run, but found ${result.task(":openApiValidate")?.outcome}")
}

}
}

0 comments on commit a8c4cbd

Please sign in to comment.