| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.010; | ||
| 6 | use autodie qw(:all); | ||
| 7 | no indirect ':fatal'; | ||
| 8 | |||
| 9 | use version ; our $VERSION = qv('1.1.1'); | ||
| 10 | |||
| 11 | use Carp; | ||
| 12 | |||
| 13 | use VSGDR::UnitTest::TestSet::Test; | ||
| 14 | use VSGDR::UnitTest::TestSet::Test::TestCondition; | ||
| 15 | use VSGDR::UnitTest::TestSet::Representation; | ||
| 16 | |||
| 17 | use VSGDR::UnitTest::TestSet::Resx; | ||
| 18 | |||
| 19 | use Getopt::Euclid qw( :vars<opt_> ); | ||
| 20 | use List::MoreUtils qw{firstidx} ; | ||
| 21 | use Data::Dumper; | ||
| 22 | use Smart::Comments; | ||
| 23 | use File::Basename; | ||
| 24 | |||
| 25 | my %ValidParserMakeArgs = ( vb => "NET::VB" | ||
| 26 | , cs => "NET::CS" | ||
| 27 | , xls => "XLS" | ||
| 28 | , xml => "XML" | ||
| 29 | ) ; | ||
| 30 | |||
| 31 | my %ValidParserMakeArgs2 = ( vb => "NET2::VB" | ||
| 32 | , cs => "NET2::CS" | ||
| 33 | ) ; | ||
| 34 | |||
| 35 | #my @validSuffixes = keys %ValidParserMakeArgs ; | ||
| 36 | #my @validSuffixes = map { '.'.$_ } keys %ValidParserMakeArgs ; | ||
| 37 | my @validSuffixes = map { '.'.$_ } keys %ValidParserMakeArgs ; | ||
| 38 | |||
| 39 | #TODO: 1. Init and cleanup test code still seems to go missing. | ||
| 40 | #TODO: 2. Fix the dangling $version variable | ||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | ### get and validate parameters | ||
| 45 | |||
| 46 | warn 'may break parsable output'; | ||
| 47 | |||
| 48 | our $opt_infile; | ||
| 49 | our $opt_outfile; | ||
| 50 | our $opt_testname; | ||
| 51 | |||
| 52 | croak 'no input file' unless defined($opt_infile); | ||
| 53 | croak 'no output file' unless defined($opt_outfile); | ||
| 54 | croak 'no test name expression' unless defined($opt_testname); | ||
| 55 | |||
| 56 | my $inFile = $opt_infile ; | ||
| 57 | my $outFile = $opt_outfile ; | ||
| 58 | |||
| 59 | my($infname, $directories, $insfx) = fileparse($inFile, @validSuffixes); | ||
| 60 | croak 'Invalid input file' unless defined $insfx ; | ||
| 61 | $insfx = lc $insfx ; | ||
| 62 | $insfx = substr(lc $insfx,1) ; | ||
| 63 | |||
| 64 | my($outfname, $outdirectories, $outsfx) = fileparse($outFile, @validSuffixes); | ||
| 65 | croak 'Invalid output file' unless defined $outsfx ; | ||
| 66 | $outsfx = lc $outsfx ; | ||
| 67 | $outsfx = substr(lc $outsfx,1) ; | ||
| 68 | |||
| 69 | my $outResxFile = "${outfname}.resx" ; | ||
| 70 | |||
| 71 | croak 'Invalid input file' unless exists $ValidParserMakeArgs{$insfx} ; | ||
| 72 | croak 'Invalid output file' unless exists $ValidParserMakeArgs{$outsfx} ; | ||
| 73 | |||
| 74 | ### check output files can be written to | ||
| 75 | # yes so it's a race-condition anyway | ||
| 76 | |||
| 77 | croak 'Output resource file cannot be written to' unless -f $outResxFile or ! -e $outResxFile ; | ||
| 78 | |||
| 79 | my $qr_testname = qr{$opt_testname} ; | ||
| 80 | |||
| 81 | ### build parsers | ||
| 82 | |||
| 83 | |||
| 84 | my %Parsers = () ; | ||
| 85 | $Parsers{${insfx}} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs{${insfx}} } ); | ||
| 86 | # if input is in a .net language, add in a .net2 parser to the list | ||
| 87 | if ( firstidx { $_ eq ${insfx} } ['cs','vb'] != -1 ) { | ||
| 88 | $Parsers{"${insfx}2"} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs2{${insfx}} } ); | ||
| 89 | } | ||
| 90 | # if output is needed in ssdt unit test format add in a .net2 parser to the list | ||
| 91 | if ($version == 1) { | ||
| 92 | $Parsers{${outsfx}} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs{${outsfx}} } ); | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | $Parsers{"${outsfx}2"} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs2{${outsfx}} } ); | ||
| 96 | } | ||
| 97 | |||
| 98 | ### build internal representations of input | ||
| 99 | |||
| 100 | my $o_resx = VSGDR::UnitTest::TestSet::Resx->new() ; | ||
| 101 | |||
| 102 | my $testSet = undef ; | ||
| 103 | eval { | ||
| 104 | $testSet = $Parsers{$insfx}->deserialise($inFile); | ||
| 105 | } ; | ||
| 106 | if ( not defined $testSet ) { | ||
| 107 | if ( exists $Parsers{"${insfx}2"}) { | ||
| 108 | eval { | ||
| 109 | $testSet = $Parsers{$insfx}->deserialise($inFile); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | croak 'Parsing failed.'; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | my $resx_data = ''; { $/ = undef ; open ( my $fh, "<", "${infname}.resx"); $resx_data = <$fh> ; close $fh } ; | ||
| 118 | my $rh_testScripts = $o_resx->parse($resx_data) ; | ||
| 119 | |||
| 120 | |||
| 121 | my $ra_tests = $testSet->tests() ; | ||
| 122 | my @filteredTests = grep { my $s=$_->testName() ; $s !~ ${qr_testname} && $_ ; } @$ra_tests ; | ||
| 123 | |||
| 124 | |||
| 125 | ### filter input to output | ||
| 126 | |||
| 127 | |||
| 128 | my $filteredTestSet = VSGDR::UnitTest::TestSet->new( { NAMESPACE => $testSet->nameSpace() | ||
| 129 | , CLASSNAME => $testSet->className() | ||
| 130 | } | ||
| 131 | ) ; | ||
| 132 | $filteredTestSet->initializeConditions($testSet->initializeConditions) ; | ||
| 133 | $filteredTestSet->cleanupConditions($testSet->cleanupConditions) ; | ||
| 134 | $filteredTestSet->tests(\@filteredTests) ; | ||
| 135 | |||
| 136 | unlink $outFile if -f $outFile ; | ||
| 137 | |||
| 138 | if ($version == 1) { | ||
| 139 | $Parsers{$outsfx}->serialise($outFile,$filteredTestSet); | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | $Parsers{"${outsfx}2"}->serialise($outFile,$filteredTestSet); | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | my $o_resx_clone = $o_resx->clone() ; | ||
| 147 | |||
| 148 | my %filtered_testScripts = () ; | ||
| 149 | map { my $s=$_->testActionLiteralName() ; exists $$rh_testScripts{$s} && ( $filtered_testScripts{$s} = $$rh_testScripts{$s} ) ; | ||
| 150 | $s=$_->preTestActionLiteralName() ; exists $$rh_testScripts{$s} && ( $filtered_testScripts{$s} = $$rh_testScripts{$s} ) ; | ||
| 151 | $s=$_->postTestActionLiteralName() ; exists $$rh_testScripts{$s} && ( $filtered_testScripts{$s} = $$rh_testScripts{$s} ) ; | ||
| 152 | } grep { my $s=$_->testName() ; $s !~ ${qr_testname} && $s ; } | ||
| 153 | @$ra_tests ; | ||
| 154 | |||
| 155 | |||
| 156 | $o_resx_clone->scripts(\%filtered_testScripts); | ||
| 157 | unlink $outResxFile if -f $outResxFile ; | ||
| 158 | $o_resx_clone->serialise($outResxFile,$o_resx_clone); | ||
| 159 | |||
| 160 | ### end | ||
| 161 | |||
| 162 | exit ; | ||
| 163 | |||
| 164 | END {} | ||
| 165 | |||
| 166 | |||
| 167 | __END__ |