-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move svn-specific commands to namespace * Add new loaders * Pass shell_exec as a dependency * Add tests for isNewSvnFile * Use passed shell_exec for all functions * Add tests for getSvnUnifiedDiff * Fix tabs in tests * Change workflows to return PhpcsMessages This refactors the workflow functions so that they return a PhpcsMessages object which makes them easier to test. The object is then formatted and output by a separate function. This change also modifies the `is_readable()` guard in `runSvnWorkflow()` to be injected so it can be mocked for testing. * Add happy path test for runSvnWorkflow * Allow PhpcsMessages::fromPhpcsMessages to have null filename * Add svn workflow test for new file * Inject validateExecutableExists into runSvnWorkflow * Group shell operations into UnixShell * Remove vars injected into mock ShellOperator * Add --version option
- Loading branch information
1 parent
81bc968
commit f8f124a
Showing
10 changed files
with
414 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpcsChanged; | ||
|
||
class NonFatalException extends \Exception { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpcsChanged; | ||
|
||
/** | ||
* Interface to perform file and shell operations | ||
*/ | ||
interface ShellOperator { | ||
public function validateExecutableExists(string $name, string $command): void; | ||
|
||
public function executeCommand(string $command): string; | ||
|
||
public function isReadable(string $fileName): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpcsChanged\SvnWorkflow; | ||
|
||
use PhpcsChanged\NonFatalException; | ||
|
||
function validateSvnFileExists(string $svnFile, callable $isReadable): void { | ||
if (! $isReadable($svnFile)) { | ||
throw new \Exception("Cannot read file '{$svnFile}'"); | ||
} | ||
} | ||
|
||
function getSvnUnifiedDiff(string $svnFile, string $svn, callable $executeCommand, callable $debug): string { | ||
$unifiedDiffCommand = "{$svn} diff " . escapeshellarg($svnFile); | ||
$debug('running diff command:', $unifiedDiffCommand); | ||
$unifiedDiff = $executeCommand($unifiedDiffCommand); | ||
if (! $unifiedDiff) { | ||
throw new NonFatalException("Cannot get svn diff for file '{$svnFile}'; skipping"); | ||
} | ||
$debug('diff command output:', $unifiedDiff); | ||
return $unifiedDiff; | ||
} | ||
|
||
function isNewSvnFile(string $svnFile, string $svn, callable $executeCommand, callable $debug): bool { | ||
$svnStatusCommand = "${svn} info " . escapeshellarg($svnFile); | ||
$debug('checking svn status of file with command:', $svnStatusCommand); | ||
$svnStatusOutput = $executeCommand($svnStatusCommand); | ||
$debug('svn status output:', $svnStatusOutput); | ||
if (! $svnStatusOutput || false === strpos($svnStatusOutput, 'Schedule:')) { | ||
throw new \Exception("Cannot get svn info for file '{$svnFile}'"); | ||
} | ||
return (false !== strpos($svnStatusOutput, 'Schedule: add')); | ||
} | ||
|
||
function getSvnBasePhpcsOutput(string $svnFile, string $svn, string $phpcs, string $phpcsStandardOption, callable $executeCommand, callable $debug): string { | ||
$oldFilePhpcsOutputCommand = "${svn} cat " . escapeshellarg($svnFile) . " | {$phpcs} --report=json" . $phpcsStandardOption; | ||
$debug('running orig phpcs command:', $oldFilePhpcsOutputCommand); | ||
$oldFilePhpcsOutput = $executeCommand($oldFilePhpcsOutputCommand); | ||
if (! $oldFilePhpcsOutput) { | ||
throw new \Exception("Cannot get old phpcs output for file '{$svnFile}'"); | ||
} | ||
$debug('orig phpcs command output:', $oldFilePhpcsOutput); | ||
return $oldFilePhpcsOutput; | ||
} | ||
|
||
function getSvnNewPhpcsOutput(string $svnFile, string $phpcs, string $cat, string $phpcsStandardOption, callable $executeCommand, callable $debug): string { | ||
$newFilePhpcsOutputCommand = "{$cat} " . escapeshellarg($svnFile) . " | {$phpcs} --report=json" . $phpcsStandardOption; | ||
$debug('running new phpcs command:', $newFilePhpcsOutputCommand); | ||
$newFilePhpcsOutput = $executeCommand($newFilePhpcsOutputCommand); | ||
if (! $newFilePhpcsOutput) { | ||
throw new \Exception("Cannot get new phpcs output for file '{$svnFile}'"); | ||
} | ||
$debug('new phpcs command output:', $newFilePhpcsOutput); | ||
return $newFilePhpcsOutput; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpcsChanged; | ||
|
||
use PhpcsChanged\ShellOperator; | ||
|
||
/** | ||
* Module to perform file and shell operations | ||
*/ | ||
class UnixShell implements ShellOperator { | ||
public function validateExecutableExists(string $name, string $command): void { | ||
exec(sprintf("type %s > /dev/null 2>&1", escapeshellarg($command)), $ignore, $returnVal); | ||
if ($returnVal != 0) { | ||
throw new \Exception("Cannot find executable for {$name}, currently set to '{$command}'."); | ||
} | ||
} | ||
|
||
public function executeCommand(string $command): string { | ||
return shell_exec($command); | ||
} | ||
|
||
public function isReadable(string $fileName): bool { | ||
return is_readable($fileName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpcsChanged; | ||
|
||
function getVersion(): string { | ||
return '2.1.0'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.