forked from netzpirat/guard-cucumber
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from martco/master
Guard::Cucumber::Focuser
- Loading branch information
Showing
9 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters