Proyectos de Subversion Moodle

Rev

| 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 script to set up all the behat test environment.
19
 *
20
 * @package    tool_behat
21
 * @copyright  2013 David Monllaó
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
if (isset($_SERVER['REMOTE_ADDR'])) {
26
    die(); // No access from web!
27
}
28
 
29
// Force OPcache reset if used, we do not want any stale caches
30
// when preparing test environment.
31
if (function_exists('opcache_reset')) {
32
    opcache_reset();
33
}
34
 
35
// Is not really necessary but adding it as is a CLI_SCRIPT.
36
define('CLI_SCRIPT', true);
37
define('CACHE_DISABLE_ALL', true);
38
 
39
// Basic functions.
40
require_once(__DIR__ . '/../../../../lib/clilib.php');
41
require_once(__DIR__ . '/../../../../lib/behat/lib.php');
42
 
43
list($options, $unrecognized) = cli_get_params(
44
    array(
45
        'parallel' => 0,
46
        'maxruns'  => false,
47
        'help'     => false,
48
        'fromrun'  => 1,
49
        'torun'    => 0,
50
        'optimize-runs' => '',
51
        'add-core-features-to-theme' => false,
52
        'axe'      => null,
53
        'disable-composer' => false,
54
        'composer-upgrade' => true,
55
        'composer-self-update' => true,
56
        'scss-deprecations' => false,
57
    ),
58
    array(
59
        'j' => 'parallel',
60
        'm' => 'maxruns',
61
        'h' => 'help',
62
        'o' => 'optimize-runs',
63
        'a' => 'add-core-features-to-theme',
64
    )
65
);
66
 
67
// Checking run.php CLI script usage.
68
$help = "
69
Behat utilities to initialise behat tests
70
 
71
Usage:
72
  php init.php      [--parallel=value [--maxruns=value] [--fromrun=value --torun=value]]
73
                    [--no-axe] [--scss-deprecations] [-o | --optimize-runs] [-a | --add-core-features-to-theme]
74
                    [--no-composer-self-update] [--no-composer-upgrade]
75
                    [--disable-composer]
76
                    [--help]
77
 
78
Options:
79
-j, --parallel      Number of parallel behat run to initialise
80
-m, --maxruns       Max parallel processes to be executed at one time
81
--fromrun           Execute run starting from (Used for parallel runs on different vms)
82
--torun             Execute run till (Used for parallel runs on different vms)
83
--no-axe            Disable axe accessibility tests.
84
--scss-deprecations Enable SCSS deprecation checks.
85
 
86
-o, --optimize-runs
87
                    Split features with specified tags in all parallel runs.
88
 
89
-a, --add-core-features-to-theme
90
                    Add all core features to specified theme's
91
 
92
--no-composer-self-update
93
                    Prevent upgrade of the composer utility using its self-update command
94
 
95
--no-composer-upgrade
96
                    Prevent update development dependencies using composer
97
 
98
--disable-composer
99
                    A shortcut to disable composer self-update and dependency update
100
                    Note: Installation of composer and/or dependencies will still happen as required
101
 
102
-h, --help          Print out this help
103
 
104
Example from Moodle root directory:
105
\$ php admin/tool/behat/cli/init.php --parallel=2
106
 
107
More info in https://moodledev.io/general/development/tools/behat/running
108
";
109
 
110
if (!empty($options['help'])) {
111
    echo $help;
112
    exit(0);
113
}
114
 
115
if ($options['axe']) {
116
    echo "Axe accessibility tests are enabled by default, to disable them, use the --no-axe option.\n";
117
} else if ($options['axe'] === false) {
118
    echo "Axe accessibility tests have been disabled.\n";
119
}
120
 
121
// Check which util file to call.
122
$utilfile = 'util_single_run.php';
123
$commandoptions = "";
124
// If parallel run then use utilparallel.
125
if ($options['parallel'] && $options['parallel'] > 1) {
126
    $utilfile = 'util.php';
127
    // Sanitize all input options, so they can be passed to util.
128
    foreach ($options as $option => $value) {
129
        $commandoptions .= behat_get_command_flags($option, $value);
130
    }
131
} else {
132
    // Only sanitize options for single run.
133
    $cmdoptionsforsinglerun = [
134
        'add-core-features-to-theme',
135
        'axe',
136
        'scss-deprecations',
137
    ];
138
 
139
    foreach ($cmdoptionsforsinglerun as $option) {
140
        $commandoptions .= behat_get_command_flags($option, $options[$option]);
141
    }
142
}
143
 
144
// Changing the cwd to admin/tool/behat/cli.
145
$cwd = getcwd();
146
$output = null;
147
 
148
if ($options['disable-composer']) {
149
    // Disable self-update and upgrade easily.
150
    // Note: Installation will still occur regardless of this setting.
151
    $options['composer-self-update'] = false;
152
    $options['composer-upgrade'] = false;
153
}
154
 
155
// Install and update composer and dependencies as required.
156
testing_update_composer_dependencies($options['composer-self-update'], $options['composer-upgrade']);
157
 
158
// Check whether the behat test environment needs to be updated.
159
chdir(__DIR__);
160
exec("php $utilfile --diag $commandoptions", $output, $code);
161
 
162
if ($code == 0) {
163
    echo "Behat test environment already installed\n";
164
 
165
} else if ($code == BEHAT_EXITCODE_INSTALL) {
166
    // Behat and dependencies are installed and we need to install the test site.
167
    chdir(__DIR__);
168
    passthru("php $utilfile --install $commandoptions", $code);
169
    if ($code != 0) {
170
        chdir($cwd);
171
        exit($code);
172
    }
173
 
174
} else if ($code == BEHAT_EXITCODE_REINSTALL) {
175
    // Test site data is outdated.
176
    chdir(__DIR__);
177
    passthru("php $utilfile --drop $commandoptions", $code);
178
    if ($code != 0) {
179
        chdir($cwd);
180
        exit($code);
181
    }
182
 
183
    chdir(__DIR__);
184
    passthru("php $utilfile --install $commandoptions", $code);
185
    if ($code != 0) {
186
        chdir($cwd);
187
        exit($code);
188
    }
189
 
190
} else {
191
    // Generic error, we just output it.
192
    echo implode("\n", $output)."\n";
193
    chdir($cwd);
194
    exit($code);
195
}
196
 
197
// Enable editing mode according to config.php vars.
198
chdir(__DIR__);
199
passthru("php $utilfile --enable $commandoptions", $code);
200
if ($code != 0) {
201
    echo "Error enabling site" . PHP_EOL;
202
    chdir($cwd);
203
    exit($code);
204
}
205
 
206
exit(0);