diff --git a/htmlSanityCheck-cli/build.gradle b/htmlSanityCheck-cli/build.gradle index 24a43f93..5d16e1f3 100644 --- a/htmlSanityCheck-cli/build.gradle +++ b/htmlSanityCheck-cli/build.gradle @@ -2,6 +2,10 @@ plugins { id 'application' } +test { + useJUnitPlatform() +} + dependencies { implementation libs.picocli.impl annotationProcessor libs.picocli.annotationprocessor @@ -11,6 +15,12 @@ dependencies { implementation libs.groovy.all implementation project(":htmlSanityCheck-core") + + testImplementation 'org.spockframework:spock-core:2.3-groovy-3.0' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1' + testImplementation 'org.mockito:mockito-junit-jupiter:3.11.2' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.1' + testImplementation( "com.athaydes:spock-reports:2.5.0-groovy-3.0" ) } compileGroovy { diff --git a/htmlSanityCheck-cli/src/main/groovy/org/aim42/htmlsanitycheck/cli/Main.groovy b/htmlSanityCheck-cli/src/main/groovy/org/aim42/htmlsanitycheck/cli/Main.groovy index 9e983acf..7bce1074 100644 --- a/htmlSanityCheck-cli/src/main/groovy/org/aim42/htmlsanitycheck/cli/Main.groovy +++ b/htmlSanityCheck-cli/src/main/groovy/org/aim42/htmlsanitycheck/cli/Main.groovy @@ -22,6 +22,16 @@ import java.nio.file.Paths class Main implements Runnable { private static final Logger logger = LoggerFactory.getLogger(Main.class) + MainRunner runner + + Main() { + Main(new MainRunner()) + } + + Main(MainRunner runner) { + this.runner = runner + } + @Option(names = ["-r", "--resultsDir"], description = "Results Directory") String resultsDirectoryName = "/tmp/results" @@ -38,7 +48,7 @@ class Main implements Runnable { File[] srcDocs static void main(String[] args) { - Main app = new Main() + Main app = new Main(new MainRunner()) CommandLine cmd = new CommandLine(app) cmd.execute(args) } @@ -52,38 +62,44 @@ class Main implements Runnable { .collect { it.toFile() } } - void run() { - var configuration = new Configuration() + static class MainRunner { + void run() { + var configuration = new Configuration() - configuration.addConfigurationItem(Configuration.ITEM_NAME_sourceDir, srcDir) + configuration.addConfigurationItem(Configuration.ITEM_NAME_sourceDir, srcDir) - def srcDocuments = srcDocs ?: findFiles() - if (!srcDocuments) { - CommandLine cmd = new CommandLine(this) - System.err.println("Please specify at least one src document (either explicitly or implicitly)") - cmd.usage(System.out) - System.exit(1) - } - configuration.addConfigurationItem(Configuration.ITEM_NAME_sourceDocuments, srcDocuments) + def srcDocuments = srcDocs ?: findFiles() + if (!srcDocuments) { + CommandLine cmd = new CommandLine(this) + System.err.println("Please specify at least one src document (either explicitly or implicitly)") + cmd.usage(System.out) + System.exit(1) + } + configuration.addConfigurationItem(Configuration.ITEM_NAME_sourceDocuments, srcDocuments) - var resultsDirectory = new File(resultsDirectoryName) - configuration.addConfigurationItem((Configuration.ITEM_NAME_checkingResultsDir), resultsDirectory) + var resultsDirectory = new File(resultsDirectoryName) + configuration.addConfigurationItem((Configuration.ITEM_NAME_checkingResultsDir), resultsDirectory) - if (configuration.isValid()) { - // create output directory for checking results - resultsDirectory.mkdirs() + if (configuration.isValid()) { + // create output directory for checking results + resultsDirectory.mkdirs() - // create an AllChecksRunner... - var allChecksRunner = new AllChecksRunner(configuration) + // create an AllChecksRunner... + var allChecksRunner = new AllChecksRunner(configuration) - // ... and perform the actual checks - var allChecks = allChecksRunner.performAllChecks() + // ... and perform the actual checks + var allChecks = allChecksRunner.performAllChecks() - // check for findings and fail build if requested - var nrOfFindingsOnAllPages = allChecks.nrOfFindingsOnAllPages() - logger.debug("Found ${nrOfFindingsOnAllPages} error(s) on all checked pages") + // check for findings and fail build if requested + var nrOfFindingsOnAllPages = allChecks.nrOfFindingsOnAllPages() + logger.debug("Found ${nrOfFindingsOnAllPages} error(s) on all checked pages") + } } } + + void run() { + runner.run() + } } /*======================================================================== diff --git a/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/MainCliSpec.groovy b/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/MainCliSpec.groovy new file mode 100644 index 00000000..96ae0f69 --- /dev/null +++ b/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/MainCliSpec.groovy @@ -0,0 +1,35 @@ +package org.aim42.htmlsanitycheck.cli + + +import picocli.CommandLine +import spock.lang.Specification +import spock.lang.Unroll + +class MainCliSpec extends Specification { + + Main.MainRunner myAppRunner = Mock(Main.MainRunner) + + @Unroll + def "test hsc with #args"() { + given: + def cmdLine = new CommandLine(new Main(myAppRunner)) + + when: + def exitCode = cmdLine.execute(args.split()) + + then: + exitCode == expectedExitCode + (runnerWasCalled ? 1 : 0) * myAppRunner.run() + + where: + args | expectedExitCode | runnerWasCalled + "-h" | 0 | false + "--help" | 0 | false + "-V" | 0 | false + "--version" | 0 | false + "" | 0 | true + "." | 0 | true + "-r /tmp/results" | 0 | true + "--resultsDir /tmp/results" | 0 | true + } +}