-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
and provide strong test coverage for Gradle task
- Loading branch information
Showing
4 changed files
with
118 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/HtmlSanityCheckBaseSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.aim42.htmlsanitycheck | ||
|
||
import org.junit.Rule | ||
import org.junit.rules.TemporaryFolder | ||
import spock.lang.Specification | ||
|
||
class HtmlSanityCheckBaseSpec extends Specification { | ||
final static VALID_HTML = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head></head><body></body><html>""" | ||
final static INVALID_HTML = """<body><span id="id"/><span id="id"/></body> """ | ||
|
||
@Rule | ||
TemporaryFolder testProjectDir = new TemporaryFolder() | ||
File sourceDir | ||
File buildDir | ||
File buildFile | ||
File htmlFile | ||
|
||
def setup() { | ||
buildDir = testProjectDir.newFolder("build") | ||
sourceDir = testProjectDir.newFolder("src") | ||
sourceDir.mkdirs() | ||
htmlFile = new File (sourceDir, "test.html") | ||
} | ||
|
||
protected void createBuildFile(String extendedTaskConfig = "") { | ||
// a note on writing paths to the build script on windows: | ||
// - the default file separator is a backslash | ||
// - as the path is written into a quoted string, backslashes should be quoted | ||
// - to avoid string manipulation or similar, we use URIs to avoid the problem | ||
// (URIs consist of / instead of backslashes) | ||
buildFile = testProjectDir.newFile('build.gradle') << """ | ||
plugins { | ||
id 'org.aim42.htmlSanityCheck' | ||
} | ||
htmlSanityCheck { | ||
sourceDir = file ("src") | ||
checkingResultsDir = file ("build") | ||
${extendedTaskConfig} | ||
} | ||
""".stripIndent() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...ck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/HtmlSanityCheckTaskSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.aim42.htmlsanitycheck | ||
|
||
import org.gradle.api.Project | ||
import org.gradle.api.Task | ||
import org.gradle.testfixtures.ProjectBuilder | ||
import spock.lang.Specification | ||
|
||
class HtmlSanityCheckTaskSpec extends HtmlSanityCheckBaseSpec { | ||
Project project | ||
Task task | ||
|
||
def setup () { | ||
project = ProjectBuilder.builder().withProjectDir(testProjectDir.root).build() | ||
task = project.tasks.register(HtmlSanityCheckPlugin.HTML_SANITY_CHECK, HtmlSanityCheckTask).get() | ||
} | ||
|
||
def "should initialize task with defaults"() { | ||
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 work with simple file"() { | ||
given: | ||
htmlFile << VALID_HTML | ||
|
||
when: | ||
task.setSourceDir(testProjectDir.root) | ||
task.httpSuccessCodes = [299] | ||
task.httpErrorCodes = [599] | ||
task.httpWarningCodes = [199] | ||
task.sanityCheckHtml() | ||
|
||
then: | ||
task.sourceDocuments != null | ||
} | ||
|
||
def "should throw exception if configuration is invalid"() { | ||
when: | ||
task.sanityCheckHtml() | ||
|
||
then: | ||
def e = thrown(MisconfigurationException) | ||
e.message.contains("source directory must not be null") | ||
} | ||
} |