File: file_util.t

package info (click to toggle)
libsharyanto-file-util-perl 0.56-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 140 kB
  • sloc: perl: 313; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 2,495 bytes parent folder | download | duplicates (3)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!perl

use 5.010;
use strict;
use warnings;

use Cwd qw(abs_path);
use File::chdir;
use File::Slurp;
use File::Spec;
use Test::More 0.98;

plan skip_all => "symlink() not available"
    unless eval { symlink "", ""; 1 };

use File::Temp qw(tempfile tempdir);
use SHARYANTO::File::Util qw(file_exists l_abs_path dir_empty);

subtest file_exists => sub {
    my ($fh1, $target)  = tempfile();
    my ($fh2, $symlink) = tempfile();

    ok(file_exists($target), "existing file");

    unlink($symlink);
    symlink($target, $symlink);
    ok(file_exists($symlink), "symlink to existing file");

    unlink($target);
    ok(!file_exists($target), "non-existing file");
    ok(file_exists($symlink), "symlink to non-existing file");

    unlink($symlink);
};

subtest l_abs_path => sub {
    my $dir = abs_path(tempdir(CLEANUP=>1));
    local $CWD = $dir;

    mkdir("tmp");
    write_file("tmp/file", "");
    symlink("file", "tmp/symfile");
    symlink("$dir/tmp", "tmp/symdir");
    symlink("not_exists", "tmp/symnef"); # non-existing file
    symlink("/not_exists".rand()."/1", "tmp/symnep"); # non-existing path

    is(  abs_path("tmp/file"   ), "$dir/tmp/file"   , "abs_path file");
    is(l_abs_path("tmp/file"   ), "$dir/tmp/file"   , "l_abs_path file");
    is(  abs_path("tmp/symfile"), "$dir/tmp/file"   , "abs_path symfile");
    is(l_abs_path("tmp/symfile"), "$dir/tmp/symfile", "l_abs_path symfile");
    is(  abs_path("tmp/symdir" ), "$dir/tmp"        , "abs_path symdir");
    is(l_abs_path("tmp/symdir" ), "$dir/tmp/symdir" , "l_abs_path symdir");
    is(  abs_path("tmp/symnef" ), "$dir/tmp/not_exists", "abs_path symnef");
    is(l_abs_path("tmp/symnef" ), "$dir/tmp/symnef" , "l_abs_path symnef");
    ok(! abs_path("tmp/symnep" ), "abs_path symnep");
    is(l_abs_path("tmp/symnep" ), "$dir/tmp/symnep" , "l_abs_path symnep");
};

subtest dir_empty => sub {
    my $dir = tempdir(CLEANUP=>1);
    local $CWD = $dir;

    mkdir "empty", 0755;

    mkdir "hasfiles", 0755;
    write_file("hasfiles/1", "");

    mkdir "hasdotfiles", 0755;
    write_file("hasdotfiles/.1", "");

    mkdir "hasdotdirs", 0755;
    mkdir "hasdotdirs/.1";

    mkdir "unreadable", 0000;

    ok( dir_empty("empty"), "empty");
    ok(!dir_empty("doesntexist"), "doesntexist");
    ok(!dir_empty("hasfiles"), "hasfiles");
    ok(!dir_empty("hasdotfiles"), "hasdotfiles");
    ok(!dir_empty("hasdotdirs"), "hasdotdirs");
    ok(!dir_empty("unreadable"), "unreadable");
};

DONE_TESTING:
done_testing();