Skip to content

Commit

Permalink
Proper spec dir detection when using the Jasmine gem. (See #120)
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Apr 4, 2013
1 parent 017813a commit ec1b1d9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Master

- [#120](https://github.com/netzpirat/guard-jasmine/issues/120): Proper spec dir detection when using the Jasmine gem.
- [#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
Expand Down
3 changes: 2 additions & 1 deletion lib/guard/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Jasmine < Guard
:rackup_config => nil,
:jasmine_url => nil,
:timeout => 60,
:spec_dir => 'spec/javascripts',
:spec_dir => nil,
:notification => true,
:hide_success => false,
:all_on_start => true,
Expand Down Expand Up @@ -86,6 +86,7 @@ class Jasmine < Guard
def initialize(watchers = [], options = { })
options = DEFAULT_OPTIONS.merge(options)

options[:spec_dir] ||= File.exists?(File.join('spec', 'javascripts')) ? File.join('spec', 'javascripts') : 'spec'
options[:port] ||= Jasmine.find_free_server_port
options[:server] ||= :auto
options[:server] = ::Guard::Jasmine::Server.detect_server(options[:spec_dir]) if options[:server] == :auto
Expand Down
11 changes: 5 additions & 6 deletions lib/guard/jasmine/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class CLI < Thor
method_option :spec_dir,
:type => :string,
:aliases => '-d',
:default => 'spec/javascripts',
:desc => 'The directory with the Jasmine specs'

method_option :url,
Expand Down Expand Up @@ -139,19 +138,17 @@ class CLI < Thor
# @param [Array<String>] paths the name of the specs to run
#
def spec(*paths)
paths = [options.spec_dir] if paths.empty?

runner_options = { }
runner_options = {}
runner_options[:port] = options.port || CLI.find_free_server_port
runner_options[:spec_dir] = options.spec_dir || (File.exists?(File.join('spec', 'javascripts')) ? File.join('spec', 'javascripts') : 'spec')
runner_options[:server] = options.server.to_sym == :auto ? ::Guard::Jasmine::Server.detect_server(runner_options[:spec_dir]) : options.server.to_sym
runner_options[:jasmine_url] = options.url || "http://localhost:#{ runner_options[:port] }#{ options.server.to_sym == :jasmine_gem ? '/' : '/jasmine' }"
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 == :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
runner_options[:spec_dir] = options.spec_dir
runner_options[:console] = [:always, :never, :failure].include?(options.console.to_sym) ? options.console.to_sym : :failure
runner_options[:errors] = [:always, :never, :failure].include?(options.errors.to_sym) ? options.errors.to_sym : :failure
runner_options[:specdoc] = [:always, :never, :failure].include?(options.specdoc.to_sym) ? options.specdoc.to_sym : :always
Expand All @@ -167,6 +164,8 @@ def spec(*paths)
runner_options[:hide_success] = true
runner_options[:max_error_notify] = 0

paths = [runner_options[:spec_dir]] if paths.empty?

if CLI.phantomjs_bin_valid?(runner_options[:phantomjs_bin])
catch(:task_has_failed) do
::Guard::Jasmine::Server.start(runner_options) unless runner_options[:server] == :none
Expand Down
28 changes: 23 additions & 5 deletions spec/guard/jasmine/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
runner.stub(:run)
server.stub(:start)
server.stub(:stop)
server.stub(:detect_server)
cli.stub(:which).and_return '/usr/local/bin/phantomjs'
cli.stub(:phantomjs_bin_valid?).and_return true
cli.stub(:runner_available?).and_return true
Expand Down Expand Up @@ -140,7 +141,7 @@
context 'without specified options' do
context 'for the server' do
it 'detects the server type' do
server.should_receive(:detect_server).with('spec/javascripts')
server.should_receive(:detect_server).with('spec')
cli.start(['spec'])
end

Expand Down Expand Up @@ -188,9 +189,26 @@

context 'for the runner' do
context 'without a specific spec dir' do
it 'runs all default specs when the paths are empty' do
runner.should_receive(:run).with(['spec/javascripts'], anything()).and_return [true, []]
cli.start(['spec'])
context 'with a spec/javascripts folder' do
before do
File.should_receive(:exists?).with('spec/javascripts').and_return true
end

it 'runs all specs in the spec/javascripts folder' do
runner.should_receive(:run).with(['spec/javascripts'], anything()).and_return [true, []]
cli.start(['spec'])
end
end

context 'without a spec/javascripts folder' do
before do
File.should_receive(:exists?).with('spec/javascripts').and_return false
end

it 'runs all specs in the spec folder' do
runner.should_receive(:run).with(['spec'], anything()).and_return [true, []]
cli.start(['spec'])
end
end
end

Expand All @@ -202,7 +220,7 @@
end

it 'sets the spec dir' do
runner.should_receive(:run).with(anything(), hash_including(:spec_dir => 'spec/javascripts')).and_return [true, []]
runner.should_receive(:run).with(anything(), hash_including(:spec_dir => 'spec')).and_return [true, []]
cli.start(['spec'])
end

Expand Down
6 changes: 3 additions & 3 deletions spec/guard/jasmine/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
let(:formatter) { Guard::Jasmine::Formatter }

let(:defaults) { Guard::Jasmine::DEFAULT_OPTIONS.merge({
:jasmine_url => 'http://localhost:8888/jasmine',
:phantomjs_bin => '/usr/local/bin/phantomjs' })
:jasmine_url => 'http://localhost:8888/jasmine',
:phantomjs_bin => '/usr/local/bin/phantomjs',
:spec_dir => 'spec/javascripts' })
}

let(:phantomjs_error_response) do
Expand Down Expand Up @@ -161,7 +162,6 @@
JSON
end


let(:phantomjs_command) do
"/usr/local/bin/phantomjs #@project_path/lib/guard/jasmine/phantomjs/guard-jasmine.js"
end
Expand Down
46 changes: 34 additions & 12 deletions spec/guard/jasmine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
formatter.stub(:notify)
server.stub(:start)
server.stub(:stop)
server.stub(:detect_server)
Guard::Jasmine.stub(:which).and_return '/usr/local/bin/phantomjs'
end

describe '#initialize' do
context 'when no options are provided' do
it 'detects the current server' do
server.should_receive(:detect_server).with('spec/javascripts')
guard.start
end

it 'sets a default :server_env option' do
guard.options[:server_env].should eql defaults[:server_env]
end
Expand All @@ -49,10 +45,6 @@
guard.options[:timeout].should eql 60
end

it 'sets a default :spec_dir option' do
guard.options[:spec_dir].should eql 'spec/javascripts'
end

it 'sets a default :all_on_start option' do
guard.options[:all_on_start].should be_true
end
Expand Down Expand Up @@ -137,6 +129,36 @@
::Guard::Jasmine.should_receive(:which).and_return '/bin/phantomjs'
guard.options[:phantomjs_bin].should eql '/bin/phantomjs'
end

context 'with a spec/javascripts folder' do
before do
File.should_receive(:exists?).with('spec/javascripts').and_return true
end

it 'sets a default :spec_dir option' do
guard.options[:spec_dir].should eql 'spec/javascripts'
end

it 'detects the current server' do
server.should_receive(:detect_server).with('spec/javascripts')
::Guard::Jasmine.new
end
end

context 'without a spec/javascripts folder' do
before do
File.should_receive(:exists?).with('spec/javascripts').and_return false
end

it 'sets a default :spec_dir option' do
guard.options[:spec_dir].should eql 'spec'
end

it 'detects the current server' do
server.should_receive(:detect_server).with('spec')
::Guard::Jasmine.new
end
end
end

context 'with other options than the default ones' do
Expand Down Expand Up @@ -364,7 +386,7 @@
server.should_receive(:start).with(hash_including(:server => :jasmine_gem,
:port => 3333,
:server_env => 'test',
:spec_dir => 'spec/javascripts',
:spec_dir => 'spec',
:rackup_config => nil))
guard.start
end
Expand Down Expand Up @@ -454,7 +476,7 @@

context 'without a specified spec dir' do
it 'starts the Runner with the default spec dir' do
runner.should_receive(:run).with(['spec/javascripts'], kind_of(Hash)).and_return [['spec/javascripts/a.js.coffee'], true]
runner.should_receive(:run).with(['spec'], kind_of(Hash)).and_return [['spec/javascripts/a.js.coffee'], true]

guard.run_all
end
Expand All @@ -475,7 +497,7 @@
let(:guard) { Guard::Jasmine.new(nil, { :run_all => { :specdoc => :overwritten } }) }

it 'starts the Runner with the merged run all options' do
runner.should_receive(:run).with(['spec/javascripts'], hash_including({ :specdoc => :overwritten })).and_return [['spec/javascripts/a.js.coffee'], true]
runner.should_receive(:run).with(['spec'], hash_including({ :specdoc => :overwritten })).and_return [['spec/javascripts/a.js.coffee'], true]

guard.run_all
end
Expand Down

0 comments on commit ec1b1d9

Please sign in to comment.