1package t::TestUtils; 2use strict; 3use warnings; 4use File::Path qw/mkpath rmtree/; 5use base 'Exporter'; 6 7our @EXPORT_OK = qw/write_module/; 8 9write_module( 10 'Foo::Bar', 11 { file => "lib/Foo/Bar.pm", version => '0.01' }, 12 { file => "t/lib/Foo/Bar.pm", version => 'Mocked' }, 13); 14write_module( 15 'Foo::Moose', 16 { file => "lib/Foo/Moose.pm", version => '0.01' }, 17 { file => "t/lib/Foo/Moose.pm", version => 'Mocked' }, 18); 19write_module( 20 'Foo::Baz', 21 { file => "lib/Foo/Baz.pm", version => '0.01' }, 22 { file => "t/ERK/Foo/Baz.pm", version => 'Mocked' }, 23); 24write_module( 25 'Foo::PreLoaded', 26 { file => "lib/Foo/PreLoaded.pm", version => '0.01' }, 27 { file => "t/lib/Foo/PreLoaded.pm", version => 'Mocked' }, 28); 29 30# Create a module that uses URI.pm 31my $unmocked_code = <<'EOT'; 32use unmocked 'URI'; 33 34sub foo { 35 my $u = URI->new('http://awesnob.com'); 36 return $u->host; 37} 38EOT 39write_module( 40 'Foo::UsingUnmocked', 41 { file => 'lib/Foo/UsingUnmocked.pm', version => '0.01', 42 extra_code => $unmocked_code }, 43 { file => 't/lib/Foo/UsingUnmocked.pm', version => 'Mocked', 44 extra_code => $unmocked_code }, 45); 46 47# Create a module that uses a nested mocked 48{ 49 my $nested_mocked = <<'EOT'; 50use mocked 'Foo::Nestee'; 51use unmocked 'Foo::Nestee2'; 52 53our $FNV = $Foo::Nestee::VERSION; 54our $FNV2 = $Foo::Nestee2::VERSION; 55EOT 56 write_module( 57 'Foo::NestedMocked', 58 { file => 'lib/Foo/NestedMocked.pm', version => '0.01', 59 extra_code => $nested_mocked }, 60 { file => 't/lib/Foo/NestedMocked.pm', version => 'Mocked', 61 extra_code => $nested_mocked }, 62 ); 63 my $nestee_code = <<'EOT'; 64use unmocked 'Data::Dumper'; 65our $DDV = $Data::Dumper::VERSION; 66EOT 67 write_module( 68 'Foo::Nestee', 69 { file => "lib/Foo/Nestee.pm", version => '0.01', 70 extra_code => $nestee_code }, 71 { file => "t/lib/Foo/Nestee.pm", version => 'Mocked', 72 extra_code => $nestee_code }, 73 ); 74 write_module( 75 'Foo::Nestee2', 76 { file => "lib/Foo/Nestee2.pm", version => '0.01' }, 77 { file => "t/lib/Foo/Nestee2.pm", version => 'Mocked' }, 78 ); 79} 80 81my $other_file = q{lib/Foo/Other.pm}; 82write_module( 83 'Foo::PreLoaded', 84 { file => $other_file, version => '0.01' }, 85 { file => "t/lib/Foo/WhatWeWant.pm", version => 'Mocked' }, 86); 87open(my $fh, qq{>>$other_file}) or die "Could not open '$other_file' for append"; 88print $fh qq{ 89package Foo::WhatWeWant; 90 91use strict; 92use warnings; 93use base 'Exporter'; 94our \@EXPORT_OK = qw(\$awesome); 95 96our \$VERSION = '0.01'; 97our \$awesome = 'like, totally'; 98 99sub module_filename { 100 return '$other_file'; 101} 102 1031; 104}; 105close($fh) or die "Could not close '$other_file' for append"; 106 107 108my @to_delete; 109sub write_module { 110 my $module = shift; 111 for my $mod_ref (@_) { 112 my $file = $mod_ref->{file}; 113 my $version = $mod_ref->{version}; 114 my $extra_code = $mod_ref->{extra_code} || ''; 115 (my $d = $file) =~ s#(.+)/.+#$1#; 116 unless (-d $d) { 117 mkpath $d or die "Can't mkpath $d: $!"; 118 push @to_delete, $d; 119 } 120 open(my $fh, ">$file") or die "Can't open $file: $!"; 121 print $fh <<EOT; 122package $module; 123use strict; 124use warnings; 125use base 'Exporter'; 126our \@EXPORT_OK = qw(\$awesome); 127 128our \$VERSION = '$version'; 129our \$awesome = 'like, totally'; 130 131sub module_filename { 132 return '$file'; 133} 134 135$extra_code 136 1371; 138EOT 139 close $fh or die "Can't write $file: $!"; 140 push @to_delete, $file; 141 } 142} 143 144END { 145 for my $f (@to_delete) { 146 if (-f $f) { 147 unlink $f or die "Can't unlink $f: $!"; 148 } 149 elsif (-d $f) { 150 rmtree $f or die "Can't rmtree $f: $!"; 151 } 152 } 153} 154 1551; 156