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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
package t::TestUtils;
use strict;
use warnings;
use File::Path qw/mkpath rmtree/;
use base 'Exporter';
our @EXPORT_OK = qw/write_module/;
write_module(
'Foo::Bar',
{ file => "lib/Foo/Bar.pm", version => '0.01' },
{ file => "t/lib/Foo/Bar.pm", version => 'Mocked' },
);
write_module(
'Foo::Moose',
{ file => "lib/Foo/Moose.pm", version => '0.01' },
{ file => "t/lib/Foo/Moose.pm", version => 'Mocked' },
);
write_module(
'Foo::Baz',
{ file => "lib/Foo/Baz.pm", version => '0.01' },
{ file => "t/ERK/Foo/Baz.pm", version => 'Mocked' },
);
write_module(
'Foo::PreLoaded',
{ file => "lib/Foo/PreLoaded.pm", version => '0.01' },
{ file => "t/lib/Foo/PreLoaded.pm", version => 'Mocked' },
);
# Create a module that uses URI.pm
my $unmocked_code = <<'EOT';
use unmocked 'URI';
sub foo {
my $u = URI->new('http://awesnob.com');
return $u->host;
}
EOT
write_module(
'Foo::UsingUnmocked',
{ file => 'lib/Foo/UsingUnmocked.pm', version => '0.01',
extra_code => $unmocked_code },
{ file => 't/lib/Foo/UsingUnmocked.pm', version => 'Mocked',
extra_code => $unmocked_code },
);
# Create a module that uses a nested mocked
{
my $nested_mocked = <<'EOT';
use mocked 'Foo::Nestee';
use unmocked 'Foo::Nestee2';
our $FNV = $Foo::Nestee::VERSION;
our $FNV2 = $Foo::Nestee2::VERSION;
EOT
write_module(
'Foo::NestedMocked',
{ file => 'lib/Foo/NestedMocked.pm', version => '0.01',
extra_code => $nested_mocked },
{ file => 't/lib/Foo/NestedMocked.pm', version => 'Mocked',
extra_code => $nested_mocked },
);
my $nestee_code = <<'EOT';
use unmocked 'Data::Dumper';
our $DDV = $Data::Dumper::VERSION;
EOT
write_module(
'Foo::Nestee',
{ file => "lib/Foo/Nestee.pm", version => '0.01',
extra_code => $nestee_code },
{ file => "t/lib/Foo/Nestee.pm", version => 'Mocked',
extra_code => $nestee_code },
);
write_module(
'Foo::Nestee2',
{ file => "lib/Foo/Nestee2.pm", version => '0.01' },
{ file => "t/lib/Foo/Nestee2.pm", version => 'Mocked' },
);
}
my $other_file = q{lib/Foo/Other.pm};
write_module(
'Foo::PreLoaded',
{ file => $other_file, version => '0.01' },
{ file => "t/lib/Foo/WhatWeWant.pm", version => 'Mocked' },
);
open(my $fh, qq{>>$other_file}) or die "Could not open '$other_file' for append";
print $fh qq{
package Foo::WhatWeWant;
use strict;
use warnings;
use base 'Exporter';
our \@EXPORT_OK = qw(\$awesome);
our \$VERSION = '0.01';
our \$awesome = 'like, totally';
sub module_filename {
return '$other_file';
}
1;
};
close($fh) or die "Could not close '$other_file' for append";
my @to_delete;
sub write_module {
my $module = shift;
for my $mod_ref (@_) {
my $file = $mod_ref->{file};
my $version = $mod_ref->{version};
my $extra_code = $mod_ref->{extra_code} || '';
(my $d = $file) =~ s#(.+)/.+#$1#;
unless (-d $d) {
mkpath $d or die "Can't mkpath $d: $!";
push @to_delete, $d;
}
open(my $fh, ">$file") or die "Can't open $file: $!";
print $fh <<EOT;
package $module;
use strict;
use warnings;
use base 'Exporter';
our \@EXPORT_OK = qw(\$awesome);
our \$VERSION = '$version';
our \$awesome = 'like, totally';
sub module_filename {
return '$file';
}
$extra_code
1;
EOT
close $fh or die "Can't write $file: $!";
push @to_delete, $file;
}
}
END {
for my $f (@to_delete) {
if (-f $f) {
unlink $f or die "Can't unlink $f: $!";
}
elsif (-d $f) {
rmtree $f or die "Can't rmtree $f: $!";
}
}
}
1;
|