Skip to content

Commit

Permalink
CLI-1276: no-scripts doesn't disable composer hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Feb 21, 2024
1 parent 6bc2749 commit 88d5d3a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 63 deletions.
26 changes: 11 additions & 15 deletions src/EventListener/ComposerScriptsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,24 @@ public function onConsoleTerminate(ConsoleTerminateEvent $event): void {
private function executeComposerScripts(ConsoleCommandEvent|ConsoleTerminateEvent $event, string $prefix): void {
/** @var CommandBase $command */
$command = $event->getCommand();
// If a command has the --no-script option and it's passed, do not execute post scripts.
if ($event->getInput()->hasOption('no-script') && $event->getInput()->getOption('no-scripts')) {
if ($event->getInput()->hasOption('no-scripts') && $event->getInput()->getOption('no-scripts')) {
return;
}
// Only successful commands should be executed.
if (is_a($command, CommandBase::class)) {
$composerJsonFilepath = Path::join($command->getProjectDir(), 'composer.json');
if (file_exists($composerJsonFilepath)) {
$composerJson = json_decode($command->localMachineHelper->readFile($composerJsonFilepath), TRUE, 512, JSON_THROW_ON_ERROR);
// Protect against invalid JSON.
if ($composerJson) {
$commandName = $command->getName();
// Replace colons with hyphens. E.g., pull:db becomes pull-db.
$scriptName = $prefix . '-acli-' . str_replace(':', '-', $commandName);
if (array_key_exists('scripts', $composerJson) && array_key_exists($scriptName, $composerJson['scripts'])) {
$event->getOutput()->writeln("Executing composer script `$scriptName` defined in `$composerJsonFilepath`", OutputInterface::VERBOSITY_VERBOSE);
$event->getOutput()->writeln($scriptName);
$command->localMachineHelper->execute(['composer', 'run-script', $scriptName]);
}
else {
$event->getOutput()->writeln("Notice: Composer script `$scriptName` does not exist in `$composerJsonFilepath`, skipping. This is not an error.", OutputInterface::VERBOSITY_VERBOSE);
}
$commandName = $command->getName();

Check warning on line 48 in src/EventListener/ComposerScriptsListener.php

View check run for this annotation

Codecov / codecov/patch

src/EventListener/ComposerScriptsListener.php#L48

Added line #L48 was not covered by tests
// Replace colons with hyphens. E.g., pull:db becomes pull-db.
$scriptName = $prefix . '-acli-' . str_replace(':', '-', $commandName);
if (array_key_exists('scripts', $composerJson) && array_key_exists($scriptName, $composerJson['scripts'])) {
$event->getOutput()->writeln("Executing composer script `$scriptName` defined in `$composerJsonFilepath`", OutputInterface::VERBOSITY_VERBOSE);
$event->getOutput()->writeln($scriptName);
$command->localMachineHelper->execute(['composer', 'run-script', $scriptName]);

Check warning on line 54 in src/EventListener/ComposerScriptsListener.php

View check run for this annotation

Codecov / codecov/patch

src/EventListener/ComposerScriptsListener.php#L50-L54

Added lines #L50 - L54 were not covered by tests
}
else {
$event->getOutput()->writeln("Notice: Composer script `$scriptName` does not exist in `$composerJsonFilepath`, skipping. This is not an error.", OutputInterface::VERBOSITY_VERBOSE);

Check warning on line 57 in src/EventListener/ComposerScriptsListener.php

View check run for this annotation

Codecov / codecov/patch

src/EventListener/ComposerScriptsListener.php#L57

Added line #L57 was not covered by tests
}
}
}
Expand Down
86 changes: 86 additions & 0 deletions tests/phpunit/src/Application/ComposerScriptsListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Application;

use Acquia\Cli\Tests\ApplicationTestBase;
use Symfony\Component\Filesystem\Path;

/**
* Tests Composer hooks handled by the Symfony Event Dispatcher.
*
* These must be tested using the ApplicationTestBase, since the Symfony
* CommandTester does not fire Event Dispatchers.
*/
class ComposerScriptsListenerTest extends ApplicationTestBase {

/**
* @group serial
* @covers \Acquia\Cli\EventListener\ComposerScriptsListener::onConsoleCommand
*/
public function testPreScripts(): void {
$json = [
'scripts' => [
'pre-acli-hello-world' => [
'echo "good morning world"',
],
],
];
file_put_contents(
Path::join($this->projectDir, 'composer.json'),
json_encode($json, JSON_THROW_ON_ERROR)
);
$this->mockRequest('getAccount');
$this->setInput([
'command' => 'hello-world',
]);
$buffer = $this->runApp();
self::assertStringContainsString('pre-acli-hello-world', $buffer);
}

/**
* @group serial
* @covers \Acquia\Cli\EventListener\ComposerScriptsListener::onConsoleTerminate
*/
public function testPostScripts(): void {
$json = [
'scripts' => [
'post-acli-hello-world' => [
'echo "goodbye world"',
],
],
];
file_put_contents(
Path::join($this->projectDir, 'composer.json'),
json_encode($json, JSON_THROW_ON_ERROR)
);
$this->mockRequest('getAccount');
$this->setInput([
'command' => 'hello-world',
]);
$buffer = $this->runApp();
self::assertStringContainsString('post-acli-hello-world', $buffer);
}

public function testNoScripts(): void {
$json = [
'scripts' => [
'pre-acli-pull-code' => [
'echo "goodbye world"',
],
],
];
file_put_contents(
Path::join($this->projectDir, 'composer.json'),
json_encode($json, JSON_THROW_ON_ERROR)
);
$this->setInput([
'--no-scripts' => TRUE,
'command' => 'pull:code',
]);
$buffer = $this->runApp();
self::assertStringNotContainsString('pre-acli-pull-code', $buffer);
}

}
49 changes: 1 addition & 48 deletions tests/phpunit/src/Application/ExceptionApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Acquia\Cli\Tests\Application;

use Acquia\Cli\Tests\ApplicationTestBase;
use Symfony\Component\Filesystem\Path;

/**
* Tests exceptions rewritten by the Symfony Event Dispatcher.
Expand All @@ -18,53 +17,7 @@ class ExceptionApplicationTest extends ApplicationTestBase {
/**
* @group serial
*/
public function testPreScripts(): void {
$json = [
'scripts' => [
'pre-acli-hello-world' => [
'echo "good morning world"',
],
],
];
file_put_contents(
Path::join($this->projectDir, 'composer.json'),
json_encode($json, JSON_THROW_ON_ERROR)
);
$this->mockRequest('getAccount');
$this->setInput([
'command' => 'hello-world',
]);
$buffer = $this->runApp();
self::assertStringContainsString('pre-acli-hello-world', $buffer);
}

/**
* @group serial
*/
public function testPostScripts(): void {
$json = [
'scripts' => [
'post-acli-hello-world' => [
'echo "goodbye world"',
],
],
];
file_put_contents(
Path::join($this->projectDir, 'composer.json'),
json_encode($json, JSON_THROW_ON_ERROR)
);
$this->mockRequest('getAccount');
$this->setInput([
'command' => 'hello-world',
]);
$buffer = $this->runApp();
self::assertStringContainsString('post-acli-hello-world', $buffer);
}

/**
* @group serial
*/
public function testInvalidApiCreds(): void {
public function testInvalidApiCredentials(): void {
$this->setInput([
'applicationUuid' => '2ed281d4-9dec-4cc3-ac63-691c3ba002c2',
'command' => 'aliases',
Expand Down

0 comments on commit 88d5d3a

Please sign in to comment.