From 16473ec1a6264ce901966e372b6a7f56b46f004d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andor=20D=C3=A1vid?= Date: Sun, 30 Jul 2017 12:54:00 +0200 Subject: [PATCH 1/5] Fix PhpDoc and type hints --- RoboFile.php | 55 ++++---------- src/Task/TsLintRunTask.php | 74 ++++++++----------- tests/_support/Helper/Acceptance.php | 1 - tests/_support/Helper/Dummy/DummyOutput.php | 4 +- tests/_support/Helper/Dummy/DummyProcess.php | 4 +- tests/_support/Helper/Unit.php | 1 - tests/acceptance.suite.yml | 1 + .../LintReportWrapper/FailureWrapperTest.php | 7 +- .../LintReportWrapper/FileWrapperTest.php | 2 +- .../LintReportWrapper/ReportWrapperTest.php | 2 +- 10 files changed, 57 insertions(+), 94 deletions(-) diff --git a/RoboFile.php b/RoboFile.php index 708e016..8fbd5bd 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -1,15 +1,13 @@ environment) { return $this->environment; @@ -91,43 +86,35 @@ protected function getEnvironment() /** * Git "pre-commit" hook callback. - * - * @return \Robo\Collection\CollectionBuilder */ - public function githookPreCommit() + public function githookPreCommit(): CollectionBuilder { $this->environment = 'git-hook'; return $this ->collectionBuilder() - ->addTaskList([ - 'lint.composer.lock' => $this->taskComposerValidate(), - 'lint.phpcs.psr2' => $this->getTaskPhpcsLint(), - 'codecept' => $this->getTaskCodecept(), - ]); + ->addTask($this->taskComposerValidate()) + ->addTask($this->getTaskPhpcsLint()) + ->addTask($this->getTaskCodecept()); } /** * Run the Robo unit tests. */ - public function test() + public function test(): CollectionBuilder { return $this->getTaskCodecept(); } /** * Run code style checkers. - * - * @return \Robo\Collection\CollectionBuilder */ - public function lint() + public function lint(): CollectionBuilder { return $this ->collectionBuilder() - ->addTaskList([ - 'lint.composer.lock' => $this->taskComposerValidate(), - 'lint.phpcs.psr2' => $this->getTaskPhpcsLint(), - ]); + ->addTask($this->taskComposerValidate()) + ->addTask($this->getTaskPhpcsLint()); } /** @@ -171,10 +158,7 @@ protected function initCodeceptionInfo() return $this; } - /** - * @return \Robo\Collection\CollectionBuilder - */ - protected function getTaskCodecept() + protected function getTaskCodecept(): CollectionBuilder { $environment = $this->getEnvironment(); $withCoverage = $environment !== 'git-hook'; @@ -283,12 +267,7 @@ protected function getTaskPhpcsLint() ->deferTaskConfiguration('setFiles', 'files')); } - /** - * @param string $extension - * - * @return bool - */ - protected function isPhpExtensionAvailable($extension) + protected function isPhpExtensionAvailable(string $extension): bool { $command = sprintf('%s -m', escapeshellcmd($this->phpExecutable)); @@ -301,10 +280,7 @@ protected function isPhpExtensionAvailable($extension) return in_array($extension, explode("\n", $process->getOutput())); } - /** - * @return bool - */ - protected function isPhpDbgAvailable() + protected function isPhpDbgAvailable(): bool { $command = sprintf( '%s -i | grep -- %s', @@ -315,10 +291,7 @@ protected function isPhpDbgAvailable() return (new Process($command))->run() === 0; } - /** - * @return string - */ - protected function getLogDir() + protected function getLogDir(): string { $this->initCodeceptionInfo(); diff --git a/src/Task/TsLintRunTask.php b/src/Task/TsLintRunTask.php index e3c70f2..2d4a17c 100644 --- a/src/Task/TsLintRunTask.php +++ b/src/Task/TsLintRunTask.php @@ -18,14 +18,6 @@ use Robo\TaskAccessor; use Symfony\Component\Process\Process; -/** - * Class TaskTsLintRun. - * - * Assert mapping: - * - report: Parsed JSON lint report. - * - * @package Sweetchuck\Robo\TsLint\Task - */ class TsLintRunTask extends BaseTask implements CommandInterface, ContainerAwareInterface, @@ -66,7 +58,7 @@ class TsLintRunTask extends BaseTask implements */ protected $processClass = Process::class; - //region Options. + // region Options. // region Option - assetNamePrefix. /** @@ -90,7 +82,7 @@ public function setAssetNamePrefix(string $value) } // endregion - //region Option - workingDirectory. + // region Option - workingDirectory. /** * @var string */ @@ -112,9 +104,9 @@ public function setWorkingDirectory(string $value) return $this; } - //endregion + // endregion - //region Option - tslintExecutable. + // region Option - tslintExecutable. /** * @var string */ @@ -134,9 +126,9 @@ public function setTslintExecutable(string $tslintExecutable) return $this; } - //endregion + // endregion - //region Option - failOn. + // region Option - failOn. /** * Severity level. * @@ -163,9 +155,9 @@ public function setFailOn(string $value) return $this; } - //endregion + // endregion - //region Option - configFile. + // region Option - configFile. /** * @var string */ @@ -187,9 +179,9 @@ public function setConfigFile(string $path) return $this; } - //endregion + // endregion - //region Option - exclude. + // region Option - exclude. /** * A filename or glob which indicates files to exclude from linting. * @@ -218,9 +210,9 @@ public function setExclude($file_paths, $include = true) return $this; } - //endregion + // endregion - //region Option - force. + // region Option - force. /** * Return status code 0 even if there are any lint errors. * @@ -242,9 +234,9 @@ public function setForce(bool $value) return $this; } - //endregion + // endregion - //region Option - out. + // region Option - out. /** * A filename to output the results to. * @@ -271,9 +263,9 @@ public function setOut(string $filePath) return $this; } - //endregion + // endregion - //region Option - rulesDir. + // region Option - rulesDir. /** * @var string */ @@ -295,9 +287,9 @@ public function setRulesDir(string $value) return $this; } - //endregion + // endregion - //region Option - formattersDir. + // region Option - formattersDir. /** * An additional formatters directory, for user-created formatters. * @@ -319,9 +311,9 @@ public function setFormattersDir(string $directory) return $this; } - //endregion + // endregion - //region Option - format. + // region Option - format. /** * @var string */ @@ -343,9 +335,9 @@ public function setFormat(string $value) return $this; } - //endregion + // endregion - //region Option - project. + // region Option - project. /** * @var string */ @@ -367,9 +359,9 @@ public function setProject(string $value) return $this; } - //endregion + // endregion - //region Option - typeCheck. + // region Option - typeCheck. /** * @var bool */ @@ -393,9 +385,9 @@ public function setTypeCheck(bool $value) return $this; } - //endregion + // endregion - //region Option - paths. + // region Option - paths. /** * @var array */ @@ -422,9 +414,9 @@ public function setPaths(array $paths, bool $include = true) return $this; } - //endregion + // endregion - //region Option - lintReporters. + // region Option - lintReporters. /** * @var string[]|\Sweetchuck\LintReport\ReporterInterface[] */ @@ -439,8 +431,6 @@ public function getLintReporters() } /** - * @param array $lintReporters - * * @return $this */ public function setLintReporters(array $lintReporters) @@ -456,7 +446,7 @@ public function setLintReporters(array $lintReporters) * * @return $this */ - public function addLintReporter($id, $lintReporter = null) + public function addLintReporter(string $id, $lintReporter = null) { $this->lintReporters[$id] = $lintReporter; @@ -472,8 +462,8 @@ public function removeLintReporter(string $id) return $this; } - //endregion - //endregion + // endregion + // endregion /** * @var array @@ -612,7 +602,7 @@ public function options(array $options) * @return bool[] * Key is the relevant value, the value is a boolean. */ - protected function createIncludeList($items, $include): array + protected function createIncludeList($items, bool $include): array { if (!is_array($items)) { $items = [$items => $include]; diff --git a/tests/_support/Helper/Acceptance.php b/tests/_support/Helper/Acceptance.php index 251968e..c166dab 100644 --- a/tests/_support/Helper/Acceptance.php +++ b/tests/_support/Helper/Acceptance.php @@ -6,5 +6,4 @@ class Acceptance extends Module { - } diff --git a/tests/_support/Helper/Dummy/DummyOutput.php b/tests/_support/Helper/Dummy/DummyOutput.php index f1165d5..705f93a 100644 --- a/tests/_support/Helper/Dummy/DummyOutput.php +++ b/tests/_support/Helper/Dummy/DummyOutput.php @@ -2,7 +2,9 @@ namespace Sweetchuck\Robo\TsLint\Test\Helper\Dummy; -class DummyOutput extends \Symfony\Component\Console\Output\Output +use Symfony\Component\Console\Output\Output; + +class DummyOutput extends Output { /** diff --git a/tests/_support/Helper/Dummy/DummyProcess.php b/tests/_support/Helper/Dummy/DummyProcess.php index a3d056e..3fdd567 100644 --- a/tests/_support/Helper/Dummy/DummyProcess.php +++ b/tests/_support/Helper/Dummy/DummyProcess.php @@ -2,7 +2,9 @@ namespace Sweetchuck\Robo\TsLint\Test\Helper\Dummy; -class DummyProcess extends \Symfony\Component\Process\Process +use Symfony\Component\Process\Process; + +class DummyProcess extends Process { /** diff --git a/tests/_support/Helper/Unit.php b/tests/_support/Helper/Unit.php index f6d58fd..8ca9a77 100644 --- a/tests/_support/Helper/Unit.php +++ b/tests/_support/Helper/Unit.php @@ -6,5 +6,4 @@ class Unit extends Module { - } diff --git a/tests/acceptance.suite.yml b/tests/acceptance.suite.yml index 247ff2f..2380383 100644 --- a/tests/acceptance.suite.yml +++ b/tests/acceptance.suite.yml @@ -1,5 +1,6 @@ class_name: 'AcceptanceTester' + modules: enabled: - '\Sweetchuck\Robo\TsLint\Test\Helper\Module\Cli' diff --git a/tests/unit/LintReportWrapper/FailureWrapperTest.php b/tests/unit/LintReportWrapper/FailureWrapperTest.php index b04ec99..4258275 100644 --- a/tests/unit/LintReportWrapper/FailureWrapperTest.php +++ b/tests/unit/LintReportWrapper/FailureWrapperTest.php @@ -9,14 +9,11 @@ class FailureWrapperTest extends Unit { /** - * @var \UnitTester + * @var \Sweetchuck\Robo\TsLint\Test\UnitTester */ protected $tester; - /** - * @return array - */ - public function casesAll() + public function casesAll(): array { return [ 'a' => [ diff --git a/tests/unit/LintReportWrapper/FileWrapperTest.php b/tests/unit/LintReportWrapper/FileWrapperTest.php index 0a71152..9480a7b 100644 --- a/tests/unit/LintReportWrapper/FileWrapperTest.php +++ b/tests/unit/LintReportWrapper/FileWrapperTest.php @@ -8,7 +8,7 @@ class FileWrapperTest extends Unit { /** - * @var \UnitTester + * @var \Sweetchuck\Robo\TsLint\Test\UnitTester */ protected $tester; diff --git a/tests/unit/LintReportWrapper/ReportWrapperTest.php b/tests/unit/LintReportWrapper/ReportWrapperTest.php index d17b446..dca1a07 100644 --- a/tests/unit/LintReportWrapper/ReportWrapperTest.php +++ b/tests/unit/LintReportWrapper/ReportWrapperTest.php @@ -8,7 +8,7 @@ class ReportWrapperTest extends Unit { /** - * @var \UnitTester + * @var \Sweetchuck\Robo\TsLint\Test\UnitTester */ protected $tester; From 18a0f83f5b6ced4828518d95b1b3bfd26de74d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andor=20D=C3=A1vid?= Date: Sun, 30 Jul 2017 12:55:14 +0200 Subject: [PATCH 2/5] Fix output message indentation in .git-hooks --- .git-hooks | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.git-hooks b/.git-hooks index de27c30..e8a17f4 100644 --- a/.git-hooks +++ b/.git-hooks @@ -4,7 +4,7 @@ echo "BEGIN Git hook: ${sghHookName}" function myExit () { - echo "End Git hook: ${sghHookName}" + echo "END Git hook: ${sghHookName}" exit $1 } From 2304041e468caf8c3247de2b86ac256556d8c342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andor=20D=C3=A1vid?= Date: Sun, 30 Jul 2017 13:34:40 +0200 Subject: [PATCH 3/5] Use sweetchuck/codeception-module-robo-task-runner RoboTaskRunner --- composer.json | 3 +- composer.lock | 37 ++++++- tests/_support/AcceptanceTester.php | 84 ++-------------- tests/_support/Helper/Module/Cli.php | 80 --------------- .../Helper/RoboFiles/TsLintRoboFile.php} | 9 +- tests/acceptance.suite.yml | 3 +- tests/acceptance/RunRoboTaskCest.php | 99 +++++++++++++++---- 7 files changed, 135 insertions(+), 180 deletions(-) delete mode 100644 tests/_support/Helper/Module/Cli.php rename tests/{_data/RoboFile.php => _support/Helper/RoboFiles/TsLintRoboFile.php} (92%) diff --git a/composer.json b/composer.json index 2fd9f8a..41f13cf 100644 --- a/composer.json +++ b/composer.json @@ -13,11 +13,12 @@ "consolidation/robo": "^1.0" }, "require-dev": { + "codeception/codeception": "^2.2", + "sweetchuck/codeception-module-robo-task-runner": "^0.0.1", "sweetchuck/git-hooks": "^0.0", "sweetchuck/lint-report": "^0.0", "sweetchuck/robo-git": "^0.0", "sweetchuck/robo-phpcs": "^0.0", - "codeception/codeception": "^2.2", "symfony/process": "^2.8 || ^3.1" }, "autoload": { diff --git a/composer.lock b/composer.lock index 2575d6f..d9c8e47 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "60ee00ba5282101895981682fd1e2c85", + "content-hash": "8ae8498c4801027bd7b1a740a93f1a45", "packages": [ { "name": "consolidation/annotated-command", @@ -3001,6 +3001,41 @@ "description": "Automatic BASH completion for Symfony Console Component based applications.", "time": "2016-02-24T05:08:54+00:00" }, + { + "name": "sweetchuck/codeception-module-robo-task-runner", + "version": "v0.0.1", + "source": { + "type": "git", + "url": "https://github.com/Sweetchuck/codeception-module-robo-task-runner.git", + "reference": "7fb951ec5f343333e95d450d25a13890cddfe49f" + }, + "dist": { + "type": "zip", + "url": "https://github.com/gitapi/repos/Sweetchuck/codeception-module-robo-task-runner/zipball/7fb951ec5f343333e95d450d25a13890cddfe49f", + "reference": "7fb951ec5f343333e95d450d25a13890cddfe49f", + "shasum": "" + }, + "require": { + "codeception/codeception": "^2.3", + "consolidation/robo": "^1.1" + }, + "require-dev": { + "squizlabs/php_codesniffer": "^3.0", + "sweetchuck/git-hooks": "^0.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Sweetchuck\\Codeception\\Module\\RoboTaskRunner\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0" + ], + "description": "Runs a Robo task.", + "time": "2017-07-29T11:31:55+00:00" + }, { "name": "sweetchuck/git-hooks", "version": "v0.0.10", diff --git a/tests/_support/AcceptanceTester.php b/tests/_support/AcceptanceTester.php index 357d677..0829267 100644 --- a/tests/_support/AcceptanceTester.php +++ b/tests/_support/AcceptanceTester.php @@ -2,7 +2,8 @@ namespace Sweetchuck\Robo\TsLint\Test; -use \PHPUnit\Framework\Assert as Assert; +use \PHPUnit\Framework\Assert; +use Symfony\Component\Finder\Finder; /** * Inherited Methods @@ -30,9 +31,10 @@ public function clearTheReportsDir() { $reportsDir = codecept_data_dir('actual'); if (is_dir($reportsDir)) { - $finder = (new \Symfony\Component\Finder\Finder()) + $finder = (new Finder()) ->in($reportsDir) ->files(); + /** @var \Symfony\Component\Finder\SplFileInfo $file */ foreach ($finder as $file) { unlink($file->getPathname()); @@ -43,44 +45,10 @@ public function clearTheReportsDir() } /** - * @param string $taskName - * @param array $args - * @param array $options - * * @return $this */ - public function runRoboTask($taskName, array $args = [], array $options = []) + public function haveAFileLikeThis(string $fileName) { - $cmdPattern = 'cd %s && ../../bin/robo %s'; - $cmdArgs = [ - escapeshellarg(codecept_data_dir()), - escapeshellarg($taskName), - ]; - - foreach ($options as $option => $value) { - $cmdPattern .= " --$option"; - if ($value !== null) { - $cmdPattern .= '=%s'; - $cmdArgs[] = escapeshellarg($value); - } - } - - $cmdPattern .= str_repeat(' %s', count($args)); - foreach ($args as $arg) { - $cmdArgs[] = escapeshellarg($arg); - } - - $this->runShellCommand(vsprintf($cmdPattern, $cmdArgs)); - - return $this; - } - - /** - * @param string $fileName - * - * @return $this - */ - public function haveAFileLikeThis($fileName) { $expectedDir = codecept_data_dir('expected'); $actualDir = codecept_data_dir('actual'); @@ -93,55 +61,17 @@ public function haveAFileLikeThis($fileName) { } /** - * @param string $fileName - * * @return $this */ - public function haveAValidCheckstyleReport($fileName) + public function haveAValidCheckstyleReport(string $fileName) { $fileName = codecept_data_dir($fileName); $doc = new \DOMDocument(); $doc->loadXML(file_get_contents($fileName)); - $xpath = new DOMXPath($doc); + $xpath = new \DOMXPath($doc); $rootElement = $xpath->query('/checkstyle'); Assert::assertEquals(1, $rootElement->length, 'Root element of the Checkstyle XML is exists.'); return $this; } - - /** - * @param string $expected - * - * @return $this - */ - public function seeThisTextInTheStdOutput($expected) - { - Assert::assertContains($expected, $this->getStdOutput()); - - return $this; - } - - /** - * @param string $expected - * - * @return $this - */ - public function seeThisTextInTheStdError($expected) - { - Assert::assertContains($expected, $this->getStdError()); - - return $this; - } - - /** - * @param int $expected - * - * @return $this - */ - public function expectTheExitCodeToBe($expected) - { - Assert::assertEquals($expected, $this->getExitCode()); - - return $this; - } } diff --git a/tests/_support/Helper/Module/Cli.php b/tests/_support/Helper/Module/Cli.php deleted file mode 100644 index c9b52df..0000000 --- a/tests/_support/Helper/Module/Cli.php +++ /dev/null @@ -1,80 +0,0 @@ -process = null; - } - - /** - * Executes a shell command. - * - * @param string $command - * - * @return $this - */ - public function runShellCommand($command) - { - $this->process = new Process($command); - $this->exitCode = $this->process->run(); - $this->stdOutput = $this->process->getOutput(); - $this->stdError = $this->process->getErrorOutput(); - - return $this; - } - - /** - * @return int|null - */ - public function getExitCode() - { - return $this->exitCode; - } - - /** - * @return string - */ - public function getStdOutput() - { - return $this->stdOutput; - } - - /** - * @return string - */ - public function getStdError() - { - return $this->stdError; - } -} diff --git a/tests/_data/RoboFile.php b/tests/_support/Helper/RoboFiles/TsLintRoboFile.php similarity index 92% rename from tests/_data/RoboFile.php rename to tests/_support/Helper/RoboFiles/TsLintRoboFile.php index 7dc2e12..d892789 100644 --- a/tests/_data/RoboFile.php +++ b/tests/_support/Helper/RoboFiles/TsLintRoboFile.php @@ -1,15 +1,20 @@ clearTheReportsDir(); } - public function lintAllInOne(AcceptanceTester $I): void + public function lintAllInOne(AcceptanceTester $i): void { - $I->runRoboTask('lint:all-in-one'); - $I->expectTheExitCodeToBe(2); - $I->seeThisTextInTheStdOutput(file_get_contents("{$this->expectedDir}/extra.verbose.txt")); - $I->seeThisTextInTheStdOutput(file_get_contents("{$this->expectedDir}/extra.summary.txt")); - $I->haveAFileLikeThis('extra.verbose.txt'); - $I->haveAFileLikeThis('extra.summary.txt'); - $I->seeThisTextInTheStdError('One or more errors were reported (and any number of warnings)'); + $id = 'lint:all-in-one'; + + $cwd = getcwd(); + chdir(codecept_data_dir()); + $i->runRoboTask( + $id, + TsLintRoboFile::class, + 'lint:all-in-one' + ); + chdir($cwd); + + $exitCode = $i->getRoboTaskExitCode($id); + $stdOutput = $i->getRoboTaskStdOutput($id); + $stdError = $i->getRoboTaskStdError($id); + + $i->assertEquals(2, $exitCode); + + $i->assertContains( + file_get_contents("{$this->expectedDir}/extra.verbose.txt"), + $stdOutput + ); + $i->assertContains( + file_get_contents("{$this->expectedDir}/extra.summary.txt"), + $stdOutput + ); + + $i->assertContains( + 'One or more errors were reported (and any number of warnings)', + $stdError + ); + + $i->haveAFileLikeThis('extra.verbose.txt'); + $i->haveAFileLikeThis('extra.summary.txt'); } - public function lintStylishFile(AcceptanceTester $I): void + public function lintStylishFile(AcceptanceTester $i): void { - $I->runRoboTask('lint:stylish-file'); - $I->expectTheExitCodeToBe(2); - $I->haveAFileLikeThis('native.stylish.txt'); - $I->seeThisTextInTheStdError('One or more errors were reported (and any number of warnings)'); + $id = 'lint:stylish-file'; + + $cwd = getcwd(); + chdir(codecept_data_dir()); + $i->runRoboTask( + $id, + TsLintRoboFile::class, + 'lint:stylish-file' + ); + chdir($cwd); + + $exitCode = $i->getRoboTaskExitCode($id); + $stdError = $i->getRoboTaskStdError($id); + + $i->assertEquals(2, $exitCode); + $i->assertContains( + 'One or more errors were reported (and any number of warnings)', + $stdError + ); + $i->haveAFileLikeThis('native.stylish.txt'); } - public function lintStylishStdOutput(AcceptanceTester $I): void + public function lintStylishStdOutput(AcceptanceTester $i): void { - $I->runRoboTask('lint:stylish-std-output'); - $I->expectTheExitCodeToBe(2); - $I->seeThisTextInTheStdOutput(file_get_contents("{$this->expectedDir}/native.stylish.txt")); - $I->seeThisTextInTheStdError('One or more errors were reported (and any number of warnings)'); + $id = 'lint:stylish-std-output'; + + $cwd = getcwd(); + chdir(codecept_data_dir()); + $i->runRoboTask( + $id, + TsLintRoboFile::class, + 'lint:stylish-std-output' + ); + chdir($cwd); + + $exitCode = $i->getRoboTaskExitCode($id); + $stdOutput = $i->getRoboTaskStdOutput($id); + $stdError = $i->getRoboTaskStdError($id); + + $i->assertEquals(2, $exitCode); + $i->assertContains( + file_get_contents("{$this->expectedDir}/native.stylish.txt"), + $stdOutput + ); + $i->assertContains( + 'One or more errors were reported (and any number of warnings)', + $stdError + ); } } From 3eb159eea14383c93ae0aad9a0c6d7973e76f6ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Andor?= Date: Sun, 30 Jul 2017 13:43:52 +0200 Subject: [PATCH 4/5] Use sweetchuck/codeception-module-robo-task-runner DummyOutput --- tests/_support/Helper/Dummy/DummyOutput.php | 22 --------------------- tests/unit/Task/TsLintRunTaskTest.php | 9 +++++++-- 2 files changed, 7 insertions(+), 24 deletions(-) delete mode 100644 tests/_support/Helper/Dummy/DummyOutput.php diff --git a/tests/_support/Helper/Dummy/DummyOutput.php b/tests/_support/Helper/Dummy/DummyOutput.php deleted file mode 100644 index 705f93a..0000000 --- a/tests/_support/Helper/Dummy/DummyOutput.php +++ /dev/null @@ -1,22 +0,0 @@ -output .= $message . ($newline ? "\n" : ''); - } -} diff --git a/tests/unit/Task/TsLintRunTaskTest.php b/tests/unit/Task/TsLintRunTaskTest.php index 5c568f4..988c808 100644 --- a/tests/unit/Task/TsLintRunTaskTest.php +++ b/tests/unit/Task/TsLintRunTaskTest.php @@ -6,8 +6,9 @@ use Codeception\Util\Stub; use Robo\Robo; use Sweetchuck\Robo\TsLint\Task\TsLintRunTask as RunTask; -use Sweetchuck\Robo\TsLint\Test\Helper\Dummy\DummyOutput; +use \Sweetchuck\Codeception\Module\RoboTaskRunner\DummyOutput; use Sweetchuck\Robo\TsLint\Test\Helper\Dummy\DummyProcess; +use Symfony\Component\Console\Output\OutputInterface; class TsLintRunTaskTest extends Unit { @@ -411,7 +412,11 @@ public function testRun( $container = Robo::createDefaultContainer(); Robo::setContainer($container); - $mainStdOutput = new DummyOutput(); + $outputConfig = [ + 'verbosity' => OutputInterface::VERBOSITY_DEBUG, + 'colors' => false, + ]; + $mainStdOutput = new DummyOutput($outputConfig); $options += [ 'workingDirectory' => 'my-working-dir', From ab407394d273e65d049fb5a5d7d4d6bb490d6b95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Andor?= Date: Sun, 30 Jul 2017 15:27:04 +0200 Subject: [PATCH 5/5] Split TsLintRunTask::run into smaller functions --- src/Task/TsLintRunTask.php | 179 ++++++++++++++++++-------- tests/unit/Task/TsLintRunTaskTest.php | 38 +++++- 2 files changed, 157 insertions(+), 60 deletions(-) diff --git a/src/Task/TsLintRunTask.php b/src/Task/TsLintRunTask.php index 2d4a17c..dfe33d1 100644 --- a/src/Task/TsLintRunTask.php +++ b/src/Task/TsLintRunTask.php @@ -46,10 +46,27 @@ class TsLintRunTask extends BaseTask implements */ const EXIT_CODE_ERROR = 2; + /** + * Couldn't create the output directory. + */ + const EXIT_CODE_OUTPUT_DIR = 3; + /** * Something is invalid. */ - const EXIT_CODE_INVALID = 3; + const EXIT_CODE_INVALID = 4; + + /** + * Exit code and error message mapping. + * + * @var string + */ + protected $taskExitMessages = [ + 0 => 'No lints were found', + 1 => 'Lints with a severity of warning were reported (no errors)', + 2 => 'One or more errors were reported (and any number of warnings)', + 3 => 'Extra lint reporters can be used only if the output format is "json".', + ]; /** * @todo Some kind of dependency injection would be awesome. @@ -465,11 +482,37 @@ public function removeLintReporter(string $id) // endregion // endregion + /** + * Process exit code. + * + * @var int + */ + protected $lintExitCode = 0; + + /** + * Process stdOutput. + * + * @var string + */ + protected $lintStdOutput = ''; + + /** + * Process stdError. + * + * @var string + */ + protected $lintStdError = ''; + + /** + * @var string + */ + protected $command = ''; + /** * @var array */ protected $assets = [ - 'report' => [], + 'report' => null, ]; protected $options = [ @@ -484,25 +527,6 @@ public function removeLintReporter(string $id) 'type-check' => 'flag', ]; - /** - * Process exit code. - * - * @var int - */ - protected $exitCode = 0; - - /** - * Exit code and error message mapping. - * - * @var string - */ - protected $exitMessages = [ - 0 => 'No lints were found', - 1 => 'Lints with a severity of warning were reported (no errors)', - 2 => 'One or more errors were reported (and any number of warnings)', - 3 => 'Extra lint reporters can be used only if the output format is "json".', - ]; - /** * TaskTsLintRun constructor. * @@ -621,45 +645,85 @@ protected function createIncludeList($items, bool $include): array */ public function run() { - $lintReporters = $this->initLintReporters(); - if ($lintReporters && $this->getFormat() === '') { + return $this + ->runPrepare() + ->runHeader() + ->runDoIt() + ->runProcessOutput() + ->runReturn(); + } + + /** + * @return $this + */ + protected function runPrepare() + { + $this->initLintReporters(); + if ($this->getLintReporters() && $this->getFormat() === '') { $this->setFormat('json'); } - $command = $this->getCommand(); + $this->command = $this->getCommand(); + + return $this; + } + + /** + * @return $this + */ + protected function runHeader() + { $this->printTaskInfo( 'TsLint task runs: {command} in directory "{workingDirectory}"', [ - 'command' => $command, + 'command' => $this->command, 'workingDirectory' => $this->workingDirectory ?: '.', ] ); + return $this; + } + + /** + * @return $this + */ + public function runDoIt() + { + $lintReporters = $this->getLintReporters(); if ($lintReporters && !$this->isOutputFormatMachineReadable()) { - $this->exitCode = static::EXIT_CODE_INVALID; + $this->lintExitCode = static::EXIT_CODE_INVALID; - return new Result($this, $this->exitCode, $this->getExitMessage($this->exitCode)); + return $this; } /** @var Process $process */ - $process = new $this->processClass($command); + $process = new $this->processClass($this->command); + // @todo Add the "mkdir -p 'foo' command to the ::getCommand()." $result = $this->prepareOutputDirectory(); if (!$result->wasSuccessful()) { - return $result; + $this->lintExitCode = static::EXIT_CODE_OUTPUT_DIR; + + return $this; } - $this->exitCode = $process->run(); + $this->lintExitCode = $process->run(); + $this->lintStdOutput = $process->getOutput(); + $this->lintStdError = $process->getErrorOutput(); + + return $this; + } - $numOfErrors = $this->exitCode; - $numOfWarnings = 0; + /** + * @return $this + */ + public function runProcessOutput() + { if ($this->isLintSuccess()) { - $originalOutput = $process->getOutput(); + $lintReporters = $this->getLintReporters(); if ($this->isOutputFormatMachineReadable()) { - $machineOutput = ($this->out ? file_get_contents($this->out) : $originalOutput); + $machineOutput = ($this->out ? file_get_contents($this->out) : $this->lintStdOutput); $reportWrapper = $this->decodeOutput($machineOutput); - $numOfErrors = $reportWrapper->numOfErrors(); - $numOfWarnings = $reportWrapper->numOfWarnings(); if ($this->isLintSuccess()) { $this->assets['report'] = $reportWrapper; @@ -673,16 +737,19 @@ public function run() } if (!$lintReporters) { - $this->output()->write($originalOutput); + $this->output()->write($this->lintStdOutput); } } - $exitCode = $this->getTaskExitCode($numOfErrors, $numOfWarnings); + return $this; + } + protected function runReturn(): Result + { return new Result( $this, - $exitCode, - $this->getExitMessage($exitCode) ?: $process->getErrorOutput(), + $this->getTaskExitCode(), + $this->getTaskExitMessage(), $this->getAssetsWithPrefixedNames() ); } @@ -768,14 +835,16 @@ protected function decodeOutput(string $output): ReportWrapperInterface } /** - * @return \Sweetchuck\LintReport\ReporterInterface[] + * @return $this */ - protected function initLintReporters(): array + protected function initLintReporters() { $lintReporters = []; $c = $this->getContainer(); foreach ($this->getLintReporters() as $id => $lintReporter) { if ($lintReporter === false) { + unset($this->lintReporters[$id]); + continue; } @@ -793,17 +862,25 @@ protected function initLintReporters(): array ->setDestination($this->output()); } } + + $this->lintReporters[$id] = $lintReporter; } - return $lintReporters; + return $this; } /** * Get the exit code regarding the failOn settings. */ - protected function getTaskExitCode(int $numOfErrors, int $numOfWarnings): int + protected function getTaskExitCode(): int { - if ($this->isLintSuccess()) { + /** @var \Sweetchuck\LintReport\ReportWrapperInterface $report */ + $report = $this->assets['report'] ?? null; + + if ($report) { + $numOfErrors = $report->numOfErrors(); + $numOfWarnings = $report->numOfWarnings(); + switch ($this->getFailOn()) { case 'never': return static::EXIT_CODE_OK; @@ -820,16 +897,12 @@ protected function getTaskExitCode(int $numOfErrors, int $numOfWarnings): int } } - return $this->exitCode; + return $this->lintExitCode; } - protected function getExitMessage(int $exitCode): ?string + protected function getTaskExitMessage(): string { - if (isset($this->exitMessages[$exitCode])) { - return $this->exitMessages[$exitCode]; - } - - return null; + return $this->taskExitMessages[$this->lintExitCode] ?? $this->lintStdError; } /** @@ -839,7 +912,7 @@ protected function getExitMessage(int $exitCode): ?string */ protected function isLintSuccess(): bool { - return in_array($this->exitCode, $this->lintSuccessExitCodes()); + return in_array($this->lintExitCode, $this->lintSuccessExitCodes()); } /** diff --git a/tests/unit/Task/TsLintRunTaskTest.php b/tests/unit/Task/TsLintRunTaskTest.php index 988c808..3cd1bc8 100644 --- a/tests/unit/Task/TsLintRunTaskTest.php +++ b/tests/unit/Task/TsLintRunTaskTest.php @@ -5,6 +5,7 @@ use Codeception\Test\Unit; use Codeception\Util\Stub; use Robo\Robo; +use Sweetchuck\Robo\TsLint\LintReportWrapper\ReportWrapper; use Sweetchuck\Robo\TsLint\Task\TsLintRunTask as RunTask; use \Sweetchuck\Codeception\Module\RoboTaskRunner\DummyOutput; use Sweetchuck\Robo\TsLint\Test\Helper\Dummy\DummyProcess; @@ -219,7 +220,8 @@ public function testExitCodeConstants(): void static::assertEquals(0, RunTask::EXIT_CODE_OK); static::assertEquals(1, RunTask::EXIT_CODE_WARNING); static::assertEquals(2, RunTask::EXIT_CODE_ERROR); - static::assertEquals(3, RunTask::EXIT_CODE_INVALID); + static::assertEquals(3, RunTask::EXIT_CODE_OUTPUT_DIR); + static::assertEquals(4, RunTask::EXIT_CODE_INVALID); } public function casesGetTaskExitCode(): array @@ -300,22 +302,41 @@ public function testGetTaskExitCode( string $failOn, int $numOfErrors, int $numOfWarnings, - int $exitCode + int $lintExitCode ): void { + $reportWrapper = null; + if ($lintExitCode < 3) { + $report = []; + foreach (['error' => $numOfErrors, 'warning' => $numOfWarnings] as $severity => $amount) { + for ($i = 0; $i < $amount; $i++) { + $report[] = [ + 'severity' => $severity, + 'name' => 'a.ts', + ]; + } + } + $reportWrapper = new ReportWrapper($report); + } + /** @var RunTask $runTask */ $runTask = Stub::construct( RunTask::class, [['failOn' => $failOn]], - ['exitCode' => $exitCode] + [ + 'lintExitCode' => $lintExitCode, + 'assets' => [ + 'report' => $reportWrapper, + ], + ] ); static::assertEquals( $expected, - static::getMethod('getTaskExitCode')->invokeArgs($runTask, [$numOfErrors, $numOfWarnings]) + static::getMethod('getTaskExitCode')->invokeArgs($runTask, []) ); } - public function casesRun(): array + public function casesRunSuccess(): array { $reportBase = []; @@ -391,6 +412,7 @@ public function casesRun(): array $c['c'], [ 'failOn' => $c['f'], + 'assetNamePrefix' => ($c['c'] === 0 ? 'foo' : ''), ], json_encode($report) ]; @@ -402,9 +424,9 @@ public function casesRun(): array /** * This way cannot be tested those cases when the lint process failed. * - * @dataProvider casesRun + * @dataProvider casesRunSuccess */ - public function testRun( + public function testRunSuccess( int $exitCode, array $options, string $expectedStdOutput @@ -437,6 +459,7 @@ public function testRun( DummyProcess::$prophecy[$processIndex] = [ 'exitCode' => $exitCode, 'stdOutput' => $expectedStdOutput, + 'stdError' => '', ]; $task->setLogger($container->get('logger')); @@ -499,6 +522,7 @@ public function testRunFailed(): void DummyProcess::$prophecy[$processIndex] = [ 'exitCode' => $exitCode, 'stdOutput' => $expectedReportJson, + 'stdError' => '', ]; $task->setConfig(Robo::config());