Ancient

 view release on metacpan or  search on metacpan

t/8000-file-basic.t  view on Meta::CPAN

    ok($mode >= 0, 'mode is non-negative');
    ok(!file::is_link($file), 'regular file is not a link');
    # is_executable depends on mode
};

# Test unlink
subtest 'unlink' => sub {
    my $file = "$tmpdir/to_delete.txt";
    file::spew($file, "delete me");
    ok(file::exists($file), 'file exists before unlink');
    ok(file::unlink($file), 'unlink returns true');
    ok(!file::exists($file), 'file gone after unlink');
    ok(!file::unlink($file), 'unlink non-existent returns false');
};

# Test copy
subtest 'copy' => sub {
    my $src = "$tmpdir/copy_src.txt";
    my $dst = "$tmpdir/copy_dst.txt";
    my $content = "content to copy\nline 2";

    file::spew($src, $content);
    ok(file::copy($src, $dst), 'copy returns true');
    ok(file::exists($dst), 'destination exists');
    is(file::slurp($dst), $content, 'copied content matches');

    # Source should still exist
    ok(file::exists($src), 'source still exists after copy');
};

# Test move
subtest 'move' => sub {
    my $src = "$tmpdir/move_src.txt";
    my $dst = "$tmpdir/move_dst.txt";
    my $content = "content to move";

    file::spew($src, $content);
    ok(file::move($src, $dst), 'move returns true');
    ok(!file::exists($src), 'source gone after move');
    ok(file::exists($dst), 'destination exists');
    is(file::slurp($dst), $content, 'moved content matches');
};

# Test touch
subtest 'touch' => sub {
    my $file = "$tmpdir/touched.txt";

    # Touch new file
    ok(file::touch($file), 'touch new file returns true');
    ok(file::exists($file), 'touched file exists');
    is(file::size($file), 0, 'touched file is empty');

    # Touch existing file - should update mtime
    my $mtime1 = file::mtime($file);
    sleep(1);  # Need a small delay
    ok(file::touch($file), 'touch existing file returns true');
    my $mtime2 = file::mtime($file);
    ok($mtime2 >= $mtime1, 'mtime updated after touch');
};

# Test chmod
subtest 'chmod' => sub {
    my $file = "$tmpdir/chmod_test.txt";
    file::spew($file, "chmod test");

    ok(file::chmod($file, 0644), 'chmod returns true');
    # Mode check is platform-specific, just verify no error
};

# Test mkdir and rmdir
subtest 'mkdir and rmdir' => sub {
    my $dir = "$tmpdir/newdir";

    ok(file::mkdir($dir), 'mkdir returns true');
    ok(file::is_dir($dir), 'created directory exists');
    ok(!file::mkdir($dir), 'mkdir existing dir returns false');

    ok(file::rmdir($dir), 'rmdir returns true');
    ok(!file::is_dir($dir), 'directory gone after rmdir');
    ok(!file::rmdir($dir), 'rmdir non-existent returns false');
};

# Test readdir
subtest 'readdir' => sub {
    my $dir = "$tmpdir/listdir";
    file::mkdir($dir);
    file::spew("$dir/a.txt", "a");
    file::spew("$dir/b.txt", "b");
    file::spew("$dir/c.txt", "c");

    my $entries = file::readdir($dir);
    is(ref($entries), 'ARRAY', 'readdir returns arrayref');
    is(scalar(@$entries), 3, 'correct number of entries');

    my %files = map { $_ => 1 } @$entries;
    ok($files{'a.txt'}, 'a.txt in listing');
    ok($files{'b.txt'}, 'b.txt in listing');
    ok($files{'c.txt'}, 'c.txt in listing');
    ok(!$files{'.'}, '. not in listing');
    ok(!$files{'..'}, '.. not in listing');
};

# Test basename
subtest 'basename' => sub {
    is(file::basename('/path/to/file.txt'), 'file.txt', 'basename with path');
    is(file::basename('file.txt'), 'file.txt', 'basename without path');
    is(file::basename('/path/to/dir/'), 'dir', 'basename with trailing slash');
    is(file::basename('/'), '', 'basename of root');
    is(file::basename(''), '', 'basename of empty string');
};

# Test dirname
subtest 'dirname' => sub {
    is(file::dirname('/path/to/file.txt'), '/path/to', 'dirname with file');
    is(file::dirname('file.txt'), '.', 'dirname without path');
    is(file::dirname('/path/to/dir/'), '/path/to', 'dirname with trailing slash');
    is(file::dirname('/path'), '/', 'dirname of root child');
    is(file::dirname('/'), '/', 'dirname of root');
};

# Test extname
subtest 'extname' => sub {
    is(file::extname('/path/to/file.txt'), '.txt', 'extname with extension');
    is(file::extname('file.tar.gz'), '.gz', 'extname with multiple dots');
    is(file::extname('noext'), '', 'extname without extension');
    is(file::extname('.hidden'), '', 'extname of hidden file (no ext)');



( run in 0.709 second using v1.01-cache-2.11-cpan-39bf76dae61 )