Module: Covet::CollectionFilter

Defined in:
lib/covet/collection_filter.rb

Overview

Responsible for filtering out files that shouldn’t be logged in the ‘run_log` during the coverage collection phase. For instance, if using the `minitest` test runner, we shouldn’t log coverage information for internal ‘minitest` methods (unless we’re using covet ON ‘minitest` itself), The same goes for `rake`, etc. This minimizes the amount of JSON we have to save in the `run_log`, and also the amount of processing we have to do on the `run_log` structure when generating the `run_list`.

Constant Summary collapse

@@gem_whitelist =
[]
@@custom_filters =
[]
@@file_whitelist =
[]
@@regexp_whitelist =
[]
@@filter_stats =
Hash.new { |h,k| h[k] = Set.new }

Class Method Summary collapse

Class Method Details

.add_custom_filter(&filter) ⇒ Object

FIXME: should take filename AND method name

Parameters:

  • filter, (Proc)

    arity = 1, takes filename



30
31
32
# File 'lib/covet/collection_filter.rb', line 30

def self.add_custom_filter(&filter)
  @@custom_filters << filter
end

.add_to_file_whitelist(fname) ⇒ Object



33
34
35
36
37
38
# File 'lib/covet/collection_filter.rb', line 33

def self.add_to_file_whitelist(fname)
  unless fname.start_with?(File::SEPARATOR)
    raise ArgumentError, "expected #{fname} to be an absolute path"
  end
  @@file_whitelist << fname
end

.add_to_regexp_whitelist(regexp) ⇒ Object



39
40
41
# File 'lib/covet/collection_filter.rb', line 39

def self.add_to_regexp_whitelist(regexp)
  @@regexp_whitelist << regexp
end

.files_filteredObject



43
44
45
# File 'lib/covet/collection_filter.rb', line 43

def self.files_filtered
  Hash[@@filter_stats.map { |k, v| [k, v.to_a] }]
end

.filter(raw_coverage_info) ⇒ Object

Returns Hash.

Returns:

  • Hash



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/covet/collection_filter.rb', line 48

def self.filter(raw_coverage_info)
  raw_coverage_info = raw_coverage_info.dup
  # NOTE: The list of activated gems isn't cached, because it could be
  # that a test method activates a gem or calls code that activates a
  # gem. In that case, we want to omit the newly activated gem from the
  # run log as well (unless it's whitelisted).
  gem_base_dirs_to_omit = Gem.loaded_specs.values.reject do |spec|
    @@gem_whitelist.include?(spec.name)
  end.map do |spec|
    spec.full_gem_path
  end

  files_to_omit = []

  # find file names to omit from the run log
  raw_coverage_info.each do |filename, _|
    if whitelisted_filename?(filename)
      @@filter_stats['whitelisted'] << filename
      next # don't omit
    end

    if filename.start_with?(ruby_stdlib_dir)
      @@filter_stats['stdlib_files'] << filename
      files_to_omit << filename
      next
    end

    omitted = gem_base_dirs_to_omit.find do |gem_base_dir|
      if filename.start_with?(gem_base_dir)
        @@filter_stats['gem_files'] << filename
        files_to_omit << filename
      end
    end
    next if omitted

    # custom filters
    @@custom_filters.find do |filter|
      if filter.call(filename)
        @@filter_stats['custom_filters'] << filename
        files_to_omit << filename
      end
    end
  end

  files_to_omit.each do |fname|
    raw_coverage_info.delete(fname)
  end

  raw_coverage_info
end

.whitelist_gem(gem_name) ⇒ Object

Parameters:

  • String|Symbol

    gem_name



24
25
26
# File 'lib/covet/collection_filter.rb', line 24

def self.whitelist_gem(gem_name)
  @@gem_whitelist << gem_name.to_s
end