Skip to content

Commit

Permalink
FIX Don't bootstrap when running behat -h or behat --help
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Feb 9, 2024
1 parent 1c4463f commit 901eb0b
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Behat\Testwork\ServiceContainer\ExtensionManager;
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
use RuntimeException;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

Expand All @@ -42,6 +43,7 @@ class Extension implements ExtensionInterface
*/
const SILVERSTRIPE_ID = 'silverstripe_extension';

private ?bool $shouldBootstrap = null;

/**
* {@inheritDoc}
Expand Down Expand Up @@ -78,8 +80,10 @@ public function initialize(ExtensionManager $extensionManager)
public function load(ContainerBuilder $container, array $config)
{
// Load yml config
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$loader->load('silverstripe.yml');
if ($this->getShouldBootstrap($container)) {
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$loader->load('silverstripe.yml');
}

// Add CLI substitutions
$this->loadSuiteLocator($container);
Expand All @@ -105,8 +109,10 @@ public function load(ContainerBuilder $container, array $config)
*/
public function process(ContainerBuilder $container)
{
$corePass = new Compiler\CoreInitializationPass();
$corePass->process($container);
if ($this->getShouldBootstrap($container)) {
$corePass = new Compiler\CoreInitializationPass();
$corePass->process($container);
}
}

public function configure(ArrayNodeDefinition $builder)
Expand Down Expand Up @@ -203,4 +209,23 @@ protected function loadCallHandlers(ContainerBuilder $container, $errorReporting
$definition->addTag(CallExtension::CALL_HANDLER_TAG, ['priority' => 50]);
$container->setDefinition(CallExtension::CALL_HANDLER_TAG . '.runtime', $definition);
}

/**
* Check whether the extension should bootstrap or not.
* The extension should always bootstrap unless the `-h` or `--help` option is passed.
*/
private function getShouldBootstrap(ContainerBuilder $container): bool
{
if ($this->shouldBootstrap === null) {
if ($container->has('cli.input')) {
/** @var ArgvInput $input */
$input = $container->get('cli.input');
$this->shouldBootstrap = !$input->hasParameterOption(['--help', '-h']);
} else {
// If the input isn't there for some bizarre reason, just assume we should bootstrap.
$this->shouldBootstrap = true;
}
}
return $this->shouldBootstrap;
}
}

0 comments on commit 901eb0b

Please sign in to comment.