Skip to content

Commit

Permalink
Verify the installed_paths after save
Browse files Browse the repository at this point in the history
As most problems reported are to do with paths not being set correctly, let's verify success within the plugin, independently of PHPCS.

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 and changes the exit code if this is not the case.

If there was a mismatch in paths and `verbose` mode is turned on, an information message will be displayed to help debugging.

At this moment I don't have a test case for this new functionality, however, I imagine it will come in useful with future bug reports.
  • Loading branch information
jrfnl committed Jan 19, 2020
1 parent f8729d0 commit 5277f67
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/Plugin.php
Expand Up @@ -214,7 +214,7 @@ private function loadInstalledPaths()
if (preg_match($regex, $output, $match) === 1) {
$phpcsInstalledPaths = str_replace(self::PHPCS_CONFIG_KEY . ': ', '', $match[0]);
$phpcsInstalledPaths = trim($phpcsInstalledPaths);

if ($phpcsInstalledPaths !== '') {
$this->installedPaths = explode(',', $phpcsInstalledPaths);
}
Expand Down Expand Up @@ -278,6 +278,9 @@ private function saveInstalledPaths()
);

$exitCode = $this->processExecutor->execute($command, $configResult, $phpcsPath);
if ($exitCode === 0) {
$exitCode = $this->verifySaveSuccess();
}

if ($exitCode === 0) {
$this->io->write($configMessage);
Expand All @@ -292,6 +295,40 @@ private function saveInstalledPaths()
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;
$expectedPaths = $this->installedPaths;

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

$registeredPaths = array_intersect($this->installedPaths, $expectedPaths);
$registeredCount = count($registeredPaths);
$expectedCount = count($expectedPaths);

if ($expectedCount === $registeredCount) {
$exitCode = 0;
}

if ($exitCode === 1 && $this->io->isVerbose()) {
$verificationMessage = sprintf(
"Paths to external standards found by the plugin: <info>%s</info>\n"
. 'Actual paths registered with PHPCS: <info>%s</info>',
implode(', ', $expectedPaths),
implode(', ', $this->installedPaths)
);
$this->io->write($verificationMessage);
}

return $exitCode;
}

/**
* Get the path to the current PHP version being used.
*
Expand Down

0 comments on commit 5277f67

Please sign in to comment.