Hello. Let’s take the following Gradle plugin with the HEAD at commit fc0556f: https://github.com/cosminpolifronie/gradle-plantuml-plugin/tree/fc0556fb019b211cb6dd6f4cff2ecaf1201261e0
We shall look at the following part of the code:
File src/main/groovy/de/gafertp/plantuml/PlantUmlPlugin.groovy (lines 7-17):
void apply(Project project) {
def extension = project.extensions.create('plantUml', PlantUmlPluginExtension)
project.tasks.register('plantUml', PlantUmlTask) {
prepareRenders(project, extension.receivedRenders).each { entry ->
inputFiles << entry.input
outputFiles << entry.output
inputPreparedRenderMap << [(entry.input): entry]
}
}
}
Why using tasks.register
the following closure gets executed, but when using tasks.create
it doesn’t?
Then, let’s take this test:
File src/test/groovy/de/gafertp/plantuml/PlantUmlPluginTest.groovy (lines 156-168):
@Test
void rejects_multiple_files_if_no_format_is_specified() {
buildFile << """
plantUml {
render input: '${diagramDir.name}/*', output: 'output/sub'
}
"""
def result = plantUmlTaskExecution().buildAndFail()
assert result.task(':plantUml').outcome == FAILED
assert result.output.toLowerCase().contains('must be explicitly specified via "format: \'xxx\'"')
}
Using the method tasks.register
, this test fails because the line assert result.task(':plantUml').outcome == FAILED
throws a NullPointerException
. It throws this because result.task(':plantUml')
returns null
that doesn’t have an outcome
property. Makes sense.
This is happening because the build fails in the task creation stage (the closure following tasks.register
throws an exception), so that task doesn’t exist yet because it failed to create. This also makes sense. I can write this test to circuvent this, but should I?
I tried using tasks.create
instead in the first code snippet, but then all the tests fail because the closure after that doesn’t get executed, so the Task
isn’t properly configured.
Am I approaching this right? Have I created the task/plugin properly? In my mind, the task should be created successfully and then configured and what not (so that the test above passes), but how can I do that?