Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 464 lines (371 sloc) 13.628 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
#!/usr/bin/perl

# Copyright 2001-2011, Paul Johnson (pjcj@cpan.org)

# This software is free. It is licensed under the same terms as Perl itself.

# The latest version of this software should be available from my homepage:
# http://www.pjcj.net

require 5.6.1;

use strict;
use warnings;

our $VERSION = "0.77";

use Devel::Cover::DB 0.77;

use Config;
use File::Spec;
use File::Find ();
use File::Path;
use FindBin '$Bin';
use Getopt::Long;
use Pod::Usage;

use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Sortkeys = 1;

my $Options =
{
    add_uncoverable_point => [],
    delete_uncoverable_point => [],
    annotation => [],
    coverage => [],
    delete => 0,
    gcov => $Config{gccversion},
    ignore => [],
    ignore_re => [],
    make => $Config{make},
    select => [],
    select_re => [],
    report => "",
    summary => 1,
    uncoverable_file => [".uncoverable", glob("~/.uncoverable")],
};

sub get_options
{
    Getopt::Long::Configure("pass_through");
    die "Bad option" unless
        GetOptions($Options, # Store the options in the Options hash.
                   "write:s" => sub
{
                       @$Options{qw( write summary )} = ($_[1], 0)
                   },
                   qw(
add_uncoverable_point=s
annotation=s
clean_uncoverable_points!
coverage=s
delete!
delete_uncoverable_point=s
dump_db!
gcov!
help|h!
ignore_re=s
ignore=s
info|i!
make=s
outputdir=s
report=s
select_re=s
select=s
silent!
summary!
test!
uncoverable_file=s
version|v!
));
    Getopt::Long::Configure("nopass_through");
    $Options->{report} ||= "html" unless exists $Options->{write};
}

sub delete_db
{
    for my $del (@_)
    {
        my $db = Devel::Cover::DB->new(db => $del);
        unless ($db->is_valid)
        {
            print "Devel::Cover: $del is an invalid database - ignoring\n"
                unless $Options->{silent};
            next;
        }
        print "Deleting database $del\n" if $db->exists && !$Options->{silent};
        $db->delete;
        rmtree($del);
    }
}

# Decide whether to run ./Build test or make test
sub test_command { -e "Build" ? mb_test_command() : mm_test_command() }

# Compiler arguments necessary to do a coverage run
sub gcov_args() { "-fprofile-arcs -ftest-coverage" }

# Test command for MakeMaker
sub mm_test_command
{
    my $test = "$Options->{make} test";

    if ($Options->{gcov})
    {
        my $o = gcov_args();
        $test .= qq{ "OPTIMIZE=-O0 $o" "OTHERLDFLAGS=$o"};
    }

    $test
}

# Test command for Module::Build
sub mb_test_command
{
    my $test = './Build test';

    if ($Options->{gcov})
    {
        my $o = gcov_args();
        $test .= qq{ "--extra_compiler_flags=-O0 $o" "--extra_linker_flags=$o"};
    }

    $test
}

