Skip to content

Commit

Permalink
#318 Add tests for CLI main
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheman committed Nov 19, 2024
1 parent 84987b4 commit 64670d9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -30,8 +40,11 @@ class Main implements Runnable {
File[] files

static void main(String[] args) {
Main app = new Main()
MainRunner runner = new MainRunner()
Main app = new Main(runner)
CommandLine cmd = new CommandLine(app)
runner.setMain(app)
runner.setCmd(cmd)
cmd.execute(args)
}

Expand All @@ -53,21 +66,26 @@ class Main implements Runnable {
)
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 64670d9

Please sign in to comment.