line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test2::Harness::Util; |
2
|
7
|
|
|
7
|
|
141792
|
use strict; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
172
|
|
3
|
7
|
|
|
7
|
|
31
|
use warnings; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
239
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.001007'; |
6
|
|
|
|
|
|
|
|
7
|
7
|
|
|
7
|
|
38
|
use Carp qw/confess/; |
|
7
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
280
|
|
8
|
7
|
|
|
7
|
|
43
|
use Importer Importer => 'import'; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
43
|
|
9
|
|
|
|
|
|
|
|
10
|
7
|
|
|
7
|
|
308
|
use Test2::Util qw/try_sig_mask do_rename/; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
3878
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw{ |
13
|
|
|
|
|
|
|
close_file |
14
|
|
|
|
|
|
|
fqmod |
15
|
|
|
|
|
|
|
local_env |
16
|
|
|
|
|
|
|
maybe_open_file |
17
|
|
|
|
|
|
|
maybe_read_file |
18
|
|
|
|
|
|
|
open_file |
19
|
|
|
|
|
|
|
read_file |
20
|
|
|
|
|
|
|
write_file |
21
|
|
|
|
|
|
|
write_file_atomic |
22
|
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub fqmod { |
25
|
4
|
|
|
4
|
0
|
8
|
my ($prefix, $input) = @_; |
26
|
4
|
100
|
|
|
|
23
|
return $1 if $input =~ m/^\+(.*)$/; |
27
|
2
|
|
|
|
|
8
|
return "$prefix\::$input"; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub maybe_read_file { |
31
|
2
|
|
|
2
|
0
|
347
|
my ($file) = @_; |
32
|
2
|
100
|
|
|
|
23
|
return undef unless -f $file; |
33
|
1
|
|
|
|
|
3
|
return read_file($file); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub read_file { |
37
|
8
|
|
|
8
|
0
|
826
|
my ($file) = @_; |
38
|
|
|
|
|
|
|
|
39
|
8
|
|
|
|
|
16
|
my $fh = open_file($file); |
40
|
6
|
|
|
|
|
18
|
local $/; |
41
|
6
|
|
|
|
|
91
|
my $out = <$fh>; |
42
|
6
|
|
|
|
|
20
|
close_file($fh, $file); |
43
|
|
|
|
|
|
|
|
44
|
6
|
|
|
|
|
34
|
return $out; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub write_file { |
48
|
2
|
|
|
2
|
0
|
5
|
my ($file, @content) = @_; |
49
|
|
|
|
|
|
|
|
50
|
2
|
|
|
|
|
4
|
my $fh = open_file($file, '>'); |
51
|
2
|
|
|
|
|
22
|
print $fh @content; |
52
|
2
|
|
|
|
|
6
|
close_file($fh, $file); |
53
|
|
|
|
|
|
|
|
54
|
2
|
|
|
|
|
9
|
return @content; |
55
|
|
|
|
|
|
|
}; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub open_file { |
58
|
30
|
|
|
30
|
0
|
358
|
my ($file, $mode) = @_; |
59
|
30
|
|
100
|
|
|
98
|
$mode ||= '<'; |
60
|
|
|
|
|
|
|
|
61
|
30
|
100
|
|
|
|
1183
|
open(my $fh, $mode, $file) or confess "Could not open file '$file' ($mode): $!"; |
62
|
27
|
|
|
|
|
109
|
return $fh; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub maybe_open_file { |
66
|
10
|
|
|
10
|
0
|
622
|
my ($file, $mode) = @_; |
67
|
10
|
100
|
|
|
|
115
|
return undef unless -f $file; |
68
|
7
|
|
|
|
|
17
|
return open_file($file, $mode); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub close_file { |
72
|
10
|
|
|
10
|
0
|
230
|
my ($fh, $name) = @_; |
73
|
10
|
100
|
|
|
|
98
|
return if close($fh); |
74
|
1
|
50
|
|
|
|
113
|
confess "Could not close file: $!" unless $name; |
75
|
0
|
|
|
|
|
0
|
confess "Could not close file '$name': $!"; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub write_file_atomic { |
79
|
1
|
|
|
1
|
0
|
444
|
my ($file, @content) = @_; |
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
3
|
my $pend = "$file.pend"; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my ($ok, $err) = try_sig_mask { |
84
|
1
|
|
|
1
|
|
37
|
write_file($pend, @content); |
85
|
1
|
|
|
|
|
5
|
my ($ren_ok, $ren_err) = do_rename($pend, $file); |
86
|
1
|
50
|
|
|
|
39
|
die $ren_err unless $ren_ok; |
87
|
1
|
|
|
|
|
6
|
}; |
88
|
|
|
|
|
|
|
|
89
|
1
|
50
|
|
|
|
23
|
die $err unless $ok; |
90
|
|
|
|
|
|
|
|
91
|
1
|
|
|
|
|
3
|
return @content; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub local_env { |
95
|
1
|
|
|
1
|
0
|
2
|
my ($env, $sub) = @_; |
96
|
|
|
|
|
|
|
|
97
|
1
|
|
|
|
|
2
|
my $old; |
98
|
1
|
|
|
|
|
4
|
for my $key (keys %$env) { |
99
|
5
|
50
|
|
|
|
12
|
$old->{$key} = $ENV{$key} if exists $ENV{$key}; |
100
|
5
|
|
|
|
|
11
|
$ENV{$key} = $env->{$key}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
1
|
|
|
|
|
2
|
my $ok = eval { $sub->(); 1 }; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1401
|
|
104
|
1
|
|
|
|
|
2
|
my $err = $@; |
105
|
|
|
|
|
|
|
|
106
|
1
|
|
|
|
|
4
|
for my $key (keys %$env) { |
107
|
|
|
|
|
|
|
# If something set an env var inside than we do not want to squash it. |
108
|
5
|
100
|
75
|
|
|
18
|
next if !defined($ENV{$key}) xor !defined($env->{$key}); |
109
|
3
|
100
|
33
|
|
|
13
|
next if defined($ENV{$key}) && defined($env->{$key}) && $ENV{$key} ne $env->{$key}; |
|
|
|
66
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
2
|
50
|
|
|
|
7
|
exists $old->{$key} ? $ENV{$key} = $old->{$key} : delete $ENV{$key}; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
1
|
50
|
|
|
|
3
|
die $err unless $ok; |
115
|
|
|
|
|
|
|
|
116
|
1
|
|
|
|
|
3
|
return $ok; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
__END__ |