Skip to content

Commit

Permalink
Improve command status code
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Nov 11, 2022
1 parent 69fa77c commit 8b04afe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Command/TwigCsFixerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$workingDir = getcwd();
$workingDir = @getcwd();
if (false === $workingDir) {
return $this->fail($output, 'Cannot get the current working directory.');
return $this->invalid($output, 'Cannot get the current working directory.');
}

try {
Expand Down Expand Up @@ -98,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$reporter = new TextFormatter($input, $output);
$reporter->display($report, $input->getOption('level'));
} catch (Throwable $exception) {
return $this->fail($output, $exception->getMessage());
return $this->invalid($output, $exception->getMessage());
}

// Return a meaningful error code.
Expand All @@ -109,10 +109,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}

private function fail(OutputInterface $output, string $message): int
private function invalid(OutputInterface $output, string $message): int
{
$output->writeln("<error>Error: {$message}</error>");

return self::FAILURE;
return self::INVALID;
}
}
13 changes: 8 additions & 5 deletions tests/Command/TwigCsFixerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace TwigCsFixer\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use TwigCsFixer\Command\TwigCsFixerCommand;
use TwigCsFixer\Config\Config;
Expand All @@ -24,7 +25,7 @@ public function testExecuteWithPaths(): void
'[ERROR] Files linted: 3, notices: 0, warnings: 0, errors: 3',
$commandTester->getDisplay()
);
static::assertSame(1, $commandTester->getStatusCode());
static::assertSame(Command::FAILURE, $commandTester->getStatusCode());
}

public function testExecuteWithConfig(): void
Expand All @@ -41,7 +42,7 @@ public function testExecuteWithConfig(): void
'[ERROR] Files linted: 3, notices: 0, warnings: 0, errors: 1',
$commandTester->getDisplay()
);
static::assertSame(1, $commandTester->getStatusCode());
static::assertSame(Command::FAILURE, $commandTester->getStatusCode());
}

public function testExecuteWithSuccess(): void
Expand All @@ -57,7 +58,7 @@ public function testExecuteWithSuccess(): void
'[OK] Files linted: 1, notices: 0, warnings: 0, errors: 0',
$commandTester->getDisplay()
);
static::assertSame(0, $commandTester->getStatusCode());
static::assertSame(Command::SUCCESS, $commandTester->getStatusCode());
}

public function testExecuteWithOptionFix(): void
Expand All @@ -74,7 +75,7 @@ public function testExecuteWithOptionFix(): void
'[OK] Files linted: 1, notices: 0, warnings: 0, errors: 0',
$commandTester->getDisplay()
);
static::assertSame(0, $commandTester->getStatusCode());
static::assertSame(Command::SUCCESS, $commandTester->getStatusCode());
}

public function testExecuteWithError(): void
Expand All @@ -88,7 +89,7 @@ public function testExecuteWithError(): void
]);

static::assertStringStartsWith('Error: ', $commandTester->getDisplay());
static::assertSame(1, $commandTester->getStatusCode());
static::assertSame(Command::INVALID, $commandTester->getStatusCode());
}

public function testExecuteWithCacheByDefault(): void
Expand All @@ -109,6 +110,7 @@ public function testExecuteWithCacheByDefault(): void
sprintf('Using cache file "%s".', Config::DEFAULT_CACHE_PATH),
$commandTester->getDisplay()
);
static::assertSame(Command::SUCCESS, $commandTester->getStatusCode());
}

public function testExecuteWithCacheDisabled(): void
Expand All @@ -131,5 +133,6 @@ public function testExecuteWithCacheDisabled(): void
'Using cache file',
$commandTester->getDisplay()
);
static::assertSame(Command::SUCCESS, $commandTester->getStatusCode());
}
}

0 comments on commit 8b04afe

Please sign in to comment.