File: 03_zglob.t

package info (click to toggle)
libfile-zglob-perl 0.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 204 kB
  • sloc: perl: 1,580; makefile: 2
file content (87 lines) | stat: -rw-r--r-- 2,584 bytes parent folder | download | duplicates (4)
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
86
87
use strict;
use warnings;
use utf8;
use Test::More;
use File::Zglob;
use Data::Dumper;
use Cwd;

{
    package Cwd::Guard;
    sub new {
        my ($class, $path) = @_;
        my $cwd = Cwd::getcwd();
        chdir($path);
        bless \$cwd, $class;
    }
    sub DESTROY {
        my $self = shift;
        chdir($$self);
    }
}

$File::Zglob::DEBUG = $ENV{DEBUG} ? 1 : 0;

{
    my $guard = Cwd::Guard->new('t/dat/');
    is_deeply2('**/normalfile', ['very/deep/normalfile']);
    is_deeply2('very/**/*', [qw(very/deep very/deep/normalfile)]);
    is_deeply2('very/deep/*', ['very/deep/normalfile']);
    is_deeply2('very/deep/.*', ['very/deep/.dotfile']);
    is_deeply2('**/*.{pm,pl}', [qw(lib/bar.pl lib/foo.pm)]);
    is_deeply2('bug/0', ['bug/0']);
    is_deeply2('./very/**/*', [qw(very/deep very/deep/normalfile)]);
    is_deeply2('./very/deep/*', ['very/deep/normalfile']);
    is_deeply2('./very/deep/.*', ['very/deep/.dotfile']);
    is_deeply2('very/./**/*', [qw(very/deep very/deep/normalfile)]);
}
is_deeply2('*/*.t', [qw(t/00_compile.t   t/02_glob_prepare_pattern.t  t/03_zglob.t t/04_dotdot.t xt/01_podspell.t  xt/02_perlcritic.t  xt/03_pod.t  xt/04_minimum_version.t)]);
is_deeply2('lib/File/Zglob.pm', ['lib/File/Zglob.pm']);
is_deeply2('lib/*/Zglob.pm', ['lib/File/Zglob.pm']);
is_deeply2('lib/File/*.pm', ['lib/File/Zglob.pm']);
is_deeply2('l*/*/*.pm', ['lib/File/Zglob.pm']);
is_samepath('~', [glob('~')]);
if (-f glob('~/.bashrc')) {
    is_samepath('~/.bashrc', [glob('~/.bashrc')]);
}
if (-f '/etc/passwd') {
    is_samepath('/etc/passwd', ['/etc/passwd']);
}
if ($ENV{USER} && $ENV{HOME} eq "/home/$ENV{USER}" && -d "/home/$ENV{USER}/") {
    is_deeply2("~", ["/home/$ENV{USER}"]);
    is_deeply2("~$ENV{USER}", ["/home/$ENV{USER}"]);
}

done_testing;

sub is_deeply2 {
    local $Data::Dumper::Purity = 1;
    local $Data::Dumper::Sortkeys = 1;
    local $Data::Dumper::Indent = 0;

    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my ($pattern, $expected, $reason) = @_;
    is(Dumper([sort { $a cmp $b } zglob($pattern)]), Dumper([sort @$expected]), $reason || $pattern) or do {
        die "ABORT" if $File::Zglob::DEBUG;
    };
}

sub normalize {
    my $path = shift;
    if ($^O eq 'MSWin32') {
        require Win32;
        Win32::GetLongPathName(Cwd::abs_path($path))
    } else {
        Cwd::abs_path($path)
    }

}
sub is_samepath {
    my ($p, $b) = @_;

    my $a = [zglob($p)];
    return 0 if !defined($a) || !defined($b) || @$a != @$b;
    for (0..$#$a) {
        is(normalize($a->[$_]), normalize($b->[$_]));
    }
}