From 0897e190937372cc6c8333eb44c3e0200d4d45d3 Mon Sep 17 00:00:00 2001 From: Luke Carrier Date: Tue, 16 Jun 2015 12:11:18 +0100 Subject: [PATCH] MDL-50611 test: use cURL extension to download Composer The curl binary is nowhere near as common in Windows environments as it is in Linux and Mac ones. In order to encourage more users to adopt Behat and PHPUnit for their testing, we should avoid introducing unnecessary hurdles. --- lib/behat/classes/behat_command.php | 4 +-- lib/behat/lib.php | 3 +- lib/testing/lib.php | 51 +++++++++++++++++++++++------ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/lib/behat/classes/behat_command.php b/lib/behat/classes/behat_command.php index 9c0d6839388..ed309f057cb 100644 --- a/lib/behat/classes/behat_command.php +++ b/lib/behat/classes/behat_command.php @@ -157,7 +157,7 @@ class behat_command { // Returning composer error code to avoid conflicts with behat and moodle error codes. self::output_msg(get_string('errorcomposer', 'tool_behat')); - return BEHAT_EXITCODE_COMPOSER; + return TESTING_EXITCODE_COMPOSER; } // Behat test command. @@ -167,7 +167,7 @@ class behat_command { // Returning composer error code to avoid conflicts with behat and moodle error codes. self::output_msg(get_string('errorbehatcommand', 'tool_behat', self::get_behat_command())); - return BEHAT_EXITCODE_COMPOSER; + return TESTING_EXITCODE_COMPOSER; } // No empty values. diff --git a/lib/behat/lib.php b/lib/behat/lib.php index 7a422ed9c24..d80486ec4dd 100644 --- a/lib/behat/lib.php +++ b/lib/behat/lib.php @@ -38,7 +38,6 @@ define('BEHAT_EXITCODE_REQUIREMENT', 251); define('BEHAT_EXITCODE_PERMISSIONS', 252); define('BEHAT_EXITCODE_REINSTALL', 253); define('BEHAT_EXITCODE_INSTALL', 254); -define('BEHAT_EXITCODE_COMPOSER', 255); define('BEHAT_EXITCODE_INSTALLED', 256); /** @@ -458,4 +457,4 @@ function cli_execute_parallel($cmds, $cwd = null) { } } return $processes; -} \ No newline at end of file +} diff --git a/lib/testing/lib.php b/lib/testing/lib.php index de9db16bca6..bfe0c303c0d 100644 --- a/lib/testing/lib.php +++ b/lib/testing/lib.php @@ -25,6 +25,13 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +/** + * Composer error exit status. + * + * @var integer + */ +define('TESTING_EXITCODE_COMPOSER', 255); + /** * Returns relative path against current working directory, * to be used for shell execution hints. @@ -170,22 +177,45 @@ function testing_error($errorcode, $text = '') { * @return void exit() if something goes wrong */ function testing_update_composer_dependencies() { - // To restore the value after finishing. $cwd = getcwd(); - // Dirroot. - chdir(__DIR__ . '/../..'); + // Set some paths. + $dirroot = dirname(dirname(__DIR__)); + $composerpath = $dirroot . DIRECTORY_SEPARATOR . 'composer.phar'; + $composerurl = 'https://getcomposer.org/composer.phar'; - // Download composer.phar if we can. - if (!file_exists(__DIR__ . '/../../composer.phar')) { - passthru("curl http://getcomposer.org/installer | php", $code); - if ($code != 0) { - exit($code); + // Switch to Moodle's dirroot for easier path handling. + chdir($dirroot); + + // Download or update composer.phar. Unfortunately we can't use the curl + // class in filelib.php as we're running within one of the test platforms. + if (!file_exists($composerpath)) { + $file = @fopen($composerpath, 'w+'); + if ($file === false) { + $errordetails = error_get_last(); + $error = sprintf("Unable to open composer.phar\nPHP error: %s", + $errordetails['message']); + testing_error(TESTING_EXITCODE_COMPOSER, $error); + } + $curl = curl_init(); + + curl_setopt($curl, CURLOPT_URL, $composerurl); + curl_setopt($curl, CURLOPT_FILE, $file); + $result = curl_exec($curl); + + $curlerrno = curl_errno($curl); + $curlerror = curl_error($curl); + + curl_close($curl); + fclose($file); + + if (!$result) { + $error = sprintf("Unable to download composer.phar\ncURL error (%d): %s", + $curlerrno, $curlerror); + testing_error(TESTING_EXITCODE_COMPOSER, $error); } } else { - - // If it is already there update the installer. passthru("php composer.phar self-update", $code); if ($code != 0) { exit($code); @@ -198,5 +228,6 @@ function testing_update_composer_dependencies() { exit($code); } + // Return to our original location. chdir($cwd); }