sub main
{
    if (!$ENV{DEVEL_COVER_SELF} && $INC{"Devel/Cover.pm"})
    {
        my $err = "$0 shouldn't be run with coverage turned on.\n";
        eval
        {
            require POSIX;
            print STDERR $err;
            POSIX::_exit(1);
        };
        die $err;
    }

    get_options;

    $Devel::Cover::Silent = 1 if $Options->{silent};

    my $format = "Devel::Cover::Report::\u$Options->{report}";
    if (length $Options->{report})
    {
        eval ("use $format");
        if ($@)
        {
            print "Error: $Options->{report} ",
                  "is not a recognised output format\n\n$@";
            exit 1;
        }
    }

    $format->get_options($Options) if $format->can("get_options");

    $Options->{annotations} = [];
    for my $a (@{$Options->{annotation}})
    {
        my $annotation = "Devel::Cover::Annotation::\u$a";
        eval ("use $annotation");
        if ($@)
        {
            print "Error: $a is not a recognised annotation\n\n$@";
            exit 1;
        }
        my $ann = $annotation->new;
        $ann->get_options($Options) if $ann->can("get_options");
        push @{$Options->{annotations}}, $ann;
    }

    print "$0 version $VERSION\n" and exit 0 if $Options->{version};
    pod2usage(-exitval => 0, -verbose => 1) if $Options->{help};
    pod2usage(-exitval => 0, -verbose => 2) if $Options->{info};

    my $dbname = File::Spec->rel2abs(@ARGV ? shift @ARGV : "cover_db");
    die "Can't open database $dbname\n"
        if !$Options->{delete} && !$Options->{test} && !-d $dbname;

    $Options->{outputdir} = $dbname unless exists $Options->{outputdir};
    my $od = File::Spec->rel2abs($Options->{outputdir});
    $Options->{outputdir} = $od if defined $od;
    mkpath($Options->{outputdir}) unless -d $Options->{outputdir};

    if ($Options->{delete})
    {
        delete_db($dbname, @ARGV);
        exit 0
    }

    if ($Options->{test})
    {
        # TODO - make this a little robust
        # system "$^X Makefile.PL" unless -e "Makefile";
        delete_db($dbname, @ARGV);
        local $ENV{ -d "t" ? "HARNESS_PERL_SWITCHES" : "PERL5OPT" } =
            "$ENV{DEVEL_COVER_TEST_OPTS} -MDevel::Cover=-db,$dbname";

        my $test = test_command();

        # touch the XS, C and H files so they rebuild
        if ($Options->{gcov})
        {
            my $t = $] > 5.7 ? undef : time;
            my $xs = sub { utime $t, $t, $_ if /\.(xs|c|h)$/ };
            File::Find::find({ wanted => $xs, no_chdir => 0 }, ".");
        }
        print STDERR "cover: running $test\n";
        system $test;
        $Options->{report} ||= "html";
    }

    if ($Options->{gcov})
    {
        my $gc = sub
{
            return unless /\.(xs|c|h)$/;

            my ($name) = /([^\/]+$)/;

            # Don't bother running gcov if there's no index files.
            # Otherwise it's noisy.
            my $graph_file = $_;
            $graph_file =~ s{\.\w+$}{.gcno};
            return unless -e $graph_file;

            my $c = "gcov -abc -o $File::Find::dir $name";
            print STDERR "cover: running $c\n";
            system $c;
        };
        File::Find::find({ wanted => $gc, no_chdir => 1 }, ".");
        my @gc;
        my $gp = sub
{
            return unless /\.gcov$/;
            my $xs = $_;
            return if $xs =~ s/\.(c|h)\.gcov$/.xs.gcov/ && -e $xs;
            s/^\.\///;
            push @gc, $_;
        };
        File::Find::find({ wanted => $gp, no_chdir => 1 }, ".");
        if (@gc)
        {
            my $c = "gcov2perl -db $dbname @gc";
            print STDERR "cover: running $c\n";
            system $c;
        }
    }

    print "Reading database from $dbname\n" unless $Options->{silent};
    my $db = Devel::Cover::DB->new
    (
        db => $dbname,
        uncoverable_file => $Options->{uncoverable_file},
    );
    $db = $db->merge_runs;

    $db->add_uncoverable ($Options->{add_uncoverable_point} );
    $db->delete_uncoverable ($Options->{delete_uncoverable_point});
    $db->clean_uncoverable if $Options->{clean_uncoverable_points} ;
    exit 0 if @{$Options->{add_uncoverable_point}} ||
              @{$Options->{delete_uncoverable_point}} ||
              $Options->{clean_uncoverable_points};

    for my $merge (@ARGV)
    {
        print "Merging database from $merge\n" unless $Options->{silent};
        my $mdb = Devel::Cover::DB->new(db => $merge);
        $mdb = $mdb->merge_runs;
        $db->merge($mdb);
    }

    if ($Options->{dump_db})
    {
        my $d = Data::Dumper->new([$db], ["db"]);
        $d->Indent(1);
        $d->Sortkeys(1) if $] >= 5.008;
        print $d->Dump;
        my $structure = Devel::Cover::DB::Structure->new(base => $dbname);
        $structure->read_all;
        my $s = Data::Dumper->new([$structure], ["structure"]);
        $s->Indent(1);
        $s->Sortkeys(1) if $] >= 5.008;
        print $s->Dump;
        exit 0
    }

    if (exists $Options->{write})
    {
        $dbname = $Options->{write} if length $Options->{write};
        print "Writing database to $dbname\n" unless $Options->{silent};
        $db->write($dbname);
    }

    return unless $Options->{summary} || $Options->{report};

    $Options->{coverage} = [ $db->collected ] unless @{$Options->{coverage}};
    $Options->{show} = { map { $_ => 1 } @{$Options->{coverage}} };
    $Options->{show}{total} = 1 if keys %{$Options->{show}};

    $db->calculate_summary(map { $_ => 1 } @{$Options->{coverage}});

    print "\n\n" unless $Options->{silent};

    $db->print_summary(@{$Options->{coverage}}) if $Options->{summary};

    return unless length $Options->{report};

    # TODO - The sense of select and ignore should be reversed to match
    # collection.

    my %f = map { $_ => 1 } (@{$Options->{select}}
                             ? map glob, @{$Options->{select}}
                             : $db->cover->items);
    delete @f{map glob, @{$Options->{ignore}}};

    my $keep = sub
{
        my ($f) = @_;
        return 0 unless exists $db->{summary}{$_};
        for (@{$Options->{ignore_re}})
        {
            return 0 if $f =~ /$_/
        }
        for (@{$Options->{select_re}})
        {
            return 1 if $f =~ /$_/
        }
        !@{$Options->{select_re}}
    };
    @{$Options->{file}} = sort grep $keep->($_), keys %f;

    $format->report($db, $Options)
}

