Skip to content

Commit

Permalink
MDL-39752 behat: Added support for behat_wwwroot and behat_dataroot
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Mar 8, 2015
1 parent b1c36d9 commit 5b9e6b5
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 23 deletions.
8 changes: 6 additions & 2 deletions admin/tool/behat/cli/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,17 @@

// Update config file if tags defined.
if ($tags) {
// Hack to set proper dataroot and wwroot.
// Hack to set proper dataroot and wwwroot.
$behatdataroot = $CFG->behat_dataroot;
$behatwwwroot = $CFG->behat_wwwroot;
for ($i = 1; $i <= $parallelrun; $i++) {
$CFG->behatrunprocess = $i;
$CFG->behat_dataroot = $behatdataroot . $i;
$CFG->behat_wwwroot = $behatwwwroot . "/" . BEHAT_PARALLEL_SITE_NAME . $i;
if (!empty($CFG->behat_parallel_run['behat_wwwroot'][$i - 1]['behat_wwwroot'])) {
$CFG->behat_wwwroot = $CFG->behat_parallel_run['behat_wwwroot'][$i - 1]['behat_wwwroot'];
} else {
$CFG->behat_wwwroot = $behatwwwroot . "/" . BEHAT_PARALLEL_SITE_NAME . $i;
}
behat_config_manager::update_config_file('', true, $tags);
}
$CFG->behat_dataroot = $behatdataroot;
Expand Down
7 changes: 6 additions & 1 deletion admin/tool/behat/cli/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@

// Display all sites which are installed/drop/diabled.
for ($i = $options['fromrun']; $i <= $options['torun']; $i++) {
echo $CFG->behat_wwwroot . "/" . BEHAT_PARALLEL_SITE_NAME . $i . PHP_EOL;
if (empty($CFG->behat_parallel_run[$i - 1]['behat_wwwroot'])) {
echo $CFG->behat_wwwroot . "/" . BEHAT_PARALLEL_SITE_NAME . $i . PHP_EOL;
} else {
echo $CFG->behat_parallel_run[$i - 1]['behat_wwwroot'] . PHP_EOL;
}

}
} else if ($options['drop']) {
echo "Acceptance tests site dropped for " . $options['parallel'] . " parallel sites" . PHP_EOL;
Expand Down
4 changes: 3 additions & 1 deletion config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,9 @@
// 'dbuser' => 'moodle',
// 'dbpass' => 'moodle',
// 'behat_prefix' => 'mdl_',
// 'wd_host' => 'http://127.0.0.1:4444/wd/hub'
// 'wd_host' => 'http://127.0.0.1:4444/wd/hub',
// 'behat_wwwroot' => 'http://127.0.0.1/moodle',
// 'behat_dataroot' => '/home/example/bht_moodledata'
// ),
// );
//
Expand Down
10 changes: 7 additions & 3 deletions lib/behat/classes/behat_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ class behat_command {
public static function get_behat_dir($runprocess = 0) {
global $CFG;

$runprocess = empty($runprocess) ? "" : $runprocess;

$behatdir = $CFG->behat_dataroot . $runprocess . '/behat';
if (empty($runprocess)) {
$behatdir = $CFG->behat_dataroot . '/behat';
} else if (isset($CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'])) {
$behatdir = $CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'] . '/behat';;
} else {
$behatdir = $CFG->behat_dataroot . $runprocess . '/behat';
}

if (!is_dir($behatdir)) {
if (!mkdir($behatdir, $CFG->directorypermissions, true)) {
Expand Down
14 changes: 13 additions & 1 deletion lib/behat/classes/behat_config_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ public static function get_behat_cli_config_filepath($runprocess = 0) {
global $CFG;

if ($runprocess) {
$command = $CFG->behat_dataroot . $runprocess;
if (isset($CFG->behat_parallel_run[$runprocess - 1 ]['behat_dataroot'])) {
$command = $CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'];
} else {
$command = $CFG->behat_dataroot . $runprocess;
}
} else {
$command = $CFG->behat_dataroot;
}
Expand Down Expand Up @@ -276,6 +280,10 @@ public final static function drop_parallel_site_links() {
// If parallel run then remove links and original file.
clearstatcache();
for ($i = 1; $i <= $parallelrun; $i++) {
// Don't delete links for specified sites, as they should be accessible.
if (!empty($CFG->behat_parallel_run['behat_wwwroot'][$i - 1]['behat_wwwroot'])) {
continue;
}
$link = $CFG->dirroot . '/' . BEHAT_PARALLEL_SITE_NAME . $i;
if (file_exists($link) && is_link($link)) {
@unlink($link);
Expand All @@ -297,6 +305,10 @@ public final static function create_parallel_site_links($fromrun, $torun) {
// Create site symlink if necessary.
clearstatcache();
for ($i = $fromrun; $i <= $torun; $i++) {
// Don't create links for specified sites, as they should be accessible.
if (!empty($CFG->behat_parallel_run['behat_wwwroot'][$i - 1]['behat_wwwroot'])) {
continue;
}
$link = $CFG->dirroot.'/'.BEHAT_PARALLEL_SITE_NAME.$i;
clearstatcache();
if (file_exists($link)) {
Expand Down
57 changes: 42 additions & 15 deletions lib/behat/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,25 @@ function behat_is_test_site() {
function behat_update_vars_for_process() {
global $CFG;

$allowedconfigoverride = array('dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'behat_prefix');
$allowedconfigoverride = array('dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'behat_prefix',
'behat_wwwroot', 'behat_dataroot');
$behatrunprocess = behat_get_run_process();
$CFG->behatrunprocess = $behatrunprocess;

if ($behatrunprocess) {
// Set www root for run process.
if (isset($CFG->behat_wwwroot) && !preg_match("#/" . BEHAT_PARALLEL_SITE_NAME . $behatrunprocess . "\$#",
$CFG->behat_wwwroot)) {
$CFG->behat_wwwroot .= "/" . BEHAT_PARALLEL_SITE_NAME . $behatrunprocess;
if (empty($CFG->behat_parallel_run[$behatrunprocess - 1]['behat_wwwroot'])) {
// Set www root for run process.
if (isset($CFG->behat_wwwroot) &&
!preg_match("#/" . BEHAT_PARALLEL_SITE_NAME . $behatrunprocess . "\$#", $CFG->behat_wwwroot)) {
$CFG->behat_wwwroot .= "/" . BEHAT_PARALLEL_SITE_NAME . $behatrunprocess;
}
}

// Set behat_dataroot.
if (!preg_match("#" . $behatrunprocess . "\$#", $CFG->behat_dataroot)) {
$CFG->behat_dataroot .= $behatrunprocess;
if (empty($CFG->behat_parallel_run[$behatrunprocess - 1]['behat_dataroot'])) {
// Set behat_dataroot.
if (!preg_match("#" . $behatrunprocess . "\$#", $CFG->behat_dataroot)) {
$CFG->behat_dataroot .= $behatrunprocess;
}
}

// Set behat_prefix for db, just suffix run process number, to avoid max length exceed.
Expand Down Expand Up @@ -372,7 +377,17 @@ function behat_get_run_process() {
if (defined('BEHAT_CURRENT_RUN') && BEHAT_CURRENT_RUN) {
$behatrunprocess = BEHAT_CURRENT_RUN;
} else if (!empty($_SERVER['REMOTE_ADDR'])) {
if (preg_match('#/' . BEHAT_PARALLEL_SITE_NAME . '(.+?)/#', $_SERVER['REQUEST_URI'])) {
// Try get it from config if present.
if (!empty($CFG->behat_parallel_run)) {
foreach ($CFG->behat_parallel_run as $run => $behatconfig) {
if (isset($behatconfig['behat_wwwroot']) && behat_is_requested_url($behatconfig['behat_wwwroot'])) {
$behatrunprocess = $run + 1; // We start process from 1.
break;
}
}
}
// Check if parallel site prefix is used.
if (empty($behatrunprocess) && preg_match('#/' . BEHAT_PARALLEL_SITE_NAME . '(.+?)/#', $_SERVER['REQUEST_URI'])) {
$dirrootrealpath = str_replace("\\", "/", realpath($CFG->dirroot));
$serverrealpath = str_replace("\\", "/", realpath($_SERVER['SCRIPT_FILENAME']));
$afterpath = str_replace($dirrootrealpath.'/', '', $serverrealpath);
Expand All @@ -385,13 +400,25 @@ function behat_get_run_process() {
} else if (defined('BEHAT_TEST') || defined('BEHAT_UTIL')) {
if ($match = preg_filter('#--run=(.+)#', '$1', $argv)) {
$behatrunprocess = reset($match);
}

if ($k = array_search('--config', $argv)) {
} else if ($k = array_search('--config', $argv)) {
$behatconfig = str_replace("\\", "/", $argv[$k + 1]);
$behatdataroot = str_replace("\\", "/", $CFG->behat_dataroot);
$behatrunprocess = preg_filter("#^{$behatdataroot}" .
"(.+?)[/|\\\]behat[/|\\\]behat\.yml#", '$1', $behatconfig);
// Try get it from config if present.
if (!empty($CFG->behat_parallel_run)) {
foreach ($CFG->behat_parallel_run as $run => $parallelconfig) {
if (!empty($parallelconfig['behat_dataroot']) &&
$parallelconfig['behat_dataroot'] . '/behat/behat.yml' == $behatconfig) {

$behatrunprocess = $run + 1; // We start process from 1.
break;
}
}
}
// Check if default behat datroot increment was done.
if (empty($behatrunprocess)) {
$behatdataroot = str_replace("\\", "/", $CFG->behat_dataroot);
$behatrunprocess = preg_filter("#^{$behatdataroot}" . "(.+?)[/|\\\]behat[/|\\\]behat\.yml#", '$1',
$behatconfig);
}
}
}

Expand Down

0 comments on commit 5b9e6b5

Please sign in to comment.