File Coverage

blib/lib/Devel/Mutator/Command/Mutate.pm
Criterion Covered Total %
statement 63 63 100.0
branch 10 16 62.5
condition 7 12 58.3
subroutine 12 12 100.0
pod 2 2 100.0
total 94 105 89.5


line stmt bran cond sub pod time code
1             package Devel::Mutator::Command::Mutate;
2              
3 1     1   29000 use strict;
  1         1  
  1         25  
4 1     1   3 use warnings;
  1         2  
  1         22  
5              
6 1     1   3 use File::Find ();
  1         2  
  1         8  
7 1     1   451 use File::Slurp ();
  1         8539  
  1         21  
8 1     1   6 use File::Path ();
  1         1  
  1         12  
9 1     1   3 use File::Basename ();
  1         1  
  1         9  
10 1     1   3 use File::Spec;
  1         0  
  1         12  
11 1     1   295 use Devel::Mutator::Generator;
  1         2  
  1         411  
12              
13             sub new {
14 2     2 1 4463 my $class = shift;
15 2         7 my (%params) = @_;
16              
17 2         5 my $self = {};
18 2         4 bless $self, $class;
19              
20 2   100     15 $self->{recursive} = $params{recursive} || 0;
21 2   50     12 $self->{verbose} = $params{verbose} || 0;
22 2   50     9 $self->{root} = $params{root} || '.';
23 2   33     40 $self->{generator} = $params{generator} || Devel::Mutator::Generator->new;
24              
25 2         6 return $self;
26             }
27              
28             sub run {
29 2     2 1 12 my $self = shift;
30 2         4 my (@files) = @_;
31              
32 2         3 my $mutated = 0;
33 2         3 my $mutants = 0;
34 2         3 foreach my $file (@files) {
35 2 100 66     25 if (-d $file && $self->{recursive}) {
    50          
36 1         26 my @subfiles;
37              
38             File::Find::find(
39 2 100   2   186 sub { push @subfiles, $File::Find::name if /\.p(?:m|l)$/; },
40 1         49 $file);
41              
42 1         7 foreach my $subfile (@subfiles) {
43 1         5 $mutants += $self->_mutate_file($subfile);
44 1         3 $mutated++;
45             }
46             }
47             elsif (-f $file) {
48 1         5 $mutants += $self->_mutate_file($file);
49 1         3 $mutated++;
50             }
51             }
52              
53 2         56 print "Mutated files: $mutated, mutants: $mutants\n";
54              
55 2         6 return $self;
56             }
57              
58             sub _mutate_file {
59 2     2   3 my $self = shift;
60 2         20 my ($file) = @_;
61              
62 2 50       7 print "Reading $file ... \n" if $self->{verbose};
63 2         9 my $content = File::Slurp::read_file($file);
64              
65 2 50       131 print "Generating mutants ... " if $self->{verbose};
66 2         10 my @mutants = $self->{generator}->generate($content);
67              
68 2 50       105 print scalar(@mutants), "\n" if $self->{verbose};
69              
70 2 50       5 print "Saving mutants ... " if $self->{verbose};
71 2         3 foreach my $mutant (@mutants) {
72 2         27 my $new_path =
73             File::Spec->catfile($self->{root}, 'mutants', $mutant->{id}, $file);
74              
75 2         712 File::Path::make_path(File::Basename::dirname($new_path));
76              
77 2         10 File::Slurp::write_file($new_path, $mutant->{content});
78             }
79 2 50       304 print "ok\n" if $self->{verbose};
80              
81 2         5 return scalar @mutants;
82             }
83              
84             1;
85             __END__