Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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