From 432acf66ba6d43d5f2df886ab3cb584017bfff2f Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Fri, 11 Jun 2021 16:50:45 +0200 Subject: [PATCH 1/3] add get coverage per line from phpunit coverage report in text format --- action.yml | 2 +- lib/coverage_report.rb | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index b8c4ad1..e2f0dd9 100644 --- a/action.yml +++ b/action.yml @@ -6,7 +6,7 @@ branding: color: "green" inputs: type: - description: "lcov | simplecov" + description: "lcov | simplecov | phpunit" required: true default: "lcov" token: diff --git a/lib/coverage_report.rb b/lib/coverage_report.rb index 8752daa..0bb4b17 100644 --- a/lib/coverage_report.rb +++ b/lib/coverage_report.rb @@ -7,6 +7,8 @@ def generate(type, report_path, data) simplecov(report_path, data) elsif type == 'lcov' lcov(report_path, data) + elsif type == 'phpunit' + phpunit(report_path, data) else raise 'InvalidCoverageReportType' end @@ -25,6 +27,12 @@ def lcov(report_path, data) { 'lines' => { 'covered_percent' => lcov_covered_percent(lcov_result), 'minumum_percent' => minumum_percent } } end + def phpunit(report_path, data) + phpunit_result = execute_phpunit_parse(report_path) + minumum_percent = data[:min] + { 'lines' => { 'covered_percent' => phpunit_covered_percent(phpunit_result), 'minumum_percent' => minumum_percent} } + end + private def lcov_covered_percent(lcov_result) @@ -39,6 +47,22 @@ def execute_lcov_parse(report_path) JSON.parse(`node #{bin_path}/lcov-parse.js #{report_path}`) end + + def phpunit_covered_percent(phpunit_result) + # example for + # phpunit --coverage-text + # Summary: + # Classes: 10.14% (14/138) + # Methods: 16.67% (107/642) + # Lines: 13.95% (1059/7591) + + /Lines: * ([0-9\.]*)%/.match(phpunit_result)[1] + end + + def execute_phpunit_parse(report_path) + File.read(report_path) + end + def read_json(path) JSON.parse(File.read(path)) end From ef143a2e3948826be33c2254fc6003dd523b3e5d Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Fri, 11 Jun 2021 17:08:04 +0200 Subject: [PATCH 2/3] add debug --- lib/coverage_report.rb | 2 ++ lib/report_adapter.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/coverage_report.rb b/lib/coverage_report.rb index 0bb4b17..1c237c7 100644 --- a/lib/coverage_report.rb +++ b/lib/coverage_report.rb @@ -29,6 +29,8 @@ def lcov(report_path, data) def phpunit(report_path, data) phpunit_result = execute_phpunit_parse(report_path) + puts phpunit_result + puts phpunit_covered_percent(phpunit_result) minumum_percent = data[:min] { 'lines' => { 'covered_percent' => phpunit_covered_percent(phpunit_result), 'minumum_percent' => minumum_percent} } end diff --git a/lib/report_adapter.rb b/lib/report_adapter.rb index c82843d..83a9a3c 100644 --- a/lib/report_adapter.rb +++ b/lib/report_adapter.rb @@ -7,6 +7,8 @@ class << self ANNOTATION_LEVEL = { notice: 'notice', warning: 'warning', failure: 'failure' }.freeze def conslusion(report) + puts report + puts lines_covered_percent(report) lines_covered_percent(report) >= lines_minimum_percent(report).to_f ? CONCLUSION_TYPES[:success] : CONCLUSION_TYPES[:failure] end From 4681b01d631afdc50aa793444beb5a8628ddd170 Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Fri, 11 Jun 2021 17:13:18 +0200 Subject: [PATCH 3/3] remove debug, fix it (hopefully) --- lib/coverage_report.rb | 4 +--- lib/report_adapter.rb | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/coverage_report.rb b/lib/coverage_report.rb index 1c237c7..73ceae4 100644 --- a/lib/coverage_report.rb +++ b/lib/coverage_report.rb @@ -29,8 +29,6 @@ def lcov(report_path, data) def phpunit(report_path, data) phpunit_result = execute_phpunit_parse(report_path) - puts phpunit_result - puts phpunit_covered_percent(phpunit_result) minumum_percent = data[:min] { 'lines' => { 'covered_percent' => phpunit_covered_percent(phpunit_result), 'minumum_percent' => minumum_percent} } end @@ -58,7 +56,7 @@ def phpunit_covered_percent(phpunit_result) # Methods: 16.67% (107/642) # Lines: 13.95% (1059/7591) - /Lines: * ([0-9\.]*)%/.match(phpunit_result)[1] + /Lines: * ([0-9\.]*)%/.match(phpunit_result)[1].to_f end def execute_phpunit_parse(report_path) diff --git a/lib/report_adapter.rb b/lib/report_adapter.rb index 83a9a3c..c82843d 100644 --- a/lib/report_adapter.rb +++ b/lib/report_adapter.rb @@ -7,8 +7,6 @@ class << self ANNOTATION_LEVEL = { notice: 'notice', warning: 'warning', failure: 'failure' }.freeze def conslusion(report) - puts report - puts lines_covered_percent(report) lines_covered_percent(report) >= lines_minimum_percent(report).to_f ? CONCLUSION_TYPES[:success] : CONCLUSION_TYPES[:failure] end