run-tests.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. <?php
  2. /**
  3. * @file
  4. * This script runs Drupal tests from command line.
  5. */
  6. define('SIMPLETEST_SCRIPT_COLOR_PASS', 32); // Green.
  7. define('SIMPLETEST_SCRIPT_COLOR_FAIL', 31); // Red.
  8. define('SIMPLETEST_SCRIPT_COLOR_EXCEPTION', 33); // Brown.
  9. // Set defaults and get overrides.
  10. list($args, $count) = simpletest_script_parse_args();
  11. if ($args['help'] || $count == 0) {
  12. simpletest_script_help();
  13. exit;
  14. }
  15. if ($args['execute-test']) {
  16. // Masquerade as Apache for running tests.
  17. simpletest_script_init("Apache");
  18. simpletest_script_run_one_test($args['test-id'], $args['execute-test']);
  19. }
  20. else {
  21. // Run administrative functions as CLI.
  22. simpletest_script_init(NULL);
  23. }
  24. // Bootstrap to perform initial validation or other operations.
  25. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  26. if (!module_exists('simpletest')) {
  27. simpletest_script_print_error("The simpletest module must be enabled before this script can run.");
  28. exit;
  29. }
  30. if ($args['clean']) {
  31. // Clean up left-over times and directories.
  32. simpletest_clean_environment();
  33. echo "\nEnvironment cleaned.\n";
  34. // Get the status messages and print them.
  35. $messages = array_pop(drupal_get_messages('status'));
  36. foreach ($messages as $text) {
  37. echo " - " . $text . "\n";
  38. }
  39. exit;
  40. }
  41. // Load SimpleTest files.
  42. $groups = simpletest_test_get_all();
  43. $all_tests = array();
  44. foreach ($groups as $group => $tests) {
  45. $all_tests = array_merge($all_tests, array_keys($tests));
  46. }
  47. $test_list = array();
  48. if ($args['list']) {
  49. // Display all available tests.
  50. echo "\nAvailable test groups & classes\n";
  51. echo "-------------------------------\n\n";
  52. foreach ($groups as $group => $tests) {
  53. echo $group . "\n";
  54. foreach ($tests as $class => $info) {
  55. echo " - " . $info['name'] . ' (' . $class . ')' . "\n";
  56. }
  57. }
  58. exit;
  59. }
  60. $test_list = simpletest_script_get_test_list();
  61. // Try to allocate unlimited time to run the tests.
  62. drupal_set_time_limit(0);
  63. simpletest_script_reporter_init();
  64. // Setup database for test results.
  65. $test_id = db_insert('simpletest_test_id')->useDefaults(array('test_id'))->execute();
  66. // Execute tests.
  67. simpletest_script_execute_batch($test_id, simpletest_script_get_test_list());
  68. // Retrieve the last database prefix used for testing and the last test class
  69. // that was run from. Use the information to read the lgo file in case any
  70. // fatal errors caused the test to crash.
  71. list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
  72. simpletest_log_read($test_id, $last_prefix, $last_test_class);
  73. // Stop the timer.
  74. simpletest_script_reporter_timer_stop();
  75. // Display results before database is cleared.
  76. simpletest_script_reporter_display_results();
  77. if ($args['xml']) {
  78. simpletest_script_reporter_write_xml_results();
  79. }
  80. // Cleanup our test results.
  81. simpletest_clean_results_table($test_id);
  82. // Test complete, exit.
  83. exit;
  84. /**
  85. * Print help text.
  86. */
  87. function simpletest_script_help() {
  88. global $args;
  89. echo <<<EOF
  90. Run Drupal tests from the shell.
  91. Usage: {$args['script']} [OPTIONS] <tests>
  92. Example: {$args['script']} Profile
  93. All arguments are long options.
  94. --help Print this page.
  95. --list Display all available test groups.
  96. --clean Cleans up database tables or directories from previous, failed,
  97. tests and then exits (no tests are run).
  98. --url Immediately precedes a URL to set the host and path. You will
  99. need this parameter if Drupal is in a subdirectory on your
  100. localhost and you have not set \$base_url in settings.php. Tests
  101. can be run under SSL by including https:// in the URL.
  102. --php The absolute path to the PHP executable. Usually not needed.
  103. --concurrency [num]
  104. Run tests in parallel, up to [num] tests at a time.
  105. --all Run all available tests.
  106. --class Run tests identified by specific class names, instead of group names.
  107. --file Run tests identified by specific file names, instead of group names.
  108. Specify the path and the extension (i.e. 'modules/user/user.test').
  109. --xml <path>
  110. If provided, test results will be written as xml files to this path.
  111. --color Output text format results with color highlighting.
  112. --verbose Output detailed assertion messages in addition to summary.
  113. <test1>[,<test2>[,<test3> ...]]
  114. One or more tests to be run. By default, these are interpreted
  115. as the names of test groups as shown at
  116. ?q=admin/config/development/testing.
  117. These group names typically correspond to module names like "User"
  118. or "Profile" or "System", but there is also a group "XML-RPC".
  119. If --class is specified then these are interpreted as the names of
  120. specific test classes whose test methods will be run. Tests must
  121. be separated by commas. Ignored if --all is specified.
  122. To run this script you will normally invoke it from the root directory of your
  123. Drupal installation as the webserver user (differs per configuration), or root:
  124. sudo -u [wwwrun|www-data|etc] php ./scripts/{$args['script']}
  125. --url http://example.com/ --all
  126. sudo -u [wwwrun|www-data|etc] php ./scripts/{$args['script']}
  127. --url http://example.com/ --class BlockTestCase
  128. \n
  129. EOF;
  130. }
  131. /**
  132. * Parse execution argument and ensure that all are valid.
  133. *
  134. * @return The list of arguments.
  135. */
  136. function simpletest_script_parse_args() {
  137. // Set default values.
  138. $args = array(
  139. 'script' => '',
  140. 'help' => FALSE,
  141. 'list' => FALSE,
  142. 'clean' => FALSE,
  143. 'url' => '',
  144. 'php' => '',
  145. 'concurrency' => 1,
  146. 'all' => FALSE,
  147. 'class' => FALSE,
  148. 'file' => FALSE,
  149. 'color' => FALSE,
  150. 'verbose' => FALSE,
  151. 'test_names' => array(),
  152. // Used internally.
  153. 'test-id' => 0,
  154. 'execute-test' => '',
  155. 'xml' => '',
  156. );
  157. // Override with set values.
  158. $args['script'] = basename(array_shift($_SERVER['argv']));
  159. $count = 0;
  160. while ($arg = array_shift($_SERVER['argv'])) {
  161. if (preg_match('/--(\S+)/', $arg, $matches)) {
  162. // Argument found.
  163. if (array_key_exists($matches[1], $args)) {
  164. // Argument found in list.
  165. $previous_arg = $matches[1];
  166. if (is_bool($args[$previous_arg])) {
  167. $args[$matches[1]] = TRUE;
  168. }
  169. else {
  170. $args[$matches[1]] = array_shift($_SERVER['argv']);
  171. }
  172. // Clear extraneous values.
  173. $args['test_names'] = array();
  174. $count++;
  175. }
  176. else {
  177. // Argument not found in list.
  178. simpletest_script_print_error("Unknown argument '$arg'.");
  179. exit;
  180. }
  181. }
  182. else {
  183. // Values found without an argument should be test names.
  184. $args['test_names'] += explode(',', $arg);
  185. $count++;
  186. }
  187. }
  188. // Validate the concurrency argument
  189. if (!is_numeric($args['concurrency']) || $args['concurrency'] <= 0) {
  190. simpletest_script_print_error("--concurrency must be a strictly positive integer.");
  191. exit;
  192. }
  193. return array($args, $count);
  194. }
  195. /**
  196. * Initialize script variables and perform general setup requirements.
  197. */
  198. function simpletest_script_init($server_software) {
  199. global $args, $php;
  200. $host = 'localhost';
  201. $path = '';
  202. // Determine location of php command automatically, unless a command line argument is supplied.
  203. if (!empty($args['php'])) {
  204. $php = $args['php'];
  205. }
  206. elseif ($php_env = getenv('_')) {
  207. // '_' is an environment variable set by the shell. It contains the command that was executed.
  208. $php = $php_env;
  209. }
  210. elseif ($sudo = getenv('SUDO_COMMAND')) {
  211. // 'SUDO_COMMAND' is an environment variable set by the sudo program.
  212. // Extract only the PHP interpreter, not the rest of the command.
  213. list($php, ) = explode(' ', $sudo, 2);
  214. }
  215. else {
  216. simpletest_script_print_error('Unable to automatically determine the path to the PHP interpreter. Supply the --php command line argument.');
  217. simpletest_script_help();
  218. exit();
  219. }
  220. // Get URL from arguments.
  221. if (!empty($args['url'])) {
  222. $parsed_url = parse_url($args['url']);
  223. $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
  224. $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  225. // If the passed URL schema is 'https' then setup the $_SERVER variables
  226. // properly so that testing will run under HTTPS.
  227. if ($parsed_url['scheme'] == 'https') {
  228. $_SERVER['HTTPS'] = 'on';
  229. }
  230. }
  231. $_SERVER['HTTP_HOST'] = $host;
  232. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  233. $_SERVER['SERVER_ADDR'] = '127.0.0.1';
  234. $_SERVER['SERVER_SOFTWARE'] = $server_software;
  235. $_SERVER['SERVER_NAME'] = 'localhost';
  236. $_SERVER['REQUEST_URI'] = $path .'/';
  237. $_SERVER['REQUEST_METHOD'] = 'GET';
  238. $_SERVER['SCRIPT_NAME'] = $path .'/index.php';
  239. $_SERVER['PHP_SELF'] = $path .'/index.php';
  240. $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
  241. if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  242. // Ensure that any and all environment variables are changed to https://.
  243. foreach ($_SERVER as $key => $value) {
  244. $_SERVER[$key] = str_replace('http://', 'https://', $_SERVER[$key]);
  245. }
  246. }
  247. chdir(realpath(dirname(__FILE__) . '/..'));
  248. define('DRUPAL_ROOT', getcwd());
  249. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  250. }
  251. /**
  252. * Execute a batch of tests.
  253. */
  254. function simpletest_script_execute_batch($test_id, $test_classes) {
  255. global $args;
  256. // Multi-process execution.
  257. $children = array();
  258. while (!empty($test_classes) || !empty($children)) {
  259. while (count($children) < $args['concurrency']) {
  260. if (empty($test_classes)) {
  261. break;
  262. }
  263. // Fork a child process.
  264. $test_class = array_shift($test_classes);
  265. $command = simpletest_script_command($test_id, $test_class);
  266. $process = proc_open($command, array(), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
  267. if (!is_resource($process)) {
  268. echo "Unable to fork test process. Aborting.\n";
  269. exit;
  270. }
  271. // Register our new child.
  272. $children[] = array(
  273. 'process' => $process,
  274. 'class' => $test_class,
  275. 'pipes' => $pipes,
  276. );
  277. }
  278. // Wait for children every 200ms.
  279. usleep(200000);
  280. // Check if some children finished.
  281. foreach ($children as $cid => $child) {
  282. $status = proc_get_status($child['process']);
  283. if (empty($status['running'])) {
  284. // The child exited, unregister it.
  285. proc_close($child['process']);
  286. if ($status['exitcode']) {
  287. echo 'FATAL ' . $test_class . ': test runner returned a non-zero error code (' . $status['exitcode'] . ').' . "\n";
  288. }
  289. unset($children[$cid]);
  290. }
  291. }
  292. }
  293. }
  294. /**
  295. * Bootstrap Drupal and run a single test.
  296. */
  297. function simpletest_script_run_one_test($test_id, $test_class) {
  298. try {
  299. // Bootstrap Drupal.
  300. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  301. simpletest_classloader_register();
  302. $test = new $test_class($test_id);
  303. $test->run();
  304. $info = $test->getInfo();
  305. $had_fails = (isset($test->results['#fail']) && $test->results['#fail'] > 0);
  306. $had_exceptions = (isset($test->results['#exception']) && $test->results['#exception'] > 0);
  307. $status = ($had_fails || $had_exceptions ? 'fail' : 'pass');
  308. simpletest_script_print($info['name'] . ' ' . _simpletest_format_summary_line($test->results) . "\n", simpletest_script_color_code($status));
  309. // Finished, kill this runner.
  310. exit(0);
  311. }
  312. catch (Exception $e) {
  313. echo (string) $e;
  314. exit(1);
  315. }
  316. }
  317. /**
  318. * Return a command used to run a test in a separate process.
  319. *
  320. * @param $test_id
  321. * The current test ID.
  322. * @param $test_class
  323. * The name of the test class to run.
  324. */
  325. function simpletest_script_command($test_id, $test_class) {
  326. global $args, $php;
  327. $command = escapeshellarg($php) . ' ' . escapeshellarg('./scripts/' . $args['script']) . ' --url ' . escapeshellarg($args['url']);
  328. if ($args['color']) {
  329. $command .= ' --color';
  330. }
  331. $command .= " --php " . escapeshellarg($php) . " --test-id $test_id --execute-test " . escapeshellarg($test_class);
  332. return $command;
  333. }
  334. /**
  335. * Get list of tests based on arguments. If --all specified then
  336. * returns all available tests, otherwise reads list of tests.
  337. *
  338. * Will print error and exit if no valid tests were found.
  339. *
  340. * @return List of tests.
  341. */
  342. function simpletest_script_get_test_list() {
  343. global $args, $all_tests, $groups;
  344. $test_list = array();
  345. if ($args['all']) {
  346. $test_list = $all_tests;
  347. }
  348. else {
  349. if ($args['class']) {
  350. // Check for valid class names.
  351. foreach ($args['test_names'] as $class_name) {
  352. if (in_array($class_name, $all_tests)) {
  353. $test_list[] = $class_name;
  354. }
  355. }
  356. }
  357. elseif ($args['file']) {
  358. $files = array();
  359. foreach ($args['test_names'] as $file) {
  360. $files[drupal_realpath($file)] = 1;
  361. }
  362. // Check for valid class names.
  363. foreach ($all_tests as $class_name) {
  364. $refclass = new ReflectionClass($class_name);
  365. $file = $refclass->getFileName();
  366. if (isset($files[$file])) {
  367. $test_list[] = $class_name;
  368. }
  369. }
  370. }
  371. else {
  372. // Check for valid group names and get all valid classes in group.
  373. foreach ($args['test_names'] as $group_name) {
  374. if (isset($groups[$group_name])) {
  375. foreach ($groups[$group_name] as $class_name => $info) {
  376. $test_list[] = $class_name;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. if (empty($test_list)) {
  383. simpletest_script_print_error('No valid tests were specified.');
  384. exit;
  385. }
  386. return $test_list;
  387. }
  388. /**
  389. * Initialize the reporter.
  390. */
  391. function simpletest_script_reporter_init() {
  392. global $args, $all_tests, $test_list, $results_map;
  393. $results_map = array(
  394. 'pass' => 'Pass',
  395. 'fail' => 'Fail',
  396. 'exception' => 'Exception'
  397. );
  398. echo "\n";
  399. echo "Drupal test run\n";
  400. echo "---------------\n";
  401. echo "\n";
  402. // Tell the user about what tests are to be run.
  403. if ($args['all']) {
  404. echo "All tests will run.\n\n";
  405. }
  406. else {
  407. echo "Tests to be run:\n";
  408. foreach ($test_list as $class_name) {
  409. $info = call_user_func(array($class_name, 'getInfo'));
  410. echo " - " . $info['name'] . ' (' . $class_name . ')' . "\n";
  411. }
  412. echo "\n";
  413. }
  414. echo "Test run started:\n";
  415. echo " " . format_date($_SERVER['REQUEST_TIME'], 'long') . "\n";
  416. timer_start('run-tests');
  417. echo "\n";
  418. echo "Test summary\n";
  419. echo "------------\n";
  420. echo "\n";
  421. }
  422. /**
  423. * Display jUnit XML test results.
  424. */
  425. function simpletest_script_reporter_write_xml_results() {
  426. global $args, $test_id, $results_map;
  427. $results = db_query("SELECT * FROM {simpletest} WHERE test_id = :test_id ORDER BY test_class, message_id", array(':test_id' => $test_id));
  428. $test_class = '';
  429. $xml_files = array();
  430. foreach ($results as $result) {
  431. if (isset($results_map[$result->status])) {
  432. if ($result->test_class != $test_class) {
  433. // We've moved onto a new class, so write the last classes results to a file:
  434. if (isset($xml_files[$test_class])) {
  435. file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
  436. unset($xml_files[$test_class]);
  437. }
  438. $test_class = $result->test_class;
  439. if (!isset($xml_files[$test_class])) {
  440. $doc = new DomDocument('1.0');
  441. $root = $doc->createElement('testsuite');
  442. $root = $doc->appendChild($root);
  443. $xml_files[$test_class] = array('doc' => $doc, 'suite' => $root);
  444. }
  445. }
  446. // For convenience:
  447. $dom_document = &$xml_files[$test_class]['doc'];
  448. // Create the XML element for this test case:
  449. $case = $dom_document->createElement('testcase');
  450. $case->setAttribute('classname', $test_class);
  451. list($class, $name) = explode('->', $result->function, 2);
  452. $case->setAttribute('name', $name);
  453. // Passes get no further attention, but failures and exceptions get to add more detail:
  454. if ($result->status == 'fail') {
  455. $fail = $dom_document->createElement('failure');
  456. $fail->setAttribute('type', 'failure');
  457. $fail->setAttribute('message', $result->message_group);
  458. $text = $dom_document->createTextNode($result->message);
  459. $fail->appendChild($text);
  460. $case->appendChild($fail);
  461. }
  462. elseif ($result->status == 'exception') {
  463. // In the case of an exception the $result->function may not be a class
  464. // method so we record the full function name:
  465. $case->setAttribute('name', $result->function);
  466. $fail = $dom_document->createElement('error');
  467. $fail->setAttribute('type', 'exception');
  468. $fail->setAttribute('message', $result->message_group);
  469. $full_message = $result->message . "\n\nline: " . $result->line . "\nfile: " . $result->file;
  470. $text = $dom_document->createTextNode($full_message);
  471. $fail->appendChild($text);
  472. $case->appendChild($fail);
  473. }
  474. // Append the test case XML to the test suite:
  475. $xml_files[$test_class]['suite']->appendChild($case);
  476. }
  477. }
  478. // The last test case hasn't been saved to a file yet, so do that now:
  479. if (isset($xml_files[$test_class])) {
  480. file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
  481. unset($xml_files[$test_class]);
  482. }
  483. }
  484. /**
  485. * Stop the test timer.
  486. */
  487. function simpletest_script_reporter_timer_stop() {
  488. echo "\n";
  489. $end = timer_stop('run-tests');
  490. echo "Test run duration: " . format_interval($end['time'] / 1000);
  491. echo "\n\n";
  492. }
  493. /**
  494. * Display test results.
  495. */
  496. function simpletest_script_reporter_display_results() {
  497. global $args, $test_id, $results_map;
  498. if ($args['verbose']) {
  499. // Report results.
  500. echo "Detailed test results\n";
  501. echo "---------------------\n";
  502. $results = db_query("SELECT * FROM {simpletest} WHERE test_id = :test_id ORDER BY test_class, message_id", array(':test_id' => $test_id));
  503. $test_class = '';
  504. foreach ($results as $result) {
  505. if (isset($results_map[$result->status])) {
  506. if ($result->test_class != $test_class) {
  507. // Display test class every time results are for new test class.
  508. echo "\n\n---- $result->test_class ----\n\n\n";
  509. $test_class = $result->test_class;
  510. // Print table header.
  511. echo "Status Group Filename Line Function \n";
  512. echo "--------------------------------------------------------------------------------\n";
  513. }
  514. simpletest_script_format_result($result);
  515. }
  516. }
  517. }
  518. }
  519. /**
  520. * Format the result so that it fits within the default 80 character
  521. * terminal size.
  522. *
  523. * @param $result The result object to format.
  524. */
  525. function simpletest_script_format_result($result) {
  526. global $results_map, $color;
  527. $summary = sprintf("%-9.9s %-10.10s %-17.17s %4.4s %-35.35s\n",
  528. $results_map[$result->status], $result->message_group, basename($result->file), $result->line, $result->function);
  529. simpletest_script_print($summary, simpletest_script_color_code($result->status));
  530. $lines = explode("\n", wordwrap(trim(strip_tags($result->message)), 76));
  531. foreach ($lines as $line) {
  532. echo " $line\n";
  533. }
  534. }
  535. /**
  536. * Print error message prefixed with " ERROR: " and displayed in fail color
  537. * if color output is enabled.
  538. *
  539. * @param $message The message to print.
  540. */
  541. function simpletest_script_print_error($message) {
  542. simpletest_script_print(" ERROR: $message\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  543. }
  544. /**
  545. * Print a message to the console, if color is enabled then the specified
  546. * color code will be used.
  547. *
  548. * @param $message The message to print.
  549. * @param $color_code The color code to use for coloring.
  550. */
  551. function simpletest_script_print($message, $color_code) {
  552. global $args;
  553. if ($args['color']) {
  554. echo "\033[" . $color_code . "m" . $message . "\033[0m";
  555. }
  556. else {
  557. echo $message;
  558. }
  559. }
  560. /**
  561. * Get the color code associated with the specified status.
  562. *
  563. * @param $status The status string to get code for.
  564. * @return Color code.
  565. */
  566. function simpletest_script_color_code($status) {
  567. switch ($status) {
  568. case 'pass':
  569. return SIMPLETEST_SCRIPT_COLOR_PASS;
  570. case 'fail':
  571. return SIMPLETEST_SCRIPT_COLOR_FAIL;
  572. case 'exception':
  573. return SIMPLETEST_SCRIPT_COLOR_EXCEPTION;
  574. }
  575. return 0; // Default formatting.
  576. }