forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.drush.inc
151 lines (135 loc) · 4.44 KB
/
unit.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
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @file
* Commands which are useful for unit tests.
*/
/**
* Implementation of hook_drush_command().
*/
function unit_drush_command() {
$items['unit'] = array(
'description' => 'No-op command, used to test completion for commands that start the same as other commands.',
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
);
$items['unit-eval'] = array(
'description' => 'Works like php-eval. Used for testing $command_specific context.',
'bootstrap' => DRUSH_BOOTSTRAP_MAX,
'callback' => 'drush_core_php_eval',
);
$items['unit-invoke'] = array(
'description' => 'Return an array indicating which invoke hooks got called.',
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
'callback' => 'drush_unit_invoke_primary',
);
$items['unit-batch'] = array(
'description' => 'Run a batch process.',
'bootstrap' => DRUSH_BOOTSTRAP_MAX,
);
$items['unit-return-options'] = array(
'description' => 'Return options as function result.',
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
);
$items['unit-return-argv'] = array(
'description' => 'Return original argv as function result.',
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
);
$items['missing-callback'] = array(
'description' => 'Command with no callback function, to test error reporting.',
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
);
$items['unit-drush-dependency'] = array(
'description' => 'Command depending on an unknown commandfile.',
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
'drush dependencies' => array('unknown-commandfile'),
);
return $items;
}
// Implement each invoke hook with the same single line of code.
// That line records that the hook was called.
function drush_unit_invoke_init() {unit_invoke_log(__FUNCTION__);}
function drush_unit_invoke_validate() {unit_invoke_log(__FUNCTION__);}
function drush_unit_pre_unit_invoke() {unit_invoke_log(__FUNCTION__);}
// Primary callback is not invoked when command specifies a 'callback'.
// function drush_unit_invoke() {unit_invoke_log(__FUNCTION__);}
function drush_unit_invoke_primary() {unit_invoke_log(__FUNCTION__);}
function drush_unit_pre_unit_invoke_rollback() {unit_invoke_log(__FUNCTION__);}
function drush_unit_post_unit_invoke_rollback() {unit_invoke_log(__FUNCTION__);}
// Record that hook_drush_init() fired.
function unit_drush_init() {
$command = drush_get_command();
if ($command['command'] == 'unit-invoke') {
unit_invoke_log(__FUNCTION__);
}
}
function drush_unit_post_unit_invoke() {
// Record that this hook was called.
unit_invoke_log(__FUNCTION__);
// Make sure we enter into rollback.
drush_set_error('');
}
/**
* The final invoke hook. Emit the call history.
* Cannot use 'exit' as it does not fire in rollback scenario.
*/
function drush_unit_invoke_validate_rollback() {
unit_invoke_log(__FUNCTION__);
print json_encode(unit_invoke_log());
}
function unit_invoke_log($function = NULL) {
static $called = array();
if ($function) {
$called[] = $function;
}
else {
return $called;
}
}
/**
* Command callback.
*/
function drush_unit_batch() {
// Reduce php memory/time limits to test backend respawn.
// TODO.
$batch = array(
'operations' => array(
array('_drush_unit_batch_operation', array()),
),
'finished' => '_drush_unit_batch_finished',
// 'file' => Doesn't work for us. Drupal 7 enforces this path
// to be relative to DRUPAL_ROOT.
// @see _batch_process().
);
batch_set($batch);
drush_backend_batch_process();
// Print the batch output.
drush_backend_output();
}
function _drush_unit_batch_operation(&$context) {
$context['message'] = "!!! ArrayObject does its job.";
for ($i = 0; $i < 5; $i++) {
drush_print("Iteration $i");
}
$context['finished'] = 1;
}
function _drush_unit_batch_finished() {
// Restore php limits.
// TODO.
}
// Return all of the option values passed in to this routine, minus the
// global options.
function drush_unit_return_options() {
$all_option_values = array_merge(drush_get_context('cli'), drush_get_context('stdin'));
foreach (drush_get_global_options() as $key => $info) {
unset($all_option_values[$key]);
}
if (isset($all_option_values['log-message'])) {
drush_log($all_option_values['log-message'], 'warning');
drush_log("done", 'warning');
unset($all_option_values['log-message']);
}
return $all_option_values;
}
// Return all of the original arguments passed to this script
function drush_unit_return_argv() {
return drush_get_context('argv');
}