parse lcov file in ruby

This commit is contained in:
miguemasx 2020-03-17 00:56:35 +01:00
parent 2454e13902
commit 718cf309b6
4 changed files with 44 additions and 15 deletions

View File

@ -20,25 +20,12 @@ class CoverageReport
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 } }
lcov = LcovParse.new(File.read(report_path))
{ 'lines' => { 'covered_percent' => lcov.covered_percent, 'minumum_percent' => data[:min] } }
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

View File

@ -7,6 +7,7 @@ require_relative './report_adapter'
require_relative './github_check_run_service'
require_relative './github_client'
require_relative './coverage_report'
require_relative './lcov_parse'
def read_json(path)
JSON.parse(File.read(path))

40
lib/lcov_parse.rb Normal file
View File

@ -0,0 +1,40 @@
class LcovParse
def initialize(lcov_content)
@lcov = lcov_content
end
def to_json
@to_json ||= @lcov.split("end_of_record").map do |file_item|
file_item.split("\n").reduce({}) do |memo, item|
type, value = item.split(":")
case type
when "DA", "FNF", "FNH", "LF", "LH", "FN", "FNDA", "BRDA", "BRF", "BRH"
memo[type] = [] unless memo[type]
memo[type].push(value&.strip)
when "TN", "SF"
memo[type] = value
end
memo
end
end
end
def lines
@lines ||=
to_json.map{|it| it["DA"] }
.flatten.compact
.map{|it| it.split(",").last }
end
def total_lines
lines.count
end
def covered_lines
lines.select{|it| it == "1" }.count
end
def covered_percent
((covered_lines / total_lines.to_f) * 100).round(2)
end
end

View File

@ -7,3 +7,4 @@ require './lib/report_adapter'
require './lib/github_check_run_service'
require './lib/github_client'
require './lib/coverage_report'
require './lib/lcov_parse'