main

__END__

=head1 NAME

cover - report coverage statistics

=head1 SYNOPSIS

cover -help -info -version
-summary -report report_format -outputdir dir
-select filename -ignore filename
-select_re RE -ignore_re RE
-write [db] -delete -dump_db
-silent
-coverage criterion
-test -gcov
-make [make]
-add_uncoverable_point -delete_uncoverable_point
-clean_uncoverable_points -uncoverable_file
[report specific options]
coverage_database [coverage_database ...]

=head1 DESCRIPTION

Report coverage statistics in a variety of formats.

The summary option produces a short textual summary. Other reports are
available by using the report option.

The following reports are currently available:

text - detailed textual summary
html - detailed HTML reports

=head1 OPTIONS

The following command line options are supported:

-h -help - show help
-i -info - show documentation
-v -version - show version

-silent - don't print informational messages (default off)
-summary - give summary report (default on)
-report report_format - report format (default html)
-outputdir - directory for output (default db)

-select filename - only report on the file (default all)
-ignore filename - don't report on the file (default none)
-select_re RE - append to REs of files to select (default none)
-ignore_re RE - append to REs of files to ignore (default none)
-write [db] - write the merged database (default off)
-delete - drop database(s) (default off)
-dump_db - dump database(s) (for debugging) (default off)

-coverage criterion - report on criterion (default all available)

-test - drop database(s) and run make test (default off)
-gcov - run gcov to cover XS code (default on if using gcc)
-make make_prog - use the given 'make' program for 'make test'

other options specific to the report

=head1 DETAILS

Any number of coverage databases may be specified on the command line.
These databases will be merged and the reports will be based on the
merged information. If no databases are specified the default database
(cover_db) will be used.

The -write option will write out the merged database. If no name is
given for the new database, the first database read in will be
overwritten. When this option is used no reports are generated by
default.

Specify -select and -ignore options to report on specific files.
Specify -coverage options to report on specific criteria. By default
all available information on all criteria in all files will be reported.

The -test option will delete the databases and run make test to generate
new coverage data before reporting on it. The -gcov option will try to
run gcov on any XS code. This requires that you are using gcc of
course. If you are this option will be turned on by default.

=head1 EXIT STATUS

The following exit values are returned:

0 All operations were completed successfully.

>0 An error occurred.

=head1 SEE ALSO

Devel::Cover

=head1 BUGS

Did I mention that this is alpha code?

See the BUGS file.

=head1 VERSION

Version 0.77 - 15th May 2011

=head1 LICENCE

Copyright 2001-2011, Paul Johnson (pjcj@cpan.org)

This software is free. It is licensed under the same terms as Perl itself.

The latest version of this software should be available from my homepage:
http://www.pjcj.net

=cut
Something went wrong with that request. Please try again.