forked from PerlDancer/Dancer2
/
file_utils.t
58 lines (45 loc) · 1.2 KB
/
file_utils.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
use strict;
use warnings;
use Test::More tests => 9;
use Test::Fatal;
use File::Spec;
use File::Temp 0.22;
use Dancer::FileUtils qw/read_file_content path_or_empty/;
sub write_file {
my ($file, $content) = @_;
open CONF, '>', $file or die "cannot write file $file : $!";
print CONF $content;
close CONF;
}
sub hexe {
my $s = shift;
$s =~ s/([\x00-\x1F])/sprintf('%#x',ord($1))/eg;
return $s;
}
like(
exception { Dancer::FileUtils::open_file('<', '/slfkjsdlkfjsdlf') },
qr{/slfkjsdlkfjsdlf' using mode '<'},
'Failure opening nonexistent file',
);
my $content = Dancer::FileUtils::read_file_content();
is $content, undef;
is Dancer::FileUtils::normalize_path(), undef;
my $p = Dancer::FileUtils::dirname('/somewhere');
is $p, '/';
my $tmp = File::Temp->new();
write_file($tmp, "one$/two");
$content = read_file_content($tmp);
is hexe($content), hexe("one$/two");
my @content = read_file_content($tmp);
is hexe($content[0]), hexe("one$/");
is $content[1], 'two';
# returns UNDEF on non-existant path
my $path = 'bla/blah';
if (! -e $path) {
is(
path_or_empty($path),
'',
'path_or_empty on non-existent path',
);
}
is(path_or_empty('/tmp'), '/tmp');