Alien-Xrepo

 view release on metacpan or  search on metacpan

t/057_runtime_deps.t  view on Meta::CPAN

                bindirs   => ["C:/store/$name/bin"]
            }
        );
    }

    method runtime_deps ( $libpath, %opts ) {
        $scans++;
        return [
            { name => 'libdep.so',    path  => '/vendor/lib/libdep.so', dir => '/vendor/lib', scope => 'store' },
            { name => 'KERNEL32.dll', scope => 'system' }
        ];
    }
    method loader_env_var {'LD_LIBRARY_PATH'}
}

# A plain spy: fetch only, no closure/runtime_deps/loader_env_var, to prove the wrapper degrades gracefully.
class Alien::Xrepo::Runtime::TestPlainSpy {

    method fetch ( $name, $version, %opts ) {
        return Alien::Xrepo::PackageInfo->new(
            includedirs => ["C:/store/$name/include"],
            libfiles    => ["C:/store/$name/bin/$name.dll"],
            license     => undef,
            linkdirs    => ["C:/store/$name/lib"],
            links       => [$name],
            shared      => 1,
            static      => 0,
            version     => $version // '9.9.9',
            libpath     => "C:/store/$name/bin/$name.dll",
            bindirs     => ["C:/store/$name/bin"],
            installdir  => "C:/store/$name",
            kind        => 'library'
        );
    }
}

class Alien::Xrepo::Runtime::TestDepsDyn : isa(Alien::Xrepo::Runtime) {
    method pkg_name {'zstd'}
}
subtest 'Runtime::runtime_deps gathers, caches and reports the diagnostic' => sub {
    my $lib  = path( $dir, 'libzstd.dylib' )->spew('not a real library');
    my $spy  = Alien::Xrepo::Runtime::TestDepsSpy->new( libpath => $lib->stringify );
    my $a    = Alien::Xrepo::Runtime::TestDepsDyn->new( repo    => $spy );
    my $deps = $a->runtime_deps;
    is scalar(@$deps), 2, 'two dependencies discovered';
    is $spy->scans,    1, 'one scan';
    $a->runtime_deps;
    is $spy->scans,                                     1,                 'subsequent access is cached';
    is [ map { $_->{name} } $a->foreign_runtime_deps ], ['libdep.so'],     'only the non-system dep is foreign';
    is [ $a->runtime_dep_dirs ],                        ['/vendor/lib'],   'the foreign dep directory is reported';
    is $a->loader_env_var,                              'LD_LIBRARY_PATH', 'loader env var from the engine';
    like $a->runtime_dep_report,   qr/libdep\.so \[store\]/, 'report names the dep and its scope';
    unlike $a->runtime_dep_report, qr/KERNEL32/,             'report omits system libraries';
    my $pi = $a->package_info;
    is scalar( @{ $pi->runtime_deps } ), 2, 'package_info carries the runtime deps';
    ok $pi->with_runtime_deps( [] ) == $pi, 'already-attached info is returned unchanged';
};
subtest 'snapshot runtime_deps paths are rebased like other paths' => sub {
    my $share = path( $dir, 'share-snap' )->mkdir;
    my $snap  = $share->child('xrepo-snapshot.json');
    $snap->spew_utf8(
        encode_json(
            {   dist_name    => 'Exotic-Z',
                install_type => 'share',
                packages     => {
                    zstd => {
                        includedirs  => ['zstd/include'],
                        libfiles     => [],
                        license      => undef,
                        linkdirs     => ['zstd/lib'],
                        links        => ['zstd'],
                        shared       => 1,
                        static       => 0,
                        version      => '1.5.6',
                        libpath      => 'zstd/bin/zstd.dll',
                        bindirs      => ['zstd/bin'],
                        installdir   => 'zstd',
                        kind         => 'library',
                        runtime_deps => [ { name => 'libdep.so', path => 'zstd/lib/libdep.so', dir => 'zstd/lib', scope => 'store' }, ],
                    }
                }
            }
        )
    );
    my $a = Alien::Xrepo::Runtime->new(
        pkg_name            => 'zstd',
        snapshot            => $snap,
        repo                => Alien::Xrepo::Runtime::TestPlainSpy->new,
        autodetect_snapshot => 0
    );
    my $dep = $a->package_info->runtime_deps->[0];
    is $dep->{path},  path( $share, 'zstd', 'lib', 'libdep.so' )->stringify, 'runtime_dep path rebased against the snapshot dir';
    is $dep->{dir},   path( $share, 'zstd', 'lib' )->stringify,              'runtime_dep dir rebased against the snapshot dir';
    is $dep->{scope}, 'store',                                               'scope preserved through the rebase';
};
subtest 'package_info degrades when the engine cannot scan' => sub {
    my $spy = Alien::Xrepo::Runtime::TestPlainSpy->new;
    my $a   = Alien::Xrepo::Runtime->new( pkg_name => 'zstd', repo => $spy );
    my $pi  = $a->package_info;
    is $pi->runtime_deps, [], 'no diagnostic from an engine that cannot scan';
    is $a->runtime_deps,  [], 'runtime_deps degrades to empty';
    is $a->loader_env_var, ( $^O eq 'MSWin32' ? 'PATH' : $^O eq 'darwin' ? 'DYLD_LIBRARY_PATH' : 'LD_LIBRARY_PATH' ),
        'loader env var falls back to the platform default';
};
#
done_testing;



( run in 0.559 second using v1.01-cache-2.11-cpan-234fa220139 )