FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fo-runTests.php
1 #!/usr/bin/php
2 <?php
3 /*
4  SPDX-FileCopyrightText: © 2008 Hewlett-Packard Development Company, L.P.
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
33 $path = '/usr/local/simpletest' . PATH_SEPARATOR;
34 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
35 if (!defined('SIMPLE_TEST'))
36 define('SIMPLE_TEST', '/usr/local/simpletest/');
37 
38 /* simpletest includes */
39 require_once SIMPLE_TEST . 'unit_tester.php';
40 require_once SIMPLE_TEST . 'reporter.php';
41 require_once SIMPLE_TEST . 'web_tester.php';
42 
43 require_once ('TestEnvironment.php');
44 if(defined('TESTROOT'))
45 {
46  echo TESTROOT . "\n";
47  require_once(TESTROOT . '/testClasses/timer.php');
48 }
49 else
50 {
51  echo "ERROR! cannot load /testClasses/timer.php, is TESTROOT defined?\n";
52  exit(1);
53 }
54 
55 $Usage = "Usage: $argv[0] options...
56  Options:
57  [ {<test-file.php> || a single test or
58  [-l 'list of tests'}] a list of tests, space seperated
59  [ -n <suite-name>] optional test suite name
60  [ -t 'A Title'] optional title\n
61  To run everything in a directory $argv[0] -l \"`ls`\"\n" ;
62 
63 /*
64  * NOTE on parameters, can't guess a suite name from a list, so
65  * only get a suite name if -n is used or no -a and a single test to run.
66  */
67 
68 /*
69  "$argv[0] -l 'list of tests space seperated'\n or\n" .
70  "$argv[0] -l \"`ls`\" to run everything in the directory\n".
71  "\n$argv[0] -t 'Title' to supply an optional title\n";
72  */
73 
74 $options = getopt("l:n:t:");
75 
76 /*
77  Must have at least 1 argument (the test file to run)
78  */
79 $RunList = array();
80 $aTest = NULL;
81 $suite = NULL;
82 
83 //print "argc,argv is:$argc\n";print_r($argv) . "\n";
84 //print "argc is:$argc\n";
85 if (empty($options)) {
86  if ($argc < 2){
87  print $Usage;
88  exit(1);
89  }
90 }
91 if($argc >= 2){
92  /*
93  If first argument does not start with a '-' then it must be a test to run
94  */
95  $len = strspn($argv[1],'-');
96  if(strspn($argv[1],'-') == 0) {
97  $aTest = $argv[1];
98  }
99 }
100 if (array_key_exists("l",$options)) {
101  /* split on spaces AND newlines so you can do a -l "`ls`" */
102  $RunList = preg_split('/\s|\n/',$options['l']);
103  //print "runx: runlist is:\n"; print_r($RunList) . "\n";
104 }
105 if (array_key_exists("n",$options)) {
106  $suite = $options['n'];
107  //print "DB: suite is:$suite\n";
108 }
109 // no suite specified
110 if (!is_null($aTest)) {
111  if(file_exists($aTest)) {
112  $suite = $aTest;
113  $RunList = array($aTest);
114  //print "DB: runlist after assignment of aTest is:\n";print_r($RunList) . "\n";
115  }
116  else {
117  print "Error! File $aTest does not exist!\n";
118  exit(1);
119  }
120 }
121 // default name
122 if(is_null($suite)) {
123  $suite = 'Generic FOSSology Test Suite';
124 }
125 $Title = NULL;
126 if (array_key_exists("t",$options)) {
127  $Title = $options['t'];
128  //print "DB: Title is:$Title\n";
129 }
130 
131 //$Svn = `svnversion`;
132 $start = new timer();
133 $date = date('Y-m-d');
134 $time = date('h:i:s-a');
135 print "Starting $suite on: " . $date . " at " . $time . "\n";
136 //print "Using Svn Version:$Svn\n";
137 $Runtest = new TestSuite("Fossology tests $Title");
138 /*
139  * tests will run serially...
140  *
141  * allow filenames without .php or with it
142  */
143 //print "DB: runlist is:\n";print_r($RunList) . "\n";
144 foreach($RunList as $ptest) {
145  if(preg_match('/^.*?\.php/',$ptest)) {
146  $test = $ptest;
147  }
148  else {
149  $test = $ptest . ".php";
150  }
151  $Runtest->addTestFile("$test");
152 }
153 
154 /*
155  * leave the code below alone, it allows the tests to be run either by
156  * the cli or in a web browser
157  */
158 
159 if (TextReporter::inCli()) {
160  $results = $Runtest->run(new TextReporter()) ? 0 : 1;
161  print "Ending $suite at: " . date('r') . "\n";
162  $elapseTime = $start->TimeAgo($start->getStartTime());
163  print "The suite $suite took {$elapseTime}to run\n\n";
164  exit($results);
165 }
166 
167 $Runtest->run(new HtmlReporter());
168 print "<pre>Ending $suite at: " . date('r') . "</pre>\n";
169 $elapseTime = $start->TimeAgo($start->getStartTime());
170 print "<pre>The suite $suite took {$elapseTime}to run</pre>\n";
Definition: timer.php:31