lcov-inject.pl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/perl
  2. #
  3. # lcov-inject.pl
  4. #
  5. # Copyright © 2014 Guillem Jover <guillem@debian.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. use strict;
  21. use warnings;
  22. use Cwd;
  23. use Devel::Cover::DB;
  24. my $dir = 'scripts';
  25. my $cwd = cwd();
  26. chdir $dir or die "cannot switch to $dir\n";
  27. my $db = Devel::Cover::DB->new(db => 'cover_db');
  28. $db = $db->merge_runs();
  29. $db->calculate_summary(map { $_ => 1 } $db->collected());
  30. chdir $cwd or die "cannot switch to $cwd\n";
  31. my $s = $db->{summary}{Total};
  32. my $tmpl = sprintf '
  33. <td class="coverFile"><a href="%s">%s</a></td>
  34. <td class="coverBar" align="center">
  35. <table border=0 cellspacing=0 cellpadding=1>
  36. <tr><td class="coverBarOutline">%s</td></tr>
  37. </table>
  38. </td>
  39. %s
  40. %s
  41. %s
  42. </tr>
  43. <tr>
  44. ', "$dir/coverage.html", $dir, bar_html($s->{total}{percentage}),
  45. box_html($s->{total}), box_html($s->{subroutine}), box_html($s->{branch});
  46. while (<>) {
  47. s/^(.*<td .*href="src\/index\.html">.*)$/$tmpl$1/;
  48. print;
  49. }
  50. sub bar_image {
  51. my ($num) = @_;
  52. return 'emerald.png' if $num >= 90;
  53. return 'ruby.png' if $num < 75;
  54. return 'amber.png';
  55. }
  56. sub bar_html {
  57. my ($num) = @_;
  58. my $html = sprintf '<img src="%s" width=%.0f height=10 alt="%.1f">',
  59. bar_image($num), $num, $num;
  60. if ($num < 100) {
  61. $html .= sprintf '<img src="snow.png" width=%.0f height=10 alt="%.1f">',
  62. 100 - $num, $num;
  63. }
  64. return $html;
  65. }
  66. sub box_rating {
  67. my ($num) = @_;
  68. return 'Hi' if $num >= 90;
  69. return 'Lo' if $num < 75;
  70. return 'Med';
  71. }
  72. sub box_html {
  73. my ($stats) = @_;
  74. return sprintf '<td class="coverPer%s">%.1f&nbsp;%%</td>' . "\n" .
  75. '<td class="coverNum%s">%d / %d</td>',
  76. box_rating($stats->{percentage}), $stats->{percentage},
  77. box_rating($stats->{percentage}), $stats->{covered}, $stats->{total};
  78. }
  79. 1;