Skip to content

Commit

Permalink
fix(test): use a random available port on http test
Browse files Browse the repository at this point in the history
Instead of using the default mojo port (2000) during http test, find the
first available port on the host an use it during tests.
  • Loading branch information
NicolasGeraud committed Sep 22, 2015
1 parent 8ed7f8b commit 69999c5
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 69999c5

Please sign in to comment.