Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Gradle Plugin ValidateTask not working under Gradle 7.0 #9453

Merged
merged 3 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}")
}

}
}