tsrc/unittestrunner/unittest_ctc.pl
changeset 22 496ad160a278
equal deleted inserted replaced
15:ccd8e69b5392 22:496ad160a278
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 #
       
    16 #!/usr/bin/perl
       
    17 
       
    18 use File::Copy;
       
    19 use Cwd;
       
    20 use Getopt::Long;
       
    21 use XML::Parser::Expat;
       
    22 
       
    23 #---------------------------------------Initialization------------------------------------------------------#
       
    24 
       
    25 $projectrootname = "/"; #is set to correct at run-time 
       
    26 $projectdrive = "z:"; #is set to correct at run-time 
       
    27 
       
    28 $eunitUnitTestRunner = "eunittest_ctc.pl";
       
    29 $scriptLocation = "mmsharinguis/tsrc/unittestrunner";
       
    30 $coverageResultsDirDefault = "mmsharinguis/tsrc/unittestrunner/results/";
       
    31 $buildResults = "BuildResults.txt";
       
    32 $coverageDat = "MON.dat";
       
    33 $coverageSymbols = "MON.sym";
       
    34 $coverageProfile = "profile.txt";
       
    35 
       
    36 $eunitResultsFile = "EUnitLog.xml";
       
    37 $coverageResultsFile = "CTCHTML/index.html";
       
    38 $finalResultsFile = "AllResults.html";
       
    39 $cssFile = "CTCHTML/ctc.css";
       
    40 $cssLink = "<link rel=\"stylesheet\"";
       
    41 
       
    42 $xmlDllLine = "dll";
       
    43 $xmlResultLine = "result";
       
    44 $xmlCaseFailed = "FAIL";
       
    45 $xmlCasePassed = "OK";
       
    46 $outputString = "";
       
    47 $outputFileBodyStart = "<body";
       
    48 
       
    49 $totalCount = 0;
       
    50 $passedCount = 0;
       
    51 $failedCount = 0;
       
    52 
       
    53 # from command line
       
    54 my ($param_noclean,
       
    55 		$eunitDlls,
       
    56 		$allocFailure,
       
    57 		$coverageResultsDir);
       
    58 
       
    59 #---------------------------------------Main Start----------------------------------------------------------#
       
    60 
       
    61 # read command line parameters
       
    62 my $result = GetOptions("noclean" 			  => \$param_noclean,
       
    63 												"eunitdlls:s"	    => \$eunitDlls,
       
    64 												"allocfailure:i"  => \$allocFailure,
       
    65 												"results:s"       => \$coverageResultsDir,
       
    66 												"help"						=> \$help);
       
    67 												
       
    68 $startdir = cwd;
       
    69 
       
    70 if (defined $help)
       
    71 {
       
    72 	print_help();
       
    73 	exit;
       
    74 }
       
    75 
       
    76 # find project root
       
    77 findProjectDriveAndRoot();
       
    78 
       
    79 # create directory for results
       
    80 if ( defined $coverageResultsDir )
       
    81 {
       
    82     if ($coverageResultsDir =~ /^(.:)/)
       
    83     {
       
    84         print("Drive name given in results dir arg\n");
       
    85     }
       
    86     else
       
    87     {  
       
    88         $coverageResultsDir = "$projectdrive" . "/" . "$coverageResultsDir";
       
    89     }
       
    90 }
       
    91 else
       
    92 {
       
    93     $coverageResultsDir = "$projectdrive$projectrootname$coverageResultsDirDefault";
       
    94 }
       
    95 print ("Writing results to $coverageResultsDir \n\n");
       
    96 if (mkdir("$coverageResultsDir", 0755))
       
    97 {
       
    98     print("The results directory was created successfully! \n");
       
    99 } 
       
   100 else 
       
   101 {
       
   102     print("The results directory already exists. \n");
       
   103 } 
       
   104 
       
   105 # set target for intrumentation result
       
   106 $ENV{'CTC_DATA_PATH'}= "$coverageResultsDir";	
       
   107 
       
   108 my $eunit_command = "$eunitUnitTestRunner -drive=$projectdrive -root=$projectrootname -results=$coverageResultsDir -eunitdlls=$eunitDlls -allocfailure=$allocFailure";
       
   109 if (!defined $param_noclean)
       
   110 {
       
   111 	# clear previous results
       
   112 	print("Cleaning previous results. \n");
       
   113 	unlink("$coverageResultsDir$buildResults");
       
   114 	unlink("$coverageResultsDir$coverageDat");
       
   115 	unlink("$coverageResultsDir$coverageSymbols");
       
   116 	unlink("$coverageResultsDir$coverageProfile");
       
   117 	unlink("$coverageResultsDir$finalResultsFile");
       
   118 }
       
   119 else
       
   120 { 	
       
   121 	$eunit_command .= " -noclean";
       
   122 }
       
   123 
       
   124 
       
   125 # run eunit tests
       
   126 system("$eunit_command");
       
   127 chdir($startdir);
       
   128 	
       
   129 
       
   130 # create textual output
       
   131 chdir("$coverageResultsDir");
       
   132 doSystemCall("ctcpost $coverageSymbols $coverageDat -p $coverageProfile -T 70");
       
   133 doSystemCall("ctc2html -i $coverageProfile -t 70");
       
   134 
       
   135 
       
   136 # clear target for intrumentation result
       
   137 $ENV{'CTC_DATA_PATH'}= "";	
       
   138 
       
   139 # combine all results
       
   140 #
       
   141 open(COVERAGE, "$coverageResultsDir$coverageResultsFile") or die("file $coverageResultsFile not found!\n");
       
   142 @coverageFileContent = <COVERAGE>;
       
   143 
       
   144 # eunit
       
   145 
       
   146 $eunitOutputString = "<span class=\"head1\">EUnit Results</span><br><br>\n";
       
   147 
       
   148 if ( -e "$coverageResultsDir$eunitResultsFile" )
       
   149 {
       
   150 	open(EUNITRESULTS,  "$coverageResultsDir$eunitResultsFile") or die ("Couldn't open $projectdrive$projectrootname$coverageResultsDir$eunitResultsFile!\n");
       
   151 	$parser = new XML::Parser::Expat;
       
   152 	$parser->setHandlers('Start' => \&sh,
       
   153                      'End'   => \&eh,
       
   154                      'Char'  => \&ch);
       
   155 	$totalCount = 0;
       
   156 	$passedCount = 0;
       
   157 	$failedCount = 0;
       
   158 		
       
   159 	$parser->parse(*EUNITRESULTS);
       
   160 		
       
   161 	close(EUNITRESULTS);
       
   162 }
       
   163 else
       
   164 {
       
   165 	print ("file $eunitResultsFile not found!\n");
       
   166 }
       
   167 
       
   168 
       
   169 # append coverage information after eunit results
       
   170 
       
   171 for ($j = 0; $j <= $#coverageFileContent; $j++)
       
   172 	{
       
   173 		$currentLine = @coverageFileContent[$j];
       
   174 		
       
   175 		if ($currentLine =~ /$cssLink/)
       
   176 		{
       
   177 			if ( open(CSSFILE,  "$coverageResultsDir$cssFile") )
       
   178 			{
       
   179 				# append css styles to results html
       
   180 				$outputString .= "<style type=\"text/css\"> body {";
       
   181 				@cssFileContent = <CSSFILE>;
       
   182 				my($line);
       
   183 				foreach $line (@cssFileContent)
       
   184 				{ 
       
   185 					$outputString .= $line;
       
   186 				}
       
   187 				$outputString .= "}</style>\n";
       
   188 				close(CSSFILE);
       
   189 			}
       
   190 		}
       
   191 		else
       
   192 		{
       
   193 			$outputString .= $currentLine;
       
   194 
       
   195 			if ($currentLine =~ /$outputFileBodyStart/)
       
   196 			{
       
   197 				$outputString .= $eunitOutputString;
       
   198 			}
       
   199 		}
       
   200 		
       
   201 	}
       
   202 	
       
   203 open(NEWRESULTSFILE, "> $coverageResultsDir$finalResultsFile") or die "Cannot open final results file\n";
       
   204 print NEWRESULTSFILE "$outputString";
       
   205 close(NEWRESULTSFILE);
       
   206 close(OUTPUT);
       
   207 close(COVERAGE);
       
   208 
       
   209 chdir($startdir);
       
   210 
       
   211 #---------------------------------------Main Ends-------------------------------------------------------------#
       
   212 
       
   213 sub findProjectDriveAndRoot()
       
   214 {
       
   215 		$tempStartDir = $startdir;
       
   216 		if ($tempStartDir =~ m/$scriptLocation/)
       
   217 		{
       
   218 			# extract project root
       
   219 			for ($tempStartDir) {
       
   220 				 s/$scriptLocation+$//;
       
   221     	}
       
   222     	# divide it to drive and root
       
   223     	if ($tempStartDir =~ /^(.:)(.*)/)
       
   224     	{
       
   225     		$projectdrive = $1;
       
   226     		$projectrootname = $2;
       
   227 			}
       
   228 			print "project drive: $projectdrive \n";
       
   229 			print "project root: $projectrootname \n";
       
   230 		}
       
   231 		else
       
   232 		{
       
   233 			print "cannot determine project drive and root, use defaults!\n";
       
   234 		}
       
   235 }
       
   236 
       
   237 sub sh
       
   238 {
       
   239   my ($p, $el, %atts) = @_;
       
   240 	if ( $el eq $xmlDllLine )
       
   241 	{
       
   242 		$eunitOutputString .= "<span class=\"head3\">";
       
   243 		$dllName = %atts->{name};
       
   244 		@dllNameParts = split m!(\\)!, $dllName;
       
   245 		$dllName = $dllNameParts[$#dllNameParts];
       
   246 		$eunitOutputString .= "$dllName => </span>";
       
   247 		$totalCount = %atts->{size};
       
   248 	}
       
   249 	
       
   250 	if ( $el eq $xmlResultLine )
       
   251 	{
       
   252 		$status = %atts->{status};
       
   253 		if ( $status eq $xmlCasePassed )
       
   254 		{
       
   255 			$passedCount++;
       
   256 		}
       
   257 		else
       
   258 		{
       
   259 			$failedCount++;
       
   260 		}
       
   261 	}
       
   262 }
       
   263 
       
   264 sub eh
       
   265 {
       
   266 	my ($p, $el) = @_;
       
   267 	if ( $el eq $xmlDllLine )
       
   268 	{
       
   269 		$totalCount = $passedCount + $failedCount;
       
   270 		if ( $failedCount > 0 || $totalCount == 0 )
       
   271 		{
       
   272 			$eunitOutputString .= "<span class=\"red\">Testcases passed/run: $passedCount/$totalCount </span><br>\n";
       
   273 		}
       
   274 		else
       
   275 		{
       
   276 			$eunitOutputString .= "<span class=\"blue\">Testcases passed/run: $passedCount/$totalCount </span><br>\n";
       
   277 		}
       
   278 		$passedCount = 0;	
       
   279 		$failedCount = 0;
       
   280 	}
       
   281 }
       
   282 
       
   283 sub ch
       
   284 {
       
   285 	my ($p, $el) = @_;
       
   286 	#print ("$el\n");
       
   287 }
       
   288 
       
   289 sub doSystemCall
       
   290 {
       
   291 	#print("\nDoing system call: $_[0]\n");
       
   292 	#system($_[0]);
       
   293 	system("echo Doing system call: $_[0] >> $coverageResultsDir$buildResults");
       
   294 	system("$_[0] >> $coverageResultsDir$buildResults 2>&1");
       
   295 }
       
   296 
       
   297 sub print_help
       
   298 {
       
   299 	print("\n*************************************************************\n\n");
       
   300 	print("Script runs by default all multimediasharing eunit tests\n");
       
   301 	print("and creates build, test and coverage results to:\n\n");
       
   302 	print("    $coverageResultsDirDefault\n\n");
       
   303 	print("Own configuration file (list of components to be processed)\n");
       
   304 	print("can be used as well:\n\n");
       
   305 	print("    >unittest_ctc.pl -eunitdlls=myowneunitcomponents.txt\n\n");
       
   306 	print("The text file should follow the syntax of default configuration file\n");
       
   307 	print("$scriptLocation/eunitdlls_ctc.txt\n\n");
       
   308 	print("Alloc failure simulation can be turned on with following command:\n\n");
       
   309 	print("    >unittest_ctc.pl -allocfailure=2\n\n");
       
   310 	print("Any number will turn eunit alloc on.\n");
       
   311 	print("When running just some set of components you might want to \"merge\" the results");
       
   312 	print("with existing coverage results.\n");
       
   313 	print("That is possible with command:\n\n");
       
   314 	print("    >unittest_ctc.pl -eunitdlls=myowneunitcomponents.txt -noclean\n");
       
   315 	print("\n*************************************************************\n");
       
   316 }