Skip to content

Commit

Permalink
WIP Add tests for CLI main
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheman committed Jan 25, 2024
1 parent 8fff7bf commit 6c01753
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 24 deletions.
10 changes: 10 additions & 0 deletions htmlSanityCheck-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ plugins {
id 'application'
}

test {
useJUnitPlatform()
}

dependencies {
implementation libs.picocli.impl
annotationProcessor libs.picocli.annotationprocessor
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
}
Expand All @@ -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()
}
}

/*========================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 6c01753

Please sign in to comment.