Skip to content

Commit

Permalink
WIP 318 Move Gradle plugin classes to sub package
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheman committed Aug 27, 2024
1 parent 62153f6 commit 3083c4a
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 24 deletions.
2 changes: 1 addition & 1 deletion htmlSanityCheck-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ gradlePlugin {
plugins {
htmlSanityCheck {
id = 'org.aim42.htmlSanityCheck'
implementationClass = 'org.aim42.htmlsanitycheck.HtmlSanityCheckPlugin'
implementationClass = 'org.aim42.htmlsanitycheck.gradle.HtmlSanityCheckPlugin'
displayName = 'Gradle HtmlSanityCheck Plugin'
description = project.description
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// see end-of-file for license information

package org.aim42.htmlsanitycheck
package org.aim42.htmlsanitycheck.gradle

import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -12,12 +12,10 @@ class HtmlSanityCheckPlugin implements Plugin<Project> {
final static String HTML_SANITY_CHECK = "htmlSanityCheck"

void apply(Project project) {

project.task( HTML_SANITY_CHECK,
type: HtmlSanityCheckTask,
description: "performs semantic checks on html files",
group: LifecycleBasePlugin.VERIFICATION_GROUP)

project.tasks.register(HTML_SANITY_CHECK, HtmlSanityCheckTask) {
it.description = "performs semantic checks on html files"
it.group = LifecycleBasePlugin.VERIFICATION_GROUP
}
}
}
// end::gradle-plugin-implementation[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.aim42.htmlsanitycheck
package org.aim42.htmlsanitycheck.gradle

import org.aim42.htmlsanitycheck.AllChecksRunner
import org.aim42.htmlsanitycheck.Configuration
import org.aim42.htmlsanitycheck.check.AllCheckers
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
Expand Down Expand Up @@ -98,8 +100,8 @@ class HtmlSanityCheckTask extends DefaultTask {
outputs.upToDateWhen { false }

// give sensible default for output directory, see https://github.com/aim42/htmlSanityCheck/issues/205
checkingResultsDir = new File(project.buildDir, '/reports/htmlSanityCheck/')
junitResultsDir = new File(project.buildDir, '/test-results/htmlSanityCheck/')
checkingResultsDir = new File(project.DEFAULT_BUILD_DIR_NAME, '/reports/htmlSanityCheck/')
junitResultsDir = new File(project.DEFAULT_BUILD_DIR_NAME, '/test-results/htmlSanityCheck/')


}
Expand Down Expand Up @@ -159,9 +161,6 @@ Your build configuration included 'failOnErrors=true', and ${nrOfFindingsOnAllPa
See ${checkingResultsDir} for a detailed report."""
throw new GradleException(failureMsg)
}
} else {
logger.warn("""Fatal configuration errors preventing checks:\n
${myConfig.toString()}""")
}
}

Expand All @@ -177,7 +176,7 @@ See ${checkingResultsDir} for a detailed report."""
protected Configuration setupConfiguration() {

Configuration result = Configuration.builder()
.sourceDocuments(sourceDocuments.files)
.sourceDocuments(sourceDocuments?.files)
.sourceDir(sourceDir)
.checkingResultsDir(checkingResultsDir)
.junitResultsDir(junitResultsDir)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.aim42.htmlsanitycheck.gradle

import org.gradle.api.Project
import org.gradle.language.base.plugins.LifecycleBasePlugin
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification

class HtmlSanityCheckPluginSpec extends Specification {

def "plugin adds an htmlSanityCheck task to the project"() {
given: "a project"
Project project = ProjectBuilder.builder().build()

when: "the plugin is applied"
project.plugins.apply HtmlSanityCheckPlugin

then: "the htmlSanityCheck task is added"
project.tasks.named(HtmlSanityCheckPlugin.HTML_SANITY_CHECK) != null
}

def "htmlSanityCheck task has correct type and properties"() {
given: "a project with the plugin applied"
Project project = ProjectBuilder.builder().build()
project.plugins.apply(HtmlSanityCheckPlugin)

when: "retrieving the htmlSanityCheck task"
def task = project.tasks.named(HtmlSanityCheckPlugin.HTML_SANITY_CHECK).get()

then: "the task is of type HtmlSanityCheckTask"
task instanceof HtmlSanityCheckTask

and: "the task has the correct description"
task.description == 'performs semantic checks on html files'

and: "the task is in the verification group"
task.group == LifecycleBasePlugin.VERIFICATION_GROUP
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.aim42.htmlsanitycheck
package org.aim42.htmlsanitycheck.gradle

import org.gradle.testkit.runner.GradleRunner
import org.jsoup.Jsoup
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.aim42.htmlsanitycheck.gradle

import org.aim42.htmlsanitycheck.MisconfigurationException
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification

class HtmlSanityCheckTaskSpec extends Specification {

def "should initialize task with defaults"() {
given:
def project = ProjectBuilder.builder().build()
def task = project.tasks.register('htmlSanityCheck', HtmlSanityCheckTask)

expect:
task.failOnErrors == false
task.httpConnectionTimeout == 5000
task.ignoreLocalHost == false
task.ignoreIPAddresses == false
task.checkingResultsDir == new File(project.DEFAULT_BUILD_DIR_NAME, '/reports/htmlSanityCheck/')
task.junitResultsDir == new File(project.DEFAULT_BUILD_DIR_NAME, '/test-results/htmlSanityCheck/')
}

def "should set source directory and files"() {
given:
def project = ProjectBuilder.builder().build()
def task = project.tasks.register('htmlSanityCheck', HtmlSanityCheckTask.class)
def sourceDir = new File(project.DEFAULT_BUILD_DIR_NAME, "/resources/test/resources")
sourceDir.mkdirs()
def testFile = new File(sourceDir, "file-to-test.html")
testFile << """<html></html>"""

when:
task.sourceDir = sourceDir
task.httpSuccessCodes = [299]
task.httpErrorCodes = [599]
task.httpWarningCodes = [199]
task.sanityCheckHtml()

then:
task.sourceDocuments != null
}

def "should throw exception if configuration is invalid"() {
given:
def project = ProjectBuilder.builder().build()
def task = project.tasks.register('htmlSanityCheck', HtmlSanityCheckTask.class)
task.failOnErrors = true

when:
task.sanityCheckHtml()

then:
def e = thrown(MisconfigurationException)
e.message.contains("source directory must not be null")
}
}
2 changes: 1 addition & 1 deletion src/docs/arc42/chapters/chap-08-gradle-plugin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ we implement a lean wrapper as described in the Gradle user guide.

[source, groovy]
----
include::{gradle-plugin-source-path}/htmlsanitycheck/HtmlSanityCheckPlugin.groovy[tag=gradle-plugin-implementation]
include::{gradle-plugin-source-path}/htmlsanitycheck/gradle/HtmlSanityCheckPlugin.groovy[tag=gradle-plugin-implementation]
----

==== Directory Structure and Required Files
Expand Down
13 changes: 6 additions & 7 deletions src/docs/development/development-intro.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ TBD
| | | | |-org
| | | | | |-aim42
| | | | | | |-htmlsanitycheck
| | | | | | | | ...
| | | | | | | |-HtmlSanityCheckPlugin.groovy // <1>
| | | | | | | |-HtmlSanityCheckTask.groovy
| | | | | | | |-gradle
| | | | | | | | |-HtmlSanityCheckPlugin.groovy // <1>
| | | | | | | | |-HtmlSanityCheckTask.groovy
| | | | |-resources
| | | | | |-META-INF // <2>
| | | | | | |-gradle-plugins
Expand All @@ -63,15 +63,14 @@ TBD
| | | | |-org
| | | | | |-aim42
| | | | | | |-htmlsanitycheck
| | | | | | | | ...
| | | | | | | |-HtmlSanityCheckPluginTest
|
| | | | | | | |-gradle
| | | | | | | | |-HtmlSanityCheckPluginTest
----
<1> the actual plugin code, a xyzPlugin and xyzTask groovy files
<2> Gradle expects plugin properties in META-INF
<3> Property file containing the name of the actual implementation class:

implementation-class=org.aim42.htmlsanitycheck.HtmlSanityCheckPlugin
implementation-class=org.aim42.htmlsanitycheck.gradle.HtmlSanityCheckPlugin

==== Notes for IntelliJ Users
If you work with IntelliJ, you should configure your run- and test configurations
Expand Down

0 comments on commit 3083c4a

Please sign in to comment.