FOSSology  4.4.0
Open Source License Compliance by Open Source Software
runAgentUnit.php
1 #!/usr/bin/php
2 <?php
3 /*
4  SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
25 global $failures;
26 
35 function checkCUnit($fileName)
36 {
37 
38  if(empty($fileName))
39  {
40  return(array("Error! illegal input $fileName"));
41  }
42  try {
43  $verFail = check4CUnitFail($fileName);
44  if(!is_null($verFail))
45  {
46  //echo "DB: failues in check4CUnitFail\n";
47  //print_r($verFail) . "\n";
48  return($verFail);
49 
50  //$failures++;
51  }
52  }
53  catch (Exception $e)
54  {
55  return(array("Error! $e\n"));
56  }
57 } // checkCUnit
58 
66 function genCunitRep($fileName)
67 {
68  if(empty($fileName))
69  {
70  return(FALSE);
71  }
72  // get List or Run string so the correct xsl file is used.
73 
74  $xslFile = "CUnit-Run.xsl"; // default
75  if(preg_grep("/Results/", array($fileName)))
76  {
77  $xslFile = "CUnit-Run.xsl";
78  }
79  else if(preg_grep("/Listing/", array($fileName)))
80  {
81  $xslFile = "CUnit-List.xsl";
82  }
83  // remove .xml from name
84  $outFile = basename($fileName, '.xml');
85  $outPath = TESTROOT . "/reports/unit/$outFile.html";
86  $xslPath = "/usr/share/CUnit/$xslFile";
87  // remove the old HTML file before creating a new one.
88  $rmLast = exec("rm -rf $outPath", $rmOut, $rmRtn);
89  //echo "DB: Starting to generate html report for $fileName\n";
90  $report = genHtml($fileName, $outPath, $xslPath);
91  if(!empty($report))
92  {
93  echo "Error: Could not generate a HTML Test report from $fileName.\n";
94  return(FALSE);
95  }
96  //echo "DB: Generated html file:$outFile" . ".html\n";
97  return(TRUE);
98 }
99 
107 function processCUnit($unitTest)
108 {
109  global $failures;
110  global $other;
111 
112  $unitTest = preg_replace('#/#', '-', $unitTest);
113  $libphp = "lib-php";
115  if ($libphp === $unitTest) return NULL;
116 
117  if(empty($unitTest))
118  {
119  return("Error! no valid input at line " . __FILE__ . " at line " .
120  __LINE__ . "\n");
121  }
122 
123  foreach(glob("$unitTest*.xml") as $fName)
124  {
125  $fileName = lcfirst($fName);
126  // Skip Listing files
127  if(preg_grep("/Listing/", array($fileName)))
128  {
129  $rmlast = exec("rm $fileName", $rmOut, $rmRtn);
130  continue;
131  }
132  if(!tweakCUnit($fileName))
133  {
134  return("Error! could not save processed xml file, they may not display properly\n");
135  }
136 
137  $errors = array();
138  // defect: if the report is corrupt, checkCUnit will say everything is OK.
139  $errors = checkCUnit($fileName);
140  //echo "DB: after checkCUnit, errors for $unitTest are\n";print_r($errors) . "\n";
141  if(is_object($errors[0]))
142  {
143  $failures++;
144  echo "There were Unit Test Failures in $unitTest\n";
145  //print_r($errors) . "\n";
146  }
147  else if(!is_NULL($errors))
148  {
149  // if we can't even check the file, then skip making the report
150  $failures++;
151  return("Failure: Could not check file $fileName for failures is the file corrupt?\n");
152  }
153 
154  if(!genCunitRep($fileName))
155  {
156  return("Error!, could not generate html report for $unitTest\n");
157  }
158  } // foreach
159 
160  return(NULL);
161 } // processCUnit
162 
171 function tweakCUnit($fileName)
172 {
173  if(empty($fileName))
174  {
175  return(FALSE);
176  }
177 
178  //echo "DB: tweaking xml file:$fileName\n";
179  $rFile = file_get_contents($fileName);
180  // fix the Ref to xsl file
181  $pat = '#href="#';
182  $replace = 'href="http://fossology.usa.hp.com/~fossology/dtds/';
183  $rFile = preg_replace($pat, $replace, $rFile);
184  //fix the Ref to the dtds for both run and list files.
185  $runPat = '/CUnit-Run\.dtd/';
186  $rReplace = 'http://fossology.usa.hp.com/~fossology/dtds/CUnit-Run.dtd';
187  $listPat = '/CUnit-List\.dtd/';
188  $lReplace = 'http://fossology.usa.hp.com/~fossology/dtds/CUnit-List.dtd';
189  $rFile = preg_replace($runPat, $rReplace, $rFile);
190  $rFile = preg_replace($listPat, $lReplace, $rFile);
191  //echo "DB: rFile after preg_replace is:\n$rFile\n";
192  if(!file_put_contents($fileName, $rFile))
193  {
194  return(FALSE);
195  }
196  else
197  {
198  return(TRUE);
199  }
200 } // tweakCUnit
201 
202 
203 /* begin main program */
204 
205 if(!defined('TESTROOT'))
206 {
207  $path = __DIR__;
208  $plenth = strlen($path);
209  // remove /unit from the end.
210  $TESTROOT = substr($path, 0, $plenth-5);
211  $_ENV['TESTROOT'] = $TESTROOT;
212  putenv("TESTROOT=$TESTROOT");
213  define('TESTROOT',$TESTROOT);
214 }
215 
216 $WORKSPACE = NULL;
217 
218 if(array_key_exists('WORKSPACE', $_ENV))
219 {
220  $WORKSPACE = $_ENV['WORKSPACE'];
221  define('WORKSPACE', $WORKSPACE);
222 }
223 
224 $unit = TESTROOT . "/unit";
225 
226 if(@chdir($unit) === FALSE)
227 {
228  echo "FATAL!, could not cd to:\n$unit\n";
229  exit(1);
230 }
231 require_once('../lib/bootstrap.php');
232 require_once('../lib/common-Report.php');
233 require_once('../lib/common-Test.php');
234 require_once('../lib/createRC.php');
235 
236 createRC();
237 $sc = getenv('SYSCONFDIR');
238 echo "DB: runUnit: sysconf from getenv is:$sc\n";
239 $sysConf = array();
240 $sysConf = bootstrap();
241 //echo "sysConf after bootstrap is:\n";print_r($sysConf) . "\n";
242 // export for other tests to use
243 
244 echo "DB: runUnit: globals:sysconfdir:{$GLOBALS['SYSCONFDIR']}\n";
245 putenv("SYSCONFDIR=$sc");
246 $_ENV['SYSCONFDIR'] = $sc;
247 
248 echo "DB: runUnit: after putenv SYSCONFDIR from env is:" . getenv('SYSCONFDIR') . "\n";
249 echo "DB: runUnit: after _ENV set, SYSCONFDIR from _ENV is:{$_ENV['SYSCONFDIR']}\n";
250 
251 $modules = array();
252 $unitList = array();
253 
254 // get the list of unit tests to run and flatten the array
255 $modules = parse_ini_file('../dataFiles/unitTests.ini',1);
256 foreach($modules as $key => $value)
257 {
258  $unitList[] = $key;
259 }
260 
261 global $unitList;
262 
263 // TODO: Uncertain, relative paths are BAD. Fix this
264 /* at this point we should be in the directory fossology/src/testing/unit/
265  So let's change directories back to fossology/src/
266  (i.e. one directory above fossology/src/testing/) */
267 backToParent('../..');
268 
269 $failures = 0;
270 foreach($unitList as $unitTest)
271 {
272  echo "\n";
273  echo "$unitTest:\n";
274  $other = substr($unitTest, 0, 3);
275  if($other == 'lib' || $other == 'cli')
276  {
277  if(@chdir($unitTest . '/tests') === FALSE)
278  {
279  echo "Error! cannot cd to " . $unitTest . "/tests, skipping test\n";
280  $failures++;
281  continue;
282  }
283  }
284  else
285  {
286  if(@chdir($unitTest . '/agent_tests/Unit') === FALSE)
287  {
288  echo "Error! cannot cd to " . $unitTest . "/agent_tests/Unit, skipping test\n";
289  $failures++;
290  continue;
291  }
292  }
293  $Make = new RunTest($unitTest);
294  $runResults = $Make->MakeTest();
295  //debugprint($runResults, "run results for $unitTest\n");
296  $Make->printResults($runResults);
297  if(processCUnit($unitTest) != NULL)
298  {
299  echo "Error: could not process cunit results file for $unitTest\n";
300  }
301  if(MakeCover($unitTest) != NULL)
302  {
303  //echo "Error: there were errors for make coverage for $unitTest\n";
304  $failures++;
305  }
306  backToParent('../../..');
307 } // foreach
308 
309 // clean up xml files left behind.
310 //cleanXMLFiles();
311 if($failures)
312 {
313  exit(1);
314 }
315 exit(0);
class for making an agent unit or functional test
bootstrap($sysconfdir="")
Bootstrap the fossology php library.
Definition: migratetest.php:82