如何在git钩子中计算用:focus过滤的RSpec示例? [英] How to count RSpec examples filtered with :focus in a git hook?

查看:57
本文介绍了如何在git钩子中计算用:focus过滤的RSpec示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Git预提交钩子,如果有一个标有:focus的示例,该钩子将不允许用户提交.

I am trying to write a Git pre-commit hook that would not let the user commit if there is an example that is tagged with :focus.

使用RSpec的API(即使是私有的也可以),是否可以通过:focus过滤器找出示例数量?

Using RSpec's API (okay even if it is private), is there any way to find out the number of examples with the :focus filter?

我找到了 example_count-instance_method .它可能很有用,但我不确定如何从外部脚本中调用它.

I found the example_count-instance_method. It could be useful but I'm not sure how it can be called from an external script.

推荐答案

这里是一个过度使用 pre_commit钩子,它使用RSpecs私有API来使用:focus过滤器找出规格:

Here is an Overcommit pre_commit hook that uses RSpecs private API to find out specs with :focus filter:

require 'rspec'

module Overcommit
  module Hook
    module PreCommit
      # NOTE: This makes use of many methods from RSpecs private API.
      class EnsureFocusFreeSpecs < Base
        def configure_rspec(applicable_files)
          RSpec.configure do |config|
            config.inclusion_filter = :focus
            config.files_or_directories_to_run = applicable_files
            config.inclusion_filter.rules
            config.requires = %w(spec_helper rails_helper)
            config.load_spec_files
          end
        end

        def run
          configure_rspec(applicable_files)

          return :pass if RSpec.world.example_count.zero?

          files = RSpec.world.filtered_examples.reject {|_k, v| v.empty?}.keys.map(&:file_path).uniq
          [:fail, "Trying to commit focused spec(s) in:\n\t#{files.join("\n\t")}"]
        end
      end
    end
  end
end

这篇关于如何在git钩子中计算用:focus过滤的RSpec示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