/
PrecompiledScriptPluginAccessorsTest.kt
577 lines (471 loc) · 17.2 KB
/
PrecompiledScriptPluginAccessorsTest.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.kotlin.dsl.plugins.precompiled
import com.nhaarman.mockito_kotlin.doAnswer
import com.nhaarman.mockito_kotlin.doReturn
import com.nhaarman.mockito_kotlin.inOrder
import com.nhaarman.mockito_kotlin.mock
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.PluginManager
import org.gradle.kotlin.dsl.fixtures.FoldersDsl
import org.gradle.kotlin.dsl.fixtures.bytecode.InternalName
import org.gradle.kotlin.dsl.fixtures.bytecode.RETURN
import org.gradle.kotlin.dsl.fixtures.bytecode.internalName
import org.gradle.kotlin.dsl.fixtures.bytecode.publicClass
import org.gradle.kotlin.dsl.fixtures.bytecode.publicDefaultConstructor
import org.gradle.kotlin.dsl.fixtures.bytecode.publicMethod
import org.gradle.kotlin.dsl.fixtures.normalisedPath
import org.gradle.kotlin.dsl.fixtures.pluginDescriptorEntryFor
import org.gradle.kotlin.dsl.support.zipTo
import org.gradle.test.fixtures.file.LeaksFileHandles
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.empty
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.not
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.psi.psiUtil.toVisibility
import org.jetbrains.kotlin.psi.psiUtil.visibilityModifierType
import org.junit.Test
import java.io.File
@LeaksFileHandles("Kotlin Compiler Daemon working directory")
class PrecompiledScriptPluginAccessorsTest : AbstractPrecompiledScriptPluginTest() {
@Test
fun `can use type-safe accessors with same name but different meaning in sibling plugins`() {
val externalPlugins = withExternalPlugins()
withFolders {
"buildSrc" {
withDefaultSettingsIn(relativePath)
withKotlinDslPlugin().appendText(
implementationDependencyOn(externalPlugins)
)
withFile("src/main/kotlin/local-app.gradle.kts", """
plugins { `external-app` }
println("*using " + external.name + " from local-app in " + project.name + "*")
""")
withFile("src/main/kotlin/local-lib.gradle.kts", """
plugins { `external-lib` }
println("*using " + external.name + " from local-lib in " + project.name + "*")
""")
}
}
withDefaultSettings().appendText("""
include("foo")
include("bar")
""")
withFolders {
"foo" {
withFile("build.gradle.kts", """
plugins { `local-app` }
""")
}
"bar" {
withFile("build.gradle.kts", """
plugins { `local-lib` }
""")
}
}
assertThat(
build("tasks").output,
allOf(
containsString("*using app from local-app in foo*"),
containsString("*using lib from local-lib in bar*")
)
)
}
private
fun implementationDependencyOn(file: File): String = """
dependencies {
implementation(files("${file.normalisedPath}"))
}
"""
@Test
fun `can use type-safe accessors for the Kotlin Gradle plugin extensions`() {
withKotlinDslPlugin().appendText("""
dependencies {
implementation(kotlin("gradle-plugin"))
}
""")
withPrecompiledKotlinScript("kotlin-library.gradle.kts", """
plugins { kotlin("jvm") }
kotlin { }
tasks.compileKotlin { kotlinOptions { } }
""")
build("generatePrecompiledScriptPluginAccessors")
compileKotlin()
}
@Test
fun `can use type-safe accessors for plugins applied by sibling plugin`() {
withKotlinDslPlugin()
withPrecompiledKotlinScript("my-java-library.gradle.kts", """
plugins { java }
""")
withPrecompiledKotlinScript("my-java-module.gradle.kts", """
plugins { id("my-java-library") }
java { }
tasks.compileJava { }
""")
compileKotlin()
}
@Test
fun `can use type-safe accessors from scripts with same name but different ids`() {
val externalPlugins = withExternalPlugins()
withKotlinDslPlugin()
withKotlinBuildSrc()
withFolders {
"buildSrc" {
existing("build.gradle.kts").appendText(
implementationDependencyOn(externalPlugins)
)
"src/main/kotlin" {
withFile("app/model.gradle.kts", """
package app
plugins { `external-app` }
println("*using " + external.name + " from app/model in " + project.name + "*")
""")
withFile("lib/model.gradle.kts", """
package lib
plugins { `external-lib` }
println("*using " + external.name + " from lib/model in " + project.name + "*")
""")
}
}
}
withDefaultSettings().appendText("""
include("lib")
include("app")
""")
withFolders {
"lib" {
withFile("build.gradle.kts", """
plugins { lib.model }
""")
}
"app" {
withFile("build.gradle.kts", """
plugins { app.model }
""")
}
}
assertThat(
build("tasks").output,
allOf(
containsString("*using app from app/model in app*"),
containsString("*using lib from lib/model in lib*")
)
)
}
@Test
fun `can apply sibling plugin whether it has a plugins block or not`() {
withKotlinDslPlugin()
withPrecompiledKotlinScript("no-plugins.gradle.kts", "")
withPrecompiledKotlinScript("java-plugin.gradle.kts", """
plugins { java }
""")
withPrecompiledKotlinScript("plugins.gradle.kts", """
plugins {
id("no-plugins")
id("java-plugin")
}
java { }
tasks.compileJava { }
""")
compileKotlin()
}
@Test
fun `can apply sibling plugin from another package`() {
withKotlinDslPlugin()
withPrecompiledKotlinScript("my/java-plugin.gradle.kts", """
package my
plugins { java }
""")
withPrecompiledKotlinScript("plugins.gradle.kts", """
plugins { id("my.java-plugin") }
java { }
tasks.compileJava { }
""")
compileKotlin()
}
@Test
fun `generated type-safe accessors are internal`() {
givenPrecompiledKotlinScript("java-project.gradle.kts", """
plugins { java }
""")
val generatedSourceFiles =
existing("build/generated-sources")
.walkTopDown()
.filter { it.isFile }
.toList()
assertThat(
generatedSourceFiles,
not(empty())
)
data class Declaration(
val packageName: String,
val name: String,
val visibility: Visibility?
)
val generatedAccessors =
KotlinParser.run {
withProject {
generatedSourceFiles.flatMap { file ->
parse(file.name, file.readText()).run {
val packageName = packageFqName.asString()
declarations.map { declaration ->
Declaration(
packageName,
declaration.name!!,
declaration.visibilityModifierType()?.toVisibility()
)
}
}
}
}
}
assertThat(
"Only the generated Gradle Plugin wrapper is not internal",
generatedAccessors.filterNot { it.visibility == Visibilities.INTERNAL },
equalTo(
listOf(
Declaration(
"",
"JavaProjectPlugin",
null
)
)
)
)
}
@Test
fun `can use core plugin spec builders`() {
givenPrecompiledKotlinScript("java-project.gradle.kts", """
plugins {
java
}
""")
val (project, pluginManager) = projectAndPluginManagerMocks()
instantiatePrecompiledScriptOf(
project,
"Java_project_gradle"
)
inOrder(pluginManager) {
verify(pluginManager).apply("org.gradle.java")
verifyNoMoreInteractions()
}
}
@Test
fun `can use plugin spec builders for plugins in the implementation classpath`() {
// given:
val pluginId = "my.plugin"
val pluginJar = jarForPlugin(pluginId, "MyPlugin")
withPrecompiledScriptApplying(pluginId, pluginJar)
assertPrecompiledScriptPluginApplies(
pluginId,
"Plugin_gradle"
)
}
private
fun assertPrecompiledScriptPluginApplies(pluginId: String, precompiledScriptClassName: String) {
compileKotlin()
val (project, pluginManager) = projectAndPluginManagerMocks()
instantiatePrecompiledScriptOf(
project,
precompiledScriptClassName
)
inOrder(pluginManager) {
verify(pluginManager).apply(pluginId)
verifyNoMoreInteractions()
}
}
@Test
fun `can use plugin specs with jruby-gradle-plugin`() {
withKotlinDslPlugin().appendText("""
dependencies {
compile("com.github.jruby-gradle:jruby-gradle-plugin:1.4.0")
}
""")
withPrecompiledKotlinScript("plugin.gradle.kts", """
plugins {
com.github.`jruby-gradle`.base
}
""")
assertPrecompiledScriptPluginApplies(
"com.github.jruby-gradle.base",
"Plugin_gradle"
)
}
@Test
fun `plugin application errors are reported but don't cause the build to fail`() {
// given:
val pluginId = "invalid.plugin"
val pluginJar = jarWithInvalidPlugin(pluginId, "InvalidPlugin")
withPrecompiledScriptApplying(pluginId, pluginJar)
gradleExecuterFor(arrayOf("classes")).withStackTraceChecksDisabled().run().apply {
assertRawOutputContains("An exception occurred applying plugin request [id: '$pluginId']")
assertRawOutputContains("'InvalidPlugin' is neither a plugin or a rule source and cannot be applied.")
}
}
private
fun withPrecompiledScriptApplying(pluginId: String, pluginJar: File) {
withKotlinDslPlugin().appendText("""
dependencies {
implementation(files("${pluginJar.normalisedPath}"))
}
""")
withPrecompiledKotlinScript("plugin.gradle.kts", """
plugins { $pluginId }
""")
}
@Test
fun `can use plugin spec builders in multi-project builds with local and external plugins`() {
withProjectRoot(newDir("external-plugins")) {
withFolders {
"external-foo" {
withKotlinDslPlugin()
withFile("src/main/kotlin/external-foo.gradle.kts", """
println("*external-foo applied*")
""")
}
"external-bar" {
withKotlinDslPlugin()
withFile("src/main/kotlin/external-bar.gradle.kts", """
println("*external-bar applied*")
""")
}
withDefaultSettingsIn(relativePath).appendText("""
include("external-foo", "external-bar")
""")
}
build("assemble")
}
val externalFoo = existing("external-plugins/external-foo/build/libs/external-foo.jar")
val externalBar = existing("external-plugins/external-bar/build/libs/external-bar.jar")
withFolders {
"buildSrc" {
"local-foo" {
withFile("src/main/kotlin/local-foo.gradle.kts", """
plugins { `external-foo` }
""")
withKotlinDslPlugin().appendText("""
dependencies {
implementation(files("${externalFoo.normalisedPath}"))
}
""")
}
"local-bar" {
withFile("src/main/kotlin/local-bar.gradle.kts", """
plugins { `external-bar` }
""")
withKotlinDslPlugin().appendText("""
dependencies {
implementation(files("${externalBar.normalisedPath}"))
}
""")
}
withDefaultSettingsIn(relativePath).appendText("""
include("local-foo", "local-bar")
""")
withFile("build.gradle.kts", """
dependencies {
subprojects.forEach {
runtime(project(it.path))
}
}
""")
}
}
withBuildScript("""
plugins {
`local-foo`
`local-bar`
}
""")
assertThat(
build("tasks").output,
allOf(
containsString("*external-foo applied*"),
containsString("*external-bar applied*")
)
)
}
private
fun withExternalPlugins(): File =
withProjectRoot(newDir("external-plugins")) {
withDefaultSettings()
withKotlinDslPlugin()
withFolders {
"src/main/kotlin" {
"extensions" {
withFile("Extensions.kt", """
open class App { var name: String = "app" }
open class Lib { var name: String = "lib" }
""")
}
withFile("external-app.gradle.kts", """
extensions.create("external", App::class)
""")
withFile("external-lib.gradle.kts", """
extensions.create("external", Lib::class)
""")
}
}
build("assemble")
existing("build/libs/external-plugins.jar")
}
private
fun FoldersDsl.withKotlinDslPlugin(): File =
withKotlinDslPluginIn(relativePath)
private
val FoldersDsl.relativePath
get() = root.relativeTo(projectRoot).path
private
fun jarWithInvalidPlugin(id: String, implClass: String): File =
pluginJarWith(
pluginDescriptorEntryFor(id, implClass),
"$implClass.class" to publicClass(InternalName(implClass))
)
private
fun jarForPlugin(id: String, implClass: String): File =
pluginJarWith(
pluginDescriptorEntryFor(id, implClass),
"$implClass.class" to emptyPluginClassNamed(implClass)
)
private
fun emptyPluginClassNamed(implClass: String): ByteArray =
publicClass(InternalName(implClass), interfaces = listOf(Plugin::class.internalName)) {
publicDefaultConstructor()
publicMethod("apply", "(Ljava/lang/Object;)V") {
RETURN()
}
}
private
fun pluginJarWith(vararg entries: Pair<String, ByteArray>): File =
newFile("my.plugin.jar").also { file ->
zipTo(file, entries.asSequence())
}
private
fun projectAndPluginManagerMocks(): Pair<Project, PluginManager> {
val pluginManager = mock<PluginManager>()
val project = mock<Project> {
on { getPluginManager() } doReturn pluginManager
on { project } doAnswer { it.mock as Project }
}
return Pair(project, pluginManager)
}
}