From 6967933d4ac5dec758d5b14d04dcdb8f7b77aaae Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Wed, 2 Mar 2016 17:07:46 +0800 Subject: [PATCH 1/2] MDL-53434 behat: Added support for $CFG->behat_profiles This config will be supported to avoid broken configuration in future. As this is limited, we need will still use ->behat_config to set config values which can't be set by behat_profiles --- admin/tool/behat/cli/run.php | 4 +- admin/tool/behat/cli/util_single_run.php | 1 + config-dist.php | 14 +++++ lib/behat/classes/behat_config_manager.php | 67 ++++++++++++++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/admin/tool/behat/cli/run.php b/admin/tool/behat/cli/run.php index 6e196598ff3..6808ebdf09a 100644 --- a/admin/tool/behat/cli/run.php +++ b/admin/tool/behat/cli/run.php @@ -120,8 +120,8 @@ $tags = ''; if ($options['profile']) { $profile = $options['profile']; - if (!isset($CFG->behat_config[$profile])) { - echo "Invalid profile passed: " . $profile; + if (!isset($CFG->behat_config[$profile]) && !isset($CFG->behat_profiles[$profile])) { + echo "Invalid profile passed: " . $profile . PHP_EOL; exit(1); } $extraopts[] = '--profile="' . $profile . '"'; diff --git a/admin/tool/behat/cli/util_single_run.php b/admin/tool/behat/cli/util_single_run.php index d523569375f..a8177fc6ce0 100644 --- a/admin/tool/behat/cli/util_single_run.php +++ b/admin/tool/behat/cli/util_single_run.php @@ -173,6 +173,7 @@ if ($options['install']) { // This is only displayed once for parallel install. if (empty($options['run'])) { + $runtestscommand = behat_command::get_behat_command(true, !empty($options['run'])); $runtestscommand .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath(); diff --git a/config-dist.php b/config-dist.php index df033d9a625..75245317e39 100644 --- a/config-dist.php +++ b/config-dist.php @@ -690,6 +690,20 @@ $CFG->admin = 'admin'; // ) // ) // ); +// You can also use the following config to override default Moodle configuration for Behat. +// This config is limited to default suite and will be supported in later versions. +// It will have precedence over $CFG->behat_config. +// $CFG->behat_profiles = array( +// 'phantomjs' => array( +// 'browser' => 'phantomjs', +// 'tags' => '~@_file_upload&&~@_alert&&~@_bug_phantomjs', +// 'wd_host' => 'http://127.0.0.1:4443/wd/hub', +// 'capabilities' => array( +// 'platform' => 'Linux', +// 'version' => 2.1 +// ) +// ), +// ); // // You can force the browser session (not user's sessions) to restart after N seconds. This could // be useful if you are using a cloud-based service with time restrictions in the browser side. diff --git a/lib/behat/classes/behat_config_manager.php b/lib/behat/classes/behat_config_manager.php index 0108064633e..faacf767e0c 100644 --- a/lib/behat/classes/behat_config_manager.php +++ b/lib/behat/classes/behat_config_manager.php @@ -434,10 +434,77 @@ class behat_config_manager { if (!empty($CFG->behat_config)) { $config = self::merge_config($config, $CFG->behat_config); } + // Check for Moodle custom ones. + if (!empty($CFG->behat_profiles) && is_array($CFG->behat_profiles)) { + foreach ($CFG->behat_profiles as $profile => $values) { + $config = self::merge_config($config, self::get_behat_profile($profile, $values)); + } + } return Symfony\Component\Yaml\Yaml::dump($config, 10, 2); } + /** + * Parse $CFG->behat_profile and return the array with required config structure for behat.yml. + * + * $CFG->behat_profiles = array( + * 'profile' = array( + * 'browser' => 'firefox', + * 'tags' => '@javascript', + * 'wd_host' => 'http://127.0.0.1:4444/wd/hub', + * 'capabilities' => array( + * 'platform' => 'Linux', + * 'version' => 44 + * ) + * ) + * ); + * + * @param string $profile profile name + * @param array $values values for profile. + * @return array + */ + protected static function get_behat_profile($profile, $values) { + // Values should be an array. + if (!is_array($values)) { + return array(); + } + + // Check suite values. + $behatprofilesuites = array(); + // Fill tags information. + if (isset($values['tags'])) { + $behatprofilesuites = array( + 'filters' => array( + 'tags' => $values['tags'], + ) + ); + } + + // Selenium2 config values. + $behatprofileextension = array(); + $seleniumconfig = array(); + if (isset($values['browser'])) { + $seleniumconfig['browser'] = $values['browser']; + } + if (isset($values['wd_host'])) { + $seleniumconfig['wd_host'] = $values['wd_host']; + } + if (isset($values['capabilities'])) { + $seleniumconfig['capabilities'] = $values['capabilities']; + } + if (!empty($seleniumconfig)) { + $behatprofileextension = array( + 'extensions' => array( + 'Behat\MinkExtension\Extension' => array( + 'selenium2' => $seleniumconfig, + ) + ) + ); + } + + return array($profile => array_merge($behatprofilesuites, $behatprofileextension)); + } + /** * Attempt to split feature list into fairish buckets using timing information, if available. * Simply add each one to lightest buckets until all files allocated. From e600e7c36809f9f3f2ff12ceac857a598657d79b Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Fri, 4 Mar 2016 13:33:41 +0800 Subject: [PATCH 2/2] MDL-53434 behat: Improved exit status of parallel run Exit status should contain pass/fail information of each run it is executing. Every bit of status will have information of pass/fail status of parallel process --- admin/tool/behat/cli/run.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/admin/tool/behat/cli/run.php b/admin/tool/behat/cli/run.php index 6808ebdf09a..3df999a3cd6 100644 --- a/admin/tool/behat/cli/run.php +++ b/admin/tool/behat/cli/run.php @@ -226,22 +226,27 @@ $exitcodes = print_combined_run_output($processes, $stoponfail); $time = round(microtime(true) - $time, 1); echo "Finished in " . gmdate("G\h i\m s\s", $time) . PHP_EOL . PHP_EOL; +ksort($exitcodes); // Print exit info from each run. -$status = false; +// Status bits contains pass/fail status of parallel runs. +$status = 0; +$processcounter = 0; foreach ($exitcodes as $exitcode) { - $status = (bool)$status || (bool)$exitcode; + if ($exitcode) { + $status |= (1 << $processcounter); + } + $processcounter++; } // Run finished. Show exit code and output from individual process. $verbose = empty($options['verbose']) ? false : true; -$verbose = $verbose || $status; +$verbose = $verbose || !empty($status); // Show exit code from each process, if any process failed. if ($verbose) { // Echo exit codes. echo "Exit codes for each behat run: " . PHP_EOL; - ksort($exitcodes); foreach ($exitcodes as $run => $exitcode) { echo $run . ": " . $exitcode . PHP_EOL; } @@ -263,7 +268,7 @@ print_each_process_info($processes, $verbose); // Remove site symlink if necessary. behat_config_manager::drop_parallel_site_links(); -exit((int) $status); +exit($status); /** * Signal handler for terminal exit.