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) {
// everything is ready
} else if ($code == 132) {
} else if ($code == PHPUNIT_EXITCODE_INSTALL) {
passthru("php util.php --install", $code);
if ($code != 0) {
exit($code);
}
} else if ($code == 133) {
} else if ($code == PHPUNIT_EXITCODE_REINSTALL) {
passthru("php util.php --drop", $code);
passthru("php util.php --install", $code);
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.
*
* Exit codes:
* 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
* Exit codes: {@see phpunit_bootstrap_error()}
*
* @package tool_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
@ -73,11 +66,7 @@ if ($options['phpunitdir']) {
// verify PHPUnit libs are loaded
if (!@include_once('PHPUnit/Autoload.php')) {
phpunit_bootstrap_error(130);
}
if (!@include_once('PHPUnit/Extensions/Database/Autoload.php')) {
phpunit_bootstrap_error(130);
phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITMISSING);
}
if ($options['run']) {
@ -147,7 +136,7 @@ if ($diag) {
if (phpunit_util::build_config_file()) {
exit(0);
} else {
phpunit_bootstrap_error(134);
phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, 'Can not create phpunit.xml configuration file, verify dirroot permissions');
}
} else if ($drop) {

View File

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

View File

@ -18,13 +18,7 @@
* Prepares PHPUnit environment, the phpunit.xml configuration
* must specify this file as bootstrap.
*
* Exit codes:
* 0 - success
* 1 - general error
* 130 - missing PHPUnit library error
* 131 - configuration problem
* 132 - install new test database
* 133 - drop existing data before installing
* Exit codes: {@see phpunit_bootstrap_error()}
*
* @package core
* @category phpunit
@ -63,10 +57,14 @@ $phpunitversion = PHPUnit_Runner_Version::id();
if ($phpunitversion === '@package_version@') {
// 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')) {
phpunit_bootstrap_error(129, $phpunitversion);
phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITWRONG, $phpunitversion);
}
unset($phpunitversion);
if (!@include_once('PHPUnit/Extensions/Database/Autoload.php')) {
phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITEXTMISSING, 'phpunit/DbUnit');
}
define('NO_OUTPUT_BUFFERING', true);
// 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);
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) {
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)) {
mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
}
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)) {
@ -115,7 +113,7 @@ 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")) {
@ -124,7 +122,7 @@ if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
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);
unset($dh);
@ -137,13 +135,13 @@ if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
// verify db 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 === '') {
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) {
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

View File

@ -25,6 +25,14 @@
* @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
* @param int $errorcode The exit error code
@ -39,35 +47,35 @@ function phpunit_bootstrap_error($errorcode, $text = '') {
case 1:
$text = 'Error: '.$text;
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';
break;
case 130:
$text = 'Moodle can not find PHPUnit PEAR library or necessary PHPUnit extension';
case PHPUNIT_EXITCODE_PHPUNITEXTMISSING:
$text = 'Moodle can not find required PHPUnit extension '.$text;
break;
case 131:
$text = 'Moodle configuration problem: '.$text;
case PHPUNIT_EXITCODE_CONFIGERROR:
$text = "Moodle PHPUnit environment configuration error:\n".$text;
break;
case 132:
$text = "Moodle PHPUnit environment is not initialised, please use:\n php admin/tool/phpunit/cli/util.php --install";
case PHPUNIT_EXITCODE_CONFIGWARNING:
$text = "Moodle PHPUnit environment configuration warning:\n".$text;
break;
case 133:
$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";
case PHPUNIT_EXITCODE_INSTALL:
$text = "Moodle PHPUnit environment is not initialised, please use:\n php admin/tool/phpunit/cli/init.php";
break;
case 134:
$text = 'Moodle can not create PHPUnit configuration file, please verify dirroot permissions';
case PHPUNIT_EXITCODE_REINSTALL:
$text = "Moodle PHPUnit environment was initialised for different version, please use:\n php admin/tool/phpunit/cli/init.php";
break;
default:
$text = empty($text) ? '' : ': '.$text;
$text = 'Unknown error '.$errorcode.$text;
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
echo($text."\n");
} else {
fwrite(STDERR, $text."\n");
}
// do not write to error stream because we need the error message in PHP exec result from web ui
echo($text."\n");
exit($errorcode);
}

View File

@ -640,31 +640,31 @@ class phpunit_util {
if (!self::is_test_site()) {
// 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)) {
return array(132, '');
return array(PHPUNIT_EXITCODE_INSTALL, '');
}
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")) {
return array(133, '');
return array(PHPUNIT_EXITCODE_REINSTALL, '');
}
$hash = phpunit_util::get_version_hash();
$oldhash = file_get_contents("$CFG->dataroot/phpunit/versionshash.txt");
if ($hash !== $oldhash) {
return array(133, '');
return array(PHPUNIT_EXITCODE_REINSTALL, '');
}
$dbhash = get_config('core', 'phpunittest');
if ($hash !== $dbhash) {
return array(133, '');
return array(PHPUNIT_EXITCODE_REINSTALL, '');
}
return array(0, '');
@ -682,7 +682,7 @@ class phpunit_util {
global $DB, $CFG;
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
@ -727,13 +727,13 @@ class phpunit_util {
global $DB, $CFG;
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()) {
list($errorcode, $message) = phpunit_util::testing_ready_problem();
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 {
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)
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
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
@ -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`
3. add some test_*() methods
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?