| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use utf8; |
| 4 |
|
| 5 |
use Test::More tests => 14; |
| 6 |
use Test::Fatal; |
| 7 |
use Path::Tiny (); |
| 8 |
use File::Temp 0.22; |
| 9 |
|
| 10 |
use Dancer2::FileUtils qw/read_file_content path_or_empty path/; |
| 11 |
|
| 12 |
sub write_file { |
| 13 |
my ( $file, $content ) = @_; |
| 14 |
|
| 15 |
open my $fh, '>', $file or die "cannot write file $file : $!"; |
| 16 |
binmode $fh, ':encoding(utf-8)'; |
| 17 |
print $fh $content; |
| 18 |
close $fh; |
| 19 |
} |
| 20 |
|
| 21 |
sub hexe { |
| 22 |
my $s = shift; |
| 23 |
$s =~ s/([\x00-\x1F])/sprintf('%#x',ord($1))/eg; |
| 24 |
return $s; |
| 25 |
} |
| 26 |
|
| 27 |
like( |
| 28 |
exception { Dancer2::FileUtils::open_file( '<', '/slfkjsdlkfjsdlf' ) }, |
| 29 |
qr/^Error open.+No such file or directory/, |
| 30 |
'Failure opening nonexistent file', |
| 31 |
); |
| 32 |
|
| 33 |
my $content = Dancer2::FileUtils::read_file_content(); |
| 34 |
is $content, undef; |
| 35 |
|
| 36 |
my $p = Dancer2::FileUtils::dirname('/somewhere'); |
| 37 |
is $p, '/'; |
| 38 |
|
| 39 |
my $tmp = File::Temp->new(); |
| 40 |
my $two = "²❷"; |
| 41 |
write_file( $tmp, "one$/$two" ); |
| 42 |
|
| 43 |
$content = read_file_content($tmp); |
| 44 |
is hexe($content), hexe("one$/$two"); |
| 45 |
|
| 46 |
my @content = read_file_content($tmp); |
| 47 |
is hexe( $content[0] ), hexe("one$/"); |
| 48 |
is $content[1], "$two"; |
| 49 |
|
| 50 |
# returns UNDEF on non-existant path |
| 51 |
my $path = 'bla/blah'; |
| 52 |
if ( !-e $path ) { |
| 53 |
is( path_or_empty($path), '', 'path_or_empty on non-existent path', ); |
| 54 |
} |
| 55 |
|
| 56 |
my $tmpdir = File::Temp->newdir; |
| 57 |
is( path_or_empty($tmpdir), Path::Tiny::path("$tmpdir")->stringify, 'path_or_empty on an existing path' ); |
| 58 |
|
| 59 |
note "escape_filename"; { |
| 60 |
my $names = [ |
| 61 |
[ undef => 'undef' ], |
| 62 |
[ 'abcdef' => 'abcdef' ], |
| 63 |
[ 'ab++ef' => 'ab+2b+2bef' ], |
| 64 |
[ 'a/../b.txt' => 'a+2f+2e+2e+2fb+2etxt' ], |
| 65 |
[ "test\0\0" => 'test+00+00' ], |
| 66 |
[ 'test☠☠☠' => 'test+2620+2620+2620' ], |
| 67 |
]; |
| 68 |
|
| 69 |
for my $case ( @$names ) { |
| 70 |
is Dancer2::FileUtils::escape_filename( $case->[0] ), $case->[1]; |
| 71 |
} |
| 72 |
} |