Skip to content

Commit

Permalink
Add the all_after_pass option
Browse files Browse the repository at this point in the history
When the all_after_pass option is enabled (which it is by default), all the tests
will be run after a failing test pass. This ensures that the process of makeing
the test pass didn't break something else.
  • Loading branch information
Maher4Ever committed Nov 30, 2011
1 parent 67998d1 commit 376de7a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 15 deletions.
50 changes: 41 additions & 9 deletions lib/guard/phpunit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ class PHPUnit < Guard
autoload :Runner, 'guard/phpunit/runner'

DEFAULT_OPTIONS = {
:all_on_start => true,
:keep_failed => true,
:cli => nil,
:tests_path => Dir.pwd
:all_on_start => true,
:all_after_pass => true,
:keep_failed => true,
:cli => nil,
:tests_path => Dir.pwd
}

# Initialize Guard::PHPUnit.
Expand All @@ -33,7 +34,8 @@ def initialize(watchers = [], options = {})
@options = defaults.merge(options)
super(watchers, @options)

@failed_paths = []
@failed_paths = []
@previous_failed = false

Inspector.tests_path = @options[:tests_path]
end
Expand All @@ -54,6 +56,8 @@ def run_all
success = Runner.run(options[:tests_path], options.merge(
:message => 'Running all tests'
))

@previous_failed = !success
throw :task_has_failed unless success
end

Expand All @@ -66,13 +70,41 @@ def run_on_change(paths)
paths = Inspector.clean(paths + @failed_paths)
success = Runner.run(paths, options)

if success
@failed_paths -= paths if @options[:keep_failed]
update_failed_paths(success, paths)
run_all_after_pass(success)
throw :task_has_failed unless success
end

private

# Adds or removes path to the failed_paths bassed
# on the tests result.
#
# @param [Boolean] tests_passed whether the tests passed or not
# @param [Array<String>] paths the tests paths
#
def update_failed_paths(tests_passed, paths)
return unless @options[:keep_failed]

if tests_passed
@failed_paths -= paths
else
@failed_paths += paths if @options[:keep_failed]
@failed_paths += paths
end
end

throw :task_has_failed unless success
# Runs all tests after the failed tests pass.
#
# @param (see .update_failed_paths)
#
def run_all_after_pass(tests_passed)
return unless @options[:all_after_pass]

if tests_passed
run_all if @previous_failed
else
@previous_failed = true
end
end
end
end
58 changes: 52 additions & 6 deletions spec/guard/phpunit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
subject.options[:all_on_start].should be_true
end

it 'sets a default :all_after_pass option' do
subject.options[:all_after_pass].should be_true
end

it 'sets a default :keep_failed option' do
subject.options[:keep_failed].should be_true
end
Expand All @@ -23,15 +27,20 @@

context 'when other options are provided' do

subject { Guard::PHPUnit.new(nil, { :all_on_start => false,
:keep_failed => false,
:cli => '--colors',
:tests_path => 'tests' }) }
subject { Guard::PHPUnit.new(nil, { :all_on_start => false,
:all_after_pass => false,
:keep_failed => false,
:cli => '--colors',
:tests_path => 'tests' }) }

it 'sets :all_on_start with the provided option' do
subject.options[:all_on_start].should be_false
end

it 'sets :all_after_pass with the provided option' do
subject.options[:all_after_pass].should be_false
end

it 'sets :keep_failed with the provided option' do
subject.options[:keep_failed].should be_false
end
Expand Down Expand Up @@ -115,9 +124,10 @@
context 'when tests fail' do
before do
runner.stub(:run).and_return(false)
subject.stub(:run_all).and_return(true)
end

context 'with the :keep_failed option is set to true' do
context 'with the :keep_failed option set to true' do
it 'runs the next changed files plus the failed tests' do
expect { subject.run_on_change ['tests/firstTest.php'] }.to throw_symbol :task_has_failed
runner.should_receive(:run).with(
Expand All @@ -128,7 +138,7 @@
end
end

context 'with the :keep_failed option is set to false' do
context 'with the :keep_failed option set to false' do
subject { Guard::PHPUnit.new(nil, :keep_failed => false) }

it 'runs the next changed files normally without the failed tests' do
Expand All @@ -141,5 +151,41 @@
end
end
end

context 'when tests fail then pass' do
before do
runner.stub(:run).and_return(false, true)
end

context 'with the :all_after_pass option set to true' do
it 'calls #run_all' do
subject.should_receive(:run_all)
expect { subject.run_on_change ['tests/firstTest.php'] }.to throw_symbol :task_has_failed
subject.run_on_change ['tests/firstTest.php']
end

it 'calls #run_all (2)' do
expect { subject.run_all }.to throw_symbol :task_has_failed
subject.should_receive(:run_all)
subject.run_on_change ['tests/firstTest.php']
end
end

context 'with the :all_after_pass option set to false' do
subject { Guard::PHPUnit.new(nil, :all_after_pass => false) }

it 'does not call #run_all' do
subject.should_not_receive(:run_all)
expect { subject.run_on_change ['tests/firstTest.php'] }.to throw_symbol :task_has_failed
subject.run_on_change ['tests/firstTest.php']
end

it 'does not call #run_all (2)' do
expect { subject.run_all }.to throw_symbol :task_has_failed
subject.should_not_receive(:run_all)
subject.run_on_change ['tests/firstTest.php']
end
end
end
end
end

0 comments on commit 376de7a

Please sign in to comment.