Added regex pattern for branch specific config. Issue #97.

This commit is contained in:
Dmitry Khomutov 2017-07-18 07:42:34 +07:00
parent 0debda46b1
commit 13e492240e
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
2 changed files with 116 additions and 16 deletions

View file

@ -49,7 +49,7 @@ class Executor
*
* @return bool
*/
public function executePlugins(&$config, $stage)
public function executePlugins($config, $stage)
{
$success = true;
$pluginsToExecute = [];
@ -70,27 +70,55 @@ class Executor
return $success;
}
/**
* @param array $config
* @param string $branch
*
* @return bool|array
*/
public function getBranchSpecificConfig($config, $branch)
{
$configSections = array_keys($config);
foreach ($configSections as $configSection) {
if (0 === strpos($configSection, 'branch-')) {
if ($configSection === ('branch-' . $branch)) {
return $config[$configSection];
}
if (0 === strpos($configSection, 'branch-regex:')) {
$pattern = '#' . substr($configSection, 13) . '#u';
preg_match($pattern, $branch, $matches);
if (!empty($matches[0])) {
return $config[$configSection];
}
}
}
}
return [];
}
/**
* Check the config for any plugins specific to the branch we're currently building.
* @param $config
* @param $stage
* @param $pluginsToExecute
*
* @param array $config
* @param string $stage
* @param array $pluginsToExecute
*
* @return array
*/
protected function getBranchSpecificPlugins(&$config, $stage, $pluginsToExecute)
protected function getBranchSpecificPlugins($config, $stage, $pluginsToExecute)
{
/** @var \PHPCensor\Model\Build $build */
$build = $this->pluginFactory->getResourceFor('PHPCensor\Model\Build');
$branch = $build->getBranch();
// If we don't have any branch-specific plugins:
if (!isset($config['branch-' . $branch][$stage]) || !is_array($config['branch-' . $branch][$stage])) {
$build = $this->pluginFactory->getResourceFor('PHPCensor\Model\Build');
$branch = $build->getBranch();
$branchConfig = $this->getBranchSpecificConfig($config, $branch);
if (!$branchConfig) {
return $pluginsToExecute;
}
// If we have branch-specific plugins to execute, add them to the list to be executed:
$branchConfig = $config['branch-' . $branch];
$plugins = $branchConfig[$stage];
$plugins = !empty($branchConfig[$stage]) ? $branchConfig[$stage] : [];
$runOption = 'after';
if (!empty($branchConfig['run-option'])) {

View file

@ -31,6 +31,13 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
);
}
protected function getFakePluginClassName($pluginName)
{
$pluginNamespace = '\\Tests\\PHPCensor\\Plugin\\Util\\Fake\\';
return $pluginNamespace . $pluginName;
}
public function testExecutePlugin_AssumesNamespaceIfNoneGiven()
{
$options = [];
@ -147,11 +154,76 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase
$this->testedExecutor->executePlugins($config, 'stageOne');
}
protected function getFakePluginClassName($pluginName)
public function testGetBranchSpecificConfig()
{
$pluginNamespace = '\\Tests\\PHPCensor\\Plugin\\Util\\Fake\\';
$config = [
'setup' => [
'composer' => 'install',
]
];
return $pluginNamespace . $pluginName;
$this->assertEquals([], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1'));
$config = [
'setup' => [
'composer' => 'install',
],
'branch-branch-1' => [
'phpunit' => [],
],
];
$this->assertEquals(['phpunit' => []], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1'));
$config = [
'setup' => [
'composer' => 'install',
],
'branch-branch-2' => [
'phpunit' => [],
],
];
$this->assertEquals([], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1'));
$config = [
'setup' => [
'composer' => [
'install',
],
],
'branch-regex:.+' => [
'phpunit' => [],
],
];
$this->assertEquals(['phpunit' => []], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1'));
$config = [
'setup' => [
'composer' => [
'install',
],
],
'branch-regex:^branch\-\d$' => [
'phpunit' => [],
],
];
$this->assertEquals(['phpunit' => []], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1'));
$config = [
'setup' => [
'composer' => [
'install',
],
],
'branch-regex:^branch\-\w{2,}$' => [
'phpunit' => [],
],
];
$this->assertEquals([], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1'));
}
}