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