Skip to content

Commit 498d31d

Browse files
author
dries
committed
- Patch #443154 by boombatower: make sure fatal errors are reported as test failures.
1 parent 479bca1 commit 498d31d

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

modules/simpletest/drupal_web_test_case.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
// $Id: drupal_web_test_case.php,v 1.119 2009-07-05 18:00:10 dries Exp $
2+
// $Id: drupal_web_test_case.php,v 1.120 2009-07-07 07:50:53 dries Exp $
33

44
/**
55
* Base class for Drupal tests.
@@ -137,6 +137,39 @@ protected function assert($status, $message = '', $group = 'Other', array $calle
137137
}
138138
}
139139

140+
/**
141+
* Make assertions from outside the test case.
142+
*
143+
* @see DrupalTestCase::assert()
144+
*/
145+
public static function assertStatic($test_id, $test_class, $status, $message = '', $group = 'Other', array $caller = NULL) {
146+
// Convert boolean status to string status.
147+
if (is_bool($status)) {
148+
$status = $status ? 'pass' : 'fail';
149+
}
150+
151+
$caller += array(
152+
'function' => t('N/A'),
153+
'line' => -1,
154+
'file' => t('N/A'),
155+
);
156+
157+
$assertion = array(
158+
'test_id' => $test_id,
159+
'test_class' => $test_class,
160+
'status' => $status,
161+
'message' => $message,
162+
'message_group' => $group,
163+
'function' => $caller['function'],
164+
'line' => $caller['line'],
165+
'file' => $caller['file'],
166+
);
167+
168+
db_insert('simpletest')
169+
->fields($assertion)
170+
->execute();
171+
}
172+
140173
/**
141174
* Cycles through backtrace until the first non-assertion method is found.
142175
*
@@ -981,7 +1014,12 @@ protected function setUp() {
9811014
$clean_url_original = variable_get('clean_url', 0);
9821015

9831016
// Generate temporary prefixed database to ensure that tests have a clean starting point.
984-
$db_prefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
1017+
$db_prefix_new = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
1018+
db_update('simpletest_test_id')
1019+
->fields(array('last_prefix' => $db_prefix_new))
1020+
->condition('test_id', $this->testId)
1021+
->execute();
1022+
$db_prefix = $db_prefix_new;
9851023

9861024
include_once DRUPAL_ROOT . '/includes/install.inc';
9871025
drupal_install_system();
@@ -1043,6 +1081,10 @@ protected function setUp() {
10431081
// Create the files directory.
10441082
file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
10451083

1084+
// Log fatal errors.
1085+
ini_set('log_errors', 1);
1086+
ini_set('error_log', $directory . '/error.log');
1087+
10461088
set_time_limit($this->timeLimit);
10471089
}
10481090

modules/simpletest/simpletest.install

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
// $Id: simpletest.install,v 1.21 2009-05-27 18:34:00 dries Exp $
2+
// $Id: simpletest.install,v 1.22 2009-07-07 07:50:53 dries Exp $
33

44
/**
55
* @file
@@ -229,6 +229,13 @@ function simpletest_schema() {
229229
'description' => 'Primary Key: Unique simpletest ID used to group test results together. Each time a set of tests
230230
are run a new test ID is used.',
231231
),
232+
'last_prefix' => array(
233+
'type' => 'varchar',
234+
'length' => 60,
235+
'not null' => FALSE,
236+
'default' => '',
237+
'description' => 'The last database prefix used during testing.',
238+
),
232239
),
233240
'primary key' => array('test_id'),
234241
);

modules/simpletest/simpletest.module

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
// $Id: simpletest.module,v 1.56 2009-07-05 18:00:10 dries Exp $
2+
// $Id: simpletest.module,v 1.57 2009-07-07 07:50:53 dries Exp $
33

44
/**
55
* @file
@@ -191,11 +191,45 @@ function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
191191
drupal_set_message(t('The test run finished in @elapsed.', array('@elapsed' => $elapsed)));
192192
}
193193
else {
194+
// Use the test_id passed as a parameter to _simpletest_batch_operation().
195+
simpletest_log_read($operations[0][1][1]);
196+
194197
drupal_set_message(t('The test run did not successfully finish.'), 'error');
195198
}
196199
module_invoke_all('test_group_finished');
197200
}
198201

202+
/**
203+
* Read the error log and report any errors as assertion failures.
204+
*
205+
* The errors in the log should only be fatal errors since any other errors
206+
* will have been recorded by the error handler.
207+
*
208+
* @param $test_id
209+
* The test ID to read log file for.
210+
*/
211+
function simpletest_log_read($test_id) {
212+
$last_prefix = db_result(db_query('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id)));
213+
$last_prefix = substr($last_prefix, 10);
214+
215+
$test_class = db_result(db_query('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id', array(':test_id' => $test_id)));
216+
$log = file_directory_path() . "/simpletest/$last_prefix/error.log";
217+
if (file_exists($log)) {
218+
foreach (file($log) as $line) {
219+
if (preg_match('/PHP Fatal error: (.*?) in (.*) on line (\d+)/', $line, $match)) {
220+
$caller = array(
221+
'line' => $match[3],
222+
'file' => $match[2],
223+
);
224+
DrupalTestCase::assertStatic($test_id, $test_class, FALSE, $match[1], 'Fatal error', $caller);
225+
}
226+
else {
227+
DrupalTestCase::assertStatic($test_id, $test_class, FALSE, $line, 'Fatal error');
228+
}
229+
}
230+
}
231+
}
232+
199233
/**
200234
* Get a list of all of the tests provided by the system.
201235
*

scripts/run-tests.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
// $Id: run-tests.sh,v 1.28 2009-06-10 16:17:02 dries Exp $
2+
// $Id: run-tests.sh,v 1.29 2009-07-07 07:50:53 dries Exp $
33
/**
44
* @file
55
* This script runs Drupal tests from command line.
@@ -466,6 +466,8 @@ function simpletest_script_reporter_init() {
466466
function simpletest_script_reporter_display_results() {
467467
global $args, $test_id, $results_map;
468468

469+
simpletest_log_read($test_id);
470+
469471
echo "\n";
470472
$end = timer_stop('run-tests');
471473
echo "Test run duration: " . format_interval($end['time'] / 1000);

0 commit comments

Comments
 (0)