FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
runAgentUnit.php
1 #!/usr/bin/php
2 <?php
3 /*
4  Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
36 global $failures;
37 
46 function checkCUnit($fileName)
47 {
48 
49  if(empty($fileName))
50  {
51  return(array("Error! illegal input $fileName"));
52  }
53  try {
54  $verFail = check4CUnitFail($fileName);
55  if(!is_null($verFail))
56  {
57  //echo "DB: failues in check4CUnitFail\n";
58  //print_r($verFail) . "\n";
59  return($verFail);
60 
61  //$failures++;
62  }
63  }
64  catch (Exception $e)
65  {
66  return(array("Error! $e\n"));
67  }
68 } // checkCUnit
69 
77 function genCunitRep($fileName)
78 {
79  if(empty($fileName))
80  {
81  return(FALSE);
82  }
83  // get List or Run string so the correct xsl file is used.
84 
85  $xslFile = "CUnit-Run.xsl"; // default
86  if(preg_grep("/Results/", array($fileName)))
87  {
88  $xslFile = "CUnit-Run.xsl";
89  }
90  else if(preg_grep("/Listing/", array($fileName)))
91  {
92  $xslFile = "CUnit-List.xsl";
93  }
94  // remove .xml from name
95  $outFile = basename($fileName, '.xml');
96  $outPath = TESTROOT . "/reports/unit/$outFile.html";
97  $xslPath = "/usr/share/CUnit/$xslFile";
98  // remove the old HTML file before creating a new one.
99  $rmLast = exec("rm -rf $outPath", $rmOut, $rmRtn);
100  //echo "DB: Starting to generate html report for $fileName\n";
101  $report = genHtml($fileName, $outPath, $xslPath);
102  if(!empty($report))
103  {
104  echo "Error: Could not generate a HTML Test report from $fileName.\n";
105  return(FALSE);
106  }
107  //echo "DB: Generated html file:$outFile" . ".html\n";
108  return(TRUE);
109 }
110 
118 function processCUnit($unitTest)
119 {
120  global $failures;
121  global $other;
122 
123  $unitTest = preg_replace('#/#', '-', $unitTest);
124  $libphp = "lib-php";
126  if ($libphp === $unitTest) return NULL;
127 
128  if(empty($unitTest))
129  {
130  return("Error! no valid input at line " . __FILE__ . " at line " .
131  __LINE__ . "\n");
132  }
133 
134  foreach(glob("$unitTest*.xml") as $fName)
135  {
136  $fileName = lcfirst($fName);
137  // Skip Listing files
138  if(preg_grep("/Listing/", array($fileName)))
139  {
140  $rmlast = exec("rm $fileName", $rmOut, $rmRtn);
141  continue;
142  }
143  if(!tweakCUnit($fileName))
144  {
145  return("Error! could not save processed xml file, they may not display properly\n");
146  }
147 
148  $errors = array();
149  // defect: if the report is corrupt, checkCUnit will say everything is OK.
150  $errors = checkCUnit($fileName);
151  //echo "DB: after checkCUnit, errors for $unitTest are\n";print_r($errors) . "\n";
152  if(is_object($errors[0]))
153  {
154  $failures++;
155  echo "There were Unit Test Failures in $unitTest\n";
156  //print_r($errors) . "\n";
157  }
158  else if(!is_NULL($errors))
159  {
160  // if we can't even check the file, then skip making the report
161  $failures++;
162  return("Failure: Could not check file $fileName for failures is the file corrupt?\n");
163  }
164 
165  if(!genCunitRep($fileName))
166  {
167  return("Error!, could not generate html report for $unitTest\n");
168  }
169  } // foreach
170 
171  return(NULL);
172 } // processCUnit
173 
182 function tweakCUnit($fileName)
183 {
184  if(empty($fileName))
185  {
186  return(FALSE);
187  }
188 
189  //echo "DB: tweaking xml file:$fileName\n";
190  $rFile = file_get_contents($fileName);
191  // fix the Ref to xsl file
192  $pat = '#href="#';
193  $replace = 'href="http://fossology.usa.hp.com/~fossology/dtds/';
194  $rFile = preg_replace($pat, $replace, $rFile);
195  //fix the Ref to the dtds for both run and list files.
196  $runPat = '/CUnit-Run\.dtd/';
197  $rReplace = 'http://fossology.usa.hp.com/~fossology/dtds/CUnit-Run.dtd';
198  $listPat = '/CUnit-List\.dtd/';
199  $lReplace = 'http://fossology.usa.hp.com/~fossology/dtds/CUnit-List.dtd';
200  $rFile = preg_replace($runPat, $rReplace, $rFile);
201  $rFile = preg_replace($listPat, $lReplace, $rFile);
202  //echo "DB: rFile after preg_replace is:\n$rFile\n";
203  if(!file_put_contents($fileName, $rFile))
204  {
205  return(FALSE);
206  }
207  else
208  {
209  return(TRUE);
210  }
211 } // tweakCUnit
212 
213 
214 /* begin main program */
215 
216 if(!defined('TESTROOT'))
217 {
218  $path = __DIR__;
219  $plenth = strlen($path);
220  // remove /unit from the end.
221  $TESTROOT = substr($path, 0, $plenth-5);
222  $_ENV['TESTROOT'] = $TESTROOT;
223  putenv("TESTROOT=$TESTROOT");
224  define('TESTROOT',$TESTROOT);
225 }
226 
227 $WORKSPACE = NULL;
228 
229 if(array_key_exists('WORKSPACE', $_ENV))
230 {
231  $WORKSPACE = $_ENV['WORKSPACE'];
232  define('WORKSPACE', $WORKSPACE);
233 }
234 
235 $unit = TESTROOT . "/unit";
236 
237 if(@chdir($unit) === FALSE)
238 {
239  echo "FATAL!, could not cd to:\n$unit\n";
240  exit(1);
241 }
242 require_once('../lib/bootstrap.php');
243 require_once('../lib/common-Report.php');
244 require_once('../lib/common-Test.php');
245 require_once('../lib/createRC.php');
246 
247 createRC();
248 $sc = getenv('SYSCONFDIR');
249 echo "DB: runUnit: sysconf from getenv is:$sc\n";
250 $sysConf = array();
251 $sysConf = bootstrap();
252 //echo "sysConf after bootstrap is:\n";print_r($sysConf) . "\n";
253 // export for other tests to use
254 
255 echo "DB: runUnit: globals:sysconfdir:{$GLOBALS['SYSCONFDIR']}\n";
256 putenv("SYSCONFDIR=$sc");
257 $_ENV['SYSCONFDIR'] = $sc;
258 
259 echo "DB: runUnit: after putenv SYSCONFDIR from env is:" . getenv('SYSCONFDIR') . "\n";
260 echo "DB: runUnit: after _ENV set, SYSCONFDIR from _ENV is:{$_ENV['SYSCONFDIR']}\n";
261 
262 $modules = array();
263 $unitList = array();
264 
265 // get the list of unit tests to run and flatten the array
266 $modules = parse_ini_file('../dataFiles/unitTests.ini',1);
267 foreach($modules as $key => $value)
268 {
269  $unitList[] = $key;
270 }
271 
272 global $unitList;
273 
274 // TODO: Uncertain, relative paths are BAD. Fix this
275 /* at this point we should be in the directory fossology/src/testing/unit/
276  So let's change directories back to fossology/src/
277  (i.e. one directory above fossology/src/testing/) */
278 backToParent('../..');
279 
280 $failures = 0;
281 foreach($unitList as $unitTest)
282 {
283  echo "\n";
284  echo "$unitTest:\n";
285  $other = substr($unitTest, 0, 3);
286  if($other == 'lib' || $other == 'cli')
287  {
288  if(@chdir($unitTest . '/tests') === FALSE)
289  {
290  echo "Error! cannot cd to " . $unitTest . "/tests, skipping test\n";
291  $failures++;
292  continue;
293  }
294  }
295  else
296  {
297  if(@chdir($unitTest . '/agent_tests/Unit') === FALSE)
298  {
299  echo "Error! cannot cd to " . $unitTest . "/agent_tests/Unit, skipping test\n";
300  $failures++;
301  continue;
302  }
303  }
304  $Make = new RunTest($unitTest);
305  $runResults = $Make->MakeTest();
306  //debugprint($runResults, "run results for $unitTest\n");
307  $Make->printResults($runResults);
308  if(processCUnit($unitTest) != NULL)
309  {
310  echo "Error: could not process cunit results file for $unitTest\n";
311  }
312  if(MakeCover($unitTest) != NULL)
313  {
314  //echo "Error: there were errors for make coverage for $unitTest\n";
315  $failures++;
316  }
317  backToParent('../../..');
318 } // foreach
319 
320 // clean up xml files left behind.
321 //cleanXMLFiles();
322 if($failures)
323 {
324  exit(1);
325 }
326 exit(0);
327 
328 ?>
class for making an agent unit or functional test
bootstrap($sysconfdir="")
Bootstrap the fossology php library.
Definition: migratetest.php:93