Dancer2

 view release on metacpan or  search on metacpan

t/file_utils.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;

use Test::More tests => 14;
use Test::Fatal;
use Path::Tiny ();
use File::Temp 0.22;

use Dancer2::FileUtils qw/read_file_content path_or_empty path/;

sub write_file {
    my ( $file, $content ) = @_;

    open my $fh, '>', $file or die "cannot write file $file : $!";
    binmode $fh, ':encoding(utf-8)';
    print $fh $content;
    close $fh;
}

sub hexe {
    my $s = shift;
    $s =~ s/([\x00-\x1F])/sprintf('%#x',ord($1))/eg;
    return $s;
}

like(
    exception { Dancer2::FileUtils::open_file( '<', '/slfkjsdlkfjsdlf' ) },
    qr/^Error open.+No such file or directory/,
    'Failure opening nonexistent file',
);

my $content = Dancer2::FileUtils::read_file_content();
is $content, undef;

my $p = Dancer2::FileUtils::dirname('/somewhere');
is $p, '/';

my $tmp = File::Temp->new();
my $two = "²❷";
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', );
}

my $tmpdir = File::Temp->newdir;
is( path_or_empty($tmpdir), Path::Tiny::path("$tmpdir")->stringify, 'path_or_empty on an existing path' );

note "escape_filename"; {
    my $names = [
        [ undef      => 'undef' ],
        [ 'abcdef'   => 'abcdef' ],
        [ 'ab++ef'   => 'ab+2b+2bef' ],
        [ 'a/../b.txt'   => 'a+2f+2e+2e+2fb+2etxt' ],
        [ "test\0\0" => 'test+00+00' ],
        [ 'test☠☠☠'  => 'test+2620+2620+2620' ],



( run in 1.085 second using v1.01-cache-2.11-cpan-54e63673c56 )