Skip to content

Commit

Permalink
Randomize server port if not specified. (Closes #89)
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Nov 19, 2012
1 parent 2ebc44d commit 0c07157
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 68 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Master

- [Issue #89](https://github.com/netzpirat/guard-jasmine/issues/89): Randomize server port if not specified.

## 1.9.4 - November 11, 2012

- [Issue #88](https://github.com/netzpirat/guard-jasmine/issues/88): Guard::Jasmine leaves server running on timeout.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ The server options configures the server environment that is needed to run Guard
:server_timeout => 30 # The number of seconds to wait for the Jasmine spec server
# default: 15

:port => 9292 # Jasmine server port to use.
# default: 8888
:port => 8888 # Jasmine server port to use.
# default: a random, free server port

:phantomjs_bin => '~/bin/phantomjs' # Path to phantomjs.
# default: auto-detect 'phantomjs'
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Jasmine < Guard
:server => :auto,
:server_env => ENV['RAILS_ENV'] || 'development',
:server_timeout => 15,
:port => 8888,
:port => nil,
:rackup_config => nil,
:jasmine_url => 'http://localhost:8888/jasmine',
:timeout => 10,
Expand Down
32 changes: 22 additions & 10 deletions lib/guard/jasmine/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class << self
def start(options)
server = options[:server]
server = detect_server(options[:spec_dir]) if server == :auto
port = options[:port]
port = options[:port] || detect_server_port
timeout = options[:server_timeout]

case server
when :webrick, :mongrel, :thin
start_rack_server(server, options)
start_rack_server(server, port, options)
when :unicorn
start_unicorn_server(options)
start_unicorn_server(port, options)
when :jasmine_gem
start_rake_server(port, 'jasmine')
else
Expand All @@ -61,15 +61,14 @@ def stop
# in the current directory.
#
# @param [Symbol] server the server name
# @param [Integer] port the server port
# @param [Hash] options the server options
# @option options [Symbol] server the rack server to use
# @option options [String] server_env the Rails environment
# @option options [Number] port the server port
# @option options [String] rackup_config custom rackup config to use (i.e. spec/dummy/config.ru for mountable engines)
#
def start_rack_server(server, options)
def start_rack_server(server, port, options)
environment = options[:server_env]
port = options[:port]
rackup_config = options[:rackup_config]

::Guard::UI.info "Guard::Jasmine starts #{ server } test server on port #{ port } in #{ environment } environment."
Expand All @@ -90,9 +89,8 @@ def start_rack_server(server, options)
# @option options [String] server_env the Rails environment
# @option options [Number] port the server port
#
def start_unicorn_server(options)
def start_unicorn_server(port, options)
environment = options[:server_env]
port = options[:port]

::Guard::UI.info "Guard::Jasmine starts Unicorn test server on port #{ port } in #{ environment } environment."

Expand All @@ -103,7 +101,7 @@ def start_unicorn_server(options)
rescue => e
::Guard::UI.error "Cannot start Unicorn server: #{ e.message }"
end

# Start the Jasmine gem server of the current project.
#
# @param [Number] port the server port
Expand Down Expand Up @@ -143,6 +141,20 @@ def detect_server(spec_dir)
end
end

# Detect the server port to use
#
# @return [Integer] a free server port
#
def detect_server_port
server = TCPServer.new('127.0.0.1', 0)
port = server.addr[1]
server.close

port
rescue Errno::EADDRINUSE
retry
end

# Wait until the Jasmine test server is running.
#
# @param [Number] port the server port
Expand All @@ -160,7 +172,7 @@ def wait_for_server(port, timeout)
sleep 0.1
end
end

rescue Timeout::Error
::Guard::UI.warning 'Timeout while waiting for the server startup. You may need to increase the `:server_timeout` option.'
throw :task_has_failed
Expand Down
Loading

0 comments on commit 0c07157

Please sign in to comment.