File: 7-check-dynaloader.t

package info (click to toggle)
libmodule-scandeps-perl 1.27-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 568 kB
  • sloc: perl: 2,880; makefile: 8; ansic: 1
file content (115 lines) | stat: -rw-r--r-- 3,271 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!perl

use strict;
use Test::More;
use Config ();

use Module::ScanDeps;
use DynaLoader;
use File::Temp;
use Data::Dumper;

plan skip_all => "No dynamic loading available in your version of perl"
    unless $Config::Config{usedl};

my @try_mods = qw( File::Glob Data::Dumper List::Util Time::HiRes Compress::Raw::Zlib );
my @dyna_mods = grep { my $mod = $_; 
                       eval("require $mod; 1") 
                       && grep { $_ eq $mod } @DynaLoader::dl_modules
                     } @try_mods;
plan skip_all => "No dynamic module found (tried @try_mods)"
    unless @dyna_mods;

my $extra_verbose = ($ENV{TEST_VERBOSE}||0) > 1;
diag "dynamic modules used for test: @dyna_mods";
if ($extra_verbose)
{
    diag "\@DynaLoader::dl_modules = @DynaLoader::dl_modules";
    diag "\@DynaLoader::dl_shared_objects = @DynaLoader::dl_shared_objects";
}

plan tests => 4 * 2 * @dyna_mods;

foreach my $module (@dyna_mods)
{
    # cf. DynaLoader.pm
    my @modparts = split(/::/,$module);
    my $modfname = defined &DynaLoader::mod2fname ? DynaLoader::mod2fname(\@modparts) : $modparts[-1];
    my $auto_path = join('/', 'auto', @modparts, "$modfname.$Config::Config{dlext}");

    check_bundle_path(static => $module, $auto_path,
        sub { scan_deps(
                files   => [ $_[0] ],
                recurse => 0);
        },
        ".pl", <<"...",
use $module;
1;
...
    );
    check_bundle_path(compile => $module, $auto_path,
        sub { scan_deps_runtime(
                files   => [ $_[0] ],
                recurse => 0,
                compile => 1);
        },
        ".pm", <<"...",
package Bar;
use $module;
1;
...
    );
    check_bundle_path(execute => $module, $auto_path,
        sub { scan_deps_runtime(
                files   => [ $_[0] ],
                recurse => 0,
                execute => 1);
        },
        ".pl", <<"...",
# no way in hell can this detected by static analysis :)
my \$req = join("", qw( r e q u i r e ));
eval "\$req $module";
exit(0);
...
    );
    check_bundle_path(execute_with_args => $module, $auto_path,
        sub { scan_deps_runtime(
                files   => [ $_[0] ],
                recurse => 0,
                execute => [ $module ]);
        },
        ".pl", <<"...",
# no way in hell can this detected by static analysis :)
my \$req = join("", qw( r e q u i r e ));
eval "\$req \$_" foreach \@ARGV;
exit(0);
...
    );
}

exit(0);

# NOTE: check_bundle_path runs 2 tests
sub check_bundle_path {
    my ($tag, $module, $auto_path, $scan, $suffix, $source) = @_;

    my ($fh, $filename) = File::Temp::tempfile( UNLINK => 1, SUFFIX => $suffix );
    print $fh $source, "\n" or die $!;
    close $fh;

    my $rv = $scan->($filename);
    diag("check_bundle_path:$tag for $module ...");
    diag(Dumper($rv)) if $extra_verbose;

    my ( $entry ) =  grep { /^\Q$auto_path\E$/ } keys %$rv;
    ok($entry,
       "check_bundle_path:$tag for $module: ".
       "found some key that looks like it pulled in its shared lib (auto_path=$auto_path)");

    # Actually we accept anything that ends with $auto_path.
    ok($rv->{$entry}{file} =~ m{/\Q$auto_path\E$}, 
       "check_bundle_path:$tag for $module: ".
       "the full bundle path we got \"$rv->{$entry}{file}\" looks legit");
}