Skip to content

Commit

Permalink
WIP 318 Unify test case setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheman committed Aug 28, 2024
1 parent 3083c4a commit 5f7b4dc
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class HscCommandSpec extends Specification {
SecurityManager originalSecurityManager = System.getSecurityManager()
SecurityManager mockSecurityManager = new NoExitSecurityMock(originalSecurityManager)
System.setSecurityManager(mockSecurityManager)
String[] args = [testProjectDir.getRoot()]
String[] args = [testProjectDir.root]

when:
HscCommand.main(args)
Expand All @@ -94,7 +94,7 @@ class HscCommandSpec extends Specification {
def "test with valid HTML file"() {
given:
htmlFile << VALID_HTML
String[] args = [testProjectDir.getRoot()]
String[] args = [testProjectDir.root]

when:
HscCommand.main(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HtmlReporterTest {
@Before
void setUp() {
runResults = new PerRunResults()
htmlReporter = new HtmlReporter(runResults, tempDir.getRoot().getAbsolutePath())
htmlReporter = new HtmlReporter(runResults, tempDir.root.getAbsolutePath())
}

@Test
Expand Down Expand Up @@ -189,7 +189,7 @@ class HtmlReporterTest {

private String getResultContents() {
htmlReporter.closeReport()
File reportFile = new File(tempDir.getRoot(), "index.html")
File reportFile = new File(tempDir.root, "index.html")
try {
String content = new String(Files.readAllBytes(reportFile.toPath()))
return content
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.aim42.htmlsanitycheck.gradle

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()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ package org.aim42.htmlsanitycheck.gradle
import org.gradle.testkit.runner.GradleRunner
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification
import spock.lang.Unroll

import static org.gradle.testkit.runner.TaskOutcome.FAILED
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

class HtmlSanityCheckTaskFunctionalTest extends Specification {
private final static VALID_HTML = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head></head><body></body><html>"""
private final static INVALID_HTML = """<body><span id="id"/><span id="id"/></body> """
class HtmlSanityCheckTaskFunctionalSpec extends HtmlSanityCheckBaseSpec {

private final static GRADLE_VERSIONS = [
// tag::tested-gradle-versions[]
// 6.x or older does not work!
Expand All @@ -24,39 +20,13 @@ class HtmlSanityCheckTaskFunctionalTest extends Specification {
// end::tested-gradle-versions[]
]

@Rule
TemporaryFolder testProjectDir = new TemporaryFolder()
File buildDir
File buildFile
File htmlFile

def setup() {
buildDir = testProjectDir.newFolder("build")
htmlFile = testProjectDir.newFile("test.html")
// 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( "${htmlFile.parentFile.toURI().path}" )
checkingResultsDir = file( "${buildDir.toURI().path}" )
}
"""
}

@Unroll
def "can execute htmlSanityCheck task with Gradle version #gradleVersion"() {
given:
htmlFile << VALID_HTML
createBuildFile()

when:

def result = runnerForHtmlSanityCheckTask(gradleVersion).build()

then:
Expand All @@ -70,11 +40,9 @@ class HtmlSanityCheckTaskFunctionalTest extends Specification {
def "invalid HTML fails build with failOnErrors=true and Gradle version #gradleVersion"() {
given:
htmlFile << INVALID_HTML
buildFile << """
htmlSanityCheck {
createBuildFile("""
failOnErrors = true
}
"""
""")

when:

Expand All @@ -93,14 +61,12 @@ class HtmlSanityCheckTaskFunctionalTest extends Specification {
given:
htmlFile << VALID_HTML
testProjectDir.newFile("test-invalid.html") << INVALID_HTML
buildFile << """
htmlSanityCheck {
createBuildFile("""
sourceDocuments = fileTree(sourceDir) {
include '**/$htmlFile.name'
}
failOnErrors = true
}
"""
""")

when:
def result = runnerForHtmlSanityCheckTask(gradleVersion).build()
Expand All @@ -116,13 +82,9 @@ class HtmlSanityCheckTaskFunctionalTest extends Specification {
def "can select a subset of all checks to be performed with Gradle version #gradleVersion"() {
given:
htmlFile << VALID_HTML
buildFile << """
import org.aim42.htmlsanitycheck.check.AllCheckers
htmlSanityCheck {
checkerClasses = [AllCheckers.CHECKER_CLASSES.first()]
}
"""
createBuildFile("""
checkerClasses = [org.aim42.htmlsanitycheck.check.AllCheckers.CHECKER_CLASSES.first()]
""")

when:
runnerForHtmlSanityCheckTask(gradleVersion).build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package org.aim42.htmlsanitycheck.gradle

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

class HtmlSanityCheckTaskSpec extends Specification {
class HtmlSanityCheckTaskSpec extends HtmlSanityCheckBaseSpec {
Project project
Task task

def "should initialize task with defaults"() {
given:
def project = ProjectBuilder.builder().build()
def task = project.tasks.register('htmlSanityCheck', HtmlSanityCheckTask)
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
Expand All @@ -20,17 +24,12 @@ class HtmlSanityCheckTaskSpec extends Specification {
task.junitResultsDir == new File(project.DEFAULT_BUILD_DIR_NAME, '/test-results/htmlSanityCheck/')
}

def "should set source directory and files"() {
def "should work with simple file"() {
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>"""
htmlFile << VALID_HTML

when:
task.sourceDir = sourceDir
task.setSourceDir(testProjectDir.root)
task.httpSuccessCodes = [299]
task.httpErrorCodes = [599]
task.httpWarningCodes = [199]
Expand All @@ -41,11 +40,6 @@ class HtmlSanityCheckTaskSpec extends Specification {
}

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()

Expand Down
1 change: 1 addition & 0 deletions htmlSanityCheck-gradle-plugin/src/test/resources

0 comments on commit 5f7b4dc

Please sign in to comment.