forked from tadfisher/gradle2nix
/
TestUtil.kt
300 lines (253 loc) · 8.33 KB
/
TestUtil.kt
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package org.nixos.gradle2nix
import com.adobe.testing.s3mock.S3MockApplication
import com.adobe.testing.s3mock.junit5.S3MockExtension
import com.adobe.testing.s3mock.testsupport.common.S3MockStarter
import com.squareup.moshi.Moshi
import dev.minutest.ContextBuilder
import dev.minutest.MinutestFixture
import dev.minutest.Node
import dev.minutest.TestContextBuilder
import dev.minutest.afterEach
import dev.minutest.beforeEach
import dev.minutest.experimental.SKIP
import dev.minutest.experimental.TransformingAnnotation
import dev.minutest.given
import dev.minutest.givenClosable
import dev.minutest.given_
import io.javalin.Javalin
import io.javalin.http.staticfiles.Location
import okio.buffer
import okio.source
import org.gradle.internal.classpath.DefaultClassPath
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.internal.PluginUnderTestMetadataReading
import org.gradle.util.GradleVersion
import org.junit.jupiter.api.Assumptions.assumeTrue
import strikt.api.Assertion
import strikt.assertions.map
import java.io.Closeable
import java.io.File
import java.io.StringWriter
import java.util.concurrent.atomic.AtomicBoolean
private val moshi = Moshi.Builder().build()
val fixtureRoot = File(System.getProperty("fixtures"))
val gradleVersion = System.getProperty("compat.gradle.version")
?.let(GradleVersion::version)
?: GradleVersion.current()
val GRADLE_4_5 = GradleVersion.version("4.5")
fun GRADLE_MIN(version: String) = object : TransformingAnnotation() {
override fun <F> transform(node: Node<F>): Node<F> =
if (gradleVersion < GradleVersion.version(version)) SKIP.transform(node) else node
}
private fun File.initscript() = resolve("init.gradle").also {
it.writer().use { out ->
val classpath = DefaultClassPath.of(PluginUnderTestMetadataReading.readImplementationClasspath())
.asFiles.joinToString { n -> "'$n'" }
out.append("""
initscript {
dependencies {
classpath files($classpath)
}
}
apply plugin: org.nixos.gradle2nix.Gradle2NixPlugin
""".trimIndent())
}
}
fun File.buildKotlin(
script: String,
configurations: List<String> = emptyList(),
subprojects: List<String> = emptyList()
): DefaultBuild {
assumeTrue(gradleVersion >= GRADLE_4_5)
resolve("build.gradle.kts").writeText(script)
return build(configurations, subprojects)
}
private fun File.build(
configurations: List<String>,
subprojects: List<String>,
extraArguments: List<String> = emptyList()
): DefaultBuild {
val log = StringWriter()
val result = GradleRunner.create()
.withGradleVersion(gradleVersion.version)
.withProjectDir(this)
.forwardStdOutput(log)
.forwardStdError(log)
.withArguments(
"nixModel",
"--init-script=${initscript()}",
"--stacktrace",
"-Porg.nixos.gradle2nix.configurations=${configurations.joinToString(",")}",
"-Porg.nixos.gradle2nix.subprojects=${subprojects.joinToString(",")}",
*(extraArguments.toTypedArray())
)
.runCatching { build() }
result.onFailure { error ->
System.err.print(log)
throw error
}
print(log)
return resolve("build/nix/model.json").run {
println(readText())
source().buffer().use { src ->
checkNotNull(moshi.adapter(DefaultBuild::class.java).fromJson(src))
}
}
}
val <T : Iterable<Artifact>> Assertion.Builder<T>.ids: Assertion.Builder<Iterable<String>>
get() = map { it.id.toString() }
private fun File.parents() = generateSequence(parentFile) { it.parentFile }
abstract class ArgumentsSupplier(private val parent: ArgumentsSupplier? = null) {
open val arguments: List<String> = emptyList()
val extraArguments: List<String> get() = (parent?.extraArguments ?: emptyList()) + arguments
}
@MinutestFixture
class RepositoryFixture(
private val server: Javalin,
parent: ArgumentsSupplier? = null
) : ArgumentsSupplier(parent), Closeable {
override fun close() {
server.stop()
}
}
@MinutestFixture
class S3Fixture(
private val name: String,
parent: ArgumentsSupplier? = null
) : ArgumentsSupplier(parent), Closeable {
private val s3mock = S3Mock(
initialBuckets = listOf(name),
secureConnection = false
)
override val arguments: List<String> get() = listOf(
"-Dorg.gradle.s3.endpoint=${s3mock.serviceEndpoint}",
"-Dorg.nixos.gradle2nix.s3test=true"
)
init {
s3mock.startServer()
val s3root = fixtureRoot.resolve(name)
val s3client = s3mock.createS3Client()
require(s3root.exists() && s3root.isDirectory) {
"$name: S3 fixture not found: $s3root"
}
s3root.walkTopDown()
.filter { it.isFile }
.forEach { file ->
val key = file.toRelativeString(s3root)
s3client.putObject(name, key, file)
}
}
override fun close() {
s3mock.stopServer()
}
}
@MinutestFixture
class TestFixture(
val name: String,
val source: File,
parent: ArgumentsSupplier? = null
) : ArgumentsSupplier(parent), Closeable {
val dest: File
init {
require(source.exists() && source.isDirectory) {
"$name: Test fixture not found: $source}"
}
dest = createTempDir(prefix = name, suffix = "")
}
override fun close() {
dest.deleteRecursively()
}
}
@MinutestFixture
data class ProjectFixture(
private val parent: TestFixture,
private val source: File
) : Closeable {
private val dest: File
init {
require(source.exists() && source.isDirectory && parent.source in source.parents()) {
"${parent.name}: Test project not found: $source"
}
val rel = source.toRelativeString(parent.source)
dest = parent.dest.resolve(rel)
}
fun copySource() {
source.copyRecursively(dest, true)
}
fun build(
configurations: List<String> = emptyList(),
subprojects: List<String> = emptyList()
) = dest.build(configurations, subprojects, parent.extraArguments)
override fun close() {
dest.deleteRecursively()
}
}
fun ContextBuilder<*>.withBucket(
name: String,
block: TestContextBuilder<*, S3Fixture>.() -> Unit
) = derivedContext<S3Fixture>("with s3 bucket: $name") {
given_ { parent ->
S3Fixture(name, parent as? ArgumentsSupplier)
}
afterEach { it.close() }
block()
}
fun ContextBuilder<*>.withRepository(
name: String,
block: TestContextBuilder<*, RepositoryFixture>.() -> Unit
) = derivedContext<RepositoryFixture>("with repository: $name") {
given_ { parent ->
RepositoryFixture(
server = Javalin.create { config ->
config.addStaticFiles("${fixtureRoot}/repositories/$name", Location.EXTERNAL)
}.start(9999),
parent = parent as? ArgumentsSupplier
)
}
afterEach { it.close() }
block()
}
fun ContextBuilder<*>.withFixture(
name: String,
block: TestContextBuilder<*, ProjectFixture>.() -> Unit
) = derivedContext<TestFixture>(name) {
val projectRoot = fixtureRoot.resolve(name).also {
check(it.exists()) { "$name: project fixture not found: $it" }
}
given_ { parent ->
TestFixture(name, projectRoot, parent as? ArgumentsSupplier)
}
val testRoots = projectRoot.listFiles()!!
.filter { it.isDirectory }
.map { it.absoluteFile }
.toList()
testRoots.forEach { testRoot ->
derivedContext<ProjectFixture>(testRoot.name) {
given_ { parent -> ProjectFixture(parent, testRoot) }
beforeEach { copySource() }
afterEach { close() }
block()
}
}
}
class S3Mock(
initialBuckets: List<String> = emptyList(),
secureConnection: Boolean = true
) : S3MockStarter(
mapOf(
S3MockApplication.PROP_INITIAL_BUCKETS to initialBuckets.joinToString(","),
S3MockApplication.PROP_SECURE_CONNECTION to secureConnection
)
) {
private val running = AtomicBoolean()
fun startServer() {
if (running.compareAndSet(false, true)) {
start()
}
}
fun stopServer() {
if (running.compareAndSet(true, false)) {
stop()
}
}
}