#!/usr/bin/env perl

use warnings;

use Test::More;

plan skip_all => "no symlink support" unless eval { symlink("",""); 1 };

use Path::Tiny 0.018;

use_ok 'File::Rotate::Simple';

for my $age ( 0..2 ) {

    subtest "dangling symlinks age = ${age}" => sub {

        my $dir  = Path::Tiny->tempdir;
        my $base = 'test.log';
        my $file = path( $dir, $base );

        my $r = File::Rotate::Simple->new(
            file  => "$file",
            touch => 1,
            age   => $age,
        );

        isa_ok $r => 'File::Rotate::Simple';

        subtest 'default accessors' => sub {
            is $r->file              => $file, 'file';
            is $r->if_missing        => 1,     'if_missing';
            is $r->extension_format  => '.%#', 'extension_format';
            is $r->replace_extension => undef, 'replace_extension';
            is $r->touch             => 1,     'touch';
        };

        ok !$file->exists || -l $file, "file does not exist";

        my $secret = path( $dir, "secret.log" );

        ok symlink( $secret, $file ), "created symlink to $secret";

        ok -l $file,         "file is a symlink";
        ok !$secret->exists, "dangling symlink";
        ok !$file->exists,   "dangling symlink";

        ok $r->rotate, "rotate";

        my $next = path( $file . ".1" );
        ok $next->exists || -l $next, "file was rotated to $next";

        ok $file->exists,    "file was touched";
        ok !-l $file,        "but file is not a link";
        ok !$secret->exists, "link still dangling";

    };

}

done_testing;
