我想对 Guard Cucumber Notification Formatter 做一个简单的更改以传递优先级,有点像 Guard Rspec 所做的,以便改进咆哮样式。真是小事。
我已经在初始化文件中将其作为猴子补丁进行了尝试,但它不起作用。我已经尝试了各种各样的东西,但无论出于何种原因,在运行测试时我都无法让它识别我的猴子补丁。我在补丁中所做的任何更改似乎都没有改变。我已经在命名空间上尝试了各种变体,以防我在那里出错 - 我认为这是最有可能的。这是我要申请的内容:
initializers/guard_cucumber_patch.rb
require 'guard'
require 'guard/notifier'
require 'cucumber/formatter/console'
require 'cucumber/formatter/io'
module Guard
class Cucumber
class NotificationFormatter
def step_name(keyword, step_match, status, source_indent, background, file_colon_line)
if [:failed, :pending, :undefined].index(status)
@rerun = true
step_name = step_match.format_args(lambda { |param| "*#{ param }*" })
::Guard::Notifier.notify(step_name,
:title => @feature_name,
:image => icon_for(status),
:priority => priority(icon_for(status)))
end
end
# Just stolen from the guard/rspec/formatter code
def priority(image)
{ :failed => 2,
:pending => -1,
:success => -2
}[image]
end
end
end
end
原guard/cucumber/notification_formatter.rb文件如下:
require 'guard'
require 'guard/notifier'
require 'cucumber/formatter/console'
require 'cucumber/formatter/io'
module Guard
class Cucumber
# 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 https://github.com/cucumber/cucumber/wiki/Custom-Formatters
#
class NotificationFormatter
include ::Cucumber::Formatter::Console
attr_reader :step_mother
# 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
#
def initialize(step_mother, path_or_io, options)
@options = options
@file_names = []
@step_mother = step_mother
end
# Notification after all features have completed.
#
# @param [Array[Cucumber::Ast::Feature]] features the ran features
#
def after_features(features)
notify_summary
write_rerun_features if !@file_names.empty?
end
# Before a feature gets run.
#
# @param [Cucumber::Ast::FeatureElement] feature_element
#
def before_feature_element(feature_element)
@rerun = false
@feature_name = feature_element.name
end
# After a feature gets run.
#
# @param [Cucumber::Ast::FeatureElement] feature_element
#
def after_feature_element(feature_element)
if @rerun
@file_names << feature_element.file_colon_line
@rerun = false
end
end
# 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
#
def step_name(keyword, step_match, status, source_indent, background, file_colon_line)
if [:failed, :pending, :undefined].index(status)
@rerun = true
step_name = step_match.format_args(lambda { |param| "*#{ param }*" })
::Guard::Notifier.notify step_name, :title => @feature_name, :image => icon_for(status)
end
end
private
# Notify the user with a system notification about the
# result of the feature tests.
#
def notify_summary
icon, messages = nil, []
[:failed, :skipped, :undefined, :pending, :passed].reverse.each do |status|
if step_mother.steps(status).any?
step_icon = icon_for(status)
icon = step_icon if step_icon
messages << dump_count(step_mother.steps(status).length, 'step', status.to_s)
end
end
::Guard::Notifier.notify messages.reverse.join(', '), :title => 'Cucumber Results', :image => icon
end
# Writes the `rerun.txt` file containing all failed features.
#
def write_rerun_features
File.open('rerun.txt', 'w') do |f|
f.puts @file_names.join(' ')
end
end
# Gives the icon name to use for the status.
#
# @param [Symbol] status the cucumber status
# @return [Symbol] the Guard notification symbol
#
def icon_for(status)
case status
when :passed
:success
when :pending, :undefined, :skipped
:pending
when :failed
:failed
else
nil
end
end
end
end
end
编辑:如果这对猴子业务有影响,我正在使用 jruby-head。