Skip to content

Commit

Permalink
#318 Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheman committed Oct 6, 2024
1 parent eec626b commit dd0bab0
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
package org.aim42.htmlsanitycheck.cli


import org.junit.Rule
import org.junit.rules.TemporaryFolder
import picocli.CommandLine
import spock.lang.Specification
import spock.lang.Unroll

import java.nio.file.Files

class MainCliSpec extends Specification {

Main.MainRunner myAppRunner = Mock(Main.MainRunner)
private final static VALID_HTML = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head></head><body></body><html>"""
private final static INVALID_HTML = """<body><span id="id"/><span id="id"/></body> """

@Rule
TemporaryFolder testProjectDir = new TemporaryFolder()
File htmlFile
ByteArrayOutputStream outContent
ByteArrayOutputStream errContent

def setup() {
outContent = new ByteArrayOutputStream()
errContent = new ByteArrayOutputStream()
System.setOut(new PrintStream(outContent))
System.setErr(new PrintStream(errContent))

htmlFile = testProjectDir.newFile("test.html")
}

def cleanup() {
System.setOut(System.out)
System.setErr(System.err)
System.setSecurityManager(null)
testProjectDir.delete()
}

@Unroll
def "test hsc with #args"() {
given:
Main.MainRunner myAppRunner = Mock(Main.MainRunner)
def cmdLine = new CommandLine(new Main(myAppRunner))

when:
Expand All @@ -22,14 +49,61 @@ class MainCliSpec extends Specification {
(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
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
}

def "test main method with -h argument"() {
given:
String[] args = ["-h"]

when:
Main.main(args)

then:
outContent.toString().contains("Usage: hsc")
outContent.toString().contains("Check HTML files for Sanity")
}

def "test with empty source directory"() {
given:
TemporaryFolder testProjectDir = new TemporaryFolder()
testProjectDir.create()
SecurityManager originalSecurityManager = System.getSecurityManager()
SecurityManager mockSecurityManager = new NoExitSecurityMock(originalSecurityManager)
System.setSecurityManager(mockSecurityManager)
String[] args = [testProjectDir.getRoot()]

when:
Main.main(args)

then:
mockSecurityManager.exitCalled == 1
errContent.toString().contains("Please specify at least one src document")
outContent.toString().contains("Usage: hsc")

cleanup:
testProjectDir.delete()
}

def "test with valid HTML file"() {
given:
htmlFile << VALID_HTML
String[] args = [testProjectDir.getRoot()]

when:
Main.main(args)

then:
outContent.toString().contains("found 0 issue, 100% successful.")
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.aim42.htmlsanitycheck.cli

import java.security.Permission

class NoExitSecurityMock extends SecurityManager {
private final SecurityManager originalSecurityManager

int exitCalled = 0

NoExitSecurityMock(SecurityManager originalSecurityManager) {
this.originalSecurityManager = originalSecurityManager
}

@Override
void checkPermission(Permission perm) {
if (originalSecurityManager != null) {
originalSecurityManager.checkPermission(perm)
}
}

@Override
void checkPermission(Permission perm, Object context) {
if (originalSecurityManager != null) {
originalSecurityManager.checkPermission(perm, context)
}
}

@Override
void checkExit(int status) {
exitCalled++
throw new SecurityException("System.exit(" + status + ") called")
}
}

0 comments on commit dd0bab0

Please sign in to comment.