FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
reportClass.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
28 /*
29  * pattern is Starting < > Tests ... data followed by either OK or
30  * FAILURES! then results, skip a line then elapse time.
31  */
32 
33 class TestReport {
34  public $Date;
35  public $Time;
36  public $Svn;
37  protected $results;
38  private $testRuns;
39  private $smarty;
40  public $resultsFile = NULL;
41  public $resultsPath = NULL;
42 
43  public function __construct($Path=NULL, $notesPath=NULL) {
44 
45  // defaults for now...
46  $Latest = '/home/fosstester/public_html/TestResults/Data/Latest';
47 
48  if (empty ($Path)) {
49  /* Default is use data in Latest*/
50  $this->resultsPath = $Latest;
51  }
52  else if(is_dir($Path)) {
53  $this->resultsPath = $Path;
54  }
55  else if(is_file($Path)) {
56  $this->resultsFILE = $Path;
57  }
58 
59  } // __construct
60 
73  protected function getException($suite) {
74 
75  /*
76  * Execptions can be identified by ^Exception\s[0-9]+!
77  */
78  $matched = preg_match_all('/^Exception\s[0-9]+.*?$/m',$suite, $matches);
79  $pm = preg_match_all('/^Unexpected PHP error.*?$/m',$suite, $ematches);
80 
81  //print "DB: matched expections is:$matched\n";
82  //print "DB: matches are:\n";print_r($matches);
83  //print "DB: matched php is:$pm\n";
84  //print "DB: ematches are:\n";print_r($ematches);
85 
86  $elist = array();
87  foreach($matches as $ex){
88  foreach($ex as $except) {
89  foreach ($ematches as $ematch){
90  foreach($ematch as $estring){
91  $elist[$except] = $estring;
92  }
93  }
94  }
95  }
96  return($elist);
97  }
98 
112  protected function getFailures($suite) {
113  /*
114  * notes: errors just keep incrementing, and are demarcated with a ^nn)\s
115  * where nn is the failure number
116  *
117  */
118  //print "DB: suite is:\n";print_r($suite) . "\n";
119  //$matched = preg_match('/^[0-9]+\).*?$/m',$suite, $matches);
120  $matched = preg_match_all('/^[0-9]+\).*?$/m',$suite, $matches);
121  $exm = preg_match_all('/Expected:.*/', $suite, $expected);
122  $gm = preg_match_all('/Got:.*/', $suite, $got);
123  //print "DB: matched errors is:$matched\n";
124  //print "DB: matches are:\n";print_r($matches) . "\n";
125  // unwind the array of arrays preg_match_all returns
126  foreach($matches as $flist){
127  foreach($flist as $failure){
128  $failList[] = $failure;
129  }
130  }
131  if(!empty($expected))
132  {
133  foreach($expected as $elist)
134  {
135  foreach($elist as $expResult)
136  {
137  $failList[] = $expResult;
138  }
139  }
140  }
141  if(!empty($got))
142  {
143  foreach($got as $glist)
144  {
145  foreach($glist as $gotResult)
146  {
147  $failList[] = $gotResult;
148  }
149  }
150  }
151  return($failList);
152  }
153 
154 
155 
156 
166  /*
167  * pattern is Starting < > Tests ... data followed by either OK or
168  * FAILURES! then results, skip a line then elapse time.
169  */
170  public function parseResultsFile($file) {
171 
172  $results = array ();
173  if (empty ($file)) {
174  return FALSE;
175  }
176 
177  $failures = array();
178  $exceptions = array();
179 
180  $FD = fopen($file, 'r');
181  while ($line = fgets($FD, 1024)) {
182  if (preg_match('/^Running\sAll/', $line)){
183  $DateTime = $this->parseDateTime($line);
184  list ($this->Date, $this->Time) = $DateTime;
185  $svnline = preg_split('/:/', $line);
186  $this->Svn = $svnline[4];
187  //print "DB: top stuff is:\ndate:$this->Date\ntime:$this->Time\nsvn:$this->Svn\n";
188  }
189  elseif (preg_match('/^Starting.*?on:/', $line)) {
190  $aSuite = $this->getSuite($FD,$line);
191  $sum = $this->suiteSummary($aSuite);
192  list($pass, $fail, $except) = preg_split('/:/',$sum[1]);
193  //print "DB: pass, fail, except are:$pass,$fail,$except\n";
194  if($fail != 0) {
195  $failures = $this->getFailures($aSuite);
196  //print "DB: failure list is:\n";print_r($failures);
197  }
198  if($except != 0) {
199  $exceptions = $this->getException($aSuite);
200  //print "DB: exception list is:\n";print_r($exceptions);
201  }
202  // unroll the summary array into a key value array of 1 level
203  //print "DB: sum is:\n";print_r($sum) . "\n";
204  for($i=0; $i < count($sum); $i++) {
205  $summary[$sum[$i]] = array($sum[$i+1]);
206  $i++;
207  }
208  //print "DB: summary is:\n";print_r($summary) . "\n";
209  if(empty($failures)) {
210  continue;
211  }
212  else {
213  $suite = $sum[0];
214  $summary[$suite][] = array('failures' => $failures);
215  $failures = array();
216  }
217  if(empty($exceptions)) {
218  continue;
219  }
220  else {
221  $suite = $sum[0];
222  $summary[$suite][] = array('exceptions' => $exceptions);
223  $exceptions = array();
224  }
225  }
226  else {
227  continue;
228  }
229  } // while
230 
231  // return all 3
232  //print "summary is:\n";print_r($summary) . "\n";
233  return ($summary);
234  } // parseResultsFile
235 
247  protected function getResult($FD) {
248 
249  if(!is_resource($FD)) {
250  return(FALSE);
251  }
252  while($line = fgets($FD,1024)) {
253  $line = trim($line);
254  if(strcasecmp($line,'<----->') == 0) {
255  break; // all done
256  }
257  $result .= $line .' ';
258  }
259  return($result);
260  }
261 
274  protected function getSuite($FD,$line) {
275 
276  $suite = null;
277 
278  /* Save the initial line, it's the start of the suite! */
279  $suite .= $line;
280 
281  /*
282  Save every line, looking for the end of the run, marked by either an OK or FAILURES key
283  word. Then save the last lines and return.
284  */
285 
286  while ($line = fgets($FD, 1024)) {
287  if (preg_match('/^OK/', $line) || preg_match('/^FAILURES/', $line)) {
288  $line = fgets($FD, 1024);
289  if (preg_match('/^Test cases run:/', $line))
290  $suite .= $line;
291  $tossme = fgets($FD, 1024);
292  $line = fgets($FD, 1024);
293  $suite .= $line;
294  //print "DB: suite is:\n$suite\n";
295  return($suite);
296  }
297  else {
298  $suite .= $line;
299  }
300  }
301  } // getSuite
314  function globdata($results, $moData)
315  {
316  $dataSize = count($moData);
317  for ($suite = 0; $suite <= $dataSize; $suite += 3)
318  {
319  if (($suite +2) > $dataSize)
320  {
321  break;
322  }
323 
324  $suiteName = $this->parseSuiteName($moData[$suite]);
325  array_push($results, $suiteName);
326  //print "parsed suite name:$suiteName\n";
327 
328  $pfe_results = $this->parseResults($moData[$suite +1]);
329  $pfe = split(':', $pfe_results);
330  array_push($results, $pfe[0]);
331  array_push($results, $pfe[1]);
332  array_push($results, $pfe[2]);
333  //print "<pre>BD-GD: resutlts are:</pre>\n"; print "<pre>"; print_r($results) . "</pre>\n";
334 
335  $etime = $this->parseElapseTime($moData[$suite +2]);
336  array_push($results, $etime);
337  //print "The elapse time was:$etime\n\n";
338  }
339  return ($results);
340  } //globdata
341 
352  private function parseDateTime($line)
353  {
354  //print "<pre>DB:PDT: line is:\n$line</pre>\n";
355  if (empty ($line))
356  {
357  return array ();
358  }
359  $pat = '.*?s\son:(.*?)\sat\s(.*?)\s';
360  $matches = preg_match("/$pat/", $line, $matched);
361  $dateTime[] = $matched[1];
362  $dateTime[] = $matched[2];
363  //print "matched is:\n"; print_r($matched) . "\n";
364  return ($dateTime);
365  }
366 
378  public function parseLicenseResults($FD) {
379 
380  if(!is_resource($FD)) {
381  return(FALSE);
382  }
383 
384  $All = array();
385  $FileName = array();
386  $LicenseType = array();
387  $VettedName = array();
388  $results = array();
389 
390  while($line = $this->getResult($FD)){
391  //$line = getResult($FD);
392  $resultParts = split(';',$line);
393  list($lKey,$licenseType) = split('=',$resultParts[0]);
394  list($fnKey,$fileName) = split('=',$resultParts[1]);
395  $FileName[] = rtrim($fileName,'.txt');
396  $LicenseType[$licenseType] = $FileName;
397  //print "PLR: before = split results is:{$resultParts[1]}\n<br>";
398  list($fnKey,$std) = split('=',$resultParts[1]);
399  $VettedName[] = str_replace(',',",<br>",$std);
400  list($pKey,$pass) = split('=',$resultParts[2]);
401  $results[] = str_replace(',',",<br>",$pass);
402  list($fKey,$fail) = split('=',$resultParts[3]);
403  $results[] = str_replace(',',",<br>",$fail);
404  }
405  $All[] = $LicenseType;
406  $All[] = $VettedName;
407  $All[] = $results;
408  return($All);
409  }
410 
420  public function parseLicenseTotals($FD) {
421 
422  if(is_resource($FD)) {
423  $agent = array();
424  $pass = array();
425  $fail = array();
426 
427  while (!feof($FD)) {
428  $line = trim(fgets($FD, 1024));
429  list($agent[],$pass[],$fail[]) = explode(':',$line);
430  }
431  fclose($FD);
432  return(array($agent,$pass,$fail));
433  }
434  else {
435  return(FALSE);
436  }
437  } // parseLicenseTotals
438 
449  private function parseSuiteName($string) {
450  if (empty ($string))
451  {
452  return (FALSE);
453  }
454  $pat = '^Starting\s(.*?)\son:';
455  $matches = preg_match("/$pat/", $string, $matched);
456  //print "<pre>matched is:<pre>\n"; print_r($matched) . "\n";
457  return ($matched[1]);
458  }
459 
471  private function parseResults($string)
472  {
473  if (empty ($string))
474  {
475  return (FALSE);
476  }
477  //$pat = '.*?(Passes):(.*?).\s(Failures):\s(.*?).+(Exceptions):\s(.*)';
478  $pat = '.*?(Passes):\s(.*?),\s(Failures):\s(.*?),\s(Exceptions):\s(.*)';
479  $matches = preg_match("/$pat/", $string, $matched);
480  $results = array ();
481  if ($matches)
482  {
483  $results[$matched[1]] = $matched[2];
484  $results[$matched[3]] = $matched[4];
485  $results[$matched[5]] = $matched[6];
486  $res = $matched[2] . ":" . $matched[4] . ":" . $matched[6];
487  }
488  //return ($results);
489  return ($res);
490  }
491 
501  private function parseElapseTime($string)
502  {
503  if (empty ($string))
504  {
505  return (FALSE);
506  }
507  $parts = array ();
508  $pat = '.+took\s(.*?)\sto\srun$';
509  $matches = preg_match("/$pat/", $string, $matched);
510  //print "the array looks like:\n"; print_r($matched) . "\n";
511  $parts = split(' ', $matched[1]);
512  //print "split array looks like:\n"; print_r($parts) . "\n";
513  //$time = 'infinity';
514  $sizep = count($parts);
515  $etime = NULL;
516  for ($i = 0; $i < $sizep; $i++)
517  {
518  $etime .= $parts[$i] . substr($parts[$i +1], 0, 1) . ":";
519  $i++;
520  }
521  $etime = rtrim($etime, ':');
522  return ($etime);
523  }
524 
525 
540  public function suiteSummary($suite) {
541  $suiteName = $this->parseSuiteName($suite);
542  $results = $this->parseResults($suite);
543  //print "DB: suiteName is:$suiteName\n";
544  //print "DB: results is:$results\n";
545  return(array($suiteName,$results));
546  }
547 }
548 ?>
getSuite($FD, $line)
getFailures($suite)
parseResults($string)
suiteSummary($suite)
getException($suite)
Definition: reportClass.php:73
parseResultsFile($file)
parseLicenseResults($FD)
globdata($results, $moData)
parseElapseTime($string)
parseDateTime($line)
parseLicenseTotals($FD)
parseSuiteName($string)
getResult($FD)
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695