Monkey Patch Guard Cucumber Notification Formatter无法正常工作

时间:2012-12-12 05:23:25

标签: ruby-on-rails cucumber guard monkeypatching

我想对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。

2 个答案:

答案 0 :(得分:1)

Guard :: Cucumber进行system调用以在子shell中启动Cucumber,并且您的猴子补丁未在该环境中加载。您需要告诉Cucumber在启动时要求您的补丁,例如通知格式化程序为required

我现在没有积极开发Guard :: Cucumber,因为我没有Cucumber的项目,但我仍然坚持它,如果你提出拉动请求,我会愉快地合并这个改进。我认为您的改进对其他用户也很有用。

答案 1 :(得分:0)

尝试将此文件放在spec/support目录中(如果不存在则创建)

这个目录内容通常在运行任何测试之前包含在spec/spec_helper.rb