Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Split TsLintRunTask::run into smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweetchuck committed Jul 30, 2017
1 parent 3eb159e commit 80850ea
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 59 deletions.
187 changes: 135 additions & 52 deletions src/Task/TsLintRunTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = [
Expand All @@ -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.
*
Expand Down Expand Up @@ -621,45 +645,95 @@ protected function createIncludeList($items, bool $include): array
*/
public function run()
{
$lintReporters = $this->initLintReporters();
return $this
->runPrepare()
->runHeader()
->runDoIt()
->runProcessOutput()
->runReturn();
}

/**
* @return $this
*/
protected function runPrepare()
{
$this->initLintReporters();
if ($this->getLintReporters() && $this->getFormat() === '') {
$this->setFormat('json');
}

$this->command = $this->getCommand();

return $this;
}

/**
* @return $this
*/
protected function runHeader()
{
$this->printTaskInfo(
'TsLint run: <info>{command}</info>',
[
'command' => $this->command,
]
);

return $this;
}

/**
* @return $this
*/
public function runDoIt()
{
$lintReporters = $this->getLintReporters();
if ($lintReporters && $this->getFormat() === '') {
$this->setFormat('json');
}

$command = $this->getCommand();
$this->printTaskInfo(
'TsLint task runs: <info>{command}</info> in directory "<info>{workingDirectory}</info>"',
[
'command' => $command,
'command' => $this->command,
'workingDirectory' => $this->workingDirectory ?: '.',
]
);

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);

$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();

$numOfErrors = $this->exitCode;
$numOfWarnings = 0;
return $this;
}

/**
* @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;
Expand All @@ -673,16 +747,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()
);
}
Expand Down Expand Up @@ -768,14 +845,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;
}

Expand All @@ -793,17 +872,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;
Expand All @@ -820,16 +907,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;
}

/**
Expand All @@ -839,7 +922,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());
}

/**
Expand Down
37 changes: 30 additions & 7 deletions tests/unit/Task/TsLintRunTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -402,9 +423,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
Expand Down Expand Up @@ -437,6 +458,7 @@ public function testRun(
DummyProcess::$prophecy[$processIndex] = [
'exitCode' => $exitCode,
'stdOutput' => $expectedStdOutput,
'stdError' => '',
];

$task->setLogger($container->get('logger'));
Expand Down Expand Up @@ -499,6 +521,7 @@ public function testRunFailed(): void
DummyProcess::$prophecy[$processIndex] = [
'exitCode' => $exitCode,
'stdOutput' => $expectedReportJson,
'stdError' => '',
];

$task->setConfig(Robo::config());
Expand Down

0 comments on commit 80850ea

Please sign in to comment.