| 1 | package Autoconf::Template::UnitTests; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Autoconf::Template::Constants qw(:all); | ||
| 7 | use Autoconf::Template::Utils qw(:all); | ||
| 8 | use Data::Dumper; | ||
| 9 | use File::Basename qw(basename); | ||
| 10 | use List::Util qw(pairs); | ||
| 11 | use Log::Log4perl qw(:easy); | ||
| 12 | use Scalar::Util qw(reftype); | ||
| 13 | |||
| 14 | use parent qw(Exporter); | ||
| 15 | |||
| 16 | our @EXPORT = qw( | ||
| 17 | create_unit_tests | ||
| 18 | create_unit_test_stub | ||
| 19 | create_unit_tests_list | ||
| 20 | ); | ||
| 21 | |||
| 22 | ######################################################################## | ||
| 23 | sub create_unit_tests { | ||
| 24 | ######################################################################## | ||
| 25 | my ($options) = @_; | ||
| 26 | |||
| 27 | my $root = $options->{root}; | ||
| 28 | |||
| 29 | # create unit test scaffolding for .pm, .pl, and .cgi files | ||
| 30 | my @unit_tests = ( | ||
| 31 | ( sprintf '%s/%s', $root, 'src/main/perl/lib' ) => | ||
| 32 | $options->{perl_modules}, | ||
| 33 | ( sprintf '%s/%s', $root, 'src/main/perl/bin' ) => | ||
| 34 | $options->{perl_scripts}, | ||
| 35 | ( sprintf '%s/%s', $root, 'src/main/perl/cgi-bin' ) => | ||
| 36 | $options->{cgi_scripts}, | ||
| 37 | ); | ||
| 38 | |||
| 39 | for my $p ( pairs @unit_tests ) { | ||
| 40 | my ( $path, $files ) = @{$p}; | ||
| 41 | |||
| 42 | TRACE Dumper( [ 'creating unit tests ', $path, $files ] ); | ||
| 43 | |||
| 44 | next if !$files; | ||
| 45 | |||
| 46 | if ( reftype($files) eq 'HASH' ) { | ||
| 47 | |||
| 48 | foreach my $dir ( keys %{$files} ) { | ||
| 49 | my @tests = @{ $files->{$dir} }; | ||
| 50 | |||
| 51 | if ( $dir ne $DOT ) { | ||
| 52 | @tests = map {"$dir-$_"} @tests; | ||
| 53 | } | ||
| 54 | |||
| 55 | TRACE Dumper( [ 'creating unit tests ', $path, \@tests ] ); | ||
| 56 | |||
| 57 | create_unit_test_stub( | ||
| 58 | files => \@tests, | ||
| 59 | path => $path, | ||
| 60 | options => $options, | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | TRACE Dumper( [ 'creating unit tests ', $path, $files ] ); | ||
| 66 | |||
| 67 | create_unit_test_stub( | ||
| 68 | files => $files, | ||
| 69 | path => $path, | ||
| 70 | options => $options, | ||
| 71 | ); | ||
| 72 | } | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | return; | ||
| 77 | } | ||
| 78 | |||
| 79 | ######################################################################## | ||
| 80 | sub create_unit_test_stub { | ||
| 81 | ######################################################################## | ||
| 82 | my (%args) = @_; | ||
| 83 | |||
| 84 | my $files = $args{files}; | ||
| 85 | |||
| 86 | foreach ( @{$files} ) { | ||
| 87 | my $filename = $_; | ||
| 88 | |||
| 89 | my $test_name = flatten_filename($filename); | ||
| 90 | |||
| 91 | render_unit_test( | ||
| 92 | %args, | ||
| 93 | file => $_, | ||
| 94 | type => /[.]pm/xsm ? 'module' : 'script', | ||
| 95 | name => $test_name, | ||
| 96 | ); | ||
| 97 | } | ||
| 98 | |||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | ######################################################################## | ||
| 103 | sub render_unit_test { | ||
| 104 | ######################################################################## | ||
| 105 | my (%args) = @_; | ||
| 106 | |||
| 107 | my ( $type, $path, $name, $test_number, $file ) | ||
| 108 | = @args{qw(type path name test_number file)}; | ||
| 109 | |||
| 110 | $test_number ||= 0; | ||
| 111 | |||
| 112 | my $unit_test = sprintf '%s/t/%02d-%s.t.in', $path, $test_number, $name; | ||
| 113 | |||
| 114 | if ( -e $unit_test ) { | ||
| 115 | WARN sprintf sprintf 'unit test %s for %s already exists...skipping', | ||
| 116 | $unit_test, $args{file}; | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | TRACE sprintf 'rendering unit test for %s to %s', $args{file}, $unit_test; | ||
| 120 | |||
| 121 | if ( $type eq 'module' ) { | ||
| 122 | $name = $file; | ||
| 123 | $name =~ s/\//::/xsmg; | ||
| 124 | $name =~ s/[.].*$//xsm; | ||
| 125 | } | ||
| 126 | |||
| 127 | render_tt_template( | ||
| 128 | { template => sprintf( '00-%s.t.tt', $type ), | ||
| 129 | parameters => { $type => $name }, | ||
| 130 | outfile => $unit_test, | ||
| 131 | } | ||
| 132 | ); | ||
| 133 | } | ||
| 134 | |||
| 135 | return; | ||
| 136 | } | ||
| 137 | |||
| 138 | # creates unit_test.pl, unit_tests.cgi unit_tests.pm | ||
| 139 | ######################################################################## | ||
| 140 | sub create_unit_tests_list { | ||
| 141 | ######################################################################## | ||
| 142 | my ($options) = @_; | ||
| 143 | |||
| 144 | my $perl_path = sprintf '%s/src/main/perl', $options->{root}; | ||
| 145 | |||
| 146 | my $unit_tests = {}; | ||
| 147 | |||
| 148 | for my $p ( pairs qw( cgi-bin cgi bin pl lib pm ) ) { | ||
| 149 | my ( $subdir, $type ) = @{$p}; | ||
| 150 | |||
| 151 | my $path = sprintf '%s/%s/t', $perl_path, $subdir; | ||
| 152 | |||
| 153 | DEBUG sprintf sprintf 'looking for unit tests in %s', $path; | ||
| 154 | |||
| 155 | my @tests = find_files_of_type( | ||
| 156 | path => $path, | ||
| 157 | type => 't', | ||
| 158 | ); | ||
| 159 | |||
| 160 | $unit_tests->{$type} = [ map { 't/' . basename $_ } @tests ]; | ||
| 161 | } | ||
| 162 | |||
| 163 | $options->{unit_tests} = $unit_tests; | ||
| 164 | |||
| 165 | return $unit_tests; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | |||
| 170 | __END__ |