加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何计算过滤的RSpec示例:聚焦在git钩子中?

发布时间:2020-12-17 03:29:37 所属栏目:百科 来源:网络整理
导读:我正在尝试编写一个 Git预提交钩子,如果有一个标记为:focus的示例,则不会让用户提交. 使用RSpec的API(即使它是私有的,也没关系),有没有办法找到带有:焦点过滤器的例子数量? 我找到了example_count-instance_method.它可能很有用,但我不确定如何从外部脚本
我正在尝试编写一个 Git预提交钩子,如果有一个标记为:focus的示例,则不会让用户提交.

使用RSpec的API(即使它是私有的,也没关系),有没有办法找到带有:焦点过滤器的例子数量?

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

解决方法

Here is一个 Overcommit pre_commit钩子,使用RSpecs私有API来查找规格:焦点过滤器:

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:nt#{files.join("nt")}"]
        end
      end
    end
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读