line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test2::Plugin::FauxHomeDir; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
366868
|
use strict; |
|
4
|
|
|
|
|
20
|
|
|
4
|
|
|
|
|
119
|
|
4
|
4
|
|
|
4
|
|
23
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
96
|
|
5
|
4
|
|
|
4
|
|
72
|
use 5.008001; |
|
4
|
|
|
|
|
15
|
|
6
|
4
|
|
|
4
|
|
2890
|
use File::Temp qw( tempdir ); |
|
4
|
|
|
|
|
75731
|
|
|
4
|
|
|
|
|
253
|
|
7
|
4
|
|
|
4
|
|
34
|
use File::Spec; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
94
|
|
8
|
4
|
|
|
4
|
|
22
|
use File::Path qw( mkpath ); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
213
|
|
9
|
4
|
|
|
4
|
|
2412
|
use if $^O eq 'cygwin', 'File::Spec::Win32'; |
|
4
|
|
|
|
|
55
|
|
|
4
|
|
|
|
|
27
|
|
10
|
4
|
|
|
4
|
|
724
|
use Test2::API qw( test2_add_callback_post_load test2_stack test2_add_callback_exit ); |
|
4
|
|
|
|
|
66289
|
|
|
4
|
|
|
|
|
2742
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: Setup a faux home directory for tests |
13
|
|
|
|
|
|
|
our $VERSION = '0.06'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $real; |
17
|
|
|
|
|
|
|
my $faux; |
18
|
|
|
|
|
|
|
my $user; |
19
|
|
|
|
|
|
|
my @mocks; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub real_home_dir |
22
|
|
|
|
|
|
|
{ |
23
|
1
|
|
|
1
|
1
|
15195
|
$real; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub import |
27
|
|
|
|
|
|
|
{ |
28
|
3
|
|
|
3
|
|
29
|
my @notes; |
29
|
|
|
|
|
|
|
|
30
|
3
|
50
|
|
|
|
16
|
unless(defined $faux) |
31
|
|
|
|
|
|
|
{ |
32
|
3
|
50
|
|
|
|
14
|
if($^O eq 'MSWin32') |
33
|
|
|
|
|
|
|
{ |
34
|
0
|
|
|
|
|
0
|
$real = $ENV{USERPROFILE}; |
35
|
|
|
|
|
|
|
$real = File::Spec->catdir($ENV{HOMEDRIVE}, $ENV{HOMEPATH}) |
36
|
0
|
0
|
|
|
|
0
|
unless defined $real; |
37
|
0
|
|
|
|
|
0
|
$user = $ENV{USERNAME}; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
else |
40
|
|
|
|
|
|
|
{ |
41
|
3
|
|
|
|
|
11
|
$real = $ENV{HOME}; |
42
|
3
|
|
|
|
|
6
|
$user = $ENV{USER}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
3
|
50
|
|
|
|
14
|
$user = eval { getlogin } unless defined $user; |
|
3
|
|
|
|
|
2250
|
|
46
|
3
|
50
|
|
|
|
23
|
$user = eval { scalar getpwuid($>) } unless defined $user; |
|
3
|
|
|
|
|
138
|
|
47
|
3
|
50
|
|
|
|
18
|
die "unable to determine username" unless defined $user; |
48
|
|
|
|
|
|
|
|
49
|
3
|
50
|
33
|
|
|
59
|
die "unable to determine 'real' home directory" |
50
|
|
|
|
|
|
|
unless defined $real && -d $real; |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
31
|
delete $ENV{USERPROFILE}; |
53
|
3
|
|
|
|
|
11
|
delete $ENV{HOME}; |
54
|
3
|
|
|
|
|
10
|
delete $ENV{HOMEDRIVE}; |
55
|
3
|
|
|
|
|
12
|
delete $ENV{HOMEPATH}; |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
26
|
$faux = File::Spec->catdir(tempdir( CLEANUP => 1 ), 'home', $user); |
58
|
3
|
|
|
|
|
2558
|
mkpath $faux, 0, 0700; |
59
|
|
|
|
|
|
|
|
60
|
3
|
50
|
|
|
|
40
|
if($^O eq 'MSWin32') |
|
|
50
|
|
|
|
|
|
61
|
|
|
|
|
|
|
{ |
62
|
0
|
|
|
|
|
0
|
$ENV{USERPROFILE} = $faux; |
63
|
0
|
|
|
|
|
0
|
($ENV{HOMEDRIVE}, $ENV{HOMEPATH}) = File::Spec->splitpath($faux,1); |
64
|
0
|
0
|
|
|
|
0
|
if(eval { require Portable }) |
|
0
|
|
|
|
|
0
|
|
65
|
|
|
|
|
|
|
{ |
66
|
0
|
|
|
|
|
0
|
push @notes, "Portable strawberry detected"; |
67
|
0
|
0
|
|
|
|
0
|
if(eval { require File::HomeDir }) |
|
0
|
|
|
|
|
0
|
|
68
|
|
|
|
|
|
|
{ |
69
|
|
|
|
|
|
|
# annoyingly, Strawberry Portable Perl patches |
70
|
|
|
|
|
|
|
# File::HomeDir, but not things like File::Glob |
71
|
|
|
|
|
|
|
# so since there isn't a good interface to override |
72
|
|
|
|
|
|
|
# this behavior, we need to patch the patch :( |
73
|
0
|
|
|
|
|
0
|
push @notes, "Patching File::HomeDir"; |
74
|
0
|
|
|
|
|
0
|
require Test2::Mock; |
75
|
|
|
|
|
|
|
push @mocks, Test2::Mock->new( |
76
|
|
|
|
|
|
|
class => 'File::HomeDir', |
77
|
|
|
|
|
|
|
override => [ |
78
|
0
|
|
|
0
|
|
0
|
my_home => sub { $faux }, |
79
|
0
|
|
|
|
|
0
|
], |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
elsif($^O eq 'cygwin') |
85
|
|
|
|
|
|
|
{ |
86
|
0
|
|
|
|
|
0
|
$ENV{USERPROFILE} = Cygwin::posix_to_win_path($faux); |
87
|
0
|
|
|
|
|
0
|
($ENV{HOMEDRIVE}, $ENV{HOMEPATH}) = File::Spec::Win32->splitpath($ENV{USERPROFILE},1); |
88
|
0
|
|
|
|
|
0
|
$ENV{HOME} = $faux; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
else |
91
|
|
|
|
|
|
|
{ |
92
|
3
|
|
|
|
|
21
|
$ENV{HOME} = $faux; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
3
|
|
|
|
|
18
|
push @notes, "Test2::Plugin::FauxHomeDir using faux home dir $faux"; |
96
|
3
|
|
|
|
|
9
|
push @notes, "Test2::Plugin::FauxHomeDir real home dir is $real"; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
test2_add_callback_post_load(sub { |
99
|
3
|
|
|
3
|
|
8254
|
test2_stack()->top; |
100
|
3
|
|
|
|
|
5323
|
my($hub) = test2_stack->all; |
101
|
|
|
|
|
|
|
$hub->send( |
102
|
|
|
|
|
|
|
Test2::Event::Note->new( |
103
|
|
|
|
|
|
|
message => $_ |
104
|
|
|
|
|
|
|
), |
105
|
3
|
|
|
|
|
73
|
) for @notes; |
106
|
3
|
|
|
|
|
27
|
}); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
test2_add_callback_exit(sub { |
109
|
3
|
|
|
3
|
|
13265
|
my ($ctx, $real, $new) = @_; |
110
|
3
|
100
|
|
|
|
24
|
if($INC{'File/HomeDir/Test.pm'}) |
111
|
|
|
|
|
|
|
{ |
112
|
1
|
|
|
|
|
4
|
my @message = ( |
113
|
|
|
|
|
|
|
'File::HomeDir::Test was loaded.', |
114
|
|
|
|
|
|
|
'You probably do not want to load both File::HomeDir::Test and Test2::Plugin::FauxHomeDir', |
115
|
|
|
|
|
|
|
); |
116
|
1
|
50
|
33
|
|
|
13
|
if($real || ($new && $$new) || !$ctx->hub->is_passing) |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
117
|
|
|
|
|
|
|
{ |
118
|
0
|
|
|
|
|
0
|
$ctx->diag($_) for @message; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else |
121
|
|
|
|
|
|
|
{ |
122
|
1
|
|
|
|
|
32
|
$ctx->note($_) for @message; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
3
|
|
|
|
|
633
|
}); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |