class Guard::Cucumber::NotificationFormatter

The notification formatter is a Cucumber formatter that Guard::Cucumber passes to the Cucumber binary. It writes the `rerun.txt` file with the failed features an creates system notifications.

@see github.com/cucumber/cucumber/wiki/Custom-Formatters

Attributes

step_mother[R]

Public Class Methods

new(step_mother, _path_or_io, options) click to toggle source

Initialize the formatter.

@param [Cucumber::Runtime] step_mother the step mother @param [String, IO] path_or_io the path or IO to the feature file @param [Hash] options the options

# File lib/guard/cucumber/notification_formatter.rb, line 40
def initialize(step_mother, _path_or_io, options)
  @options = options
  @file_names = []
  @step_mother = step_mother
  @feature = nil
end

Public Instance Methods

after_feature_element(feature_element) click to toggle source

After a feature gets run.

@param [Cucumber::Ast::FeatureElement] feature_element

# File lib/guard/cucumber/notification_formatter.rb, line 78
def after_feature_element(feature_element)
  if @rerun
    @file_names << feature_element.location.to_s
    @rerun = false
  end
end
after_features(_features) click to toggle source

Notification after all features have completed.

@param [Array] features the ran features

# File lib/guard/cucumber/notification_formatter.rb, line 59
def after_features(_features)
  notify_summary
  write_rerun_features if !@file_names.empty?
end
before_background(_background) click to toggle source
# File lib/guard/cucumber/notification_formatter.rb, line 47
def before_background(_background)
  # NOTE: background.feature is nil on newer gherkin versions
end
before_feature(feature) click to toggle source
# File lib/guard/cucumber/notification_formatter.rb, line 51
def before_feature(feature)
  @feature = feature
end
before_feature_element(_feature_element) click to toggle source

Before a feature gets run.

@param [Cucumber::Ast::FeatureElement] feature_element

# File lib/guard/cucumber/notification_formatter.rb, line 68
def before_feature_element(_feature_element)
  @rerun = false
  # TODO: show feature element name instead?
  # @feature = feature_element.name
end
step_name(_keyword, step_match, status, _src_indent, _bckgnd, _loc) click to toggle source

Gets called when a step is done.

@param [String] keyword the keyword @param [Cucumber::StepMatch] step_match the step match @param [Symbol] status the status of the step @param [Integer] source_indent the source indentation @param [Cucumber::Ast::Background] background the feature background @param [String] file name and line number describing where the step is used

# File lib/guard/cucumber/notification_formatter.rb, line 95
def step_name(_keyword, step_match, status, _src_indent, _bckgnd, _loc)
  return unless [:failed, :pending, :undefined].index(status)

  @rerun = true
  step_name = step_match.format_args(lambda { |param| "*#{param}*" })

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

Private Instance Methods

dump_count(count, what, state = nil) click to toggle source
# File lib/guard/cucumber/notification_formatter.rb, line 148
def dump_count(count, what, state = nil)
  [count, state, "#{what}#{count == 1 ? '' : 's'}"].compact.join(' ')
end
icon_for(status) click to toggle source

Gives the icon name to use for the status.

@param [Symbol] status the cucumber status @return [Symbol] the Guard notification symbol

# File lib/guard/cucumber/notification_formatter.rb, line 137
def icon_for(status)
  case status
  when :passed
    :success
  when :pending, :undefined, :skipped
    :pending
  when :failed
    :failed
  end
end
notify_summary() click to toggle source

Notify the user with a system notification about the result of the feature tests.

# File lib/guard/cucumber/notification_formatter.rb, line 110
def notify_summary
  # TODO: MOVE THIS OUTSIDE THE FORMATTER!!!!
  statuses = [:failed, :skipped, :undefined, :pending, :passed]
  statuses = statuses.reverse
  statuses.select! { |status| step_mother.steps(status).any? }

  messages = statuses.map { |status| status_to_message(status) }

  icon = statuses.reverse.detect { |status| icon_for(status) }

  msg = messages.reverse.join(", ")
  Guard::Compat::UI.notify msg, title: "Cucumber Results", image: icon
end
status_to_message(status) click to toggle source
# File lib/guard/cucumber/notification_formatter.rb, line 152
def status_to_message(status)
  len = step_mother.steps(status).length
  dump_count(len, "step", status.to_s)
end
write_rerun_features() click to toggle source

Writes the `rerun.txt` file containing all failed features.

# File lib/guard/cucumber/notification_formatter.rb, line 126
def write_rerun_features
  File.open("rerun.txt", "w") do |f|
    f.puts @file_names.join(" ")
  end
end