MDL-32400 rework phpunit exit codes, use only php based init

This commit is contained in:
Petr Skoda 2012-04-13 12:45:18 +02:00
parent 589376d385
commit 0d8e51a65f
9 changed files with 57 additions and 115 deletions

View file

@ -1,22 +0,0 @@
@ECHO OFF
ECHO Initialising Moodle PHPUnit test environment...
CALL php %~dp0\util.php --diag > NUL 2>&1
IF ERRORLEVEL 133 GOTO drop
IF ERRORLEVEL 132 GOTO install
IF ERRORLEVEL 1 GOTO unknown
GOTO done
:drop
CALL php %~dp0\util.php --drop
IF ERRORLEVEL 1 GOTO done
:install
CALL php %~dp0\util.php --install
GOTO done
:unknown
CALL php %~dp0\util.php --diag
:done

View file

@ -43,13 +43,13 @@ exec("php util.php --diag", $output, $code);
if ($code == 0) { if ($code == 0) {
// everything is ready // everything is ready
} else if ($code == 132) { } else if ($code == PHPUNIT_EXITCODE_INSTALL) {
passthru("php util.php --install", $code); passthru("php util.php --install", $code);
if ($code != 0) { if ($code != 0) {
exit($code); exit($code);
} }
} else if ($code == 133) { } else if ($code == PHPUNIT_EXITCODE_REINSTALL) {
passthru("php util.php --drop", $code); passthru("php util.php --drop", $code);
passthru("php util.php --install", $code); passthru("php util.php --install", $code);
if ($code != 0) { if ($code != 0) {

View file

@ -1,31 +0,0 @@
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
CLIDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
UTIL="$CLIDIR/util.php"
echo "Initialising Moodle PHPUnit test environment..."
DIGERROR=`php $UTIL --diag`
DIAG=$?
if [ $DIAG -eq 132 ] ; then
php $UTIL --install
else
if [ $DIAG -eq 133 ] ; then
php $UTIL --drop
RESULT=$?
if [ $RESULT -gt 0 ] ; then
exit $RESULT
fi
php $UTIL --install
else
if [ $DIAG -gt 0 ] ; then
echo $DIGERROR
exit $DIAG
fi
fi
fi
php $UTIL --buildconfig

View file

@ -17,14 +17,7 @@
/** /**
* PHPUnit related utilities. * PHPUnit related utilities.
* *
* Exit codes: * Exit codes: {@see phpunit_bootstrap_error()}
* 0 - success
* 1 - general error
* 130 - missing PHPUnit library error
* 131 - configuration problem
* 132 - install new test database
* 133 - drop existing data before installing
* 134 - can not create main phpunit.xml
* *
* @package tool_phpunit * @package tool_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org} * @copyright 2012 Petr Skoda {@link http://skodak.org}
@ -73,11 +66,7 @@ if ($options['phpunitdir']) {
// verify PHPUnit libs are loaded // verify PHPUnit libs are loaded
if (!@include_once('PHPUnit/Autoload.php')) { if (!@include_once('PHPUnit/Autoload.php')) {
phpunit_bootstrap_error(130); phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITMISSING);
}
if (!@include_once('PHPUnit/Extensions/Database/Autoload.php')) {
phpunit_bootstrap_error(130);
} }
if ($options['run']) { if ($options['run']) {
@ -147,7 +136,7 @@ if ($diag) {
if (phpunit_util::build_config_file()) { if (phpunit_util::build_config_file()) {
exit(0); exit(0);
} else { } else {
phpunit_bootstrap_error(134); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, 'Can not create phpunit.xml configuration file, verify dirroot permissions');
} }
} else if ($drop) { } else if ($drop) {

View file

@ -67,7 +67,7 @@ if ($execute) {
if ($code == 0) { if ($code == 0) {
// everything is ready // everything is ready
} else if ($code == 132) { } else if ($code == PHPUNIT_EXITCODE_INSTALL) {
tool_phpunit_header(); tool_phpunit_header();
echo $OUTPUT->box_start('generalbox'); echo $OUTPUT->box_start('generalbox');
echo '<pre>'; echo '<pre>';
@ -87,7 +87,7 @@ if ($execute) {
echo $OUTPUT->footer(); echo $OUTPUT->footer();
die(); die();
} else if ($code == 133) { } else if ($code == PHPUNIT_EXITCODE_REINSTALL) {
tool_phpunit_header(); tool_phpunit_header();
echo $OUTPUT->box_start('generalbox'); echo $OUTPUT->box_start('generalbox');
echo '<pre>'; echo '<pre>';

View file

@ -18,13 +18,7 @@
* Prepares PHPUnit environment, the phpunit.xml configuration * Prepares PHPUnit environment, the phpunit.xml configuration
* must specify this file as bootstrap. * must specify this file as bootstrap.
* *
* Exit codes: * Exit codes: {@see phpunit_bootstrap_error()}
* 0 - success
* 1 - general error
* 130 - missing PHPUnit library error
* 131 - configuration problem
* 132 - install new test database
* 133 - drop existing data before installing
* *
* @package core * @package core
* @category phpunit * @category phpunit
@ -63,10 +57,14 @@ $phpunitversion = PHPUnit_Runner_Version::id();
if ($phpunitversion === '@package_version@') { if ($phpunitversion === '@package_version@') {
// library checked out from git, let's hope dev knows that 3.6.0 is required // library checked out from git, let's hope dev knows that 3.6.0 is required
} else if (version_compare($phpunitversion, '3.6.0', 'lt')) { } else if (version_compare($phpunitversion, '3.6.0', 'lt')) {
phpunit_bootstrap_error(129, $phpunitversion); phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITWRONG, $phpunitversion);
} }
unset($phpunitversion); unset($phpunitversion);
if (!@include_once('PHPUnit/Extensions/Database/Autoload.php')) {
phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITEXTMISSING, 'phpunit/DbUnit');
}
define('NO_OUTPUT_BUFFERING', true); define('NO_OUTPUT_BUFFERING', true);
// only load CFG from config.php, stop ASAP in lib/setup.php // only load CFG from config.php, stop ASAP in lib/setup.php
@ -93,16 +91,16 @@ if (isset($CFG->phpunit_directorypermissions)) {
} }
$CFG->filepermissions = ($CFG->directorypermissions & 0666); $CFG->filepermissions = ($CFG->directorypermissions & 0666);
if (!isset($CFG->phpunit_dataroot)) { if (!isset($CFG->phpunit_dataroot)) {
phpunit_bootstrap_error(131, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
} }
if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) { if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
phpunit_bootstrap_error(131, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
} }
if (!file_exists($CFG->phpunit_dataroot)) { if (!file_exists($CFG->phpunit_dataroot)) {
mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions); mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
} }
if (!is_dir($CFG->phpunit_dataroot)) { if (!is_dir($CFG->phpunit_dataroot)) {
phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory can not be created, can not run tests!'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
} }
if (!is_writable($CFG->phpunit_dataroot)) { if (!is_writable($CFG->phpunit_dataroot)) {
@ -115,7 +113,7 @@ if (!is_writable($CFG->phpunit_dataroot)) {
} }
} }
if (!is_writable($CFG->phpunit_dataroot)) { if (!is_writable($CFG->phpunit_dataroot)) {
phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory is not writable, can not run tests!'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
} }
} }
if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) { if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
@ -124,7 +122,7 @@ if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') { if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
continue; continue;
} }
phpunit_bootstrap_error(131, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
} }
closedir($dh); closedir($dh);
unset($dh); unset($dh);
@ -137,13 +135,13 @@ if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
// verify db prefix // verify db prefix
if (!isset($CFG->phpunit_prefix)) { if (!isset($CFG->phpunit_prefix)) {
phpunit_bootstrap_error(131, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
} }
if ($CFG->phpunit_prefix === '') { if ($CFG->phpunit_prefix === '') {
phpunit_bootstrap_error(131, '$CFG->phpunit_prefix can not be empty, can not run tests!'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_prefix can not be empty, can not run tests!');
} }
if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) { if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
phpunit_bootstrap_error(131, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
} }
// override CFG settings if necessary and throw away extra CFG settings // override CFG settings if necessary and throw away extra CFG settings

View file

@ -25,6 +25,14 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
define('PHPUNIT_EXITCODE_PHPUNITMISSING', 129);
define('PHPUNIT_EXITCODE_PHPUNITWRONG', 130);
define('PHPUNIT_EXITCODE_PHPUNITEXTMISSING', 131);
define('PHPUNIT_EXITCODE_CONFIGERROR', 135);
define('PHPUNIT_EXITCODE_CONFIGWARNING', 136);
define('PHPUNIT_EXITCODE_INSTALL', 140);
define('PHPUNIT_EXITCODE_REINSTALL', 141);
/** /**
* Print error and stop execution * Print error and stop execution
* @param int $errorcode The exit error code * @param int $errorcode The exit error code
@ -39,35 +47,35 @@ function phpunit_bootstrap_error($errorcode, $text = '') {
case 1: case 1:
$text = 'Error: '.$text; $text = 'Error: '.$text;
break; break;
case 129: case PHPUNIT_EXITCODE_PHPUNITMISSING:
$text = "Moodle can not find PHPUnit PEAR library";
break;
case PHPUNIT_EXITCODE_PHPUNITWRONG:
$text = 'Moodle requires PHPUnit 3.6.x, '.$text.' is not compatible'; $text = 'Moodle requires PHPUnit 3.6.x, '.$text.' is not compatible';
break; break;
case 130: case PHPUNIT_EXITCODE_PHPUNITEXTMISSING:
$text = 'Moodle can not find PHPUnit PEAR library or necessary PHPUnit extension'; $text = 'Moodle can not find required PHPUnit extension '.$text;
break; break;
case 131: case PHPUNIT_EXITCODE_CONFIGERROR:
$text = 'Moodle configuration problem: '.$text; $text = "Moodle PHPUnit environment configuration error:\n".$text;
break; break;
case 132: case PHPUNIT_EXITCODE_CONFIGWARNING:
$text = "Moodle PHPUnit environment is not initialised, please use:\n php admin/tool/phpunit/cli/util.php --install"; $text = "Moodle PHPUnit environment configuration warning:\n".$text;
break; break;
case 133: case PHPUNIT_EXITCODE_INSTALL:
$text = "Moodle PHPUnit environment was initialised for different version, please use:\n php admin/tool/phpunit/cli/util.php --drop\n php admin/tool/phpunit/cli/util.php --install"; $text = "Moodle PHPUnit environment is not initialised, please use:\n php admin/tool/phpunit/cli/init.php";
break; break;
case 134: case PHPUNIT_EXITCODE_REINSTALL:
$text = 'Moodle can not create PHPUnit configuration file, please verify dirroot permissions'; $text = "Moodle PHPUnit environment was initialised for different version, please use:\n php admin/tool/phpunit/cli/init.php";
break; break;
default: default:
$text = empty($text) ? '' : ': '.$text; $text = empty($text) ? '' : ': '.$text;
$text = 'Unknown error '.$errorcode.$text; $text = 'Unknown error '.$errorcode.$text;
break; break;
} }
if (defined('PHPUNIT_UTIL') and PHPUNIT_UTIL) {
// do not write to error stream because we need the error message in PHP exec result from web ui // do not write to error stream because we need the error message in PHP exec result from web ui
echo($text."\n"); echo($text."\n");
} else {
fwrite(STDERR, $text."\n");
}
exit($errorcode); exit($errorcode);
} }

View file

@ -640,31 +640,31 @@ class phpunit_util {
if (!self::is_test_site()) { if (!self::is_test_site()) {
// dataroot was verified in bootstrap, so it must be DB // dataroot was verified in bootstrap, so it must be DB
return array(131, 'Can not use database for testing, try different prefix'); return array(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not use database for testing, try different prefix');
} }
if (empty($tables)) { if (empty($tables)) {
return array(132, ''); return array(PHPUNIT_EXITCODE_INSTALL, '');
} }
if (!file_exists("$CFG->dataroot/phpunit/tabledata.ser") or !file_exists("$CFG->dataroot/phpunit/tablestructure.ser")) { if (!file_exists("$CFG->dataroot/phpunit/tabledata.ser") or !file_exists("$CFG->dataroot/phpunit/tablestructure.ser")) {
return array(133, ''); return array(PHPUNIT_EXITCODE_REINSTALL, '');
} }
if (!file_exists("$CFG->dataroot/phpunit/versionshash.txt")) { if (!file_exists("$CFG->dataroot/phpunit/versionshash.txt")) {
return array(133, ''); return array(PHPUNIT_EXITCODE_REINSTALL, '');
} }
$hash = phpunit_util::get_version_hash(); $hash = phpunit_util::get_version_hash();
$oldhash = file_get_contents("$CFG->dataroot/phpunit/versionshash.txt"); $oldhash = file_get_contents("$CFG->dataroot/phpunit/versionshash.txt");
if ($hash !== $oldhash) { if ($hash !== $oldhash) {
return array(133, ''); return array(PHPUNIT_EXITCODE_REINSTALL, '');
} }
$dbhash = get_config('core', 'phpunittest'); $dbhash = get_config('core', 'phpunittest');
if ($hash !== $dbhash) { if ($hash !== $dbhash) {
return array(133, ''); return array(PHPUNIT_EXITCODE_REINSTALL, '');
} }
return array(0, ''); return array(0, '');
@ -682,7 +682,7 @@ class phpunit_util {
global $DB, $CFG; global $DB, $CFG;
if (!self::is_test_site()) { if (!self::is_test_site()) {
phpunit_bootstrap_error(131, 'Can not drop non-test site!!'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not drop non-test site!!');
} }
// purge dataroot // purge dataroot
@ -727,13 +727,13 @@ class phpunit_util {
global $DB, $CFG; global $DB, $CFG;
if (!self::is_test_site()) { if (!self::is_test_site()) {
phpunit_bootstrap_error(131, 'Can not install on non-test site!!'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not install on non-test site!!');
} }
if ($DB->get_tables()) { if ($DB->get_tables()) {
list($errorcode, $message) = phpunit_util::testing_ready_problem(); list($errorcode, $message) = phpunit_util::testing_ready_problem();
if ($errorcode) { if ($errorcode) {
phpunit_bootstrap_error(133, 'Database tables already present, Moodle PHPUnit test environment can not be initialised'); phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
} else { } else {
phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised'); phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised');
} }

View file

@ -13,7 +13,7 @@ Installation
1. install PEAR package manager - see [PEAR Manual](http://pear.php.net/manual/en/installation.php) 1. install PEAR package manager - see [PEAR Manual](http://pear.php.net/manual/en/installation.php)
2. install PHPUnit package and phpunit/DbUnit extension - see [PHPUnit installation documentation](http://www.phpunit.de/manual/current/en/installation.html) 2. install PHPUnit package and phpunit/DbUnit extension - see [PHPUnit installation documentation](http://www.phpunit.de/manual/current/en/installation.html)
3. edit main config.php - add `$CFG->phpunit_prefix` and `$CFG->phpunit_dataroot` - see config-dist.php 3. edit main config.php - add `$CFG->phpunit_prefix` and `$CFG->phpunit_dataroot` - see config-dist.php
4. execute `admin/tool/phpunit/cli/init.sh` to initialise the test environemnt, repeat it after every upgrade or installation of plugins 4. execute `php admin/tool/phpunit/cli/init.php` to initialise the test environemnt, repeat it after every upgrade or installation of plugins
Test execution Test execution
@ -29,7 +29,7 @@ How to add more tests?
2. add `local/mytest/tests/my_test.php` file with `local_my_testcase` class that extends `basic_testcase` or `advanced_testcase` 2. add `local/mytest/tests/my_test.php` file with `local_my_testcase` class that extends `basic_testcase` or `advanced_testcase`
3. add some test_*() methods 3. add some test_*() methods
4. execute your new test case `phpunit local_my_testcase local/mytest/tests/my_test.php` 4. execute your new test case `phpunit local_my_testcase local/mytest/tests/my_test.php`
5. execute `admin/tool/phpunit/cli/init.sh` to get the plugin tests included in main phpunit.xml configuration file 5. execute `php admin/tool/phpunit/cli/init.php` to get the plugin tests included in main phpunit.xml configuration file
How to convert existing tests? How to convert existing tests?