This repository has been archived on 2023-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
coverage-check-action/lib/coverage_report.rb
Miguel Savignano d734a59c05
Lcov report (#2)
* execute node bin

* calculate percentage for lcov file

* install node in docker image

* use relative path to bin folder

* update readme
2019-11-03 02:05:38 +01:00

47 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class CoverageReport
class << self
def generate(type, report_path, data)
if type == 'simplecov'
simplecov(report_path, data)
elsif type == 'lcov'
lcov(report_path, data)
else
raise 'InvalidCoverageReportType'
end
end
def simplecov(report_path, data)
report = read_json(report_path)
minumum_percent = data[:min]
covered_percent = report.dig('result', 'covered_percent')
{ 'lines' => { 'covered_percent' => covered_percent, 'minumum_percent' => minumum_percent } }
end
def lcov(report_path, data)
lcov_result = execute_lcov_parse(report_path)
minumum_percent = data[:min]
{ 'lines' => { 'covered_percent' => lcov_covered_percent(lcov_result), 'minumum_percent' => minumum_percent } }
end
private
def lcov_covered_percent(lcov_result)
lines = lcov_result.map { |r| r['lines']['details'] }.flatten
total_lines = lines.count.to_f.round(2)
covered_lines = lines.select { |r| r['hit'] >= 1 }.count.to_f
((covered_lines / total_lines) * 100).round(2)
end
def execute_lcov_parse(report_path)
bin_path = "#{File.dirname(__FILE__)}/../bin"
JSON.parse(`node #{bin_path}/lcov-parse.js #{report_path}`)
end
def read_json(path)
JSON.parse(File.read(path))
end
end
end