Skip to content

Commit

Permalink
Take :feature_sets into account when cleaning the paths. (Fixes #36)
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Oct 31, 2012
1 parent 6254ae5 commit cba25c2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 48 deletions.
3 changes: 2 additions & 1 deletion lib/guard/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Cucumber < Guard
#
def initialize(watchers = [], options = { })
super

@options = {
:all_after_pass => true,
:all_on_start => true,
Expand Down Expand Up @@ -83,7 +84,7 @@ def reload
#
def run_on_changes(paths)
paths += @failed_paths if @options[:keep_failed]
paths = Inspector.clean(paths)
paths = Inspector.clean(paths, options[:feature_sets])
options = @options[:change_format] ? change_format(@options[:change_format]) : @options
passed = Runner.run(paths, paths.include?('features') ? options.merge({ :message => 'Running all features' }) : options)

Expand Down
22 changes: 13 additions & 9 deletions lib/guard/cucumber/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class << self
# Cucumber features.
#
# @param [Array<String>] paths the changed paths
# @param [Array<String>] feature_sets the feature sets
# @return [Array<String>] the valid feature files
#
def clean(paths)
def clean(paths, feature_sets)
paths.uniq!
paths.compact!
paths = paths.select { |p| cucumber_file?(p) || cucumber_folder?(p) }
paths = paths.select { |p| cucumber_file?(p, feature_sets) || cucumber_folder?(p, feature_sets) }
paths = paths.delete_if { |p| included_in_other_path?(p, paths) }
clear_cucumber_files_list
paths
Expand All @@ -27,29 +28,32 @@ def clean(paths)
# Tests if the file is the features folder.
#
# @param [String] file the file
# @param [Array<String>] feature_sets the feature sets
# @return [Boolean] when the file is the feature folder
#
def cucumber_folder?(path)
path.match(/^\/?features/) && !path.match(/\..+$/)
def cucumber_folder?(path, feature_sets)
path.match(/^\/?(#{ feature_sets.join('|') })/) && !path.match(/\..+$/)
end

# Tests if the file is valid.
#
# @param [String] file the file
# @param [Array<String>] feature_sets the feature sets
# @return [Boolean] when the file valid
#
def cucumber_file?(path)
cucumber_files.include?(path.split(':').first)
def cucumber_file?(path, feature_sets)
cucumber_files(feature_sets).include?(path.split(':').first)
end

# Scans the project and keeps a list of all
# feature files in the `features` directory.
#
# @see #clear_jasmine_specs
# @param [Array<String>] feature_sets the feature sets
# @return [Array<String>] the valid files
#
def cucumber_files
@cucumber_files ||= Dir.glob('features/**/*.feature')
def cucumber_files(feature_sets)
@cucumber_files ||= Dir.glob("#{ feature_sets.join(',') }/**/*.feature")
end

# Clears the list of features in this project.
Expand All @@ -66,7 +70,7 @@ def clear_cucumber_files_list
#
def included_in_other_path?(path, paths)
paths = paths.select { |p| p != path }
massaged = path[0...(path.index(":")||path.size)]
massaged = path[0...(path.index(':') || path.size)]
paths.any? { |p| (path.include?(p) && (path.gsub(p, '')).include?('/')) || massaged.include?(p) }
end
end
Expand Down
101 changes: 64 additions & 37 deletions spec/guard/cucumber/inspector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,77 @@

let(:inspector) { Guard::Cucumber::Inspector }

before do
Dir.stub(:glob).and_return ['features/a.feature', 'features/subfolder/b.feature']
end

describe '.clean' do
it 'removes non-feature files' do
inspector.clean(['features/a.feature', 'b.rb']).should == ['features/a.feature']
end
context 'with the standard feature set' do
before do
Dir.stub(:glob).and_return %w(features/a.feature features/subfolder/b.feature)
end

it 'removes non-existing feature files' do
inspector.clean(['features/a.feature', 'features/x.feature']).should == ['features/a.feature']
end
it 'removes non-feature files' do
inspector.clean(%w(features/a.feature b.rb), %w(features)).should == %w(features/a.feature)
end

it 'keeps a feature folder' do
inspector.clean(['features/a.feature', 'features/subfolder']).should == ['features/a.feature',
'features/subfolder']
end
it 'removes non-existing feature files' do
inspector.clean(%w(features/a.feature features/x.feature), %w(features)).should == %w(features/a.feature)
end

it 'removes duplicate paths' do
inspector.clean(['features', 'features']).should == ['features']
end

it 'removes individual feature tests if the path is already in paths to run' do
inspector.clean(['features/a.feature', 'features/a.feature:10']).should == ['features/a.feature']
end
it 'keeps a feature folder' do
inspector.clean(%w(features/a.feature features/subfolder), %w(features)).should == %w(features/a.feature features/subfolder)
end

it 'removes feature folders included in other feature folders' do
inspector.clean(['features/subfolder', 'features']).should == ['features']
end
it 'removes duplicate paths' do
inspector.clean(%w(features features), %w(features)).should == %w(features)
end

it 'removes individual feature tests if the path is already in paths to run' do
inspector.clean(%w(features/a.feature features/a.feature:10), %w(features)).should == %w(features/a.feature)
end

it 'removes feature folders included in other feature folders' do
inspector.clean(%w(features/subfolder features), %w(features)).should == %w(features)
end

it 'removes feature files includes in feature folder' do
inspector.clean(['features/subfolder/b.feature', 'features']).should == ['features']
it 'removes feature files includes in feature folder' do
inspector.clean(%w(features/subfolder/b.feature features), %w(features)).should == %w(features)
end
end
end

describe ".included_in_other_path?" do
it "detects dups when a scenario is specified, but the feature is already included" do
paths = Dir.glob

# dup, add a specific test
dup_path = paths.last + ":22"
paths << dup_path

inspector.send(:included_in_other_path?, dup_path, paths).should be_true

context 'with an additional feature set' do
before do
Dir.stub(:glob).and_return %w(feature_set_1/a.feature feature_set_1/subfolder/b.feature feature_set_2/c.feature feature_set_2/subfolder/d.feature)
end

it 'removes non-feature files' do
inspector.clean(%w(feature_set_1/a.feature feature_set_2/c.feature b.rb), %w(feature_set_1, feature_set_2)).should == %w(feature_set_1/a.feature feature_set_2/c.feature)
end

it 'removes non-existing feature files' do
inspector.clean(%w(feature_set_1/a.feature feature_set_1/x.feature feature_set_2/c.feature feature_set_2/y.feature), %w(feature_set_1 feature_set_2)).should == %w(feature_set_1/a.feature feature_set_2/c.feature)
end

it 'keeps the feature folders' do
inspector.clean(%w(feature_set_1/a.feature feature_set_1/subfolder feature_set_2/c.feature feature_set_2/subfolder), %w(feature_set_1 feature_set_2)).should == %w(feature_set_1/a.feature feature_set_1/subfolder feature_set_2/c.feature feature_set_2/subfolder)
end

it 'removes duplicate paths' do
inspector.clean(%w(feature_set_1 feature_set_1 feature_set_2 feature_set_2), %w(feature_set_1 feature_set_2)).should == %w(feature_set_1 feature_set_2)
end

it 'removes individual feature tests if the path is already in paths to run' do
inspector.clean(%w(feature_set_1/a.feature feature_set_1/a.feature:10 feature_set_2/c.feature feature_set_2/c.feature:25), %w(features feature_set_2)).should == %w(feature_set_1/a.feature feature_set_2/c.feature)
end

it 'removes feature folders included in other feature folders' do
inspector.clean(%w(feature_set_1/subfolder feature_set_1 feature_set_2/subfolder feature_set_2), %w(feature_set_1 feature_set_2)).should == %w(feature_set_1 feature_set_2)
inspector.clean(%w(feature_set_1 feature_set_2/subfolder), %w(feature_set_1 feature_set_2)).should == %w(feature_set_1 feature_set_2/subfolder)
inspector.clean(%w(feature_set_2 feature_set_1/subfolder), %w(feature_set_1 feature_set_2)).should == %w(feature_set_2 feature_set_1/subfolder)
end

it 'removes feature files includes in feature folder' do
inspector.clean(%w(feature_set_1/subfolder/b.feature feature_set_1 feature_set_2/subfolder/c.feature feature_set_2), %w(feature_set_1 feature_set_2)).should == %w(feature_set_1 feature_set_2)
inspector.clean(%w(feature_set_1/subfolder/b.feature feature_set_2), %w(feature_set_1 feature_set_2)).should == %w(feature_set_1/subfolder/b.feature feature_set_2)
inspector.clean(%w(feature_set_2/subfolder/d.feature feature_set_1), %w(feature_set_1 feature_set_2)).should == %w(feature_set_2/subfolder/d.feature feature_set_1)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/guard/cucumber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@

it 'passes the matched paths to the inspector for cleanup' do
runner.stub(:run).and_return(true)
Guard::Cucumber::Inspector.should_receive(:clean).with(['features']).and_return ['features']
Guard::Cucumber::Inspector.should_receive(:clean).with(['features'], ['features']).and_return ['features']
guard.run_on_changes(['features'])
end

Expand Down

0 comments on commit cba25c2

Please sign in to comment.