# use "*_t" for basename of this file.
# symlink to *-<perl version string to test>.t.
# e.g.,
#   "20-foo-v5.5.3.t"
#   "20-bar-5.005_003"
#   "20-bar-v5.005003"
# will test varieties of the same perl version string.
########################################################################
# housekeeping
########################################################################

use 5.006;
use strict;
use version;

use Test::More;
use Test::Deep;

use File::Temp      qw( tempfile    );

use File::Basename  qw( basename    );
use FindBin         qw( $Bin        );

########################################################################
# package variables 
########################################################################

my $madness = 'Module::FromPerlVer';

########################################################################
# test reading version from "use" or "no".
########################################################################

delete $ENV{ PERL_VERSION };

SKIP:
{
    my $base    = basename $0, '.t';

    my ( $perl_v )
    =  do
    {
        my $i   = rindex $base, '-';
        substr $base, ++$i
    }
    or 
    skip "Botched test: no perl version in '$base'.", 1;

    if( version->parse( $perl_v )->numify )
    {
        note "Testing perl version: '$perl_v'.";
    }
    else
    {
        skip "Botched test: non-parsable '$perl_v' ($base).";
    }

    my $v_file
    = eval
    {
        if
        (
            my ( $fh, $path ) 
            = tempfile 'perl_version.XXXX'
        )
        {
            print $fh "use $perl_v;\n"
            or die "Failed print: '$path', $!\n";

            close $fh
            or die "Failed close: '$path', $!\n";

            END
            {
                if( -e $path )
                {
                    unlink $path
                    or warn "Failed unlink: '$path', $!";
                }
            }

            $path
        }
        else
        {
            die "Failed 'tempfile', $!\n";
        }
    }
    or skip $@, 1;

    eval
    {
        my @argz    = ( use_dir => 1,  version_from => $v_file );

        use_ok $madness => @argz
    }
    or skip "Use failed; $@", 1;

    for my $found ( scalar $madness->source_files )
    {
        @$found
        ? pass 'Found source files in version directory'
        : fail 'Missing source files for cleanup'
        ;

        note "Source files:\n", explain $found;

        ok -e , "Found: '$_'"
        for @$found;
    }

    eval
    {
        $madness->cleanup;
        unlink $v_file;

        pass "Survived cleanup.";
    }
    or 
    fail "Failed cleanup: $@";
}

done_testing;
__END__
