forked from sprice/drush_test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.drush.inc
138 lines (124 loc) · 3.88 KB
/
test.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
// $Id$
/**
* @file
* Simpletest module drush integration.
*/
/**
* Implementation of hook_drush_command().
*/
function test_drush_command() {
$items = array();
$items['test'] = array(
'callback' => 'simpletest_drush_test',
'description' => "List all the available tests for the site.",
'drupal dependencies' => array('simpletest'),
);
return $items;
}
/**
* Implementation of hook_drush_help().
*/
function test_drush_help($section) {
switch ($section) {
case 'drush:test':
return dt("List all the available tests for the site.");
}
}
/**
* Test command callback.
*/
function simpletest_drush_test($param = NULL) {
// Check runtime requirements.
if (!drush_get_option('uri')) {
return drush_log(dt("You must specify this site's URL using the --uri parameter."), 'error');
}
cache_clear_all('simpletest', 'cache');
// Retrieve all tests and groups.
if (function_exists('simpletest_get_all_tests')) {
$all_tests = simpletest_get_all_tests();
$groups = simpletest_drush_test_groups($all_tests);
}
else {
$groups = simpletest_test_get_all();
$all_tests = array();
foreach ($groups as $group) {
$all_tests = array_merge($all_tests, array_keys($group));
}
}
// Specific test class specified.
if (isset($param) && in_array($param, $all_tests, TRUE)) {
simpletest_drush_run_test($param);
simpletest_clean_environment();
return;
}
// Specific group specified.
else if (isset($param) && isset($groups[$param])) {
foreach (array_keys($groups[$param]) as $class) {
drush_backend_invoke('test '. $class);
}
return;
}
// Run all tests.
else if (isset($param) && $param === 'all') {
foreach (array_keys($groups) as $group) {
foreach (array_keys($groups[$group]) as $class) {
drush_backend_invoke('test '. $class);
}
}
return;
}
// Clean test environment.
else if (isset($param) && $param === 'clean') {
simpletest_clean_environment();
return;
}
// No args, list tests.
if (!isset($param)) {
$rows = array();
$rows[] = array(dt('Command'), dt('Description'));
$rows[] = array('-------', '-----------');
foreach ($groups as $group_name => $group_tests) {
foreach ($group_tests as $test_class => $test_info) {
if (!isset($rows[$test_info['group']])) {
$rows[$test_info['group']] = array($group_name, $test_info['group']);
}
$rows[] = array(" {$test_class}", " {$test_info['name']}");
}
}
drush_print_table($rows, TRUE);
return;
}
}
/**
* Run a single test and display any failure messages.
*/
function simpletest_drush_run_test($class) {
db_query('INSERT INTO {simpletest_test_id} (test_id) VALUES (default)');
$test_id = db_last_insert_id('simpletest_test_id', 'test_id');
$test = new $class($test_id);
$test->run();
$info = $test->getInfo();
$status = ((isset($test->results['#fail']) && $test->results['#fail'] > 0)
|| (isset($test->results['#exception']) && $test->results['#exception'] > 0) ? 'error' : 'ok');
drush_log($info['name'] . ' ' . _simpletest_format_summary_line($test->results), $status);
// If there were some failed tests show them.
if ($status === 'error') {
$result = db_query("SELECT * FROM {simpletest} WHERE test_id = %d AND status = 'fail' ORDER BY test_class, message_id", $test_id);
while ($row = db_fetch_object($result)) {
drush_set_error('DRUSH_TEST_FAIL', dt("Test @function failed: @message", array('@function' => $row->function, '@message' => $row->message)));
}
}
}
/**
* Retrieve all test groups and sanitize their names to make them command-line
* friendly.
*/
function simpletest_drush_test_groups($tests) {
$groups = array();
foreach (simpletest_categorize_tests($tests) as $name => $group) {
$sanitized = strtr($name, array(' ' => ''));
$groups[$sanitized] = $group;
}
return $groups;
}