From 5f7b4dcd9f4dd715905cad38c76139a6a4a12dd0 Mon Sep 17 00:00:00 2001 From: Gerd Aschemann Date: Wed, 28 Aug 2024 15:18:18 +0200 Subject: [PATCH] WIP 318 Unify test case setup --- .../htmlsanitycheck/cli/HscCommandSpec.groovy | 4 +- .../report/HtmlReporterTest.groovy | 4 +- .../gradle/HtmlSanityCheckBaseSpec.groovy | 45 ++++++++++++++ ... HtmlSanityCheckTaskFunctionalSpec.groovy} | 58 ++++--------------- .../gradle/HtmlSanityCheckTaskSpec.groovy | 32 +++++----- .../src/test/resources | 1 + 6 files changed, 73 insertions(+), 71 deletions(-) create mode 100644 htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckBaseSpec.groovy rename htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/{HtmlSanityCheckTaskFunctionalTest.groovy => HtmlSanityCheckTaskFunctionalSpec.groovy} (69%) create mode 120000 htmlSanityCheck-gradle-plugin/src/test/resources diff --git a/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/HscCommandSpec.groovy b/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/HscCommandSpec.groovy index d11038ab..3fec1cb1 100644 --- a/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/HscCommandSpec.groovy +++ b/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/HscCommandSpec.groovy @@ -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) @@ -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) diff --git a/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/report/HtmlReporterTest.groovy b/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/report/HtmlReporterTest.groovy index 36274fa0..9893f3d7 100644 --- a/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/report/HtmlReporterTest.groovy +++ b/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/report/HtmlReporterTest.groovy @@ -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 @@ -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 diff --git a/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckBaseSpec.groovy b/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckBaseSpec.groovy new file mode 100644 index 00000000..b5e85fe7 --- /dev/null +++ b/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckBaseSpec.groovy @@ -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 = """""" + final static INVALID_HTML = """ """ + + @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() + } + +} diff --git a/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskFunctionalTest.groovy b/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskFunctionalSpec.groovy similarity index 69% rename from htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskFunctionalTest.groovy rename to htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskFunctionalSpec.groovy index ff24e8c7..738c9433 100644 --- a/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskFunctionalTest.groovy +++ b/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskFunctionalSpec.groovy @@ -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 = """""" - private final static INVALID_HTML = """ """ +class HtmlSanityCheckTaskFunctionalSpec extends HtmlSanityCheckBaseSpec { + private final static GRADLE_VERSIONS = [ // tag::tested-gradle-versions[] // 6.x or older does not work! @@ -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: @@ -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: @@ -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() @@ -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() diff --git a/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskSpec.groovy b/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskSpec.groovy index 7f393234..58144609 100644 --- a/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskSpec.groovy +++ b/htmlSanityCheck-gradle-plugin/src/test/groovy/org/aim42/htmlsanitycheck/gradle/HtmlSanityCheckTaskSpec.groovy @@ -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 @@ -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 << """""" + htmlFile << VALID_HTML when: - task.sourceDir = sourceDir + task.setSourceDir(testProjectDir.root) task.httpSuccessCodes = [299] task.httpErrorCodes = [599] task.httpWarningCodes = [199] @@ -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() diff --git a/htmlSanityCheck-gradle-plugin/src/test/resources b/htmlSanityCheck-gradle-plugin/src/test/resources new file mode 120000 index 00000000..2c7089f1 --- /dev/null +++ b/htmlSanityCheck-gradle-plugin/src/test/resources @@ -0,0 +1 @@ +../../../htmlSanityCheck-core/src/test/resources \ No newline at end of file