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 Oct 6, 2024
1 parent a4ebefa commit fc1f442
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 35 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ configure(subprojects) {
description "${rootProject.description} - Module ${project.name}"

dependencies {
implementation platform (libs.slf4j.bom)

testImplementation platform(libs.spock)
testImplementation "org.spockframework:spock-core"
testImplementation "org.spockframework:spock-junit4"
Expand Down
5 changes: 2 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ junit-vintage = { module = 'org.junit.vintage:junit-vintage-engine', version.ref
lombok = 'org.projectlombok:lombok:1.18.34'
picocli-impl = { module = 'info.picocli:picocli', version.ref = "picocli-version" }
picocli-annotationprocessor = { module = 'info.picocli:picocli-codegen', version.ref = "picocli-version" }
slf4j-api = { module = 'org.slf4j:slf4j-api', version.ref = 'slf4j-version' }
slf4j-nop = { module = 'org.slf4j:slf4j-nop', version.ref = 'slf4j-version' }
spock = 'org.spockframework:spock-bom:2.3-groovy-3.0'
slf4j-bom = { module = "org.slf4j:slf4j-bom", version.ref = "slf4j-version" }
spock = "org.spockframework:spock-bom:2.3-groovy-3.0"

[plugins]
gradle-versions = { id= 'com.github.ben-manes.versions', version = '0.51.0' }
Expand Down
38 changes: 34 additions & 4 deletions htmlSanityCheck-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ dependencies {
implementation libs.picocli.impl
annotationProcessor libs.picocli.annotationprocessor

implementation libs.slf4j.api
implementation libs.slf4j.simple
implementation libs.groovy.all
implementation 'org.slf4j:slf4j-api'
implementation 'org.slf4j:slf4j-simple'

implementation project(":htmlSanityCheck-core")

testImplementation libs.mockito
}

compileGroovy {
Expand All @@ -20,4 +21,33 @@ compileGroovy {
application {
mainClass = 'org.aim42.htmlsanitycheck.cli.Main'
applicationName = 'hsc'
}
}

/*
* Copyright Gerd Aschemann and aim42 contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
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,8 +48,11 @@ class Main implements Runnable {
File[] srcDocs

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 @@ -52,38 +65,55 @@ class Main implements Runnable {
.collect { it.toFile() }
}

void run() {
var configuration = new Configuration()

configuration.addConfigurationItem(Configuration.ITEM_NAME_sourceDir, srcDir)
static class MainRunner implements CommandLine.IFactory {

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)
@Override
<T> T create(Class<T> cls) throws Exception {
if (cls == Main.class) {
return (T) new Main()
} else {
throw new IllegalArgumentException("Cannot create CLI applications of class '${cls}'")
}
}
configuration.addConfigurationItem(Configuration.ITEM_NAME_sourceDocuments, srcDocuments)

var resultsDirectory = new File(resultsDirectoryName)
configuration.addConfigurationItem((Configuration.ITEM_NAME_checkingResultsDir), resultsDirectory)
Main main
CommandLine cmd

if (configuration.isValid()) {
// create output directory for checking results
resultsDirectory.mkdirs()
void run() {
def srcDocuments = main.srcDocs ?: main.findFiles()
if (!srcDocuments) {
System.err.println("Please specify at least one src document (either explicitly or implicitly)")
cmd.usage(System.out)
System.exit(1)
}

// create an AllChecksRunner...
var allChecksRunner = new AllChecksRunner(configuration)
var configuration = new Configuration()
configuration.addConfigurationItem(Configuration.ITEM_NAME_sourceDir, main.srcDir)
configuration.addConfigurationItem(Configuration.ITEM_NAME_sourceDocuments, srcDocuments)

// ... and perform the actual checks
var allChecks = allChecksRunner.performAllChecks()
var resultsDirectory = new File(main.resultsDirectoryName)
configuration.addConfigurationItem((Configuration.ITEM_NAME_checkingResultsDir), resultsDirectory)

// check for findings and fail build if requested
var nrOfFindingsOnAllPages = allChecks.nrOfFindingsOnAllPages()
logger.debug("Found ${nrOfFindingsOnAllPages} error(s) on all checked pages")
if (configuration.isValid()) {
// create output directory for checking results
resultsDirectory.mkdirs()

// create an AllChecksRunner...
var allChecksRunner = new AllChecksRunner(configuration)

// ... 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")
}
}
}

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
}
}
2 changes: 2 additions & 0 deletions htmlSanityCheck-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies {
}
implementation libs.slf4j.api
testImplementation libs.slf4j.nop
implementation 'org.slf4j:slf4j-api'
testImplementation 'org.slf4j:slf4j-nop'
// jsoup is our awesome html parser, see jsoup.org
implementation libs.jsoup

Expand Down
8 changes: 4 additions & 4 deletions htmlSanityCheck-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ publishing {
}

dependencies {
implementation gradleApi()
implementation project(":htmlSanityCheck-core")

testImplementation(
libs.jsoup,
gradleTestKit()
)

implementation gradleApi()
implementation project(":htmlSanityCheck-core")
testImplementation libs.jsoup
}

/*
Expand Down

0 comments on commit fc1f442

Please sign in to comment.