Ancient

 view release on metacpan or  search on metacpan

t/8005-file-edge-cases.t  view on Meta::CPAN

#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use File::Temp qw(tempdir);

use_ok('file');

my $tmpdir = tempdir(CLEANUP => 1);

# ============================================
# Error handling for nonexistent files
# ============================================

subtest 'nonexistent file operations' => sub {
    my $nonexistent = "$tmpdir/does_not_exist.txt";

    is(file::slurp($nonexistent), undef, 'slurp nonexistent returns undef');
    is(file::size($nonexistent), -1, 'size nonexistent returns -1');
    is(file::mtime($nonexistent), -1, 'mtime nonexistent returns -1');
    is(file::atime($nonexistent), -1, 'atime nonexistent returns -1');
    is(file::ctime($nonexistent), -1, 'ctime nonexistent returns -1');
    is(file::mode($nonexistent), -1, 'mode nonexistent returns -1');

    ok(!file::exists($nonexistent), 'exists nonexistent returns false');
    ok(!file::is_file($nonexistent), 'is_file nonexistent returns false');
    ok(!file::is_dir($nonexistent), 'is_dir nonexistent returns false');
    ok(!file::is_readable($nonexistent), 'is_readable nonexistent returns false');
    ok(!file::is_writable($nonexistent), 'is_writable nonexistent returns false');
    ok(!file::is_link($nonexistent), 'is_link nonexistent returns false');

    ok(!file::unlink($nonexistent), 'unlink nonexistent returns false');
    ok(!file::rmdir($nonexistent), 'rmdir nonexistent returns false');

    my $lines = file::lines($nonexistent);
    is(ref($lines), 'ARRAY', 'lines returns arrayref');
    is(scalar(@$lines), 0, 'lines of nonexistent is empty');
};

# ============================================
# Path manipulation edge cases
# ============================================

subtest 'path edge cases' => sub {
    # Empty string
    is(file::basename(''), '', 'basename of empty');
    is(file::dirname(''), '.', 'dirname of empty');
    is(file::extname(''), '', 'extname of empty');

    # Root only
    is(file::basename('/'), '', 'basename of root');
    is(file::dirname('/'), '/', 'dirname of root');

    # Multiple slashes
    is(file::basename('//'), '', 'basename of double slash');
    is(file::basename('/a//b//c'), 'c', 'basename with double slashes');
    is(file::dirname('/a//b//c'), '/a//b', 'dirname with double slashes');

    # Trailing slashes
    is(file::basename('/path/dir/'), 'dir', 'basename with trailing slash');
    is(file::dirname('/path/dir/'), '/path', 'dirname with trailing slash');

    # Hidden files
    is(file::basename('.hidden'), '.hidden', 'basename of hidden');
    is(file::extname('.hidden'), '', 'extname of hidden (no ext)');
    is(file::extname('.hidden.txt'), '.txt', 'extname of hidden with ext');

    # Multiple extensions
    is(file::extname('file.tar.gz'), '.gz', 'extname of tar.gz');
    is(file::extname('file.min.js'), '.js', 'extname of min.js');

    # No extension
    is(file::extname('Makefile'), '', 'extname of Makefile');
    is(file::extname('/path/to/README'), '', 'extname of README');

    # Dot at end



( run in 0.691 second using v1.01-cache-2.11-cpan-0fb53d1c279 )