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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#!/usr/bin/perl
use strict;
use warnings;
use File::Temp;
use File::Spec;
use IPC::Run3;
use Test::More;
use lib 't/data/autosplit';
BEGIN { use_ok( 'Module::ScanDeps' ); }
sub create_script
{
my ($text) = @_;
my ($fh, $filename) = File::Temp::tempfile( UNLINK => 1, SUFFIX => '.pl' );
print $fh $text;
close $fh;
return $filename;
}
sub test_autosplit
{
my ($tag, $scan, $expected, $text) = @_;
diag($tag);
my $filename = create_script($text);
my $rv = $scan->($filename);
foreach my $mod (@$expected)
{
ok($rv->{$mod}, "$mod detected");
}
my @bogus = grep { File::Spec->file_name_is_absolute($_) or m|^\.[/\\]| } keys %$rv;
is("@bogus", "", "no bogus keys in \$rv");
}
test_autosplit(
'use autosplitted module - static scan',
sub
{
scan_deps(files => [$_[0]], recurse => 1);
},
[qw(AutoLoader.pm Foo.pm auto/Foo/autosplit.ix auto/Foo/barnie.al auto/Foo/fred.al)],
'use Foo');
test_autosplit(
'use autosplitted module - runtime scan, absolute search path',
sub
{
my $rv = eval # scan_deps_runtime() may die
{
scan_deps_runtime(files => [$_[0]], recurse => 1, execute => [qw(fee fo fum)]);
};
if ($@)
{
print STDERR "scan_deps_runtime died: $@\n" if $@;
return {};
}
return $rv;
},
[qw(AutoLoader.pm Foo.pm auto/Foo/autosplit.ix auto/Foo/barnie.al)],
<< '...');
use Cwd;
use lib getcwd().'/t/data/autosplit';
# "use" that can't be resolved by static analysis
my $Foo = "Foo";
eval "use $Foo";
die qq["use $Foo" failed: $@] if $@;
Foo::blab(@ARGV);
Foo::barnie();
...
test_autosplit(
'use autosplitted module - runtime scan, relative search path',
sub
{
my $rv = eval # scan_deps_runtime() may die
{
scan_deps_runtime(files => [$_[0]], recurse => 1, execute => 1);
};
if ($@)
{
print STDERR "scan_deps_runtime died: $@\n" if $@;
return {};
}
return $rv;
},
[qw(AutoLoader.pm Foo.pm auto/Foo/autosplit.ix auto/Foo/barnie.al)],
<< '...');
use lib 't/data/autosplit';
# "use" that can't be resolved by static analysis
my $Foo = "Foo";
eval "use $Foo";
die qq["use $Foo" failed: $@] if $@;
Foo::blab(@ARGV);
Foo::barnie();
...
my $scanner = create_script(<< '...');
use Module::ScanDeps;
my ($file, @args) = @ARGV;
scan_deps_runtime(files => [$file], recurse => 1, execute => \@args);
...
my $file = create_script(<< '...');
use lib 't/data/autosplit';
# "use" that can't be resolved by static analysis
my $Foo = "Foo";
eval "use $Foo";
die qq["use $Foo" failed: $@] if $@;
Foo::blab(@ARGV);
Foo::barnie();
...
my @args = qw(fee fo fum);
# run $file with @args once and capture its output
my ($out, $err);
run3([$^X, $file, @args], \undef, \$out, \$err);
is($?, 0, "script ran successfully")
or diag("stdout:$out\nstderr:$err\n");
my $exp = $out;
my $rx = join(".*", map { quotemeta($_) } @args, "barnie!");
like($exp, qr/$rx/s, "script output");
# run $scanner on $file with @args
run3([$^X, "-Mblib", $scanner, $file, @args], \undef, \$out, \$err);
is($?, 0, "scanner ran successfully");
is($out, $exp, "scanner output");
done_testing();
|