From 026105d275dd9ec54a6f2944029956fac8e07525 Mon Sep 17 00:00:00 2001 From: Konstantin Kolmar Date: Wed, 22 Nov 2023 22:03:07 +0100 Subject: [PATCH] Tests for MpsExecute --- .../de/itemis/mps/gradle/tasks/MpsExecute.kt | 2 - .../de/itemis/mps/gradle/MpsCheckTaskTest.kt | 6 +- .../itemis/mps/gradle/MpsExecuteTaskTest.kt | 118 ++++++++++++++++++ .../solutions/NewSolution/NewSolution.msd | 4 + .../models/NewSolution.myModel.mps | 86 +++++++++++++ 5 files changed, 210 insertions(+), 6 deletions(-) create mode 100644 src/test/kotlin/test/de/itemis/mps/gradle/MpsExecuteTaskTest.kt diff --git a/src/main/kotlin/de/itemis/mps/gradle/tasks/MpsExecute.kt b/src/main/kotlin/de/itemis/mps/gradle/tasks/MpsExecute.kt index 5eb79053..a716c8fc 100644 --- a/src/main/kotlin/de/itemis/mps/gradle/tasks/MpsExecute.kt +++ b/src/main/kotlin/de/itemis/mps/gradle/tasks/MpsExecute.kt @@ -24,11 +24,9 @@ abstract class MpsExecute : JavaExec() { abstract val mpsHome: DirectoryProperty @get:Internal - @get:Optional abstract val mpsVersion: Property @get:Internal - @get:Optional abstract val projectLocation: DirectoryProperty @get:Classpath diff --git a/src/test/kotlin/test/de/itemis/mps/gradle/MpsCheckTaskTest.kt b/src/test/kotlin/test/de/itemis/mps/gradle/MpsCheckTaskTest.kt index cbbb64cc..0d9f630d 100644 --- a/src/test/kotlin/test/de/itemis/mps/gradle/MpsCheckTaskTest.kt +++ b/src/test/kotlin/test/de/itemis/mps/gradle/MpsCheckTaskTest.kt @@ -1,8 +1,6 @@ package test.de.itemis.mps.gradle -import de.itemis.mps.gradle.tasks.MpsCheckErrors -import org.gradle.api.GradleException -import org.gradle.api.invocation.Gradle +import de.itemis.mps.gradle.ErrorMessages import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.TaskOutcome import org.hamcrest.CoreMatchers.containsString @@ -67,7 +65,7 @@ class MpsCheckTaskTest { val result = gradleRunner().withArguments("checkProject").buildAndFail() Assert.assertEquals(TaskOutcome.FAILED, result.task(":checkProject")?.outcome) - assertThat(result.output, containsString(MpsCheckErrors.noMpsProjectIn(testProjectDir.root.canonicalFile))) + assertThat(result.output, containsString(ErrorMessages.noMpsProjectIn(testProjectDir.root.canonicalFile))) } @Test diff --git a/src/test/kotlin/test/de/itemis/mps/gradle/MpsExecuteTaskTest.kt b/src/test/kotlin/test/de/itemis/mps/gradle/MpsExecuteTaskTest.kt new file mode 100644 index 00000000..f2b08622 --- /dev/null +++ b/src/test/kotlin/test/de/itemis/mps/gradle/MpsExecuteTaskTest.kt @@ -0,0 +1,118 @@ +package test.de.itemis.mps.gradle + +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome +import org.junit.Assert +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File + +class MpsExecuteTaskTest { + @Rule + @JvmField + val testProjectDir: TemporaryFolder = TemporaryFolder() + + private lateinit var buildFile: File + private lateinit var mpsTestProjectPath: File + + @Before + fun setUp() { + buildFile = testProjectDir.newFile("build.gradle.kts") + + val settingsFile = testProjectDir.newFile("settings.gradle.kts") + settingsFile.writeText(settingsScriptBoilerplate()) + + mpsTestProjectPath = testProjectDir.newFolder("mps-prj") + extractTestProject("test-project", mpsTestProjectPath) + } + + private fun settingsScriptBoilerplate() = """ + plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version ("0.7.0") + } + """.trimIndent() + + private fun buildScriptBoilerplate(mpsVersion: String) = """ + import de.itemis.mps.gradle.tasks.MpsExecute + + plugins { + id("de.itemis.mps.gradle.common") + id("generate-models") + } + + repositories { + mavenCentral() + maven("https://artifacts.itemis.cloud/repository/maven-mps") + } + + val mps = configurations.create("mps") + + dependencies { + mps("com.jetbrains:mps:$mpsVersion") + } + + val resolveMps by tasks.registering(Sync::class) { + from({ zipTree(mps.singleFile) }) + into(layout.buildDirectory.dir("mps")) + } + + generate { + projectLocation = file("${mpsTestProjectPath.canonicalPath}") + mpsConfig = mps + } + + val generate by tasks.existing { + dependsOn(resolveMps) + doFirst { + println(layout.buildDirectory.dir("mps").get().asFile.listFiles()?.toList()) + } + } + + val execute by tasks.registering(MpsExecute::class) { + dependsOn(generate) + mpsHome.set(layout.buildDirectory.dir("mps")) + projectLocation.set(file("${mpsTestProjectPath.canonicalPath}")) + doFirst { + println(resolveMps.map { it.destinationDir }.get()) + } + } + """.trimIndent() + "\n" + + @Test + fun `execute with Project`() { + buildFile.writeText(buildScriptBoilerplate("2021.3.4") + """ + execute { + module.set("NewSolution") + className.set("NewSolution.myModel.MyClass") + method.set("onlyProject") + } + """.trimIndent()) + + val result = gradleRunner().withArguments("execute").build() + + Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":execute")?.outcome) + } + + @Test + fun `execute with Project and args`() { + buildFile.writeText(buildScriptBoilerplate("2021.3.4") + """ + execute { + module.set("NewSolution") + className.set("NewSolution.myModel.MyClass") + method.set("projectAndArgs") + + methodArguments.set(listOf("arg1", "arg2")) + } + """.trimIndent()) + + val result = gradleRunner().withArguments("execute").build() + + Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":execute")?.outcome) + } + + private fun gradleRunner(): GradleRunner = GradleRunner.create() + .withProjectDir(testProjectDir.root) + .withPluginClasspath() +} diff --git a/src/test/resources/test-project/solutions/NewSolution/NewSolution.msd b/src/test/resources/test-project/solutions/NewSolution/NewSolution.msd index 0b2f2daf..188f59c1 100644 --- a/src/test/resources/test-project/solutions/NewSolution/NewSolution.msd +++ b/src/test/resources/test-project/solutions/NewSolution/NewSolution.msd @@ -13,6 +13,7 @@ 6354ebe7-c22a-4a0f-ac54-50b52ab9b065(JDK) + 6ed54515-acc8-4d1e-a16c-9fd6cfe951ea(MPS.Core) @@ -29,7 +30,10 @@ + + + diff --git a/src/test/resources/test-project/solutions/NewSolution/models/NewSolution.myModel.mps b/src/test/resources/test-project/solutions/NewSolution/models/NewSolution.myModel.mps index 8a525c35..6d4064b9 100644 --- a/src/test/resources/test-project/solutions/NewSolution/models/NewSolution.myModel.mps +++ b/src/test/resources/test-project/solutions/NewSolution/models/NewSolution.myModel.mps @@ -5,12 +5,17 @@ + + + + + @@ -53,16 +58,35 @@ + + + + + + + + + + + + + + + + + + + @@ -93,6 +117,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +