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 index 96ae0f69..4d3b67b9 100644 --- a/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/MainCliSpec.groovy +++ b/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/MainCliSpec.groovy @@ -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 = """
""" + private final static INVALID_HTML = """ """ + + @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: @@ -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.") } + + } diff --git a/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/NoExitSecurityMock.groovy b/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/NoExitSecurityMock.groovy new file mode 100644 index 00000000..3840c97f --- /dev/null +++ b/htmlSanityCheck-cli/src/test/groovy/org/aim42/htmlsanitycheck/cli/NoExitSecurityMock.groovy @@ -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") + } +} \ No newline at end of file