source: moodle/trunk/fuentes/admin/tool/behat/cli/util_single_run.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 8.6 KB
Line 
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 *
23 * @package    tool_behat
24 * @copyright  2012 David Monllaó
25 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28
29if (isset($_SERVER['REMOTE_ADDR'])) {
30    die(); // No access from web!.
31}
32
33// Basic functions.
34require_once(__DIR__ . '/../../../../lib/clilib.php');
35require_once(__DIR__ . '/../../../../lib/behat/lib.php');
36
37// CLI options.
38list($options, $unrecognized) = cli_get_params(
39    array(
40        'help'        => false,
41        'install'     => false,
42        'parallel'    => 0,
43        'run'         => '',
44        'drop'        => false,
45        'enable'      => false,
46        'disable'     => false,
47        'diag'        => false,
48        'tags'        => '',
49        'updatesteps' => false,
50    ),
51    array(
52        'h' => 'help'
53    )
54);
55
56if ($options['install'] or $options['drop']) {
57    define('CACHE_DISABLE_ALL', true);
58}
59
60// Checking util_single_run.php CLI script usage.
61$help = "
62Behat utilities to manage the test environment
63
64Usage:
65  php util_single_run.php [--install|--drop|--enable|--disable|--diag|--updatesteps|--help]
66
67Options:
68--install     Installs the test environment for acceptance tests
69--drop        Drops the database tables and the dataroot contents
70--enable      Enables test environment and updates tests list
71--disable     Disables test environment
72--diag        Get behat test environment status code
73--updatesteps Update feature step file.
74
75-h, --help Print out this help
76
77Example from Moodle root directory:
78\$ php admin/tool/behat/cli/util_single_run.php --enable
79
80More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests
81";
82
83if (!empty($options['help'])) {
84    echo $help;
85    exit(0);
86}
87
88// Describe this script.
89define('BEHAT_UTIL', true);
90define('CLI_SCRIPT', true);
91define('NO_OUTPUT_BUFFERING', true);
92define('IGNORE_COMPONENT_CACHE', true);
93
94// Set run value, to be used by setup for configuring proper CFG variables.
95if ($options['run']) {
96    define('BEHAT_CURRENT_RUN', $options['run']);
97}
98
99// Only load CFG from config.php, stop ASAP in lib/setup.php.
100define('ABORT_AFTER_CONFIG', true);
101require_once(__DIR__ . '/../../../../config.php');
102
103// Remove error handling overrides done in config.php.
104$CFG->debug = (E_ALL | E_STRICT);
105$CFG->debugdisplay = 1;
106error_reporting($CFG->debug);
107ini_set('display_errors', '1');
108ini_set('log_errors', '1');
109
110// Finish moodle init.
111define('ABORT_AFTER_CONFIG_CANCEL', true);
112require("$CFG->dirroot/lib/setup.php");
113
114raise_memory_limit(MEMORY_HUGE);
115
116require_once($CFG->libdir.'/adminlib.php');
117require_once($CFG->libdir.'/upgradelib.php');
118require_once($CFG->libdir.'/clilib.php');
119require_once($CFG->libdir.'/installlib.php');
120require_once($CFG->libdir.'/testing/classes/test_lock.php');
121
122if ($unrecognized) {
123    $unrecognized = implode(PHP_EOL . "  ", $unrecognized);
124    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
125}
126
127// Behat utilities.
128require_once($CFG->libdir . '/behat/classes/util.php');
129require_once($CFG->libdir . '/behat/classes/behat_command.php');
130require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
131
132// Ensure run option is <= parallel run installed.
133if ($options['run']) {
134    if (!$options['parallel']) {
135        $options['parallel'] = behat_config_manager::get_parallel_test_runs();
136    }
137    if (empty($options['parallel']) || $options['run'] > $options['parallel']) {
138        echo "Parallel runs can't be more then ".$options['parallel'].PHP_EOL;
139        exit(1);
140    }
141    $CFG->behatrunprocess = $options['run'];
142}
143
144// Run command (only one per time).
145if ($options['install']) {
146    behat_util::install_site();
147
148    // This is only displayed once for parallel install.
149    if (empty($options['run'])) {
150        mtrace("Acceptance tests site installed");
151    }
152
153} else if ($options['drop']) {
154    // Ensure no tests are running.
155    test_lock::acquire('behat');
156    behat_util::drop_site();
157    // This is only displayed once for parallel install.
158    if (empty($options['run'])) {
159        mtrace("Acceptance tests site dropped");
160    }
161
162} else if ($options['enable']) {
163    if (!empty($options['parallel'])) {
164        // Save parallel site info for enable and install options.
165        $filepath = behat_config_manager::get_parallel_test_file_path();
166        if (!file_put_contents($filepath, $options['parallel'])) {
167            behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $filepath . ' can not be created');
168        }
169    }
170
171    // Enable test mode.
172    behat_util::start_test_mode();
173
174    // This is only displayed once for parallel install.
175    if (empty($options['run'])) {
176        $runtestscommand = behat_command::get_behat_command(true, !empty($options['run']));
177
178        $runtestscommand .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
179        mtrace("Acceptance tests environment enabled on $CFG->behat_wwwroot, to run the tests use: " . PHP_EOL .
180            $runtestscommand);
181    }
182
183} else if ($options['disable']) {
184    behat_util::stop_test_mode();
185    // This is only displayed once for parallel install.
186    if (empty($options['run'])) {
187        mtrace("Acceptance tests environment disabled");
188    }
189
190} else if ($options['diag']) {
191    $code = behat_util::get_behat_status();
192    exit($code);
193
194} else if ($options['updatesteps']) {
195    if (defined('BEHAT_FEATURE_STEP_FILE') && BEHAT_FEATURE_STEP_FILE) {
196        $behatstepfile = BEHAT_FEATURE_STEP_FILE;
197    } else {
198        echo "BEHAT_FEATURE_STEP_FILE is not set, please ensure you set this to writable file" . PHP_EOL;
199        exit(1);
200    }
201
202    // Run behat command to get steps in feature files.
203    $featurestepscmd = behat_command::get_behat_command(true);
204    $featurestepscmd .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
205    $featurestepscmd .= ' --dry-run --format=moodle_step_count';
206    $processes = cli_execute_parallel(array($featurestepscmd), __DIR__ . "/../../../../");
207    $status = print_update_step_output(array_pop($processes), $behatstepfile);
208
209    exit($status);
210} else {
211    echo $help;
212    exit(1);
213}
214
215exit(0);
216
217/**
218 * Print update progress as dots for updating feature file step list.
219 *
220 * @param Process $process process executing update step command.
221 * @param string $featurestepfile feature step file in which steps will be saved.
222 * @return int exitcode.
223 */
224function print_update_step_output($process, $featurestepfile) {
225    $printedlength = 0;
226
227    echo "Updating steps feature file for parallel behat runs" . PHP_EOL;
228
229    // Show progress while running command.
230    while ($process->isRunning()) {
231        usleep(10000);
232        $op = $process->getIncrementalOutput();
233        if (trim($op)) {
234            echo ".";
235            $printedlength++;
236            if ($printedlength > 70) {
237                $printedlength = 0;
238                echo PHP_EOL;
239            }
240        }
241    }
242
243    // If any error then exit.
244    $exitcode = $process->getExitCode();
245    // Output err.
246    if ($exitcode != 0) {
247        echo $process->getErrorOutput();
248        exit($exitcode);
249    }
250
251    // Extract features with step info and save it in file.
252    $featuresteps = $process->getOutput();
253    $featuresteps = explode(PHP_EOL, $featuresteps);
254
255    $realroot = realpath(__DIR__.'/../../../../').'/';
256    foreach ($featuresteps as $featurestep) {
257        if (trim($featurestep)) {
258            $step = explode("::", $featurestep);
259            $step[0] = str_replace($realroot, '', $step[0]);
260            $steps[$step[0]] = $step[1];
261        }
262    }
263
264    if ($existing = @json_decode(file_get_contents($featurestepfile), true)) {
265        $steps = array_merge($existing, $steps);
266    }
267    arsort($steps);
268
269    if (!@file_put_contents($featurestepfile, json_encode($steps, JSON_PRETTY_PRINT))) {
270        behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $featurestepfile . ' can not be created');
271        $exitcode = -1;
272    }
273
274    echo PHP_EOL. "Updated step count in " . $featurestepfile . PHP_EOL;
275
276    return $exitcode;
277}
Note: See TracBrowser for help on using the repository browser.