########################################################################
# housekeeping
########################################################################
package Testy;
use v5.40;

########################################################################
# test the current module: $0 is */t/X/foo.t, the module is in */lib.
#
# lib must exist; if it is a symlink it must resolve to the lib next
# to the physical t directory, code outside the checkout is not
# loaded.  add it to @INC unless it is already there, or a blib is
# ("make test", "prove -b").

my $lib_d;

BEGIN
{
    use Cwd                   qw( abs_path );
    use File::Spec::Functions qw( catdir catpath rel2abs splitdir splitpath );
    use List::Util            qw( first );

    my ( $vol, $dir )   = splitpath rel2abs $0;
    my @dirz            = splitdir $dir;

    pop @dirz
    while @dirz && $dirz[-1] ne 't';

    @dirz
    or die "Bogus test path: no 't' directory in '$0'.\n";

    my $t_d     = catpath $vol, ( catdir @dirz ), '';

    $dirz[-1]   = 'lib';
    $lib_d      = catpath $vol, ( catdir @dirz ), '';

    -d $lib_d
    or die "Bogus test path: '$lib_d' from '$0' is not a directory.\n";

    if( -l $lib_d )
    {
        my ( $t_vol, $t_phys )  = splitpath abs_path( $t_d ), 1;
        my @t_dirz              = splitdir $t_phys;

        $t_dirz[-1] = 'lib';

        my $expect  = catpath $t_vol, ( catdir @t_dirz ), '';
        my $found   = abs_path $lib_d;

        $found eq $expect
        or die "Bogus lib: symlink '$lib_d' resolves to '$found', "
        . "expected '$expect' next to the test directory.\n";
    }

    first
    {
        ! ref $_
        and
        (
            m{ (?: ^ | / ) blib / }x
            or rel2abs( $_ ) eq $lib_d
        )
    }
    @INC
    or unshift @INC, $lib_d;
}
use FindBin::libs;
use FindBin::libs   qw( base=lib export=lib_d          );
use FindBin::libs   qw( base=t   export=test_d  scalar );
use autodie;

use File::Basename;
use Test::More;

use List::Util  qw( first );

use File::Spec::Functions
qw
(
    rel2abs
    catdir
    catpath
);

########################################################################
# package variables and sanity checks
########################################################################

my $base0   = basename $0;
my $dir0    = dirname  $0;

my $path
= do
{
    my $base = basename $0 => qw( .t );
    $base   =~ s{~}{/}g;
    $base
};

for( $path )
{
    -e      or BAIL_OUT "Non-existant: '$path'\n";
    -r _    or BAIL_OUT "Non-readable: '$path'\n";
}

my $test_d  = dirname $dir0; 
my $work_d  = dirname $test_d;

chdir $work_d   or BAIL_OUT "chdir $work_d, $!";

my $last_prove  = $ENV{ LAST_PROVE } // ( stat '.prove' )[9] // 0;

SKIP:
{
    ( stat $path )[9] > $last_prove
    or skip "File unchanged: $path", 1;

    $ENV{ PERL5LIB } = './lib:./t/lib';

    chomp( my @output = qx{ perl -wc $path 2>&1 } );

    if( $output[-1] =~ m{syntax \s OK $}x )
    {
        pass "Syntax OK: $base0";

        1 < @output
        and diag join "\n\t" => "\nWarnings:", @output;
    }
    else
    {
        fail "Fails compilation: $base0";
        diag join "\n\t" => "\nWarnings:", @output;
    }
}

done_testing

__END__

=head1 NAME

02-pm_t - generic unit test for perl modules.

=back
