FOSSology  4.4.0
Open Source License Compliance by Open Source Software
reportClass.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
17 /*
18  * pattern is Starting < > Tests ... data followed by either OK or
19  * FAILURES! then results, skip a line then elapse time.
20  */
21 
22 class TestReport {
23  public $Date;
24  public $Time;
25  public $Svn;
26  protected $results;
27  private $testRuns;
28  private $smarty;
29  public $resultsFile = NULL;
30  public $resultsPath = NULL;
31 
32  public function __construct($Path=NULL, $notesPath=NULL) {
33 
34  // defaults for now...
35  $Latest = '/home/fosstester/public_html/TestResults/Data/Latest';
36 
37  if (empty ($Path)) {
38  /* Default is use data in Latest*/
39  $this->resultsPath = $Latest;
40  }
41  else if(is_dir($Path)) {
42  $this->resultsPath = $Path;
43  }
44  else if(is_file($Path)) {
45  $this->resultsFILE = $Path;
46  }
47 
48  } // __construct
49 
62  protected function getException($suite) {
63 
64  /*
65  * Execptions can be identified by ^Exception\s[0-9]+!
66  */
67  $matched = preg_match_all('/^Exception\s[0-9]+.*?$/m',$suite, $matches);
68  $pm = preg_match_all('/^Unexpected PHP error.*?$/m',$suite, $ematches);
69 
70  //print "DB: matched expections is:$matched\n";
71  //print "DB: matches are:\n";print_r($matches);
72  //print "DB: matched php is:$pm\n";
73  //print "DB: ematches are:\n";print_r($ematches);
74 
75  $elist = array();
76  foreach($matches as $ex){
77  foreach($ex as $except) {
78  foreach ($ematches as $ematch){
79  foreach($ematch as $estring){
80  $elist[$except] = $estring;
81  }
82  }
83  }
84  }
85  return($elist);
86  }
87 
101  protected function getFailures($suite) {
102  /*
103  * notes: errors just keep incrementing, and are demarcated with a ^nn)\s
104  * where nn is the failure number
105  *
106  */
107  //print "DB: suite is:\n";print_r($suite) . "\n";
108  //$matched = preg_match('/^[0-9]+\‍).*?$/m',$suite, $matches);
109  $matched = preg_match_all('/^[0-9]+\‍).*?$/m',$suite, $matches);
110  $exm = preg_match_all('/Expected:.*/', $suite, $expected);
111  $gm = preg_match_all('/Got:.*/', $suite, $got);
112  //print "DB: matched errors is:$matched\n";
113  //print "DB: matches are:\n";print_r($matches) . "\n";
114  // unwind the array of arrays preg_match_all returns
115  foreach($matches as $flist){
116  foreach($flist as $failure){
117  $failList[] = $failure;
118  }
119  }
120  if(!empty($expected))
121  {
122  foreach($expected as $elist)
123  {
124  foreach($elist as $expResult)
125  {
126  $failList[] = $expResult;
127  }
128  }
129  }
130  if(!empty($got))
131  {
132  foreach($got as $glist)
133  {
134  foreach($glist as $gotResult)
135  {
136  $failList[] = $gotResult;
137  }
138  }
139  }
140  return($failList);
141  }
142 
143 
144 
145 
155  /*
156  * pattern is Starting < > Tests ... data followed by either OK or
157  * FAILURES! then results, skip a line then elapse time.
158  */
159  public function parseResultsFile($file) {
160 
161  $results = array ();
162  if (empty ($file)) {
163  return FALSE;
164  }
165 
166  $failures = array();
167  $exceptions = array();
168 
169  $FD = fopen($file, 'r');
170  while ($line = fgets($FD, 1024)) {
171  if (preg_match('/^Running\sAll/', $line)){
172  $DateTime = $this->parseDateTime($line);
173  list ($this->Date, $this->Time) = $DateTime;
174  $svnline = preg_split('/:/', $line);
175  $this->Svn = $svnline[4];
176  //print "DB: top stuff is:\ndate:$this->Date\ntime:$this->Time\nsvn:$this->Svn\n";
177  }
178  elseif (preg_match('/^Starting.*?on:/', $line)) {
179  $aSuite = $this->getSuite($FD,$line);
180  $sum = $this->suiteSummary($aSuite);
181  list($pass, $fail, $except) = preg_split('/:/',$sum[1]);
182  //print "DB: pass, fail, except are:$pass,$fail,$except\n";
183  if($fail != 0) {
184  $failures = $this->getFailures($aSuite);
185  //print "DB: failure list is:\n";print_r($failures);
186  }
187  if($except != 0) {
188  $exceptions = $this->getException($aSuite);
189  //print "DB: exception list is:\n";print_r($exceptions);
190  }
191  // unroll the summary array into a key value array of 1 level
192  //print "DB: sum is:\n";print_r($sum) . "\n";
193  for($i=0; $i < count($sum); $i++) {
194  $summary[$sum[$i]] = array($sum[$i+1]);
195  $i++;
196  }
197  //print "DB: summary is:\n";print_r($summary) . "\n";
198  if(empty($failures)) {
199  continue;
200  }
201  else {
202  $suite = $sum[0];
203  $summary[$suite][] = array('failures' => $failures);
204  $failures = array();
205  }
206  if(empty($exceptions)) {
207  continue;
208  }
209  else {
210  $suite = $sum[0];
211  $summary[$suite][] = array('exceptions' => $exceptions);
212  $exceptions = array();
213  }
214  }
215  else {
216  continue;
217  }
218  } // while
219 
220  // return all 3
221  //print "summary is:\n";print_r($summary) . "\n";
222  return ($summary);
223  } // parseResultsFile
224 
236  protected function getResult($FD) {
237 
238  if(!is_resource($FD)) {
239  return(FALSE);
240  }
241  while($line = fgets($FD,1024)) {
242  $line = trim($line);
243  if(strcasecmp($line,'<----->') == 0) {
244  break; // all done
245  }
246  $result .= $line .' ';
247  }
248  return($result);
249  }
250 
263  protected function getSuite($FD,$line) {
264 
265  $suite = null;
266 
267  /* Save the initial line, it's the start of the suite! */
268  $suite .= $line;
269 
270  /*
271  Save every line, looking for the end of the run, marked by either an OK or FAILURES key
272  word. Then save the last lines and return.
273  */
274 
275  while ($line = fgets($FD, 1024)) {
276  if (preg_match('/^OK/', $line) || preg_match('/^FAILURES/', $line)) {
277  $line = fgets($FD, 1024);
278  if (preg_match('/^Test cases run:/', $line))
279  $suite .= $line;
280  $tossme = fgets($FD, 1024);
281  $line = fgets($FD, 1024);
282  $suite .= $line;
283  //print "DB: suite is:\n$suite\n";
284  return($suite);
285  }
286  else {
287  $suite .= $line;
288  }
289  }
290  } // getSuite
303  function globdata($results, $moData)
304  {
305  $dataSize = count($moData);
306  for ($suite = 0; $suite <= $dataSize; $suite += 3)
307  {
308  if (($suite +2) > $dataSize)
309  {
310  break;
311  }
312 
313  $suiteName = $this->parseSuiteName($moData[$suite]);
314  $results[] = $suiteName;
315  //print "parsed suite name:$suiteName\n";
316 
317  $pfe_results = $this->parseResults($moData[$suite +1]);
318  $pfe = explode(':', $pfe_results);
319  $results[] = $pfe[0];
320  $results[] = $pfe[1];
321  $results[] = $pfe[2];
322  //print "<pre>BD-GD: resutlts are:</pre>\n"; print "<pre>"; print_r($results) . "</pre>\n";
323 
324  $etime = $this->parseElapseTime($moData[$suite +2]);
325  $results[] = $etime;
326  //print "The elapse time was:$etime\n\n";
327  }
328  return ($results);
329  } //globdata
330 
341  private function parseDateTime($line)
342  {
343  //print "<pre>DB:PDT: line is:\n$line</pre>\n";
344  if (empty ($line))
345  {
346  return array ();
347  }
348  $pat = '.*?s\son:(.*?)\sat\s(.*?)\s';
349  $matches = preg_match("/$pat/", $line, $matched);
350  $dateTime[] = $matched[1];
351  $dateTime[] = $matched[2];
352  //print "matched is:\n"; print_r($matched) . "\n";
353  return ($dateTime);
354  }
355 
367  public function parseLicenseResults($FD) {
368 
369  if(!is_resource($FD)) {
370  return(FALSE);
371  }
372 
373  $All = array();
374  $FileName = array();
375  $LicenseType = array();
376  $VettedName = array();
377  $results = array();
378 
379  while($line = $this->getResult($FD)){
380  //$line = getResult($FD);
381  $resultParts = explode(';',$line);
382  list($lKey,$licenseType) = explode('=',$resultParts[0]);
383  list($fnKey,$fileName) = explode('=',$resultParts[1]);
384  $FileName[] = rtrim($fileName,'.txt');
385  $LicenseType[$licenseType] = $FileName;
386  //print "PLR: before = explode results is:{$resultParts[1]}\n<br>";
387  list($fnKey,$std) = explode('=',$resultParts[1]);
388  $VettedName[] = str_replace(',',",<br>",$std);
389  list($pKey,$pass) = explode('=',$resultParts[2]);
390  $results[] = str_replace(',',",<br>",$pass);
391  list($fKey,$fail) = explode('=',$resultParts[3]);
392  $results[] = str_replace(',',",<br>",$fail);
393  }
394  $All[] = $LicenseType;
395  $All[] = $VettedName;
396  $All[] = $results;
397  return($All);
398  }
399 
409  public function parseLicenseTotals($FD) {
410 
411  if(is_resource($FD)) {
412  $agent = array();
413  $pass = array();
414  $fail = array();
415 
416  while (!feof($FD)) {
417  $line = trim(fgets($FD, 1024));
418  list($agent[],$pass[],$fail[]) = explode(':',$line);
419  }
420  fclose($FD);
421  return(array($agent,$pass,$fail));
422  }
423  else {
424  return(FALSE);
425  }
426  } // parseLicenseTotals
427 
438  private function parseSuiteName($string) {
439  if (empty ($string))
440  {
441  return (FALSE);
442  }
443  $pat = '^Starting\s(.*?)\son:';
444  $matches = preg_match("/$pat/", $string, $matched);
445  //print "<pre>matched is:<pre>\n"; print_r($matched) . "\n";
446  return ($matched[1]);
447  }
448 
460  private function parseResults($string)
461  {
462  if (empty ($string))
463  {
464  return (FALSE);
465  }
466  //$pat = '.*?(Passes):(.*?).\s(Failures):\s(.*?).+(Exceptions):\s(.*)';
467  $pat = '.*?(Passes):\s(.*?),\s(Failures):\s(.*?),\s(Exceptions):\s(.*)';
468  $matches = preg_match("/$pat/", $string, $matched);
469  $results = array ();
470  if ($matches)
471  {
472  $results[$matched[1]] = $matched[2];
473  $results[$matched[3]] = $matched[4];
474  $results[$matched[5]] = $matched[6];
475  $res = $matched[2] . ":" . $matched[4] . ":" . $matched[6];
476  }
477  //return ($results);
478  return ($res);
479  }
480 
490  private function parseElapseTime($string)
491  {
492  if (empty ($string))
493  {
494  return (FALSE);
495  }
496  $parts = array ();
497  $pat = '.+took\s(.*?)\sto\srun$';
498  $matches = preg_match("/$pat/", $string, $matched);
499  //print "the array looks like:\n"; print_r($matched) . "\n";
500  $parts = explode(' ', $matched[1]);
501  //print "explode array looks like:\n"; print_r($parts) . "\n";
502  //$time = 'infinity';
503  $sizep = count($parts);
504  $etime = NULL;
505  for ($i = 0; $i < $sizep; $i++)
506  {
507  $etime .= $parts[$i] . substr($parts[$i +1], 0, 1) . ":";
508  $i++;
509  }
510  $etime = rtrim($etime, ':');
511  return ($etime);
512  }
513 
514 
529  public function suiteSummary($suite) {
530  $suiteName = $this->parseSuiteName($suite);
531  $results = $this->parseResults($suite);
532  //print "DB: suiteName is:$suiteName\n";
533  //print "DB: results is:$results\n";
534  return(array($suiteName,$results));
535  }
536 }
getException($suite)
Definition: reportClass.php:62
parseLicenseTotals($FD)
parseLicenseResults($FD)
parseResultsFile($file)
getSuite($FD, $line)
parseDateTime($line)
globdata($results, $moData)
getResult($FD)
getFailures($suite)
suiteSummary($suite)
parseSuiteName($string)
parseElapseTime($string)
parseResults($string)
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308