From 718cf309b6fdd18281c6bb6fc28789647496f76c Mon Sep 17 00:00:00 2001 From: miguemasx Date: Tue, 17 Mar 2020 00:56:35 +0100 Subject: [PATCH] parse lcov file in ruby --- lib/coverage_report.rb | 17 ++--------------- lib/index.rb | 1 + lib/lcov_parse.rb | 40 ++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 1 + 4 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 lib/lcov_parse.rb diff --git a/lib/coverage_report.rb b/lib/coverage_report.rb index 8752daa..1fe17b3 100644 --- a/lib/coverage_report.rb +++ b/lib/coverage_report.rb @@ -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 diff --git a/lib/index.rb b/lib/index.rb index e888441..e9df26f 100644 --- a/lib/index.rb +++ b/lib/index.rb @@ -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)) diff --git a/lib/lcov_parse.rb b/lib/lcov_parse.rb new file mode 100644 index 0000000..bbc66cb --- /dev/null +++ b/lib/lcov_parse.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index be4f8ff..3b2a6a8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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'