From 9269110ecb00dae8282da03790cad655848d2604 Mon Sep 17 00:00:00 2001 From: Andrew Oberstar Date: Sat, 5 Oct 2024 12:59:31 -0500 Subject: [PATCH] chore: Test for multi project service plugin This still hasn't reproduced #356. I also tried creating a separate test project and could not reproduce it. Until someone experiencing this issue reports how to reproduce it'll be closed as invalid. --- .../grgit/gradle/GrgitPluginCompatTest.groovy | 1 - .../GrgitServicePluginCompatTest.groovy | 6 +- ...ServicePluginMultiProjectCompatTest.groovy | 153 ++++++++++++++++++ 3 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginMultiProjectCompatTest.groovy diff --git a/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitPluginCompatTest.groovy b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitPluginCompatTest.groovy index 92225c6c..a1b3fc34 100644 --- a/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitPluginCompatTest.groovy +++ b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitPluginCompatTest.groovy @@ -106,7 +106,6 @@ task doStuff { private BuildResult build(String... args) { return GradleRunner.create() .withGradleVersion(System.properties['compat.gradle.version']) - .withPluginClasspath() .withProjectDir(projectDir) .forwardOutput() .withArguments((args + '--stacktrace') as String[]) diff --git a/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginCompatTest.groovy b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginCompatTest.groovy index 2faebefe..5c5027dd 100644 --- a/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginCompatTest.groovy +++ b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginCompatTest.groovy @@ -56,7 +56,7 @@ class DoStuffTask extends DefaultTask { given: // nothing when: - def result = buildAndFail('doStuff', '--configuration-cache') + def result = buildAndFail('doStuff', '--no-configuration-cache') then: result.task(':doStuff').outcome == TaskOutcome.FAILED } @@ -69,7 +69,7 @@ class DoStuffTask extends DefaultTask { git.commit(message: 'yay') git.tag.add(name: '1.0.0') when: - def result = build('doStuff', '--quiet', '--configuration-cache') + def result = build('doStuff', '--quiet', '--no-configuration-cache') then: result.task(':doStuff').outcome == TaskOutcome.SUCCESS result.output.normalize() == '1.0.0\n' @@ -83,7 +83,7 @@ class DoStuffTask extends DefaultTask { git.commit(message: 'yay') git.tag.add(name: '1.0.0') when: - def result = build('doStuff', '--info', '--configuration-cache') + def result = build('doStuff', '--info', '--no-configuration-cache') then: result.task(':doStuff').outcome == TaskOutcome.SUCCESS result.output.contains('Closing Git repo') diff --git a/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginMultiProjectCompatTest.groovy b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginMultiProjectCompatTest.groovy new file mode 100644 index 00000000..c9984ccb --- /dev/null +++ b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginMultiProjectCompatTest.groovy @@ -0,0 +1,153 @@ +package org.ajoberstar.grgit.gradle + +import spock.lang.Specification + +import org.ajoberstar.grgit.Grgit +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.TaskOutcome +import spock.lang.TempDir + +class GrgitServicePluginMultiProjectCompatTest extends Specification { + @TempDir File tempDir + File projectDir + File settingsFile + File buildRootFile + File build1File + File build2File + + def setup() { + projectDir = new File(tempDir, 'project') + settingsFile = projectFile('settings.gradle') + settingsFile << """\ +pluginManagement { + repositories { + mavenCentral() + mavenLocal() + } +} + +include 'sub1', 'sub2' +""" + + build1File = projectFile('sub1/build.gradle') + build1File << """\ +import org.ajoberstar.grgit.gradle.GrgitService + +plugins { + id 'org.ajoberstar.grgit.service' version '${System.properties['compat.plugin.version']}' +} + +tasks.register("doStuff", DoStuffTask.class) { + service = grgitService.service +} + +class DoStuffTask extends DefaultTask { + @Input + final Property service + + @Inject + DoStuffTask(ObjectFactory objectFactory) { + this.service = objectFactory.property(GrgitService.class); + } + + @TaskAction + void execute() { + println service.get().grgit.describe() + } +} +""" + + build2File = projectFile('sub2/build.gradle') + build2File << """\ +import org.ajoberstar.grgit.gradle.GrgitService + +plugins { + id 'org.ajoberstar.grgit.service' version '${System.properties['compat.plugin.version']}' +} + +tasks.register("doStuff", DoStuffTask.class) { + service = grgitService.service +} + +class DoStuffTask extends DefaultTask { + @Input + final Property service + + @Inject + DoStuffTask(ObjectFactory objectFactory) { + this.service = objectFactory.property(GrgitService.class); + } + + @TaskAction + void execute() { + println service.get().grgit.describe() + } +} +""" + } + + def 'with no repo, accessing service fails'() { + given: + // nothing + when: + def result = buildAndFail('doStuff', '--no-configuration-cache') + then: + result.task(':sub1:doStuff')?.outcome in [null, TaskOutcome.FAILED] + result.task(':sub2:doStuff')?.outcome in [null, TaskOutcome.FAILED] + } + + def 'with repo, plugin opens the repo as grgit'() { + given: + Grgit git = Grgit.init(dir: projectDir) + projectFile('1.txt') << '1' + git.add(patterns: ['1.txt']) + git.commit(message: 'yay') + git.tag.add(name: '1.0.0') + when: + def result = build('doStuff', '--quiet', '--no-configuration-cache') + then: + result.task(':sub1:doStuff')?.outcome == TaskOutcome.SUCCESS + result.task(':sub2:doStuff')?.outcome == TaskOutcome.SUCCESS + result.output.normalize() == '1.0.0\n1.0.0\n' + } + + def 'with repo, plugin closes the repo after build is finished'() { + given: + Grgit git = Grgit.init(dir: projectDir) + projectFile('1.txt') << '1' + git.add(patterns: ['1.txt']) + git.commit(message: 'yay') + git.tag.add(name: '1.0.0') + when: + def result = build('doStuff', '--info', '--no-configuration-cache') + then: + result.task(':sub1:doStuff')?.outcome == TaskOutcome.SUCCESS + result.task(':sub2:doStuff')?.outcome == TaskOutcome.SUCCESS + result.output.contains('Closing Git repo') + } + + private BuildResult build(String... args) { + return GradleRunner.create() + .withGradleVersion(System.properties['compat.gradle.version']) + .withProjectDir(projectDir) + .forwardOutput() + .withArguments((args + '--stacktrace') as String[]) + .build() + } + + private BuildResult buildAndFail(String... args) { + return GradleRunner.create() + .withGradleVersion(System.properties['compat.gradle.version']) + .withProjectDir(projectDir) + .forwardOutput() + .withArguments((args + '--stacktrace') as String[]) + .buildAndFail() + } + + private File projectFile(String path) { + File file = new File(projectDir, path) + file.parentFile.mkdirs() + return file + } +}