Skip to content

Commit

Permalink
Merge pull request asciidoctor#172 from NicolasGeraud/issue-171
Browse files Browse the repository at this point in the history
fix(test): use a random available port on http test
  • Loading branch information
LightGuard committed Sep 22, 2015
2 parents 8ed7f8b + 69999c5 commit 22789b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class AsciidoctorHttpMojoTest extends Specification {
System.setOut(new PrintStream(newOut))
System.setIn(newIn)

def httpPort = new AsciidoctorMojoTestHelper().availablePort

def content = new File(srcDir, "content.asciidoc")
content.withWriter{ it <<
'''Document Title
Expand All @@ -35,6 +37,7 @@ class AsciidoctorHttpMojoTest extends Specification {

def mojo = new AsciidoctorHttpMojo()
mojo.backend = 'html5'
mojo.port = httpPort
mojo.sourceDirectory = srcDir
mojo.outputDirectory = outputDir
mojo.headerFooter = true
Expand All @@ -52,7 +55,7 @@ class AsciidoctorHttpMojoTest extends Specification {
}

when:
def html = 'http://localhost:2000/content'.toURL().text
def html = "http://localhost:${httpPort}/content".toURL().text

then:
assert html.contains('This is test, only a test')
Expand All @@ -79,6 +82,8 @@ class AsciidoctorHttpMojoTest extends Specification {
def newOut = new DoubleOuputStream(originalOut)
def newIn = new PrefilledInputStream('exit\r\n'.bytes, inputLatch)

def httpPort = new AsciidoctorMojoTestHelper().availablePort

System.setOut(new PrintStream(newOut))
System.setIn(newIn)

Expand All @@ -91,6 +96,7 @@ class AsciidoctorHttpMojoTest extends Specification {

def mojo = new AsciidoctorHttpMojo()
mojo.backend = 'html5'
mojo.port = httpPort
mojo.sourceDirectory = srcDir
mojo.outputDirectory = outputDir
mojo.headerFooter = true
Expand All @@ -108,7 +114,7 @@ class AsciidoctorHttpMojoTest extends Specification {
}

when:
def html = 'http://localhost:2000/'.toURL().text
def html = "http://localhost:${httpPort}/".toURL().text

then:
assert html.contains('DEFAULT')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.asciidoctor.maven.test

class AsciidoctorMojoTestHelper {
def getAvailablePort() {
ServerSocket socket = new ServerSocket(0)
def port = socket.getLocalPort()
socket.close()
return port
}
}

0 comments on commit 22789b3

Please sign in to comment.