MDL-34147 use cygwin style paths in phpunit hints
[moodle.git] / admin / tool / phpunit / cli / util.php
CommitLineData
5bd40408
PS
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 * PHPUnit related utilities.
19 *
0d8e51a6 20 * Exit codes: {@see phpunit_bootstrap_error()}
5bd40408
PS
21 *
22 * @package tool_phpunit
23 * @copyright 2012 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
3604563c
PS
27if (isset($_SERVER['REMOTE_ADDR'])) {
28 die; // no access from web!
4b173695
PS
29}
30
3604563c
PS
31require_once(__DIR__.'/../../../../lib/clilib.php');
32require_once(__DIR__.'/../../../../lib/phpunit/bootstraplib.php');
5bd40408
PS
33
34// now get cli options
35list($options, $unrecognized) = cli_get_params(
36 array(
8e5c963e
PS
37 'drop' => false,
38 'install' => false,
39 'buildconfig' => false,
40 'buildcomponentconfigs' => false,
41 'diag' => false,
42 'phpunitdir' => false,
43 'run' => false,
44 'help' => false,
5bd40408
PS
45 ),
46 array(
47 'h' => 'help'
48 )
49);
50
3604563c
PS
51if ($options['phpunitdir']) {
52 // nasty skodak's hack for testing of future PHPUnit versions - intentionally not documented
53 if (!file_exists($options['phpunitdir'])) {
54 cli_error('Invalid custom PHPUnit lib location');
55 }
56 $files = scandir($options['phpunitdir']);
57 foreach ($files as $file) {
58 $path = $options['phpunitdir'].'/'.$file;
59 if (!is_dir($path) or strpos($file, '.') === 0) {
60 continue;
61 }
62 ini_set('include_path', $path . PATH_SEPARATOR . ini_get('include_path'));
63 }
64 unset($files);
65 unset($file);
66}
67
68// verify PHPUnit libs are loaded
0684ea99 69if (!include_once('PHPUnit/Autoload.php')) {
0d8e51a6 70 phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITMISSING);
4e5ff72e
PS
71}
72
3604563c
PS
73if ($options['run']) {
74 unset($options);
75 unset($unrecognized);
76
77 foreach ($_SERVER['argv'] as $k=>$v) {
78 if (strpos($v, '--run') === 0 or strpos($v, '--phpunitdir') === 0) {
79 unset($_SERVER['argv'][$k]);
2fde5392 80 $_SERVER['argc'] = $_SERVER['argc'] - 1;
3604563c
PS
81 }
82 }
83 $_SERVER['argv'] = array_values($_SERVER['argv']);
84 PHPUnit_TextUI_Command::main();
85 exit(0);
86}
87
88define('PHPUNIT_UTIL', true);
89
90require(__DIR__ . '/../../../../lib/phpunit/bootstrap.php');
91
92// from now on this is a regular moodle CLI_SCRIPT
93
94require_once($CFG->libdir.'/adminlib.php');
95require_once($CFG->libdir.'/upgradelib.php');
96require_once($CFG->libdir.'/clilib.php');
97require_once($CFG->libdir.'/pluginlib.php');
98require_once($CFG->libdir.'/installlib.php');
99
5bd40408
PS
100if ($unrecognized) {
101 $unrecognized = implode("\n ", $unrecognized);
102 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
103}
104
7b0ff213 105$diag = $options['diag'];
5bd40408
PS
106$drop = $options['drop'];
107$install = $options['install'];
108$buildconfig = $options['buildconfig'];
8e5c963e 109$buildcomponentconfigs = $options['buildcomponentconfigs'];
5bd40408 110
8e5c963e 111if ($options['help'] or (!$drop and !$install and !$buildconfig and !$buildcomponentconfigs and !$diag)) {
5bd40408
PS
112 $help = "Various PHPUnit utility functions
113
114Options:
8e5c963e
PS
115--drop Drop database and dataroot
116--install Install database
117--diag Diagnose installation and return error code only
118--run Execute PHPUnit tests (alternative for standard phpunit binary)
119--buildconfig Build /phpunit.xml from /phpunit.xml.dist that runs all tests
120--buildcomponentconfigs
121 Build distributed phpunit.xml files for each component
5bd40408 122
8e5c963e 123-h, --help Print out this help
5bd40408
PS
124
125Example:
880f8e7b 126\$ php ".phpunit_bootstrap_cli_argument_path('/admin/tool/phpunit/cli/util.php')." --install
5bd40408
PS
127";
128 echo $help;
7b0ff213 129 exit(0);
5bd40408
PS
130}
131
7b0ff213
PS
132if ($diag) {
133 list($errorcode, $message) = phpunit_util::testing_ready_problem();
134 if ($errorcode) {
135 phpunit_bootstrap_error($errorcode, $message);
136 }
5bd40408
PS
137 exit(0);
138
7b0ff213
PS
139} else if ($buildconfig) {
140 if (phpunit_util::build_config_file()) {
141 exit(0);
142 } else {
8e5c963e 143 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, 'Can not create main /phpunit.xml configuration file, verify dirroot permissions');
cb481742
EL
144 }
145
8e5c963e
PS
146} else if ($buildcomponentconfigs) {
147 phpunit_util::build_component_config_files();
148 exit(0);
7b0ff213 149
5bd40408 150} else if ($drop) {
4be2ad36
PS
151 // make sure tests do not run in parallel
152 phpunit_util::acquire_test_lock();
5bd40408
PS
153 phpunit_util::drop_site();
154 // note: we must stop here because $CFG is messed up and we can not reinstall, sorry
155 exit(0);
156
157} else if ($install) {
158 phpunit_util::install_site();
159 exit(0);
160}