Skip to content

Commit

Permalink
MDL-32323 allow execution of tests through util.php and add option to…
Browse files Browse the repository at this point in the history
… include alternative phpunit files (for testing only!)
  • Loading branch information
skodak committed Apr 10, 2012
1 parent 5987e69 commit 3604563
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
71 changes: 56 additions & 15 deletions admin/tool/phpunit/cli/util.php
Expand Up @@ -31,22 +31,12 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('PHPUNIT_UTIL', true);

require_once(__DIR__ . '/../../../../lib/phpunit/bootstraplib.php');

// verify PHPUnit installation
if (!@include_once('PHPUnit/Autoload.php')) {
phpunit_bootstrap_error(130);
if (isset($_SERVER['REMOTE_ADDR'])) {
die; // no access from web!
}

require(__DIR__ . '/../../../../lib/phpunit/bootstrap.php');
require_once($CFG->libdir.'/phpunit/lib.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/upgradelib.php');
require_once($CFG->libdir.'/clilib.php');
require_once($CFG->libdir.'/pluginlib.php');
require_once($CFG->libdir.'/installlib.php');
require_once(__DIR__.'/../../../../lib/clilib.php');
require_once(__DIR__.'/../../../../lib/phpunit/bootstraplib.php');

// now get cli options
list($options, $unrecognized) = cli_get_params(
Expand All @@ -55,13 +45,63 @@
'install' => false,
'buildconfig' => false,
'diag' => false,
'phpunitdir' => false,
'run' => false,
'help' => false,
),
array(
'h' => 'help'
)
);

if ($options['phpunitdir']) {
// nasty skodak's hack for testing of future PHPUnit versions - intentionally not documented
if (!file_exists($options['phpunitdir'])) {
cli_error('Invalid custom PHPUnit lib location');
}
$files = scandir($options['phpunitdir']);
foreach ($files as $file) {
$path = $options['phpunitdir'].'/'.$file;
if (!is_dir($path) or strpos($file, '.') === 0) {
continue;
}
ini_set('include_path', $path . PATH_SEPARATOR . ini_get('include_path'));
}
unset($files);
unset($file);
}

// verify PHPUnit libs are loaded
if (!@include_once('PHPUnit/Autoload.php')) {
phpunit_bootstrap_error(130);
}

if ($options['run']) {
unset($options);
unset($unrecognized);

foreach ($_SERVER['argv'] as $k=>$v) {
if (strpos($v, '--run') === 0 or strpos($v, '--phpunitdir') === 0) {
unset($_SERVER['argv'][$k]);
}
}
$_SERVER['argv'] = array_values($_SERVER['argv']);
PHPUnit_TextUI_Command::main();
exit(0);
}

define('PHPUNIT_UTIL', true);

require(__DIR__ . '/../../../../lib/phpunit/bootstrap.php');

// from now on this is a regular moodle CLI_SCRIPT

require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/upgradelib.php');
require_once($CFG->libdir.'/clilib.php');
require_once($CFG->libdir.'/pluginlib.php');
require_once($CFG->libdir.'/installlib.php');

if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
Expand All @@ -80,11 +120,12 @@
--install Install database
--buildconfig Build /phpunit.xml from /phpunit.xml.dist that includes suites for all plugins and core
--diag Diagnose installation and return error code only
--run Execute PHPUnit tests (alternative for standard phpunit binary)
-h, --help Print out this help
Example:
\$/usr/bin/php lib/phpunit/tool.php
\$/usr/bin/php lib/phpunit/tool.php --install
";
echo $help;
exit(0);
Expand Down
7 changes: 1 addition & 6 deletions admin/tool/phpunit/webrunner.php
Expand Up @@ -52,11 +52,6 @@
tool_phpunit_problem('$CFG->phpunit_dataroot in not writable, can not execute tests.');
}
$output = null;
exec('phpunit --version', $output, $code);
if ($code != 0) {
tool_phpunit_problem('Can not execute \'phpunit\' script.');
}
$output = null;
exec('php --version', $output, $code);
if ($code != 0) {
tool_phpunit_problem('Can not execute \'php\' binary.');
Expand Down Expand Up @@ -140,7 +135,7 @@
$path = escapeshellcmd($path);
$path = str_replace('\*', '*', $path);
chdir($CFG->dirroot);
passthru("phpunit -c $configdir $path", $code);
passthru("php $CFG->admin/tool/phpunit/cli/util.php --run -c $configdir $path", $code);
chdir($oldcwd);

echo '</pre>';
Expand Down
2 changes: 1 addition & 1 deletion lib/clilib.php
Expand Up @@ -24,7 +24,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();
// NOTE: no MOODLE_INTERNAL test here, sometimes we use this before requiring Moodle libs!

/**
* Get input from user
Expand Down

0 comments on commit 3604563

Please sign in to comment.