### ----------------------------------------------------
### -- paths
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: t/02-symlinks.rakutest
### ----------------------------------------------------
use Test;
use paths;

(my $tmpdir := $*SPEC.tmpdir.resolve.add("paths.test-" ~ rand)).mkdir;
(my $dir    := $tmpdir.add("dir")).mkdir;
(my $foo    := $tmpdir.add("foo")).mkdir;
(my $symfoo := $dir.add("symfoo")).unlink;

LEAVE {
    $symfoo.unlink;
    .rmdir for $foo, $dir, $tmpdir;
}

if $foo.symlink($symfoo) && $symfoo.e {
    plan 16;

    (my $one    := $foo.add("one.json")).spurt("one.json");
    (my $foobar := $foo.add("bar")).mkdir;
    (my $two   := $foobar.add("two.json")).spurt("two.json");
    (my $three := $foobar.add("three.txt")).spurt("three.txt");
    (my $foobardot := $foobar.add(".dot")).mkdir;
    (my $four := $foobardot.add("four.t")).spurt("four.t");
    (my $five := $foobardot.add("five.t")).spurt("five.t");

    LEAVE {
        .unlink for $five, $four, $three, $two, $one, $symfoo;
        .rmdir  for $foobardot, $foobar;
    }

    my $root := $tmpdir.absolute.subst('\\' ,'/', :global);
    my $from := $root.chars + 1;
    my @got;

    sub verify-path-roots($plan, |c) is test-assertion {
        my @seen;
        subtest 'test path roots', {
            plan $plan;
            for paths(:follow-symlinks, |c).map(*.subst: '\\', '/', :global) -> $path {
                ok $path.starts-with($root), "root of $path.IO.basename() ok";
                @seen.push($path.substr($from));
            }
        }
        @got := @seen.sort.List;
    }

    sub verify-files(@expected, $comment) is test-assertion {
        subtest $comment => {
            plan 1 + @expected;
            is-deeply @got, @expected, 'are all files accounted for';
            for @expected {
                my $io := $tmpdir.add($_);
                is $io.slurp, $io.basename, "is the content '$io.basename()'";
            }
        }
    }

    verify-path-roots 3, $dir;
    verify-files <
      foo/bar/three.txt
      foo/bar/two.json
      foo/one.json
    >, 'did we get all the paths for $dir';

    verify-path-roots 3, $dir, :dir(Any), :file(Any);
    verify-files <
      foo/bar/three.txt
      foo/bar/two.json
      foo/one.json
    >, 'did we get all the paths for $dir';

    verify-path-roots 5, $dir, :dir;
    verify-files <
      foo/bar/.dot/five.t
      foo/bar/.dot/four.t
      foo/bar/three.txt
      foo/bar/two.json
      foo/one.json
    >, 'did we get all the paths for $dir :dir';

    verify-path-roots 2, $dir, :file(*.ends-with('.json'));
    verify-files <
      foo/bar/two.json
      foo/one.json
    >, 'did we get all the paths for $dir :file(*.ends-with(json))';

    verify-path-roots 0, $dir, :file(*.ends-with('.t'));
    is +@got, 0, 'did we get no paths for $dir :file(*.ends-with(t))';

    verify-path-roots 0, $dir, :dir(* eq '.dot');
    is +@got, 0, 'did we get no paths for $dir :dir(* eq .dot)';

    verify-path-roots 2, $dir, :dir(* eq '.dot'), :recurse;
    verify-files <
      foo/bar/.dot/five.t
      foo/bar/.dot/four.t
    >, 'did we get all the paths for $dir :dir(* eq .dot) :recurse';

    verify-path-roots 1, $three;
    verify-files ('foo/bar/three.txt',), 'did we get the single existing file';
}

else {
    plan 1;
    pass "Could not create symlink for testing";
}

# vim: expandtab shiftwidth=4


