line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test2::Plugin::FauxHomeDir; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
303341
|
use strict; |
|
4
|
|
|
|
|
18
|
|
|
4
|
|
|
|
|
104
|
|
4
|
4
|
|
|
4
|
|
18
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
84
|
|
5
|
4
|
|
|
4
|
|
78
|
use 5.008001; |
|
4
|
|
|
|
|
12
|
|
6
|
4
|
|
|
4
|
|
2431
|
use File::Temp qw( tempdir ); |
|
4
|
|
|
|
|
62956
|
|
|
4
|
|
|
|
|
216
|
|
7
|
4
|
|
|
4
|
|
26
|
use File::Spec; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
74
|
|
8
|
4
|
|
|
4
|
|
16
|
use File::Path qw( mkpath ); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
174
|
|
9
|
4
|
|
|
4
|
|
2016
|
use if $^O eq 'cygwin', 'File::Spec::Win32'; |
|
4
|
|
|
|
|
45
|
|
|
4
|
|
|
|
|
20
|
|
10
|
4
|
|
|
4
|
|
585
|
use Test2::API qw( test2_add_callback_post_load test2_stack test2_add_callback_exit ); |
|
4
|
|
|
|
|
52151
|
|
|
4
|
|
|
|
|
2430
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: Setup a faux home directory for tests |
13
|
|
|
|
|
|
|
our $VERSION = '0.05'; # 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
|
12770
|
$real; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub import |
27
|
|
|
|
|
|
|
{ |
28
|
3
|
|
|
3
|
|
27
|
my @notes; |
29
|
|
|
|
|
|
|
|
30
|
3
|
50
|
|
|
|
13
|
unless(defined $faux) |
31
|
|
|
|
|
|
|
{ |
32
|
3
|
50
|
|
|
|
13
|
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
|
|
|
|
|
8
|
$real = $ENV{HOME}; |
42
|
3
|
|
|
|
|
6
|
$user = $ENV{USER}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
3
|
50
|
|
|
|
12
|
$user = eval { getlogin } unless defined $user; |
|
3
|
|
|
|
|
1951
|
|
46
|
3
|
50
|
|
|
|
19
|
$user = eval { scalar getpwuid($>) } unless defined $user; |
|
3
|
|
|
|
|
119
|
|
47
|
3
|
50
|
|
|
|
14
|
die "unable to determine username" unless defined $user; |
48
|
|
|
|
|
|
|
|
49
|
3
|
50
|
33
|
|
|
57
|
die "unable to determine 'real' home directory" |
50
|
|
|
|
|
|
|
unless defined $real && -d $real; |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
27
|
delete $ENV{USERPROFILE}; |
53
|
3
|
|
|
|
|
10
|
delete $ENV{HOME}; |
54
|
3
|
|
|
|
|
10
|
delete $ENV{HOMEDRIVE}; |
55
|
3
|
|
|
|
|
9
|
delete $ENV{HOMEPATH}; |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
54
|
$faux = File::Spec->catdir(tempdir( CLEANUP => 1 ), 'home', $user); |
58
|
3
|
|
|
|
|
5915
|
mkpath $faux, 0, 0700; |
59
|
|
|
|
|
|
|
|
60
|
3
|
50
|
|
|
|
23
|
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
|
|
|
|
|
46
|
$ENV{HOME} = $faux; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
3
|
|
|
|
|
25
|
push @notes, "Test2::Plugin::FauxHomeDir using faux home dir $faux"; |
96
|
3
|
|
|
|
|
10
|
push @notes, "Test2::Plugin::FauxHomeDir real home dir is $real"; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
test2_add_callback_post_load(sub { |
99
|
3
|
|
|
3
|
|
6881
|
test2_stack()->top; |
100
|
3
|
|
|
|
|
4187
|
my($hub) = test2_stack->all; |
101
|
|
|
|
|
|
|
$hub->send( |
102
|
|
|
|
|
|
|
Test2::Event::Note->new( |
103
|
|
|
|
|
|
|
message => $_ |
104
|
|
|
|
|
|
|
), |
105
|
3
|
|
|
|
|
54
|
) for @notes; |
106
|
3
|
|
|
|
|
23
|
}); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
test2_add_callback_exit(sub { |
109
|
3
|
|
|
3
|
|
9939
|
my ($ctx, $real, $new) = @_; |
110
|
3
|
100
|
|
|
|
27
|
if($INC{'File/HomeDir/Test.pm'}) |
111
|
|
|
|
|
|
|
{ |
112
|
1
|
|
|
|
|
3
|
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
|
|
|
11
|
if($real || ($new && $$new) || !$ctx->hub->is_passing) |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
117
|
|
|
|
|
|
|
{ |
118
|
0
|
|
|
|
|
0
|
$ctx->diag($_) for @message; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else |
121
|
|
|
|
|
|
|
{ |
122
|
1
|
|
|
|
|
25
|
$ctx->note($_) for @message; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
3
|
|
|
|
|
658
|
}); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |