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   3593 use strict;
  3         6  
  3         98  
5 3     3   19 use warnings qw(FATAL all NONFATAL misc);
  3         7  
  3         139  
6 3     3   18 use base qw/File::Spec/;
  3         7  
  3         257  
7 3     3   18 use fields qw(basedir Dict List cf_auto_clean cf_quiet);
  3         6  
  3         22  
8              
9             sub new {
10 7     7 0 4248 my MY $self = fields::new(shift);
11 7         611 $self->{basedir} = shift;
12 7         34 while (my ($name, $value) = splice @_, 0, 2) {
13 8         35 $self->{"cf_$name"} = $value;
14             }
15 7         24 $self->mkdir();
16 7         22 $self
17             }
18              
19             sub mkdir {
20 17     17 0 100 (my MY $self, my ($fn)) = @_;
21 17         46 my $real = $_[2] = $self->catdir(grep {defined} $self->{basedir}, $fn);
  34         206  
22 17 100       398 unless (-d $real) {
23 16 50       830 CORE::mkdir($real) or die "Can't mkdir $real: $!";
24 16 100       258 print "# o mkdir $real\n" unless $self->{cf_quiet};
25 16         37 push @{$self->{List}}, [rmdir => $real];
  16         85  
26             } else {
27 1 50       6 print "# o exists $real\n" unless $self->{cf_quiet};
28             }
29 17         71 $fn;
30             }
31              
32             sub add {
33 31     31 0 9054 (my MY $self, my ($fn, $content)) = @_;
34 31         110 my $real = "$self->{basedir}/$fn";
35 31         57 my $old_mtime;
36 31   100     775 while (-e $real and ($old_mtime = (stat($real))[9]) == time) {
37             # wait until mtime is changed.
38 3         3000760 sleep 1;
39             }
40 31 50       2312 open my $fh, '>', $real or die "Can't create $real: $!";
41 31         297 print $fh $content;
42 31         1684 close $fh;
43 31 50 66     258 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       248 unless ($self->{Dict}{$real}++) {
48 27         52 push @{$self->{List}}, [unlink => $real];
  27         126  
49             }
50 31 100       405 print "# o written: $real\n" unless $self->{cf_quiet};
51 31         227 $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   313 my MY $self = shift;
59 7 50       91 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;