Module: Devformance::TestFramework::CoverageHelper

Defined in:
lib/devformance/test_framework/coverage_helper.rb

Class Method Summary collapse

Class Method Details

.calculate_coverage_pct(resultset, file_path) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/devformance/test_framework/coverage_helper.rb', line 61

def self.calculate_coverage_pct(resultset, file_path)
  return nil unless resultset

  rel_path = file_path.sub(Rails.root.to_s + "/", "")
  all_lines = resultset.values.flat_map do |r|
    r.dig("coverage", rel_path)&.compact || []
  end.compact

  return nil if all_lines.empty?

  covered = all_lines.count { |v| v.to_i > 0 }
  (covered.to_f / all_lines.size * 100).round(1)
end

.parse_coverage_jsonObject



52
53
54
55
56
57
58
59
# File 'lib/devformance/test_framework/coverage_helper.rb', line 52

def self.parse_coverage_json
  resultset_path = Rails.root.join(Devformance.configuration.coverage_dir || "coverage", ".resultset.json")
  return nil unless File.exist?(resultset_path)

  JSON.parse(File.read(resultset_path))
rescue JSON::ParserError
  nil
end

.run_minitest_with_coverage(file_path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/devformance/test_framework/coverage_helper.rb', line 40

def self.run_minitest_with_coverage(file_path)
  require "simplecov"
  SimpleCov.start do
    add_filter "/vendor/"
    add_filter "/test/"
  end

  require "rails/test_help"
  require "minitest/autorun"
  Minitest.run
end

.run_rspec_with_coverage(file_path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/devformance/test_framework/coverage_helper.rb', line 28

def self.run_rspec_with_coverage(file_path)
  require "simplecov"
  SimpleCov.start do
    add_filter "/vendor/"
    add_filter "/spec/"
  end

  require "rspec/core"
  exit_code = RSpec::Core::Runner.run([ file_path, "--format", "progress" ])
  { exit_code: exit_code, coverage: SimpleCov.result }
end

.run_with_coverage(file_path, framework_name) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/devformance/test_framework/coverage_helper.rb', line 18

def self.run_with_coverage(file_path, framework_name)
  if framework_name == "RSpec"
    run_rspec_with_coverage(file_path)
  elsif framework_name == "Minitest"
    run_minitest_with_coverage(file_path)
  else
    raise "Unsupported framework: #{framework_name}"
  end
end

.setup_simplecovObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/devformance/test_framework/coverage_helper.rb', line 4

def self.setup_simplecov
  return unless defined?(SimpleCov)

  SimpleCov.start do
    add_filter "/vendor/"
    add_filter "/spec/"
    add_filter "/test/"
    add_filter "/config/"
    add_filter "/db/"

    minimum_coverage Devformance.configuration.coverage_minimum_coverage || 80
  end
end