jenkinsci / gradle-jpi-plugin

Build Jenkins Plugins with Gradle
79 stars 50 forks source link

Improve Generated Tests #183

Closed sghill closed 3 years ago

sghill commented 3 years ago

This change simplifies test generation, adds test coverage, makes use of lazy configuration, and isolates generated tests for less surprising behavior.

Test generation is disabled by default before and after this change.

Test Task

test now depends on generateTestHpl, which is needed for running @JenkinsRule tests that expect the plugin to be installed. There is now a test verifying this works as expected, which revealed that the jcip-annotations and findbugs both need to be on the testCompileOnly classpath to avoid compiler warnings.

Previously testClasses depended on generateTestHpl, but the hpl is needed for running the tests, not generating them.

This should reduce the amount of prior knowledge needed to write plugins. For example, I have been using JUnit 5 for unit tests for a long time, not realize that this would skip the generated tests unless I explicitly included the junit-vintage-engine. That's no longer the case with a dedicated task.

GenerateJenkinsTests Task

Formerly the insertTest task, this now models the generated class with the excellent JavaPoet library. The generated class has a test that compiles it with -Werror to ensure we don't generate with warnings.

This task is implemented using the worker api, just like the checkAccessModifier task introduced in #162. The benefit of this approach is using an isolated classpath that does not leak dependencies on the buildscript classpath.

GeneratedJenkinsTest Task

This task is newly introduced. check depends on generatedJenkinsTest, so it will be run with a typical build. This task also depends on generateJenkinsTests. Like with the test task:

Having an isolated test task for the generated tests improves things in a couple ways:

Fixes #180 Related to #125