Skip to content

Commit

Permalink
Refactor default server option detection.
Browse files Browse the repository at this point in the history
This refactoring moves the server detection before
the default Jasmine URL generation, since in case
of the Jasmine Gem as server, we want to have
`/` as runner url and not `/jasmine` like all the other
servers.

Fixes #120, Closes #121
  • Loading branch information
netzpirat committed Apr 3, 2013
1 parent e852f01 commit 017813a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 162 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

- [#120](https://github.com/netzpirat/guard-jasmine/issues/120): Proper Jasmine URL generation when server is `:auto` and using the Jasmine gem.

## 1.14.0 - April 3, 2013

- [#119](https://github.com/netzpirat/guard-jasmine/issues/119): Add a `verbose` option to the CLI runner.
Expand Down
10 changes: 6 additions & 4 deletions lib/guard/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ class Jasmine < Guard
# @option options [Hash] :run_all options overwrite options when run all specs
#
def initialize(watchers = [], options = { })
options[:port] ||= Jasmine.find_free_server_port
options[:jasmine_url] = "http://localhost:#{ options[:port] }#{ options[:server] == :jasmine_gem ? '/' : '/jasmine' }" unless options[:jasmine_url]
options = DEFAULT_OPTIONS.merge(options)
options[:specdoc] = :failure if ![:always, :never, :failure].include? options[:specdoc]
options[:server] ||= :auto

options[:port] ||= Jasmine.find_free_server_port
options[:server] ||= :auto
options[:server] = ::Guard::Jasmine::Server.detect_server(options[:spec_dir]) if options[:server] == :auto
options[:jasmine_url] = "http://localhost:#{ options[:port] }#{ options[:server] == :jasmine_gem ? '/' : '/jasmine' }" unless options[:jasmine_url]
options[:specdoc] = :failure if ![:always, :never, :failure].include? options[:specdoc]
options[:phantomjs_bin] = Jasmine.which('phantomjs') unless options[:phantomjs_bin]

self.run_all_options = options.delete(:run_all) || { }
Expand Down
3 changes: 2 additions & 1 deletion lib/guard/jasmine/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'guard/jasmine/formatter'
require 'guard/jasmine/server'
require 'guard/jasmine/util'
require 'guard/jasmine/server'

module Guard
class Jasmine
Expand Down Expand Up @@ -146,7 +147,7 @@ def spec(*paths)
runner_options[:phantomjs_bin] = options.bin || CLI.which('phantomjs')
runner_options[:timeout] = options.timeout
runner_options[:verbose] = options.verbose
runner_options[:server] = options.server.to_sym
runner_options[:server] = options.server.to_sym == :auto ? ::Guard::Jasmine::Server.detect_server(options.spec_dir) : options.server.to_sym
runner_options[:server_env] = options.server_env
runner_options[:server_timeout] = options.server_timeout
runner_options[:rackup_config] = options.rackup_config
Expand Down
47 changes: 23 additions & 24 deletions lib/guard/jasmine/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class << self
#
def start(options)
server = options[:server]
server = detect_server(options[:spec_dir]) if server == :auto
port = options[:port]
timeout = options[:server_timeout]

Expand All @@ -54,6 +53,29 @@ def stop
end
end

# Detect the server to use
#
# @param [String] spec_dir the spec directory
# @return [Symbol] the server strategy
#
def detect_server(spec_dir)
if File.exists?('config.ru')
%w(unicorn thin mongrel).each do |server|
begin
require server
return server.to_sym
rescue LoadError
# Ignore missing server and try next
end
end
:webrick
elsif spec_dir && File.exists?(File.join(spec_dir, 'support', 'jasmine.yml'))
:jasmine_gem
else
:none
end
end

private

# Start the Rack server of the current project. This
Expand Down Expand Up @@ -123,29 +145,6 @@ def start_rake_server(port, task, options)
::Guard::UI.error "Cannot start Rake task server: #{ e.message }"
end

# Detect the server to use
#
# @param [String] spec_dir the spec directory
# @return [Symbol] the server strategy
#
def detect_server(spec_dir)
if File.exists?('config.ru')
%w(unicorn thin mongrel).each do |server|
begin
require server
return server.to_sym
rescue LoadError
# Ignore missing server and try next
end
end
:webrick
elsif File.exists?(File.join(spec_dir, 'support', 'jasmine.yml'))
:jasmine_gem
else
:none
end
end

# Wait until the Jasmine test server is running.
#
# @param [Number] port the server port
Expand Down
9 changes: 7 additions & 2 deletions spec/guard/jasmine/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
cli.start(['spec', '--spec-dir', 'specs'])
end

it 'detects the server type' do
server.should_receive(:detect_server).with('specs')
cli.start(['spec', '--spec-dir', 'specs'])
end

it 'enables focus mode' do
runner.should_receive(:run).with(anything(), hash_including(:focus => true)).and_return [true, []]
cli.start(['spec'])
Expand Down Expand Up @@ -134,8 +139,8 @@

context 'without specified options' do
context 'for the server' do
it 'sets the server type' do
server.should_receive(:start).with(hash_including(:server => :auto))
it 'detects the server type' do
server.should_receive(:detect_server).with('spec/javascripts')
cli.start(['spec'])
end

Expand Down
177 changes: 57 additions & 120 deletions spec/guard/jasmine/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,126 +22,6 @@
end

describe '.start' do
context 'with the :auto strategy' do
let(:options) do
defaults
end

context 'with a rackup config file' do
before do
File.should_receive(:exists?).with('config.ru').and_return true
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(options)
end

context 'with unicorn available' do
before do
Guard::Jasmine::Server.should_receive(:require).with('unicorn').and_return true
end

it 'uses unicorn as server' do
server.should_receive(:start_unicorn_server).with(8888, options)
server.start(options)
end
end

context 'with thin available' do
before do
Guard::Jasmine::Server.should_receive(:require).with('unicorn').and_raise LoadError
Guard::Jasmine::Server.should_receive(:require).with('thin').and_return true
end

it 'uses thin as server' do
server.should_receive(:start_rack_server).with(:thin, 8888, options)
server.start(options)
end
end

context 'with mongrel available' do
before do
Guard::Jasmine::Server.should_receive(:require).with('unicorn').and_raise LoadError
Guard::Jasmine::Server.should_receive(:require).with('thin').and_raise LoadError
Guard::Jasmine::Server.should_receive(:require).with('mongrel').and_return true
end

it 'uses mongrel as server' do
server.should_receive(:start_rack_server).with(:mongrel, 8888, options)
server.start(options)
end
end

context 'with unicorn, thin or mongrel not being available' do
before do
Guard::Jasmine::Server.should_receive(:require).with('unicorn').and_raise LoadError
Guard::Jasmine::Server.should_receive(:require).with('thin').and_raise LoadError
Guard::Jasmine::Server.should_receive(:require).with('mongrel').and_raise LoadError
end

it 'uses webrick as server' do
server.should_receive(:start_rack_server).with(:webrick, 8888, options)
server.start(options)
end
end
end

context 'with a jasmine config file' do
context 'with the default spec dir' do
before do
File.should_receive(:exists?).with('config.ru').and_return false
File.should_receive(:exists?).with(File.join('spec', 'javascripts', 'support', 'jasmine.yml')).and_return true
end

it 'chooses the jasmine_gem server strategy' do
server.should_receive(:start_rake_server)
server.start(options)
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(options)
end
end

context 'with a custom spec dir' do
let(:options) do
defaults.merge({ :spec_dir => 'specs' })
end

before do
File.should_receive(:exists?).with('config.ru').and_return false
File.should_receive(:exists?).with(File.join('specs', 'support', 'jasmine.yml')).and_return true
end

it 'chooses the jasmine_gem server strategy' do
server.should_receive(:start_rake_server)
server.start(options)
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(options)
end
end
end

context 'without any server config files' do
before do
File.should_receive(:exists?).with('config.ru').and_return false
File.should_receive(:exists?).with(File.join('spec', 'javascripts', 'support', 'jasmine.yml')).and_return false
end

it 'does not start a server' do
server.should_not_receive(:start_rack_server)
server.should_not_receive(:start_rake_server)
server.should_not_receive(:wait_for_server)
server.start(options)
end
end
end

context 'with the :thin strategy' do
let(:options) do
defaults.merge({ :server => :thin })
Expand Down Expand Up @@ -319,4 +199,61 @@
end
end

describe '.detect_server' do
context 'with a `config.ru` file' do
before do
File.should_receive(:exists?).with('config.ru').and_return true
end

context 'with unicorn available' do
before do
Guard::Jasmine::Server.should_receive(:require).with('unicorn').and_return true
end

it 'returns `:unicorn` as server' do
server.detect_server('spec/javascripts').should eql(:unicorn)
end
end

context 'with thin available' do
before do
Guard::Jasmine::Server.should_receive(:require).with('unicorn').and_raise LoadError
Guard::Jasmine::Server.should_receive(:require).with('thin').and_return true
end

it 'returns `:thin` as server' do
server.detect_server('spec/javascripts').should eql(:thin)
end
end

context 'with mongrel available' do
before do
Guard::Jasmine::Server.should_receive(:require).with('unicorn').and_raise LoadError
Guard::Jasmine::Server.should_receive(:require).with('thin').and_raise LoadError
Guard::Jasmine::Server.should_receive(:require).with('mongrel').and_return true
end

it 'returns `:mongrel` as server' do
server.detect_server('spec/javascripts').should eql(:mongrel)
end
end
end

context 'with a `support/jasmine.yml` file in the spec folder' do
before do
File.should_receive(:exists?).with('config.ru').and_return false
File.should_receive(:exists?).with(File.join('spec', 'javascripts', 'support', 'jasmine.yml')).and_return true
end

it 'returns `:jasmine_gem` as server' do
server.detect_server('spec/javascripts').should eql(:jasmine_gem)
end
end

context 'without a recognized server configuration' do
it 'returns `:none` as server' do
server.detect_server('spec/javascripts').should eql(:none)
end
end
end
end
Loading

0 comments on commit 017813a

Please sign in to comment.