From a00417b278b5232f8fa9d25246ccf956c19f534e Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Mon, 21 Aug 2023 19:56:35 +0200 Subject: [PATCH] fix: Add main sourceSet output to the inputs Fixes #24 --- build.gradle | 29 ++++- .../JandexPluginPluginFunctionalTest.groovy | 116 ++++++++++++++++++ .../gradle/plugin/jandex/JandexPlugin.groovy | 3 + 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/functionalTest/groovy/org/kordamp/gradle/plugin/jandex/JandexPluginPluginFunctionalTest.groovy diff --git a/build.gradle b/build.gradle index 113d9f3..f949366 100644 --- a/build.gradle +++ b/build.gradle @@ -56,6 +56,11 @@ dependencies { compileOnly "io.smallrye:jandex:$jandexVersion" api "org.kordamp.gradle:base-gradle-plugin:$kordampPluginVersion" + + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2' + testImplementation 'org.assertj:assertj-core:3.11.1' + + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' } processResources { @@ -68,4 +73,26 @@ processResources { jandexVersion: jandexVersion ) } -} \ No newline at end of file +} + +// Add a source set for the functional test suite +sourceSets { + functionalTest { + } +} + +gradlePlugin.testSourceSets(sourceSets.functionalTest) +configurations.functionalTestImplementation.extendsFrom(configurations.testImplementation) +configurations.functionalTestRuntimeOnly.extendsFrom(configurations.testRuntimeOnly) + +// Add a task to run the functional tests +tasks.register('functionalTest', Test) { + testClassesDirs = sourceSets.functionalTest.output.classesDirs + classpath = sourceSets.functionalTest.runtimeClasspath + useJUnitPlatform() +} + +tasks.named('check') { + // Run the functional tests as part of `check` + dependsOn(tasks.functionalTest) +} diff --git a/src/functionalTest/groovy/org/kordamp/gradle/plugin/jandex/JandexPluginPluginFunctionalTest.groovy b/src/functionalTest/groovy/org/kordamp/gradle/plugin/jandex/JandexPluginPluginFunctionalTest.groovy new file mode 100644 index 0000000..e38a1e3 --- /dev/null +++ b/src/functionalTest/groovy/org/kordamp/gradle/plugin/jandex/JandexPluginPluginFunctionalTest.groovy @@ -0,0 +1,116 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2019-2023 Andres Almiray. + * + * 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.kordamp.gradle.plugin.jandex + +import org.assertj.core.api.Assertions +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.Test + +class JandexPluginPluginFunctionalTest { + + private File projectDir + + private getBuildFile() { + new File(projectDir, "build.gradle") + } + + private getSettingsFile() { + new File(projectDir, "settings.gradle") + } + + private getSourceFileAClass() { + new File(projectDir, 'src/main/java/com/sample').mkdirs() + new File(projectDir, 'src/main/java/com/sample/AClass.java') + } + + @Test + void testApply() { + projectDir = new File("build/functionalTestFixture/apply_${System.currentTimeMillis()}") + projectDir.mkdirs() + + def indexFile = new File(projectDir, 'build/resources/main/META-INF/jandex.idx') + Assertions.assertThat(indexFile).doesNotExist(); + + settingsFile.text = "" + buildFile.text = """ +plugins { + id 'org.kordamp.gradle.jandex' +} + +repositories { + mavenCentral() +} +""" + + sourceFileAClass.text = """ +package com.sample; + +public class AClass { + public void sayHi() { + System.out.println("hi"); + } +} +""" + def runner1 = createRunner() + def result1 = runner1.build() + Assertions.assertThat(result1.task(':jandex').outcome).isEqualTo(TaskOutcome.SUCCESS); + Assertions.assertThat(indexFile).exists(); + def content1 = indexFile.getText("UTF-8") + Assertions.assertThat(content1).contains("sayHi"); + + //Re run without any changes, to be sure the task is up-to-date: + def runner2 = createRunner() + def result2 = runner2.build() + Assertions.assertThat(result2.task(':jandex').outcome).isEqualTo(TaskOutcome.UP_TO_DATE); + + //Modify source file, and verify jandex was executed again: + sourceFileAClass.text = """ +package com.sample; + +public class AClass { + public void sayHello() { + System.out.println("hello"); + } +} +""" + def runner3 = createRunner() + def result3 = runner3.build() + Assertions.assertThat(result3.task(':jandex').outcome).isEqualTo(TaskOutcome.SUCCESS); + Assertions.assertThat(indexFile).exists(); + def content3 = indexFile.getText("UTF-8") + Assertions.assertThat(content3) + .isNotEqualTo(content1) + .contains("sayHello"); + + //Re run without any changes, to be sure the task is up-to-date: + def runner4 = createRunner() + def result4 = runner4.build() + Assertions.assertThat(result4.task(':jandex').outcome).isEqualTo(TaskOutcome.UP_TO_DATE); + } + + def createRunner() { + def runner = GradleRunner.create() + runner.forwardOutput() + runner.withPluginClasspath() + runner.withArguments("jandex", "--stacktrace") + runner.withProjectDir(projectDir) + return runner + } +} diff --git a/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexPlugin.groovy b/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexPlugin.groovy index 3c84f13..759afb2 100644 --- a/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexPlugin.groovy +++ b/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexPlugin.groovy @@ -73,6 +73,9 @@ class JandexPlugin implements Plugin { t.processResourcesTask = project.tasks.named('processResources', Copy) t.layout.set(project.layout) t.sourceSets.addAll(project.extensions.findByType(SourceSetContainer)) + if (t.resolvedProcessDefaultFileSet.get()) { + t.inputs.files(t.sourceSets.findByName('main').output.classesDirs*.absolutePath.flatten()) + } } })