-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathapp-perlbrew-path.t
308 lines (230 loc) · 8.43 KB
/
app-perlbrew-path.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env perl
use Test2::V0;
use Test2::Tools::Spec;
use File::Temp qw[];
use App::Perlbrew::Path;
sub looks_like_perlbrew_path;
sub arrange_testdir;
describe "App::Perlbrew::Path" => sub {
describe "new()" => sub {
it "should accept one parameter" => sub {
my $path = App::Perlbrew::Path->new("foo/bar/baz");
looks_like_perlbrew_path $path, "foo/bar/baz";
};
it "should concatenate multiple parameters into single path" => sub {
my $path = App::Perlbrew::Path->new("foo", "bar/baz");
looks_like_perlbrew_path $path, "foo/bar/baz";
};
it "should die with undefined element" => sub {
ok dies {
App::Perlbrew::Path->new('foo', undef, 'bar')
}, qr/^Received an undefined entry as a parameter/;
};
it "should concatenate long (root) path" => sub {
my $path = App::Perlbrew::Path->new('', qw(this is a long path to check if it is joined ok));
looks_like_perlbrew_path $path, '/this/is/a/long/path/to/check/if/it/is/joined/ok';
};
};
describe "basename()" => sub {
it "should return basename" => sub {
my $path = App::Perlbrew::Path->new("foo/bar/baz.tar.gz");
is $path->basename, "baz.tar.gz";
};
it "should trim provided suffix match" => sub {
my $path = App::Perlbrew::Path->new("foo/bar/baz.tar.gz");
is $path->basename(qr/\.tar\.(?:gz|bz2|xz)$/), "baz";
};
};
describe "child()" => sub {
it "should create direct child" => sub {
my $path = App::Perlbrew::Path->new("foo/bar")->child(1);
looks_like_perlbrew_path $path, "foo/bar/1";
};
it "should accept multiple children" => sub {
my $path = App::Perlbrew::Path->new("foo/bar")->child(1, 2);
looks_like_perlbrew_path $path, "foo/bar/1/2";
};
it "should return chainable object" => sub {
my $path = App::Perlbrew::Path->new("foo/bar")->child(1, 2)->child(3);
looks_like_perlbrew_path $path, "foo/bar/1/2/3";
};
};
describe "children()" => sub {
it "should return empty list on empty match" => sub {
my $root = arrange_testdir;
is 0 + $root->children(), 0;
};
it "should return Path instances" => sub {
my $root = arrange_testdir;
for (qw[ aa ab ba ]) {
my $child = $root->child($_);
$child->mkpath;
looks_like_perlbrew_path $child, "$child";
}
};
};
describe "mkpath()" => sub {
it "should create recursive path" => sub {
my $path = arrange_testdir ("foo/bar/baz");
$path->mkpath;
ok -d $path;
};
it "should not die when path already exists" => sub {
my $path = arrange_testdir ("foo/bar/baz");
$path->mkpath;
ok lives { $path->mkpath };
};
it "should return self" => sub {
my $path = arrange_testdir ("foo/bar/baz");
looks_like_perlbrew_path $path->mkpath, "$path";
};
};
describe "readlink()" => sub {
my $test_root;
my $link;
before_each arrange_testdir => sub {
$test_root = arrange_testdir;
$link = $test_root->child('link');
};
describe "when path doesn't exist" => sub {
it "should return undef" => sub {
is $link->readlink, undef;
};
};
describe "when path isn't a symlink" => sub {
before_each mkpath => sub { $link->mkpath };
it "should return undef" => sub {
is $link->readlink, undef;
};
};
describe "should return path object of link content" => sub {
my $dest;
before_each mkpath => sub {
$dest = $test_root->child('dest');
$dest->mkpath;
symlink $dest, $link;
};
it "should return link path" => sub {
my $read = $link->readlink;
looks_like_perlbrew_path $read, "$dest";
};
};
};
describe "rmpath()" => sub {
it "should remove path recursively" => sub {
my $path = arrange_testdir ("foo");
my $child = $path->child("bar/baz");
$child->mkpath;
$path->rmpath;
ok ! -d $path;
};
it "should not die when path doesn't exist" => sub {
my $path = arrange_testdir ("foo");
ok lives { $path->rmpath };
};
it "should return self" => sub {
my $path = arrange_testdir ("foo/bar/baz");
looks_like_perlbrew_path $path, "$path";
};
};
describe "stringify_with_tilde" => sub {
it "should expand leading homedir" => sub {
local $ENV{HOME} = "/home/perlbrew";
my $path = App::Perlbrew::Path->new("/home/perlbrew/.root");
is $path->stringify_with_tilde, "~/.root";
};
it "should not expand leading homedir prefix" => sub {
local $ENV{HOME} = "/home/perlbrew";
my $path = App::Perlbrew::Path->new("/home/perlbrew-stable/.root");
is $path->stringify_with_tilde, "/home/perlbrew-stable/.root";
};
};
describe "symlink()" => sub {
it "should create symlink" => sub {
my $root = arrange_testdir;
my $file = $root->child('file');
open my $fh, '>>', $file or die $!;
my $symlink = $file->symlink($root->child('symlink'));
unless ($symlink) {
fail;
diag "Symlink failed: $!";
return;
}
unless ($symlink eq $root->child("symlink")) {
fail;
diag "is should return symlink object";
diag "got : $symlink";
diag "expected: $root/symlink";
return;
}
my $readlink = $symlink->readlink;
unless ($readlink eq $file) {
fail;
diag "it should point to origin file";
diag "got : $readlink";
diag "expected: $root/file";
return;
}
pass;
};
it "should replace existing file in force mode" => sub {
my $root = arrange_testdir;
my $file = $root->child('file');
open my $fh, '>>', $file or die $!;
$root->child('foo')->symlink($root->child('symlink'))
or diag "init failed: $!";
my $symlink = $file->symlink($root->child('symlink'), 'force');
unless ($symlink) {
fail;
diag "Symlink failed: $!";
return;
}
unless ($symlink eq $root->child("symlink")) {
fail;
diag "is should return symlink object";
diag "got : $symlink";
diag "expected: $root/symlink";
return;
}
my $readlink = $symlink->readlink;
unless ($readlink eq $file) {
fail;
diag "it should point to origin file";
diag "got : $readlink";
diag "expected: $root/file";
return;
}
pass;
};
};
describe "unlink()" => sub {
it "should not report failure if file doesn't exist" => sub {
my $root = arrange_testdir;
my $file = $root->child('file');
$file->unlink;
ok ! -e $file;
};
it "should remove file" => sub {
my $root = arrange_testdir;
my $file = $root->child('file');
open my $fh, '>>', $file or die $!;
fail and diag ("file doesn't exist yet") and return
unless -e $file;
$file->unlink;
ok ! -e $file;
};
};
};
done_testing;
sub looks_like_perlbrew_path {
my ($actual, $expected) = @_;
subtest "looks like a perlbrew path object", sub {
is "$actual", $expected;
is $actual->stringify(), $expected;
isa_ok $actual, 'App::Perlbrew::Path';
};
}
sub arrange_testdir {
my (@path) = @_;
App::Perlbrew::Path->new(File::Temp::tempdir (CLEANUP => 1), @path);
}