1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* CLI tool with utilities to manage Behat integration in Moodle
|
|
|
19 |
*
|
|
|
20 |
* All CLI utilities uses $CFG->behat_dataroot and $CFG->prefix_dataroot as
|
|
|
21 |
* $CFG->dataroot and $CFG->prefix
|
|
|
22 |
* Same applies for $CFG->behat_dbname, $CFG->behat_dbuser, $CFG->behat_dbpass
|
|
|
23 |
* and $CFG->behat_dbhost. But if any of those is not defined $CFG->dbname,
|
|
|
24 |
* $CFG->dbuser, $CFG->dbpass and/or $CFG->dbhost will be used.
|
|
|
25 |
*
|
|
|
26 |
* @package tool_behat
|
|
|
27 |
* @copyright 2012 David Monllaó
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
if (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
33 |
die(); // No access from web!.
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
// Basic functions.
|
|
|
37 |
require_once(__DIR__ . '/../../../../lib/clilib.php');
|
|
|
38 |
require_once(__DIR__ . '/../../../../lib/behat/lib.php');
|
|
|
39 |
|
|
|
40 |
// CLI options.
|
|
|
41 |
list($options, $unrecognized) = cli_get_params(
|
|
|
42 |
array(
|
|
|
43 |
'help' => false,
|
|
|
44 |
'install' => false,
|
|
|
45 |
'parallel' => 0,
|
|
|
46 |
'run' => 0,
|
|
|
47 |
'drop' => false,
|
|
|
48 |
'enable' => false,
|
|
|
49 |
'disable' => false,
|
|
|
50 |
'diag' => false,
|
|
|
51 |
'tags' => '',
|
|
|
52 |
'updatesteps' => false,
|
|
|
53 |
'optimize-runs' => '',
|
|
|
54 |
'add-core-features-to-theme' => false,
|
|
|
55 |
'axe' => true,
|
|
|
56 |
'scss-deprecations' => false,
|
|
|
57 |
),
|
|
|
58 |
array(
|
|
|
59 |
'h' => 'help',
|
|
|
60 |
'o' => 'optimize-runs',
|
|
|
61 |
'a' => 'add-core-features-to-theme',
|
|
|
62 |
)
|
|
|
63 |
);
|
|
|
64 |
|
|
|
65 |
if ($options['install'] or $options['drop']) {
|
|
|
66 |
define('CACHE_DISABLE_ALL', true);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
// Checking util_single_run.php CLI script usage.
|
|
|
70 |
$help = "
|
|
|
71 |
Behat utilities to manage the test environment
|
|
|
72 |
|
|
|
73 |
Usage:
|
|
|
74 |
php util_single_run.php [--install|--drop|--enable|--disable|--diag|--updatesteps|--help]
|
|
|
75 |
|
|
|
76 |
Options:
|
|
|
77 |
--install Installs the test environment for acceptance tests
|
|
|
78 |
--drop Drops the database tables and the dataroot contents
|
|
|
79 |
--enable Enables test environment and updates tests list
|
|
|
80 |
--disable Disables test environment
|
|
|
81 |
--diag Get behat test environment status code
|
|
|
82 |
--updatesteps Update feature step file.
|
|
|
83 |
--no-axe Disable axe accessibility tests.
|
|
|
84 |
--scss-deprecations Enable SCSS deprecation checks.
|
|
|
85 |
|
|
|
86 |
-o, --optimize-runs Split features with specified tags in all parallel runs.
|
|
|
87 |
-a, --add-core-features-to-theme Add all core features to specified theme's
|
|
|
88 |
|
|
|
89 |
-h, --help Print out this help
|
|
|
90 |
|
|
|
91 |
Example from Moodle root directory:
|
|
|
92 |
\$ php admin/tool/behat/cli/util_single_run.php --enable
|
|
|
93 |
|
|
|
94 |
More info in https://moodledev.io/general/development/tools/behat/running
|
|
|
95 |
";
|
|
|
96 |
|
|
|
97 |
if (!empty($options['help'])) {
|
|
|
98 |
echo $help;
|
|
|
99 |
exit(0);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
// Describe this script.
|
|
|
103 |
define('BEHAT_UTIL', true);
|
|
|
104 |
define('CLI_SCRIPT', true);
|
|
|
105 |
define('NO_OUTPUT_BUFFERING', true);
|
|
|
106 |
define('IGNORE_COMPONENT_CACHE', true);
|
|
|
107 |
|
|
|
108 |
// Set run value, to be used by setup for configuring proper CFG variables.
|
|
|
109 |
if ($options['run']) {
|
|
|
110 |
define('BEHAT_CURRENT_RUN', $options['run']);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
// Only load CFG from config.php, stop ASAP in lib/setup.php.
|
|
|
114 |
define('ABORT_AFTER_CONFIG', true);
|
|
|
115 |
require_once(__DIR__ . '/../../../../config.php');
|
|
|
116 |
|
|
|
117 |
// Remove error handling overrides done in config.php.
|
|
|
118 |
$CFG->debug = (E_ALL | E_STRICT);
|
|
|
119 |
$CFG->debugdisplay = 1;
|
|
|
120 |
error_reporting($CFG->debug);
|
|
|
121 |
ini_set('display_errors', '1');
|
|
|
122 |
ini_set('log_errors', '1');
|
|
|
123 |
|
|
|
124 |
// Finish moodle init.
|
|
|
125 |
define('ABORT_AFTER_CONFIG_CANCEL', true);
|
|
|
126 |
require("$CFG->dirroot/lib/setup.php");
|
|
|
127 |
|
|
|
128 |
raise_memory_limit(MEMORY_HUGE);
|
|
|
129 |
|
|
|
130 |
require_once($CFG->libdir.'/adminlib.php');
|
|
|
131 |
require_once($CFG->libdir.'/upgradelib.php');
|
|
|
132 |
require_once($CFG->libdir.'/clilib.php');
|
|
|
133 |
require_once($CFG->libdir.'/installlib.php');
|
|
|
134 |
require_once($CFG->libdir.'/testing/classes/test_lock.php');
|
|
|
135 |
|
|
|
136 |
if ($unrecognized) {
|
|
|
137 |
$unrecognized = implode(PHP_EOL . " ", $unrecognized);
|
|
|
138 |
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
// Behat utilities.
|
|
|
142 |
require_once($CFG->libdir . '/behat/classes/util.php');
|
|
|
143 |
require_once($CFG->libdir . '/behat/classes/behat_command.php');
|
|
|
144 |
require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
|
|
|
145 |
|
|
|
146 |
// Ensure run option is <= parallel run installed.
|
|
|
147 |
$run = 0;
|
|
|
148 |
$parallel = 0;
|
|
|
149 |
if ($options['run']) {
|
|
|
150 |
$run = $options['run'];
|
|
|
151 |
// If parallel option is not passed, then try get it form config.
|
|
|
152 |
if (!$options['parallel']) {
|
|
|
153 |
$parallel = behat_config_manager::get_behat_run_config_value('parallel');
|
|
|
154 |
} else {
|
|
|
155 |
$parallel = $options['parallel'];
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
if (empty($parallel) || $run > $parallel) {
|
|
|
159 |
echo "Parallel runs can't be more then ".$parallel.PHP_EOL;
|
|
|
160 |
exit(1);
|
|
|
161 |
}
|
|
|
162 |
$CFG->behatrunprocess = $run;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
// Run command (only one per time).
|
|
|
166 |
if ($options['install']) {
|
|
|
167 |
behat_util::install_site();
|
|
|
168 |
|
|
|
169 |
// This is only displayed once for parallel install.
|
|
|
170 |
if (empty($run)) {
|
|
|
171 |
mtrace("Acceptance tests site installed");
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
// Note: Do not build the themes here. This is done during the 'enable' stage.
|
|
|
175 |
|
|
|
176 |
} else if ($options['drop']) {
|
|
|
177 |
// Ensure no tests are running.
|
|
|
178 |
test_lock::acquire('behat');
|
|
|
179 |
behat_util::drop_site();
|
|
|
180 |
// This is only displayed once for parallel install.
|
|
|
181 |
if (empty($run)) {
|
|
|
182 |
mtrace("Acceptance tests site dropped");
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
} else if ($options['enable']) {
|
|
|
186 |
if (!empty($parallel)) {
|
|
|
187 |
// Save parallel site info for enable and install options.
|
|
|
188 |
behat_config_manager::set_behat_run_config_value('behatsiteenabled', 1);
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
// Configure axe according to option.
|
|
|
192 |
behat_config_manager::set_behat_run_config_value('axe', $options['axe']);
|
|
|
193 |
|
|
|
194 |
// Define whether to run Behat with SCSS deprecation checks.
|
|
|
195 |
behat_config_manager::set_behat_run_config_value('scss-deprecations', $options['scss-deprecations']);
|
|
|
196 |
|
|
|
197 |
// Enable test mode.
|
|
|
198 |
$timestart = microtime(true);
|
|
|
199 |
mtrace('Creating Behat configuration ...', '');
|
|
|
200 |
behat_util::start_test_mode($options['add-core-features-to-theme'], $options['optimize-runs'], $parallel, $run);
|
|
|
201 |
mtrace(' done in ' . round(microtime(true) - $timestart, 2) . ' seconds.');
|
|
|
202 |
|
|
|
203 |
// Themes are only built in the 'enable' command.
|
|
|
204 |
behat_util::build_themes(true);
|
|
|
205 |
mtrace("Testing environment themes built");
|
|
|
206 |
|
|
|
207 |
// This is only displayed once for parallel install.
|
|
|
208 |
if (empty($run)) {
|
|
|
209 |
// Notify user that 2.5 profile has been converted to 3.5.
|
|
|
210 |
if (behat_config_manager::$autoprofileconversion) {
|
|
|
211 |
mtrace("2.5 behat profile detected, automatically converted to current 3.x format");
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
$runtestscommand = behat_command::get_behat_command(true, !empty($run));
|
|
|
215 |
|
|
|
216 |
$runtestscommand .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
|
|
|
217 |
mtrace("Acceptance tests environment enabled on $CFG->behat_wwwroot, to run the tests use: " . PHP_EOL .
|
|
|
218 |
$runtestscommand);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
} else if ($options['disable']) {
|
|
|
222 |
behat_util::stop_test_mode($run);
|
|
|
223 |
// This is only displayed once for parallel install.
|
|
|
224 |
if (empty($run)) {
|
|
|
225 |
mtrace("Acceptance tests environment disabled");
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
} else if ($options['diag']) {
|
|
|
229 |
$code = behat_util::get_behat_status();
|
|
|
230 |
exit($code);
|
|
|
231 |
|
|
|
232 |
} else if ($options['updatesteps']) {
|
|
|
233 |
if (defined('BEHAT_FEATURE_STEP_FILE') && BEHAT_FEATURE_STEP_FILE) {
|
|
|
234 |
$behatstepfile = BEHAT_FEATURE_STEP_FILE;
|
|
|
235 |
} else {
|
|
|
236 |
echo "BEHAT_FEATURE_STEP_FILE is not set, please ensure you set this to writable file" . PHP_EOL;
|
|
|
237 |
exit(1);
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
// Run behat command to get steps in feature files.
|
|
|
241 |
$featurestepscmd = behat_command::get_behat_command(true);
|
|
|
242 |
$featurestepscmd .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
|
|
|
243 |
$featurestepscmd .= ' --dry-run --format=moodle_stepcount';
|
|
|
244 |
$processes = cli_execute_parallel(array($featurestepscmd), __DIR__ . "/../../../../");
|
|
|
245 |
$status = print_update_step_output(array_pop($processes), $behatstepfile);
|
|
|
246 |
|
|
|
247 |
exit($status);
|
|
|
248 |
} else {
|
|
|
249 |
echo $help;
|
|
|
250 |
exit(1);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
exit(0);
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* Print update progress as dots for updating feature file step list.
|
|
|
257 |
*
|
|
|
258 |
* @param Process $process process executing update step command.
|
|
|
259 |
* @param string $featurestepfile feature step file in which steps will be saved.
|
|
|
260 |
* @return int exitcode.
|
|
|
261 |
*/
|
|
|
262 |
function print_update_step_output($process, $featurestepfile) {
|
|
|
263 |
$printedlength = 0;
|
|
|
264 |
|
|
|
265 |
echo "Updating steps feature file for parallel behat runs" . PHP_EOL;
|
|
|
266 |
|
|
|
267 |
// Show progress while running command.
|
|
|
268 |
while ($process->isRunning()) {
|
|
|
269 |
usleep(10000);
|
|
|
270 |
$op = $process->getIncrementalOutput();
|
|
|
271 |
if (trim($op)) {
|
|
|
272 |
echo ".";
|
|
|
273 |
$printedlength++;
|
|
|
274 |
if ($printedlength > 70) {
|
|
|
275 |
$printedlength = 0;
|
|
|
276 |
echo PHP_EOL;
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
// If any error then exit.
|
|
|
282 |
$exitcode = $process->getExitCode();
|
|
|
283 |
// Output err.
|
|
|
284 |
if ($exitcode != 0) {
|
|
|
285 |
echo $process->getErrorOutput();
|
|
|
286 |
exit($exitcode);
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
// Extract features with step info and save it in file.
|
|
|
290 |
$featuresteps = $process->getOutput();
|
|
|
291 |
$featuresteps = explode(PHP_EOL, $featuresteps);
|
|
|
292 |
|
|
|
293 |
$realroot = realpath(__DIR__.'/../../../../').'/';
|
|
|
294 |
foreach ($featuresteps as $featurestep) {
|
|
|
295 |
if (trim($featurestep)) {
|
|
|
296 |
$step = explode("::", $featurestep);
|
|
|
297 |
$step[0] = str_replace($realroot, '', $step[0]);
|
|
|
298 |
$steps[$step[0]] = $step[1];
|
|
|
299 |
}
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
if ($existing = @json_decode(file_get_contents($featurestepfile), true)) {
|
|
|
303 |
$steps = array_merge($existing, $steps);
|
|
|
304 |
}
|
|
|
305 |
arsort($steps);
|
|
|
306 |
|
|
|
307 |
if (!@file_put_contents($featurestepfile, json_encode($steps, JSON_PRETTY_PRINT))) {
|
|
|
308 |
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $featurestepfile . ' can not be created');
|
|
|
309 |
$exitcode = -1;
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
echo PHP_EOL. "Updated step count in " . $featurestepfile . PHP_EOL;
|
|
|
313 |
|
|
|
314 |
return $exitcode;
|
|
|
315 |
}
|