-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathGenerateTaskConfigurationCacheTest.kt
More file actions
129 lines (107 loc) · 4.6 KB
/
GenerateTaskConfigurationCacheTest.kt
File metadata and controls
129 lines (107 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.openapitools.generator.gradle.plugin
import org.gradle.testkit.runner.TaskOutcome
import org.testng.SkipException
import org.testng.annotations.BeforeMethod
import org.testng.annotations.DataProvider
import org.testng.annotations.Test
import java.io.File
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class GenerateTaskConfigurationCacheTest : TestBase() {
private lateinit var projectDirCC: File
@BeforeMethod
override fun before() {
initialize()
projectDirCC = temp.resolve("projectDirCC").apply { mkdir() }
}
@DataProvider(name = "gradle_version_provider")
private fun gradleVersionProviderWithConfigurationCache(): Array<Array<String>> = arrayOf(arrayOf("8.14.4"), arrayOf("8.14.4"))
@DataProvider(name = "gradle_version_provider_without_cc")
private fun gradleVersionProviderWithoutConfigurationCache(): Array<Array<String>> = arrayOf(arrayOf("5.6.1"))
// inputSpec tests
private val inputSpecExtensionContents = """
generatorName = "kotlin"
inputSpec = file("spec.yaml").absolutePath
cleanupOutput.set(true)
""".trimIndent()
@Test(dataProvider = "gradle_version_provider")
fun `openApiGenerate should reuse configuration cache`(gradleVersion: String) {
// Arrange
withProject(inputSpecExtensionContents)
// Act
val result1 = build {
withProjectDir(projectDirCC)
withArguments("--configuration-cache", "clean", "openApiGenerate")
withGradleVersion(gradleVersion)
}
val expectedRelativeFilePathSet = projectDirCC.toRelativeFilePathSet()
val result2 = build {
withProjectDir(projectDirCC)
withArguments("--configuration-cache", "clean", "openApiGenerate")
withGradleVersion(gradleVersion)
}
// Assert
assertEquals(TaskOutcome.SUCCESS, result1.task(":openApiGenerate")?.outcome)
assertTrue(result1.output.contains("Configuration cache entry stored."))
assertEquals(TaskOutcome.SUCCESS, result2.task(":openApiGenerate")?.outcome)
assertTrue(result2.output.contains("Configuration cache entry reused."))
assertEquals(expectedRelativeFilePathSet, projectDirCC.toRelativeFilePathSet())
}
private fun getJavaVersion(): Int {
val version = System.getProperty("java.version")
val parts = version.split('.')
if (parts.first() == "1") return parts.getOrElse(1) { "0" }.toInt()
return parts.first().toInt()
}
@Test(dataProvider = "gradle_version_provider_without_cc")
fun `openApiGenerate should work with Gradle legacy versions`(gradleVersion: String) {
if(getJavaVersion() > 12) {
// https://docs.gradle.org/current/userguide/compatibility.html
throw SkipException("Skipping test as Gradle ${gradleVersion} is not compatible with Java ${getJavaVersion()}")
}
// Arrange
withProject(inputSpecExtensionContents)
// Act
val result1 = build {
withProjectDir(projectDirCC)
withArguments("clean", "openApiGenerate")
withGradleVersion(gradleVersion)
}
val expectedRelativeFilePathSet = projectDirCC.toRelativeFilePathSet()
val result2 = build {
withProjectDir(projectDirCC)
withArguments("clean", "openApiGenerate")
withGradleVersion(gradleVersion)
}
// Assert
assertEquals(TaskOutcome.SUCCESS, result1.task(":openApiGenerate")?.outcome)
assertEquals(TaskOutcome.SUCCESS, result2.task(":openApiGenerate")?.outcome)
assertEquals(expectedRelativeFilePathSet, projectDirCC.toRelativeFilePathSet())
}
// Helper methods & test fixtures
private fun File.toRelativeFilePathSet() =
resolve("build").walk().map { it.toRelativeString(resolve("build")) }.toSet()
private fun withProject(extensionContents: String) {
val settingsContents = """
rootProject.name = "openapi-generator"
""".trimIndent()
val buildContents = """
plugins {
id 'base'
id 'org.openapi.generator'
}
openApiGenerate {
$extensionContents
}
""".trimIndent()
val projectFiles = mapOf(
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml")!!
)
withProject(
projectDir = projectDirCC,
settingsContents = settingsContents,
buildContents = buildContents,
projectFiles = projectFiles
)
}
}