Skip to content

Commit

Permalink
Allow configuration of Rack server environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Nov 22, 2011
1 parent a43f5bf commit 88bd679
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ The general options configures the environment that is needed to run Guard::Jasm
:server => :jasmine_gem # Jasmine server to use, either :auto, :rack, :jasmine_gem or :none
# default: :auto

:server_env => :development # Jasmine server Rails environment to set, e.g. :development or :test
# default: :test

:port => 9292 # Jasmine server port to use.
# default: 8888

Expand Down
4 changes: 3 additions & 1 deletion lib/guard/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Jasmine < Guard

DEFAULT_OPTIONS = {
:server => :auto,
:server_env => 'test',
:port => 8888,
:jasmine_url => 'http://localhost:8888/jasmine',
:timeout => 10000,
Expand All @@ -39,6 +40,7 @@ class Jasmine < Guard
# @param [Array<Guard::Watcher>] watchers the watchers in the Guard block
# @param [Hash] options the options for the Guard
# @option options [String] :server the server to use, either :rails or :jasmine
# @option options [String] :server_env the server environment to use, for example :development, :test
# @option options [String] :port the port for the Jasmine test server
# @option options [String] :jasmine_url the url of the Jasmine test runner
# @option options [String] :phantomjs_bin the location of the PhantomJS binary
Expand Down Expand Up @@ -73,7 +75,7 @@ def initialize(watchers = [], options = { })
def start
if phantomjs_bin_valid?(options[:phantomjs_bin])

Server.start(options[:server], options[:port]) unless options[:server] == :none
Server.start(options[:server], options[:port], options[:server_env]) unless options[:server] == :none

if jasmine_runner_available?(options[:jasmine_url])
run_all if options[:all_on_start]
Expand Down
10 changes: 6 additions & 4 deletions lib/guard/jasmine/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ class << self
#
# @param [String] strategy the server strategy to use
# @param [Number] port the server port
# @param [String] environment the Rails environment
#
def start(strategy, port)
def start(strategy, port, environment)
strategy = detect_server if strategy == :auto

case strategy
when :rack
start_rack_server(port)
start_rack_server(port, environment)
when :jasmine_gem
start_jasmine_gem_server(port)
end
Expand All @@ -42,14 +43,15 @@ def stop
# in the current directory.
#
# @param [Number] port the server port
# @param [String] environment the Rails environment
#
def start_rack_server(port)
def start_rack_server(port, environment)
require 'rack'

::Guard::UI.info "Guard::Jasmine starts Rack test server on port #{ port }."

self.thread = Thread.new {
ENV['RAILS_ENV'] = 'test'
ENV['RAILS_ENV'] = environment.to_s
Rack::Server.start(:config => 'config.ru', :Port => port, :AccessLog => [])
}

Expand Down
22 changes: 11 additions & 11 deletions spec/guard/jasmine/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

it 'chooses the rack server strategy' do
server.should_receive(:start_rack_server)
server.start(:auto, 8888)
server.start(:auto, 8888, 'test')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:auto, 8888)
server.start(:auto, 8888, 'test')
end
end

Expand All @@ -37,12 +37,12 @@

it 'chooses the jasmine_gem server strategy' do
server.should_receive(:start_jasmine_gem_server)
server.start(:auto, 8888)
server.start(:auto, 8888, 'test')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:auto, 8888)
server.start(:auto, 8888, 'test')
end
end

Expand All @@ -56,46 +56,46 @@
server.should_not_receive(:start_rack_server)
server.should_not_receive(:start_jasmine_gem_server)
server.should_not_receive(:wait_for_server)
server.start(:auto, 8888)
server.start(:auto, 8888, 'test')
end
end
end

context 'with the :rack strategy' do
it 'does not auto detect a server' do
server.should_not_receive(:detect_server)
server.start(:rack, 8888)
server.start(:rack, 8888, 'test')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:rack, 8888)
server.start(:rack, 8888, 'test')
end
end

context 'with the :jasmine_gem strategy' do
it 'does not auto detect a server' do
server.should_not_receive(:detect_server)
server.start(:jasmine_gem, 8888)
server.start(:jasmine_gem, 8888, 'test')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:jasmine_gem, 8888)
server.start(:jasmine_gem, 8888, 'test')
end
end

context 'with the :none strategy' do
it 'does not auto detect a server' do
server.should_not_receive(:detect_server)
server.start(:none, 8888)
server.start(:none, 8888, 'test')
end

it 'does not start a server' do
server.should_not_receive(:start_rack_server)
server.should_not_receive(:start_jasmine_gem_server)
server.should_not_receive(:wait_for_server)
server.start(:none, 8888)
server.start(:none, 8888, 'test')
end
end

Expand Down
12 changes: 11 additions & 1 deletion spec/guard/jasmine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
guard.options[:server].should eql :auto
end

it 'sets a default :server option' do
guard.options[:server_env].should eql 'test'
end

it 'sets a default :port option' do
guard.options[:port].should eql 8888
end
Expand Down Expand Up @@ -90,6 +94,7 @@

context 'with other options than the default ones' do
let(:guard) { Guard::Jasmine.new(nil, { :server => :jasmine_gem,
:server_env => 'development',
:port => 4321,
:jasmine_url => 'http://192.168.1.5/jasmine',
:phantomjs_bin => '~/bin/phantomjs',
Expand All @@ -108,6 +113,10 @@
guard.options[:server].should eql :jasmine_gem
end

it 'sets the :server_env option' do
guard.options[:server_env].should eql 'development'
end

it 'sets the :jasmine_url option' do
guard.options[:port].should eql 4321
end
Expand Down Expand Up @@ -282,11 +291,12 @@
context 'with the server set to something other than :none' do
before do
guard.options[:server] = :jasmine_gem
guard.options[:server_env] = 'test'
guard.options[:port] = 3333
end

it 'does start a server' do
server.should_receive(:start).with(:jasmine_gem, 3333)
server.should_receive(:start).with(:jasmine_gem, 3333, 'test')
guard.start
end
end
Expand Down

0 comments on commit 88bd679

Please sign in to comment.