From cba25c2150a31b19c56adbbcc2bcade2477f3465 Mon Sep 17 00:00:00 2001 From: Michael Kessler Date: Wed, 31 Oct 2012 09:40:23 +0100 Subject: [PATCH] Take `:feature_sets` into account when cleaning the paths. (Fixes #36) --- lib/guard/cucumber.rb | 3 +- lib/guard/cucumber/inspector.rb | 22 +++--- spec/guard/cucumber/inspector_spec.rb | 101 ++++++++++++++++---------- spec/guard/cucumber_spec.rb | 2 +- 4 files changed, 80 insertions(+), 48 deletions(-) diff --git a/lib/guard/cucumber.rb b/lib/guard/cucumber.rb index 189b202..4f0fdee 100644 --- a/lib/guard/cucumber.rb +++ b/lib/guard/cucumber.rb @@ -30,6 +30,7 @@ class Cucumber < Guard # def initialize(watchers = [], options = { }) super + @options = { :all_after_pass => true, :all_on_start => true, @@ -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) diff --git a/lib/guard/cucumber/inspector.rb b/lib/guard/cucumber/inspector.rb index 13a5ca0..bb4a299 100644 --- a/lib/guard/cucumber/inspector.rb +++ b/lib/guard/cucumber/inspector.rb @@ -11,12 +11,13 @@ class << self # Cucumber features. # # @param [Array] paths the changed paths + # @param [Array] feature_sets the feature sets # @return [Array] 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 @@ -27,29 +28,32 @@ def clean(paths) # Tests if the file is the features folder. # # @param [String] file the file + # @param [Array] 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] 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] feature_sets the feature sets # @return [Array] 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. @@ -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 diff --git a/spec/guard/cucumber/inspector_spec.rb b/spec/guard/cucumber/inspector_spec.rb index 04bcc0d..c4374e4 100644 --- a/spec/guard/cucumber/inspector_spec.rb +++ b/spec/guard/cucumber/inspector_spec.rb @@ -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 diff --git a/spec/guard/cucumber_spec.rb b/spec/guard/cucumber_spec.rb index 96f83ad..b29bf2a 100644 --- a/spec/guard/cucumber_spec.rb +++ b/spec/guard/cucumber_spec.rb @@ -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