-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.gradle.kts
419 lines (359 loc) · 15.7 KB
/
build.gradle.kts
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
import com.google.common.collect.ImmutableMultimap
import com.google.common.collect.Multimap
import com.google.common.collect.Multimaps
import java.nio.file.Files
import java.util.*
plugins {
base
id("base-conventions")
id("de.itemis.mps.gradle.launcher")
}
val executeGenerators by configurations.creating
val modelcheck by configurations.creating
val execute by configurations.creating
dependencies {
executeGenerators(project(":execute-generators"))
modelcheck(project(":modelcheck"))
execute(project(":execute"))
}
val SUPPORTED_MPS_VERSIONS = arrayOf("2022.3.3", "2023.2.2", "2024.1.1", "2024.3")
val GENERATION_TESTS = listOf(
GenerationTest("generateBuildSolution", "generate-build-solution", listOf("--model", "my.build.script"),
expectation = GenerationTestExpectation.SuccessWithFiles("solutions/my.build/build.xml")),
GenerationTest("generateBuildSolutionParallel", "generate-build-solution",
listOf("--model", "my.build.script", "--parallel-generation-threads=4"),
expectation = GenerationTestExpectation.SuccessWithFiles("solutions/my.build/build.xml")),
GenerationTest("generateSimple", "generate-simple", listOf()),
GenerationTest("generateBuildSolutionWithMpsEnvironment",
"generate-build-solution", listOf("--model", "my.build.script", "--environment", "MPS")),
GenerationTest("generateEmpty", "generate-simple", listOf("--exclude-model", "my.solution.java"),
expectation = GenerationTestExpectation.Empty),
GenerationTest(
name = "generateIncorrect",
project = "generate-with-errors",
args = listOf("--log-level=all"),
expectation = GenerationTestExpectation.Failure),
GenerationTest(
name = "generateExcludeIncorrectModule",
project = "generate-with-errors",
args = listOf("--exclude-module", "solution.with.errors")
),
GenerationTest(
name = "generateExcludeIncorrectModel",
project = "generate-with-errors",
args = listOf("--exclude-model", "solution.with.errors.incorrect")
),
GenerationTest(
name = "generateIncludeCorrectModelOnly",
project = "generate-with-errors",
args = listOf("--model", "correct.model")
)
)
val MODELCHECK_TESTS = listOf(
ModelCheckTest("modelcheckSimple",
project = "modelcheck",
args = listOf("--module", "my.solution.with.errors"),
expectSuccess = false),
ModelCheckTest("modelcheckModule",
project = "modelcheck",
args = listOf("--module", "my.solution.with.module.errors", "--result-format", "module-and-model"),
expectSuccess = false),
ModelCheckTest("modelcheckSimpleWithMpsEnvironment",
project = "modelcheck",
args = listOf("--module", "my.solution.with.errors", "--environment", "MPS"),
expectSuccess = false),
ModelCheckTest("modelcheckExcludeModule",
project = "modelcheck",
args = listOf("--exclude-module", "my.solution.with.*")
),
ModelCheckTest("modelcheckExcludeModuleWithMpsEnvironment",
project = "modelcheck",
args = listOf("--exclude-module", "my.solution.with.*", "--environment", "MPS")
),
ModelCheckTest("modelcheckExcludeModel",
project = "modelcheck",
args = listOf("--exclude-model", "my.solution.with.errors.java", "--exclude-model", "my.solution.with.errors.brokenref")
),
ModelCheckTest("modelcheckBrokenrefs",
project = "modelcheck",
args = listOf("--model", "my.solution.with.errors.brokenref"),
expectSuccess = false
),
ModelCheckTest("modelcheckParallel",
project = "modelcheck",
args = listOf("--module", "my.solution", "--parallel")
)
)
val EXECUTE_TESTS = run {
val commonArgs = arrayOf("--module", "my.solution", "--method", "execute")
listOf(
ExecuteTest(
"executeMethodWithArgumentsPassingArguments", "execute-method",
listOf(*commonArgs, "--class", "my.solution.java.WithArguments", "--arg", "arg1", "--arg", "arg2")
),
ExecuteTest(
"executeMethodWithArgumentsNotPassingArguments",
"execute-method",
listOf(*commonArgs, "--class", "my.solution.java.WithArguments")
),
ExecuteTest(
"executeMethodWithoutArgumentsPassingArguments",
"execute-method",
listOf(*commonArgs, "--class", "my.solution.java.WithoutArguments", "--arg", "arg1", "--arg", "arg2"),
expectSuccess = false
),
ExecuteTest(
"executeMethodWithoutArgumentsNotPassingArguments",
"execute-method",
listOf(*commonArgs, "--class", "my.solution.java.WithoutArguments")
),
ExecuteTest(
"executeMissingMethod", "execute-method",
listOf(*commonArgs, "--class", "my.solution.java.MissingMethod"),
expectSuccess = false
),
ExecuteTest(
"executeMethodInMissingClass", "execute-method",
listOf(*commonArgs, "--class", "my.solution.java.MissingClass"),
expectSuccess = false
)
)
}
/**
* Describes a project to generate with the supported MPS versions
*
* @param project project folder name (in `projects/`)
* @param args additional arguments to the command
*/
data class GenerationTest(val name: String, val project: String, val args: List<Any>,
val expectation: GenerationTestExpectation = GenerationTestExpectation.Success) {
val projectDir = file("projects/$project")
}
interface GenerationTestExpectation {
fun verify(testCase: GenerationTest, exitCode: Int): String?
fun prepare(testCase: GenerationTest) {}
object Success : GenerationTestExpectation {
override fun verify(testCase: GenerationTest, exitCode: Int): String? =
if (exitCode == 0) null else "generation failed unexpectedly (exit value $exitCode)"
}
object Failure : GenerationTestExpectation {
override fun verify(testCase: GenerationTest, exitCode: Int): String? =
if (exitCode == 255) null else "generation did not fail unexpectedly (exit value $exitCode)"
}
object Empty : GenerationTestExpectation {
override fun verify(testCase: GenerationTest, exitCode: Int): String? =
if (exitCode == 254) null else "generation was not empty unexpectedly (exit value $exitCode)"
}
data class SuccessWithFiles(val files: Set<File>) : GenerationTestExpectation {
constructor(vararg fileNames: String) : this(fileNames.map { File(it) }.toSet())
override fun verify(testCase: GenerationTest, exitCode: Int): String? {
if (exitCode != 0) return "generation failed unexpectedly (actual exit value $exitCode)"
val missingFiles = files.filter { !testCase.projectDir.resolve(it).exists() }
if (missingFiles.isEmpty()) return null
return missingFiles.joinToString(prefix = "the following files were not generated in ${testCase.projectDir}: ")
}
override fun prepare(testCase: GenerationTest) {
files.forEach {
val absolutePath = testCase.projectDir.resolve(it).toPath()
if (Files.exists(absolutePath)) Files.delete(absolutePath)
}
}
}
}
/**
* Describes a project to check with the supported MPS versions
*
* @param name test name
* @param project project folder name (in `projects/`)
* @param args additional arguments to the command
*/
data class ModelCheckTest(val name: String, val project: String, val args: List<Any>, val expectSuccess: Boolean = true) {
val projectDir = file("projects/$project")
}
/**
* Describes a project to execute code in generated classes with the supported MPS versions
*
* @param name test name
* @param project project folder name (in `projects/`)
* @param args additional arguments to the command
*/
data class ExecuteTest(val name: String, val project: String, val args: List<Any>, val expectSuccess: Boolean = true) {
val projectDir = file("projects/$project")
}
enum class TestKind {
GENERATE,
MODELCHECK,
EXECUTE
}
fun String.capitalize() = replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.ROOT) else it.toString() }
/**
* Creates and returns the Gradle tasks to perform all the tests with a single MPS version.
* Also creates any necessary Gradle objects (configurations, dependencies, etc.)
*/
fun tasksForMpsVersion(mpsVersion: String): Multimap<TestKind, TaskProvider<out Task>> {
val configuration = configurations.create("mps$mpsVersion")
dependencies.add(configuration.name, "com.jetbrains:mps:$mpsVersion@zip")
val mpsHome = File(buildDir, "mps-$mpsVersion")
val unpackTask = tasks.register("unpackMps$mpsVersion", Sync::class) {
dependsOn(configuration)
from({ configuration.resolve().map(project::zipTree) })
into(mpsHome)
}
fun JavaExecSpec.configureGenerateTaskForSpec(projectDir: File, temporaryDir: File) {
mpsBackendLauncher.forMpsHome(mpsHome)
.withMpsVersion(mpsVersion)
.withJetBrainsJvm()
.withTemporaryDirectory(temporaryDir)
.configure(this)
classpath(fileTree(mpsHome) {
include("lib/**/*.jar")
})
classpath(executeGenerators)
mainClass.set("de.itemis.mps.gradle.generate.MainKt")
args("--project", projectDir)
}
fun cleanBeforeGeneration(projectDir: File) {
println("Deleting generated sources in $projectDir")
delete(fileTree(projectDir) {
include("**/source_gen/**")
include("**/source_gen.caches/**")
include("**/classes_gen/**")
})
println("Deleting MPS caches in ${mpsHome}/system")
delete(fileTree(mpsHome) { include("system/**") })
}
fun JavaExec.configureGenerateTask(projectDir: File) {
configureGenerateTaskForSpec(projectDir, this.temporaryDir)
dependsOn(unpackTask)
doFirst {
cleanBeforeGeneration(projectDir)
}
}
val generateTasks = GENERATION_TESTS.map { testCase ->
tasks.register("generate${testCase.name.capitalize()}WithMps$mpsVersion", JavaExec::class) {
dependsOn(executeGenerators)
configureGenerateTask(testCase.projectDir)
group = LifecycleBasePlugin.VERIFICATION_GROUP
args(testCase.args)
doFirst {
testCase.expectation.prepare(testCase)
}
// Check exit value manually
isIgnoreExitValue = true
doLast {
val actualExitValue = executionResult.get().exitValue
val message = testCase.expectation.verify(testCase, actualExitValue)
if (message != null) {
throw GradleException("Generation test ${testCase.name} failed: $message")
}
}
}
}
val modelcheckTasks = MODELCHECK_TESTS.map { testCase ->
tasks.register("modelcheckTest${testCase.name.capitalize()}WithMps$mpsVersion", JavaExec::class) {
dependsOn(modelcheck)
mpsBackendLauncher.forMpsHome(mpsHome)
.withMpsVersion(mpsVersion)
.withJetBrainsJvm()
.configure(this)
dependsOn(unpackTask)
group = LifecycleBasePlugin.VERIFICATION_GROUP
classpath(fileTree(mpsHome) {
include("lib/**/*.jar")
// modelcheck uses HttpSupportUtil#getURL()
include("plugins/mps-httpsupport/**/*.jar")
})
classpath(modelcheck)
mainClass.set("de.itemis.mps.gradle.modelcheck.MainKt")
args("--project", testCase.projectDir)
args("--result-file", file("$buildDir/TEST-${testCase.name}-mps-${mpsVersion}-results.xml"))
args(testCase.args)
// Check exit value manually
isIgnoreExitValue = true
doLast {
val actualExitValue = executionResult.get().exitValue
val actualSuccess = actualExitValue == 0
if (actualSuccess != testCase.expectSuccess) {
throw GradleException(
"Modelcheck outcome: expected success: ${testCase.expectSuccess}, but was: $actualSuccess" +
" (actual exit value $actualExitValue)"
)
}
}
}
}
val executeTasks: List<TaskProvider<out Task>> = EXECUTE_TESTS.map { testCase ->
tasks.register("executeTest${testCase.name.capitalize()}WithMps$mpsVersion") {
dependsOn(unpackTask)
dependsOn(execute, executeGenerators)
doLast {
cleanBeforeGeneration(testCase.projectDir)
val generationResult = javaexec {
configureGenerateTaskForSpec(testCase.projectDir, temporaryDir)
}
if (generationResult.exitValue != 0) {
throw GradleException("Generation failed with exit code ${generationResult.exitValue}")
}
val executionResult = javaexec {
mpsBackendLauncher.forMpsHome(mpsHome)
.withMpsVersion(mpsVersion)
.withJetBrainsJvm()
.withTemporaryDirectory(temporaryDir)
.configure(this)
group = LifecycleBasePlugin.VERIFICATION_GROUP
classpath(fileTree(mpsHome) {
include("lib/**/*.jar")
})
classpath(execute)
mainClass.set("de.itemis.mps.gradle.execute.MainKt")
args("--project", testCase.projectDir)
args(testCase.args)
isIgnoreExitValue = true
}
val actualExitValue = executionResult.exitValue
val actualSuccess = actualExitValue == 0
val expectedSuccess = testCase.expectSuccess
if (actualSuccess != expectedSuccess) {
throw GradleException(
"Execute outcome: expected success: $expectedSuccess, but was: $actualSuccess" +
" (actual exit value $actualExitValue)"
)
}
}
}
}
return ImmutableMultimap.builder<TestKind, TaskProvider<out Task>>()
.putAll(TestKind.EXECUTE, executeTasks)
.putAll(TestKind.GENERATE, generateTasks)
.putAll(TestKind.MODELCHECK, modelcheckTasks)
.build()
}
data class Key(val kind: TestKind, val mpsVersion: String)
fun buildTestMatrix(): Multimap<Key, TaskProvider<out Task>> {
val builder = ImmutableMultimap.builder<Key, TaskProvider<out Task>>()
for (mpsVersion in SUPPORTED_MPS_VERSIONS) {
val tasksForThisVersion = tasksForMpsVersion(mpsVersion)
for (entry in tasksForThisVersion.entries()) {
builder.put(Key(entry.key, mpsVersion), entry.value)
}
}
return builder.build()
}
val testMatrix = buildTestMatrix()
tasks.named("check") {
dependsOn(testMatrix.values())
}
for (kind in TestKind.values()) {
tasks.register("test${kind.name.lowercase().capitalize()}") {
group = LifecycleBasePlugin.VERIFICATION_GROUP
description = "Run all '${kind.name.lowercase()}' tests"
dependsOn(Multimaps.filterKeys(testMatrix) { it!!.kind == kind }.values())
}
}
for (mpsVersion in SUPPORTED_MPS_VERSIONS) {
tasks.register("testMps$mpsVersion") {
group = LifecycleBasePlugin.VERIFICATION_GROUP
description = "Run all tests with MPS $mpsVersion"
dependsOn(Multimaps.filterKeys(testMatrix) { it!!.mpsVersion == mpsVersion }.values())
}
}