Skip to content

Commit

Permalink
fix crash on cucumber 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Mar 29, 2015
1 parent a298c8c commit 3dd7df3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
15 changes: 10 additions & 5 deletions lib/guard/cucumber/notification_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ def initialize(step_mother, _path_or_io, options)
@options = options
@file_names = []
@step_mother = step_mother
@feature = nil
end

def before_background(background)
@feature_name = background.feature.name
def before_background(_background)
# NOTE: background.feature is nil on newer gherkin versions
end

def before_feature(feature)
@feature = feature
end

# Notification after all features have completed.
Expand All @@ -62,7 +67,8 @@ def after_features(_features)
#
def before_feature_element(feature_element)
@rerun = false
@feature_name = feature_element.name
# TODO: show feature element name instead?
# @feature = feature_element.name
end

# After a feature gets run.
Expand All @@ -89,11 +95,10 @@ def after_feature_element(feature_element)
def step_name(_keyword, step_match, status, _src_indent, _bckgnd, _loc)
return unless [:failed, :pending, :undefined].index(status)

# TODO: NO COVERAGE HERE!
@rerun = true
step_name = step_match.format_args(lambda { |param| "*#{ param }*" })

options = { title: @feature_name, image: icon_for(status) }
options = { title: @feature.name, image: icon_for(status) }
Guard::Compat::UI.notify(step_name, options)
end

Expand Down
28 changes: 20 additions & 8 deletions spec/guard/cucumber/notification_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,31 @@
end

describe "#step_name" do
let(:step_match) { instance_double(Cucumber::StepMatch) }
let(:feature) { instance_double(Cucumber::Ast::Feature, name: "feature1") }

before do
subject.before_feature(feature)
subject.before_background(background)
allow(step_match).to receive(:format_args) do |block|
block.call "step_name1"
end
end

context "when failure is in a background step" do
let(:step_match) { instance_double(Cucumber::StepMatch) }
let(:feature) { instance_double(Cucumber::Ast::Feature, name: "feature1") }
let(:background) { instance_double(Cucumber::Ast::Background, feature: feature) }

before do
subject.before_background(background)
allow(step_match).to receive(:format_args) do |block|
block.call "step_name1"
end
it "notifies with a valid feature name" do
expect(Guard::Compat::UI).to receive(:notify).with("*step_name1*", hash_including(title: "feature1"))
subject.step_name(nil, step_match, :failed, nil, nil, nil)
end
end

it "notifies with a valid feature name" do
# workaround for: https://github.com/cucumber/gherkin/issues/334
context "with a buggy Background implementation" do
let(:background) { instance_double(Cucumber::Ast::Background, feature: nil) }

it "correctly gets the feature name" do
expect(Guard::Compat::UI).to receive(:notify).with("*step_name1*", hash_including(title: "feature1"))
subject.step_name(nil, step_match, :failed, nil, nil, nil)
end
Expand Down

0 comments on commit 3dd7df3

Please sign in to comment.