File Coverage

blib/lib/YATT/Lite/Test/TestFiles.pm
Criterion Covered Total %
statement 44 54 81.4
branch 13 24 54.1
condition 5 6 83.3
subroutine 8 10 80.0
pod 0 5 0.0
total 70 99 70.7


line stmt bran cond sub pod time code
1             package
2             YATT::Lite::Test::TestFiles;
3             sub MY () {__PACKAGE__}
4 3     3   4883 use strict;
  3         49  
  3         164  
5 3     3   17 use warnings qw(FATAL all NONFATAL misc);
  3         6  
  3         170  
6 3     3   22 use base qw/File::Spec/;
  3         494  
  3         294  
7 3     3   17 use fields qw(basedir Dict List cf_auto_clean cf_quiet);
  3         4  
  3         25  
8              
9             sub new {
10 7     7 0 4187 my MY $self = fields::new(shift);
11 7         649 $self->{basedir} = shift;
12 7         33 while (my ($name, $value) = splice @_, 0, 2) {
13 8         41 $self->{"cf_$name"} = $value;
14             }
15 7         23 $self->mkdir();
16 7         19 $self
17             }
18              
19             sub mkdir {
20 17     17 0 1387 (my MY $self, my ($fn)) = @_;
21 17         34 my $real = $_[2] = $self->catdir(grep {defined} $self->{basedir}, $fn);
  34         178  
22 17 100       414 unless (-d $real) {
23 16 50       1002 CORE::mkdir($real) or die "Can't mkdir $real: $!";
24 16 100       228 print "# o mkdir $real\n" unless $self->{cf_quiet};
25 16         27 push @{$self->{List}}, [rmdir => $real];
  16         67  
26             } else {
27 1 50       5 print "# o exists $real\n" unless $self->{cf_quiet};
28             }
29 17         77 $fn;
30             }
31              
32             sub add {
33 31     31 0 8355 (my MY $self, my ($fn, $content)) = @_;
34 31         102 my $real = "$self->{basedir}/$fn";
35 31         44 my $old_mtime;
36 31   100     843 while (-e $real and ($old_mtime = (stat($real))[9]) == time) {
37             # wait until mtime is changed.
38 3         3001007 sleep 1;
39             }
40 31 50       2674 open my $fh, '>', $real or die "Can't create $real: $!";
41 31         232 print $fh $content;
42 31         1636 close $fh;
43 31 50 66     467 if (defined $old_mtime and (stat($real))[9] <= $old_mtime) {
44 0         0 sleep 1;
45 0 0       0 utime(undef, undef, $real) || warn "Can't touch $real: $!";
46             }
47 31 100       179 unless ($self->{Dict}{$real}++) {
48 27         36 push @{$self->{List}}, [unlink => $real];
  27         112  
49             }
50 31 100       207 print "# o written: $real\n" unless $self->{cf_quiet};
51 31         182 $self
52             }
53              
54 0 0   0 0 0 sub rmdir {my ($self, $fn) = @_; CORE::rmdir($fn) or warn "# rmdir $fn: $!"};
  0         0  
55 0 0   0 0 0 sub unlink {my ($self, $fn) = @_; CORE::unlink($fn) or warn "# rm $fn: $!"};
  0         0  
56              
57             sub DESTROY {
58 7     7   1078 my MY $self = shift;
59 7 50       110 return unless $self->{cf_auto_clean};
60 0           foreach my $item (reverse @{$self->{List}}) {
  0            
61 0           my ($method, $arg) = @$item;
62             # print "# $method $arg\n";
63 0           $self->$method($arg);
64             }
65             }
66              
67             1;