Skip to content

Commit

Permalink
Merge pull request #6 from martco/master
Browse files Browse the repository at this point in the history
Guard::Cucumber::Focuser
  • Loading branch information
netzpirat committed Dec 28, 2012
2 parents a5ca519 + 12d59ef commit 98d6bc2
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.2.2 - Dec 28, 2012

- Add Guard::Cucmber::Focuser

## 1.2.2 - Oct 31, 2012

- Add accessors for `last_failed` and `failed_path`
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ unless ENV['TRAVIS']
gem 'rb-fsevent', :require => false
gem 'rb-fchange', :require => false

group :test do
gem "fakefs", :require => "fakefs/safe"
end

require 'rbconfig'

if RbConfig::CONFIG['target_os'] =~ /darwin/i
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ Former `:color`, `:drb`, `:port` and `:profile` options are thus deprecated and
# other shell script.
# The example generates: 'xvfb-run bundle exec cucumber ...'
# default: nil

:focus_on => 'dev' # Focus on scenarios tagged with '@dev'
# If '@dev' is on line 6 in 'foo.feature',
# this example runs: 'bundle exec cucumber foo.feature:6'
# default: nil
```

## Cucumber configuration
Expand Down
1 change: 1 addition & 0 deletions lib/guard/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Cucumber < Guard

autoload :Runner, 'guard/cucumber/runner'
autoload :Inspector, 'guard/cucumber/inspector'
autoload :Focuser, 'guard/cucumber/focuser'

attr_accessor :last_failed, :failed_path

Expand Down
84 changes: 84 additions & 0 deletions lib/guard/cucumber/focuser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module Guard
class Cucumber

# The Cucumber focuser updates cucumber feature paths to
# focus on sections tagged with a provided focus_tag.
#
# For example, if the `foo.feature` file has the provided focus tag
# `@bar` on line 8, then the path will be updated using the cucumber
# syntax for focusing on a section:
#
# foo.feature:8
#
# If '@bar' is found on lines 8 and 16, the path is updated as follows:
#
# foo.feature:8:16
#
# The path is not updated if it does not contain the focus tag.
#
#
module Focuser
class << self

# Focus the supplied paths using the provided focus tag.
#
# @param [Array<String>] paths the locations of the feature files
# @param [String] focus_tag the focus tag to look for in each path
# @return [Array<String>] the updated paths
#
def focus(paths, focus_tag)
return false if paths.empty?

updated_paths = []

paths.each do |path|
focussed_line_numbers = scan_path_for_focus_tag(path, focus_tag)

unless focussed_line_numbers.empty?
updated_paths << append_line_numbers_to_path(
focussed_line_numbers, path
)
else
updated_paths << path
end
end

updated_paths
end

# Checks to see if the file at path contains the focus tag
#
# @param [String] path the file path to search
# @param [String] focus_tag the focus tag to look for in each path
# @return [Array<Integer>] the line numbers that include the focus tag in path
#
def scan_path_for_focus_tag(path, focus_tag)
line_numbers = []

File.open(path, 'r') do |f|
while (line = f.gets)
if line.include?(focus_tag)
line_numbers << f.lineno
end
end
end

line_numbers
end

# Appends the line numbers to the path
#
# @param [Array<Integer>] line_numbers the line numbers to append to the path
# @param [String] path the path that will receive the appended line numbers
# @return [String] the string containing the path appended with the line number
#
def append_line_numbers_to_path(line_numbers, path)
line_numbers.each { |num| path += ":" + num.to_s }

path
end

end
end
end
end
2 changes: 2 additions & 0 deletions lib/guard/cucumber/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def run(paths, options = { })
return false if paths.empty?

message = options[:message] || (paths == ['features'] ? "Running all Cucumber features: #{ cucumber_command(paths, options) }" : "Running Cucumber features: #{ cucumber_command(paths, options) }")
paths = options[:focus_on] ? Focuser.focus(paths, options[:focus_on]) : paths

UI.info message, :reset => true

system(cucumber_command(paths, options))
Expand Down
97 changes: 97 additions & 0 deletions spec/guard/cucumber/focuser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
require 'spec_helper'
require 'fakefs/spec_helpers'

describe Guard::Cucumber::Focuser do
let(:focuser) { Guard::Cucumber::Focuser }
let(:focus_tag) { '@focus' }
let(:null_device) { RUBY_PLATFORM.index('mswin') ? 'NUL' : '/dev/null' }

it "should load Focuser" do
# focuser
end

describe "#focus" do

context "when passed an empty paths list" do
it "returns false" do
focuser.focus([], '@focus').should be_false
end
end

context "when passed a paths argument"
include FakeFS::SpecHelpers
let(:dir) { 'features' }
let(:path) { 'foo.feature' }
let(:path_two) { 'bar.feature' }

before do
File.open(path, 'w') do |f|
f.write "@focus\nScenario: Foo\n\tGiven bar\n"
f.write "Scenario: Bar\n\tGiven lorem\n"
f.write "@focus\nScenario: Ipsum\n\tGiven dolor"
end
end


describe "#focus" do
before do
File.open(path_two, 'w') do |f|
f.write "@focus\nScenario: Lorem\n\tGiven ipsum\n"
f.write "@focus\nScenario: Bar\n\tGiven lorem\n"
f.write "Scenario: Dolor\n\tGiven sit\n"
end
end

it "should return an array of paths updated to focus on line numbers" do
paths = [path, path_two]

focuser.focus(paths, focus_tag).should eq([
'foo.feature:1:6',
'bar.feature:1:4'
])
end

end

describe "#scan_path_for_focus_tag" do

context "file with focus tags in it" do

it "should return an array of line numbers" do
focuser.scan_path_for_focus_tag(path, focus_tag).should eq([1, 6])
end

describe "#append_line_numbers_to_path" do

it "should return a path with line numbers appended" do
line_numbers = [1,2]
returned_path = focuser.append_line_numbers_to_path(line_numbers, path)
returned_path.should eq(path + ":1:2")
end

end

end

context "file without focus tags in it" do
before do
File.open(path, 'w') do |f|
f.write "Scenario: Foo\n\tGiven bar"
f.write "\nScenario: Bar\n\tGiven lorem"
f.write "\nScenario: Ipsum\n\tGiven dolor"
end
end

it "should return an empty array" do
focuser.scan_path_for_focus_tag(path, focus_tag).should eq([])
end

end

end


end

end

17 changes: 17 additions & 0 deletions spec/guard/cucumber/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
end

context 'with a paths argument' do

it 'runs the given paths' do
runner.should_receive(:system).with(
/features\/foo\.feature features\/bar\.feature$/
Expand Down Expand Up @@ -62,6 +63,22 @@
end
end

context 'with a :focus_on option' do
it 'passes the value in :focus_on to the Focuser' do
paths = ['features']
focus_on_hash = {
:focus_on => '@focus'
}

Guard::Cucumber::Focuser.should_receive(:focus).with(
paths, focus_on_hash[:focus_on]
).and_return(paths)

runner.run(paths, focus_on_hash)
end
end


describe ":binstubs" do
it "runs without Bundler with binstubs option to true and bundler option to false" do
subject.should_receive(:system).with(
Expand Down
8 changes: 7 additions & 1 deletion spec/guard/cucumber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
:all_on_start => false,
:keep_failed => false,
:cli => '--color',
:feature_sets => ['feature_set_a', 'feature_set_b'] }) }
:feature_sets => ['feature_set_a', 'feature_set_b'],
:focus_on => '@focus' }) }

it 'sets the provided :all_after_pass option' do
guard.options[:all_after_pass].should be_false
Expand All @@ -68,6 +69,11 @@
it 'sets the provided :feature_sets option' do
guard.options[:feature_sets].should eql ['feature_set_a', 'feature_set_b']
end

it 'sets the provided :focus_on option' do
guard.options[:focus_on].should eql '@focus'
end

end
end

Expand Down

0 comments on commit 98d6bc2

Please sign in to comment.