Skip to content

Commit

Permalink
Send an exit code when the script terminates
Browse files Browse the repository at this point in the history
This fixes the issue identified in #80 (comment) where even when the setting of `installed_paths` failed, the plugin would exit with exit code `0`.

Technical choices:

As PHPCS returns exit code `0`, even when the setting of the `installed_paths` failed, we need to verify that the paths we tried to set, have actually been set.

To that end, a new function `verifySaveSuccess()` has been added.
This function takes the paths which were to be saved and compares then to the paths which are actually set in PHPCS after the save to determine whether or not the plugin was successful.

A `0` exit code will be returned if successful, `1` if not.
  • Loading branch information
jrfnl committed Dec 9, 2019
1 parent 6fa8d1e commit dec20ac
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/Plugin.php
Expand Up @@ -163,6 +163,7 @@ public function onDependenciesChangedEvent()
{
$io = $this->io;
$isVerbose = $io->isVerbose();
$exitCode = 0;

if ($isVerbose) {
$io->write(sprintf('<info>%s</info>', self::MESSAGE_RUNNING_INSTALLER));
Expand All @@ -174,13 +175,16 @@ public function onDependenciesChangedEvent()
$installPathUpdated = $this->updateInstalledPaths();

if ($installPathCleaned === true || $installPathUpdated === true) {
$this->saveInstalledPaths();
$exitCode = $this->saveInstalledPaths();
} elseif ($isVerbose) {
$io->write(sprintf('<info>%s</info>', self::MESSAGE_NOTHING_TO_INSTALL));
}
} elseif ($isVerbose) {
$io->write(sprintf('<info>%s</info>', self::MESSAGE_NOT_INSTALLED));
$exitCode = 1;
}

exit($exitCode);
}

/**
Expand Down Expand Up @@ -217,6 +221,8 @@ private function loadInstalledPaths()
* @throws LogicException
* @throws ProcessFailedException
* @throws RuntimeException
*
* @return int Exit code of the PHPCS command.
*/
private function saveInstalledPaths()
{
Expand Down Expand Up @@ -249,9 +255,45 @@ private function saveInstalledPaths()
$this->composer->getConfig()->get('bin-dir')
);

if ($this->io->isVerbose() && !empty($configResult)) {
$this->io->write(sprintf('<info>%s</info>', $configResult));
$exitCode = $this->verifySaveSuccess();

if ($exitCode === 0) {
if ($this->io->isVerbose() && !empty($configResult)) {
$this->io->write(sprintf('<info>%s</info>', $configResult));
}
} else {
// Fail message.
$this->io->write('<info>FAILED</info>');
}

return $exitCode;
}

/**
* Verify that the paths which were expected to be saved, have been.
*
* @return int Exit code. 0 for success, 1 for failure.
*/
private function verifySaveSuccess() {
$exitCode = 1;
$expected = $this->installedPaths;
$expectedCount = count($expected);

// Request the currently set installed paths after t.
$this->loadInstalledPaths();

if ($expectedCount === 0) {
if (count($this->installedPaths) === 0) {
$exitCode = 0;
}
} else {
$registered = array_intersect($this->installedPaths, $expected);
if ($expectedCount === count($registered)) {
$exitCode = 0;
}
}

return $exitCode;
}

/**
Expand Down

0 comments on commit dec20ac

Please sign in to comment.