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.

Open questions:
* Should additional debug output be generated by the verification function when `verbose` mode is turned on ? Something like `Expected paths to be set to: %s, found: %s`.
    This could also come in handy to verify that all paths which should be set were found by the plugin.

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 17, 2020
1 parent bb64a27 commit 1ca8c0d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Plugin.php
Expand Up @@ -274,6 +274,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 @@ -288,6 +291,33 @@ 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;
$expected = $this->installedPaths;
$expectedCount = count($expected);

// Request the currently set installed paths after the save.
$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;
}

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

0 comments on commit 1ca8c0d

Please sign in to comment.