diff --git a/CHANGELOG.md b/CHANGELOG.md index ceabff7..94cf87f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/guard/jasmine.rb b/lib/guard/jasmine.rb index bd5be3a..5f38236 100644 --- a/lib/guard/jasmine.rb +++ b/lib/guard/jasmine.rb @@ -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, @@ -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 diff --git a/lib/guard/jasmine/cli.rb b/lib/guard/jasmine/cli.rb index ac25d87..6e3759c 100644 --- a/lib/guard/jasmine/cli.rb +++ b/lib/guard/jasmine/cli.rb @@ -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, @@ -139,19 +138,17 @@ class CLI < Thor # @param [Array] 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 @@ -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 diff --git a/spec/guard/jasmine/cli_spec.rb b/spec/guard/jasmine/cli_spec.rb index 02f03bc..32f5e5a 100644 --- a/spec/guard/jasmine/cli_spec.rb +++ b/spec/guard/jasmine/cli_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/guard/jasmine/runner_spec.rb b/spec/guard/jasmine/runner_spec.rb index 55a05e0..7a88211 100644 --- a/spec/guard/jasmine/runner_spec.rb +++ b/spec/guard/jasmine/runner_spec.rb @@ -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 @@ -161,7 +162,6 @@ JSON end - let(:phantomjs_command) do "/usr/local/bin/phantomjs #@project_path/lib/guard/jasmine/phantomjs/guard-jasmine.js" end diff --git a/spec/guard/jasmine_spec.rb b/spec/guard/jasmine_spec.rb index f3db3aa..f1061ca 100644 --- a/spec/guard/jasmine_spec.rb +++ b/spec/guard/jasmine_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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