/
file.t
147 lines (128 loc) · 5.16 KB
/
file.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
use Mojo::Base -strict;
use Test::More;
use Cwd 'getcwd';
use File::Basename qw(basename dirname);
use File::Spec::Functions qw(abs2rel canonpath catfile rel2abs splitdir);
use File::Temp;
use Mojo::File qw(path tempdir);
# Constructor
is(Mojo::File->new, canonpath(getcwd), 'same path');
is path(), canonpath(getcwd), 'same path';
is path()->to_string, canonpath(getcwd), 'same path';
is path('/foo/bar'), '/foo/bar', 'same path';
is path('foo', 'bar', 'baz'), catfile('foo', 'bar', 'baz'), 'same path';
# Tap into method chain
is path('/home')->tap(sub { $$_ .= '/sri' })->to_string, '/home/sri',
'same path';
# Children
is path('foo', 'bar')->child('baz', 'yada'),
catfile(catfile('foo', 'bar'), 'baz', 'yada'), 'same path';
# Array
is_deeply path('foo', 'bar')->to_array, [splitdir catfile('foo', 'bar')],
'same structure';
is_deeply [@{path('foo', 'bar')}], [splitdir catfile('foo', 'bar')],
'same structure';
# Absolute
is path('file.t')->to_abs, rel2abs('file.t'), 'same path';
# Relative
is path('test.txt')->to_abs->to_rel(getcwd),
abs2rel(rel2abs('test.txt'), getcwd), 'same path';
# Basename
is path('file.t')->to_abs->basename, basename(rel2abs 'file.t'), 'same path';
is path('file.t')->to_abs->basename('.t'), basename(rel2abs('file.t'), '.t'),
'same path';
# Dirname
is path('file.t')->to_abs->dirname, dirname(rel2abs 'file.t'), 'same path';
# Checks
ok path(__FILE__)->to_abs->is_abs, 'path is absolute';
ok !path('file.t')->is_abs, 'path is not absolute';
# Temporary directory
my $dir = tempdir;
my $path = "$dir";
ok -d $path, 'directory exists';
undef $dir;
ok !-d $path, 'directory does not exist anymore';
$dir = tempdir 'mytestXXXXX';
ok -d $dir, 'directory exists';
like $dir->basename, qr/mytest.{5}$/, 'right format';
# Temporary diectory (separate object)
$dir = Mojo::File->new(File::Temp->newdir);
$path = "$dir";
ok -d $path, 'directory exists';
undef $dir;
ok !-d $path, 'directory does not exist anymore';
# Make path
$dir = tempdir;
my $subdir = $dir->child('foo', 'bar');
ok !-d $subdir, 'directory does not exist anymore';
$subdir->make_path;
ok -d $subdir, 'directory exists';
my $nextdir = $dir->child('foo', 'foobar')->make_path({error => \my $error});
ok -d $nextdir, 'directory exists';
isa_ok $error, 'ARRAY', 'make_path modified variable';
# Remove tree
$dir = tempdir;
$dir->child('foo', 'bar')->make_path->child('test.txt')->spurt('test!');
is $dir->child('foo', 'bar', 'test.txt')->slurp, 'test!', 'right content';
$subdir = $dir->child('foo', 'foobar')->make_path;
ok -e $subdir->child('bar')->make_path->child('a.out')->spurt('ELF'), 'created';
ok -d $subdir->remove_tree({keep_root => 1}), 'arg passed through and dir kept';
ok !-e $subdir->child('bar'), 'sub tree has been removed';
ok !-e $dir->child('foo')->remove_tree->to_string, 'tree has been removed';
# Move to
$dir = tempdir;
my $destination = $dir->child('dest.txt');
my $source = $dir->child('src.txt')->spurt('works!');
ok -f $source, 'file exists';
ok !-f $destination, 'file does not exists';
ok !-f $source->move_to($destination), 'file no longer exists';
ok -f $destination, 'file exists';
is $destination->slurp, 'works!', 'right content';
# List
is_deeply path('does_not_exist')->list->to_array, [], 'no files';
is_deeply path(__FILE__)->list->to_array, [], 'no files';
my $lib = path(__FILE__)->dirname->child('lib', 'Mojo');
my @files = map { path($lib)->child(split '/') }
('DeprecationTest.pm', 'LoaderException.pm', 'LoaderException2.pm');
is_deeply path($lib)->list->map('to_string')->to_array, \@files, 'right files';
unshift @files, $lib->child('.hidden.txt')->to_string;
is_deeply path($lib)->list({hidden => 1})->map('to_string')->to_array, \@files,
'right files';
@files = map { path($lib)->child(split '/') } (
'BaseTest', 'DeprecationTest.pm',
'LoaderException.pm', 'LoaderException2.pm',
'LoaderTest'
);
is_deeply path($lib)->list({dir => 1})->map('to_string')->to_array, \@files,
'right files';
my @hidden = map { path($lib)->child(split '/') } '.hidden.txt', '.test';
is_deeply path($lib)->list({dir => 1, hidden => 1})->map('to_string')->to_array,
[@hidden, @files], 'right files';
# List tree
is_deeply path('does_not_exist')->list_tree->to_array, [], 'no files';
is_deeply path(__FILE__)->list_tree->to_array, [], 'no files';
@files = map { path($lib)->child(split '/') } (
'BaseTest/Base1.pm', 'BaseTest/Base2.pm',
'BaseTest/Base3.pm', 'DeprecationTest.pm',
'LoaderException.pm', 'LoaderException2.pm',
'LoaderTest/A.pm', 'LoaderTest/B.pm',
'LoaderTest/C.pm'
);
is_deeply path($lib)->list_tree->map('to_string')->to_array, \@files,
'right files';
@hidden = map { path($lib)->child(split '/') } '.hidden.txt',
'.test/hidden.txt';
is_deeply path($lib)->list_tree({hidden => 1})->map('to_string')->to_array,
[@hidden, @files], 'right files';
# I/O
$dir = tempdir;
my $file = $dir->child('test.txt')->spurt('just works!');
is $file->slurp, 'just works!', 'right content';
is $file->spurt('w', 'orks', ' too!')->slurp, 'works too!', 'right content';
{
no warnings 'redefine';
local *IO::Handle::syswrite = sub { $! = 0; 5 };
eval { $file->spurt("just\nworks!") };
like $@, qr/Can't write to file ".*/, 'right error';
}
done_testing();