From 432acf66ba6d43d5f2df886ab3cb584017bfff2f Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Fri, 11 Jun 2021 16:50:45 +0200 Subject: [PATCH] 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 @@ class CoverageReport 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 @@ class CoverageReport { '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 @@ class CoverageReport 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