From 19a40b1468faf06bc0697dac53bfc50ea06da2a4 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Wed, 8 Jan 2014 09:40:11 +0800 Subject: [PATCH 1/2] MDL-43461 behat: Using linux-style directory separators when using cygwin --- admin/tool/behat/cli/util.php | 4 ++-- lib/behat/classes/behat_command.php | 20 +++++++++++++++++-- lib/behat/classes/behat_config_manager.php | 23 ++++++++++++++++++---- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/admin/tool/behat/cli/util.php b/admin/tool/behat/cli/util.php index 790406fa1466e..d843bf2fb6bb4 100644 --- a/admin/tool/behat/cli/util.php +++ b/admin/tool/behat/cli/util.php @@ -127,8 +127,8 @@ mtrace("Acceptance tests site dropped"); } else if ($options['enable']) { behat_util::start_test_mode(); - $runtestscommand = behat_command::get_behat_command() . ' --config ' - . $CFG->behat_dataroot . DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . 'behat.yml'; + $runtestscommand = behat_command::get_behat_command(true) . + ' --config ' . behat_config_manager::get_behat_cli_config_filepath(); mtrace("Acceptance tests environment enabled on $CFG->behat_wwwroot, to run the tests use:\n " . $runtestscommand); } else if ($options['disable']) { behat_util::stop_test_mode(); diff --git a/lib/behat/classes/behat_command.php b/lib/behat/classes/behat_command.php index 619dcd6588836..b70afaaf59375 100644 --- a/lib/behat/classes/behat_command.php +++ b/lib/behat/classes/behat_command.php @@ -66,10 +66,26 @@ public static function get_behat_dir() { /** * Returns the executable path + * + * Allows returning a customized command for cygwin when the + * command is just displayed, when using exec(), system() and + * friends we stay with DIRECTORY_SEPARATOR as they use the + * normal cmd.exe (in Windows). + * + * @param bool $custombyterm If the provided command should depend on the terminal where it runs * @return string */ - public final static function get_behat_command() { - return 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'behat'; + public final static function get_behat_command($custombyterm = false) { + + $separator = DIRECTORY_SEPARATOR; + $exec = 'behat'; + + // Cygwin uses linux-style directory separators. + if ($custombyterm && testing_is_cygwin()) { + $exec = 'behat.bat'; + $separator = '/'; + } + return 'vendor' . $separator . 'bin' . $separator . $exec; } /** diff --git a/lib/behat/classes/behat_config_manager.php b/lib/behat/classes/behat_config_manager.php index a967cad3527c5..5c05312b4b267 100644 --- a/lib/behat/classes/behat_config_manager.php +++ b/lib/behat/classes/behat_config_manager.php @@ -154,21 +154,36 @@ public static function get_components_steps_definitions() { /** * Returns the behat config file path used by the steps definition list * - * Note this can only be called from web-based scripts so it will return the - * production dataroot not behat_dataroot. With this the steps definitions - * list is accessible without having to install the behat test site. - * * @return string */ public static function get_steps_list_config_filepath() { global $USER; + // We don't cygwin-it as it is called using exec(). $userdir = behat_command::get_behat_dir() . '/users/' . $USER->id; make_writable_directory($userdir); return $userdir . '/behat.yml'; } + /** + * Returns the behat config file path used by the behat cli command. + * + * @return string + */ + public static function get_behat_cli_config_filepath() { + global $CFG; + + $command = $CFG->behat_dataroot . DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . 'behat.yml'; + + // Cygwin uses linux-style directory separators. + if (testing_is_cygwin()) { + $command = str_replace('\\', '/', $command); + } + + return $command; + } + /** * Behat config file specifing the main context class, * the required Behat extensions and Moodle test wwwroot. From ff7139d6cbe3a430344dcb6eb206fca34adc9680 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Wed, 8 Jan 2014 11:05:08 +0800 Subject: [PATCH 2/2] MDL-43461 testing: Avoid switching to .bat files when running MinGW MinGW can not execute .bat files (http://sourceforge.net/p/mingw/bugs/1902) so we should not switch to .bat files when running MinGW. --- lib/behat/classes/behat_command.php | 6 +++++- lib/behat/classes/behat_config_manager.php | 2 +- lib/phpunit/classes/hint_resultprinter.php | 4 +++- lib/testing/lib.php | 24 ++++++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/behat/classes/behat_command.php b/lib/behat/classes/behat_command.php index b70afaaf59375..76a4d258699d8 100644 --- a/lib/behat/classes/behat_command.php +++ b/lib/behat/classes/behat_command.php @@ -82,8 +82,12 @@ public final static function get_behat_command($custombyterm = false) { // Cygwin uses linux-style directory separators. if ($custombyterm && testing_is_cygwin()) { - $exec = 'behat.bat'; $separator = '/'; + + // MinGW can not execute .bat scripts. + if (!testing_is_mingw()) { + $exec = 'behat.bat'; + } } return 'vendor' . $separator . 'bin' . $separator . $exec; } diff --git a/lib/behat/classes/behat_config_manager.php b/lib/behat/classes/behat_config_manager.php index 5c05312b4b267..7da36b90290a2 100644 --- a/lib/behat/classes/behat_config_manager.php +++ b/lib/behat/classes/behat_config_manager.php @@ -159,7 +159,7 @@ public static function get_components_steps_definitions() { public static function get_steps_list_config_filepath() { global $USER; - // We don't cygwin-it as it is called using exec(). + // We don't cygwin-it as it is called using exec() which uses cmd.exe. $userdir = behat_command::get_behat_dir() . '/users/' . $USER->id; make_writable_directory($userdir); diff --git a/lib/phpunit/classes/hint_resultprinter.php b/lib/phpunit/classes/hint_resultprinter.php index a04b32628d219..2d5dfe79b6c7f 100644 --- a/lib/phpunit/classes/hint_resultprinter.php +++ b/lib/phpunit/classes/hint_resultprinter.php @@ -108,7 +108,9 @@ protected function printDefectTrace(PHPUnit_Framework_TestFailure $defect) { $executable = 'phpunit'; if (testing_is_cygwin()) { $file = str_replace('\\', '/', $file); - $executable = 'phpunit.bat'; + if (!testing_is_mingw()) { + $executable = 'phpunit.bat'; + } } } diff --git a/lib/testing/lib.php b/lib/testing/lib.php index c67a1a9c207b0..13c84668880a5 100644 --- a/lib/testing/lib.php +++ b/lib/testing/lib.php @@ -97,6 +97,30 @@ function testing_is_cygwin() { } } +/** + * Returns whether a mingw CLI is running. + * + * MinGW sets $_SERVER['TERM'] to cygwin, but it + * can not run .bat files; this function may be useful + * when we need to output proposed commands to users + * using Windows CLI interfaces. + * + * @link http://sourceforge.net/p/mingw/bugs/1902 + * @return bool + */ +function testing_is_mingw() { + + if (!testing_is_cygwin()) { + return false; + } + + if (!empty($_SERVER['MSYSTEM'])) { + return true; + } + + return false; +} + /** * Mark empty dataroot to be used for testing. * @param string $dataroot The dataroot directory