From 732cc625a8ba1073cecc88929ce30d50f21481ab Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 1 Jun 2011 17:14:23 -0400 Subject: [PATCH] better handling for when spec runner is kiled prematurely --- Gemfile | 1 + lib/guard/jasmine-headless-webkit.rb | 26 +++++++++++--- lib/guard/jasmine-headless-webkit/runner.rb | 18 +++++++--- .../jasmine-headless-webkit/runner_spec.rb | 35 +++++++++++++++++++ .../lib/guard/jasmine-headless-webkit_spec.rb | 30 ++++++++++++++++ 5 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 spec/lib/guard/jasmine-headless-webkit/runner_spec.rb diff --git a/Gemfile b/Gemfile index 68bb1db..4156a87 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,4 @@ gem 'rspec' gem 'mocha' gem 'rake', '0.8.7' gem 'growl' +gem 'fakefs', :require => nil diff --git a/lib/guard/jasmine-headless-webkit.rb b/lib/guard/jasmine-headless-webkit.rb index f8bf392..f69c546 100644 --- a/lib/guard/jasmine-headless-webkit.rb +++ b/lib/guard/jasmine-headless-webkit.rb @@ -18,11 +18,14 @@ module Guard end def run_all - JasmineHeadlessWebkitRunner.run if run_before + JasmineHeadlessWebkitRunner.run if run_before and run_jammit + @ran_jammit = false end def run_on_change(paths) - if run_before + @ran_jammit = false + if run_before and run_jammit + @ran_jammit = true run_all if JasmineHeadlessWebkitRunner.run(paths) == 0 end end @@ -30,13 +33,26 @@ module Guard private def run_before if @options[:run_before] - UI.info "Guard::JasmineHeadlessWebkit running #{@options[:run_before]} first..." - system @options[:run_before] - $?.exitstatus == 0 + run_program(@options[:run_before]) else true end end + + def run_jammit + if @options[:jammit] && !@ran_jammit + run_program("Jammit", %{jammit -f 2>/dev/null}) + else + true + end + end + + def run_program(name, command = nil) + command ||= name + UI.info "Guard::JasmineHeadlessWebkit running #{name}..." + system command + $?.exitstatus == 0 + end end class Dsl diff --git a/lib/guard/jasmine-headless-webkit/runner.rb b/lib/guard/jasmine-headless-webkit/runner.rb index ca2c79c..d9851ac 100644 --- a/lib/guard/jasmine-headless-webkit/runner.rb +++ b/lib/guard/jasmine-headless-webkit/runner.rb @@ -9,15 +9,25 @@ module Guard system %{jasmine-headless-webkit --report #{file.path} -c #{paths.join(" ")}} - total, fails, any_console, secs = File.read(file.path).strip.split('/') + notify(file.path) + end - Notifier.notify(message(total, fails, secs, any_console == "T"), :title => 'Jasmine results', :image => image(any_console == "T", fails)) - fails.to_i + def notify(file) + if (data = File.read(file).strip).empty? + Notifier.notify('Spec runner interrupted!', :title => 'Jasmine results', :image => :failed) + else + total, fails, any_console, secs = File.read(file).strip.split('/') + + Notifier.notify(message(total, fails, secs, any_console == "T"), :title => 'Jasmine results', :image => image(any_console == "T", fails)) + fails.to_i + end end private def message(total, fails, secs, any_console) - "#{total} tests, #{fails} failures, #{secs} secs#{any_console ? ', console.log used' : ''}." + total_word = (total.to_i == 1) ? "test" : "tests" + + "#{total} #{total_word}, #{fails} failures, #{secs} secs#{any_console ? ', console.log used' : ''}." end def image(any_console, fails) diff --git a/spec/lib/guard/jasmine-headless-webkit/runner_spec.rb b/spec/lib/guard/jasmine-headless-webkit/runner_spec.rb new file mode 100644 index 0000000..cecc65e --- /dev/null +++ b/spec/lib/guard/jasmine-headless-webkit/runner_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' +require 'guard/jasmine-headless-webkit/runner' +require 'fakefs/spec_helpers' + +describe Guard::JasmineHeadlessWebkitRunner do + describe '.notify' do + include FakeFS::SpecHelpers + + let(:file) { 'temp.txt' } + + before do + File.open(file, 'w') { |fh| fh.print data } + end + + context 'system run not interrupted' do + let(:data) { '1/0/F/5' } + + it 'should notify with the right information' do + Guard::Notifier.expects(:notify).with("1 test, 0 failures, 5 secs.", { :title => 'Jasmine results', :image => :success }) + + Guard::JasmineHeadlessWebkitRunner.notify(file) + end + end + + context 'system run interrupted' do + let(:data) { '' } + + it 'should notify failure' do + Guard::Notifier.expects(:notify).with("Spec runner interrupted!", { :title => 'Jasmine results', :image => :failed }) + + Guard::JasmineHeadlessWebkitRunner.notify(file) + end + end + end +end diff --git a/spec/lib/guard/jasmine-headless-webkit_spec.rb b/spec/lib/guard/jasmine-headless-webkit_spec.rb index f81984c..4aaa8ef 100644 --- a/spec/lib/guard/jasmine-headless-webkit_spec.rb +++ b/spec/lib/guard/jasmine-headless-webkit_spec.rb @@ -50,6 +50,7 @@ describe Guard::JasmineHeadlessWebkit do context 'with failing command' do before do Guard::JasmineHeadlessWebkitRunner.expects(:run).never + Guard::UI.expects(:info).with(regexp_matches(/false/)) end let(:options) { { :run_before => 'false' } } @@ -62,6 +63,7 @@ describe Guard::JasmineHeadlessWebkit do context 'with succeeding command' do before do Guard::JasmineHeadlessWebkitRunner.expects(:run).once + Guard::UI.expects(:info).with(regexp_matches(/true/)) end let(:options) { { :run_before => 'true' } } @@ -71,4 +73,32 @@ describe Guard::JasmineHeadlessWebkit do end end end + + describe 'run jammit first' do + context 'run on run_all if called first' do + before do + guard.expects(:run_program).once.returns(true) + Guard::JasmineHeadlessWebkitRunner.expects(:run).once + end + + let(:options) { { :jammit => true } } + + it "should run jammit first" do + guard.run_all + end + end + + context 'only run once if run_on_change is successful' do + before do + guard.expects(:run_program).once.returns(true) + Guard::JasmineHeadlessWebkitRunner.expects(:run).twice.returns(0) + end + + let(:options) { { :jammit => true } } + + it "should run jammit only once" do + guard.run_on_change([]) + end + end + end end