[FrameworkBundle][TranslationDebug] Return non-zero exit code on failure
This commit is contained in:
parent
7ecb5aad8e
commit
0baafd8bc5
@ -38,6 +38,10 @@ use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
*/
|
||||
class TranslationDebugCommand extends Command
|
||||
{
|
||||
const EXIT_CODE_GENERAL_ERROR = 64;
|
||||
const EXIT_CODE_MISSING = 65;
|
||||
const EXIT_CODE_UNUSED = 66;
|
||||
const EXIT_CODE_FALLBACK = 68;
|
||||
const MESSAGE_MISSING = 0;
|
||||
const MESSAGE_UNUSED = 1;
|
||||
const MESSAGE_EQUALS_FALLBACK = 2;
|
||||
@ -123,6 +127,9 @@ EOF
|
||||
|
||||
$locale = $input->getArgument('locale');
|
||||
$domain = $input->getOption('domain');
|
||||
|
||||
$exitCode = 0;
|
||||
|
||||
/** @var KernelInterface $kernel */
|
||||
$kernel = $this->getApplication()->getKernel();
|
||||
|
||||
@ -191,7 +198,7 @@ EOF
|
||||
|
||||
$io->getErrorStyle()->warning($outputMessage);
|
||||
|
||||
return 0;
|
||||
return self::EXIT_CODE_GENERAL_ERROR;
|
||||
}
|
||||
|
||||
// Load the fallback catalogues
|
||||
@ -212,9 +219,13 @@ EOF
|
||||
if ($extractedCatalogue->defines($messageId, $domain)) {
|
||||
if (!$currentCatalogue->defines($messageId, $domain)) {
|
||||
$states[] = self::MESSAGE_MISSING;
|
||||
|
||||
$exitCode = $exitCode | self::EXIT_CODE_MISSING;
|
||||
}
|
||||
} elseif ($currentCatalogue->defines($messageId, $domain)) {
|
||||
$states[] = self::MESSAGE_UNUSED;
|
||||
|
||||
$exitCode = $exitCode | self::EXIT_CODE_UNUSED;
|
||||
}
|
||||
|
||||
if (!\in_array(self::MESSAGE_UNUSED, $states) && true === $input->getOption('only-unused')
|
||||
@ -226,6 +237,8 @@ EOF
|
||||
if ($fallbackCatalogue->defines($messageId, $domain) && $value === $fallbackCatalogue->get($messageId, $domain)) {
|
||||
$states[] = self::MESSAGE_EQUALS_FALLBACK;
|
||||
|
||||
$exitCode = $exitCode | self::EXIT_CODE_FALLBACK;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -241,7 +254,7 @@ EOF
|
||||
|
||||
$io->table($headers, $rows);
|
||||
|
||||
return 0;
|
||||
return $exitCode;
|
||||
}
|
||||
|
||||
private function formatState(int $state): string
|
||||
|
@ -26,42 +26,48 @@ class TranslationDebugCommandTest extends TestCase
|
||||
public function testDebugMissingMessages()
|
||||
{
|
||||
$tester = $this->createCommandTester(['foo' => 'foo']);
|
||||
$tester->execute(['locale' => 'en', 'bundle' => 'foo']);
|
||||
$res = $tester->execute(['locale' => 'en', 'bundle' => 'foo']);
|
||||
|
||||
$this->assertRegExp('/missing/', $tester->getDisplay());
|
||||
$this->assertEquals(TranslationDebugCommand::EXIT_CODE_MISSING, $res);
|
||||
}
|
||||
|
||||
public function testDebugUnusedMessages()
|
||||
{
|
||||
$tester = $this->createCommandTester([], ['foo' => 'foo']);
|
||||
$tester->execute(['locale' => 'en', 'bundle' => 'foo']);
|
||||
$res = $tester->execute(['locale' => 'en', 'bundle' => 'foo']);
|
||||
|
||||
$this->assertRegExp('/unused/', $tester->getDisplay());
|
||||
$this->assertEquals(TranslationDebugCommand::EXIT_CODE_UNUSED, $res);
|
||||
}
|
||||
|
||||
public function testDebugFallbackMessages()
|
||||
{
|
||||
$tester = $this->createCommandTester([], ['foo' => 'foo']);
|
||||
$tester->execute(['locale' => 'fr', 'bundle' => 'foo']);
|
||||
$tester = $this->createCommandTester(['foo' => 'foo'], ['foo' => 'foo']);
|
||||
$res = $tester->execute(['locale' => 'fr', 'bundle' => 'foo']);
|
||||
|
||||
$this->assertRegExp('/fallback/', $tester->getDisplay());
|
||||
$this->assertEquals(TranslationDebugCommand::EXIT_CODE_FALLBACK, $res);
|
||||
}
|
||||
|
||||
public function testNoDefinedMessages()
|
||||
{
|
||||
$tester = $this->createCommandTester();
|
||||
$tester->execute(['locale' => 'fr', 'bundle' => 'test']);
|
||||
$res = $tester->execute(['locale' => 'fr', 'bundle' => 'test']);
|
||||
|
||||
$this->assertRegExp('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
|
||||
$this->assertEquals(TranslationDebugCommand::EXIT_CODE_GENERAL_ERROR, $res);
|
||||
}
|
||||
|
||||
public function testDebugDefaultDirectory()
|
||||
{
|
||||
$tester = $this->createCommandTester(['foo' => 'foo'], ['bar' => 'bar']);
|
||||
$tester->execute(['locale' => 'en']);
|
||||
$res = $tester->execute(['locale' => 'en']);
|
||||
$expectedExitStatus = TranslationDebugCommand::EXIT_CODE_MISSING | TranslationDebugCommand::EXIT_CODE_UNUSED;
|
||||
|
||||
$this->assertRegExp('/missing/', $tester->getDisplay());
|
||||
$this->assertRegExp('/unused/', $tester->getDisplay());
|
||||
$this->assertEquals($expectedExitStatus, $res);
|
||||
}
|
||||
|
||||
public function testDebugDefaultRootDirectory()
|
||||
@ -72,11 +78,14 @@ class TranslationDebugCommandTest extends TestCase
|
||||
$this->fs->mkdir($this->translationDir.'/translations');
|
||||
$this->fs->mkdir($this->translationDir.'/templates');
|
||||
|
||||
$expectedExitStatus = TranslationDebugCommand::EXIT_CODE_MISSING | TranslationDebugCommand::EXIT_CODE_UNUSED;
|
||||
|
||||
$tester = $this->createCommandTester(['foo' => 'foo'], ['bar' => 'bar'], null, [$this->translationDir.'/trans'], [$this->translationDir.'/views']);
|
||||
$tester->execute(['locale' => 'en']);
|
||||
$res = $tester->execute(['locale' => 'en']);
|
||||
|
||||
$this->assertRegExp('/missing/', $tester->getDisplay());
|
||||
$this->assertRegExp('/unused/', $tester->getDisplay());
|
||||
$this->assertEquals($expectedExitStatus, $res);
|
||||
}
|
||||
|
||||
public function testDebugCustomDirectory()
|
||||
@ -89,11 +98,14 @@ class TranslationDebugCommandTest extends TestCase
|
||||
->with($this->equalTo($this->translationDir.'/customDir'))
|
||||
->willThrowException(new \InvalidArgumentException());
|
||||
|
||||
$expectedExitStatus = TranslationDebugCommand::EXIT_CODE_MISSING | TranslationDebugCommand::EXIT_CODE_UNUSED;
|
||||
|
||||
$tester = $this->createCommandTester(['foo' => 'foo'], ['bar' => 'bar'], $kernel);
|
||||
$tester->execute(['locale' => 'en', 'bundle' => $this->translationDir.'/customDir']);
|
||||
$res = $tester->execute(['locale' => 'en', 'bundle' => $this->translationDir.'/customDir']);
|
||||
|
||||
$this->assertRegExp('/missing/', $tester->getDisplay());
|
||||
$this->assertRegExp('/unused/', $tester->getDisplay());
|
||||
$this->assertEquals($expectedExitStatus, $res);
|
||||
}
|
||||
|
||||
public function testDebugInvalidDirectory()
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
|
||||
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
@ -32,7 +33,11 @@ class TranslationDebugCommandTest extends AbstractWebTestCase
|
||||
$tester = $this->createCommandTester();
|
||||
$ret = $tester->execute(['locale' => 'en']);
|
||||
|
||||
$this->assertSame(0, $ret, 'Returns 0 in case of success');
|
||||
$this->assertSame(
|
||||
TranslationDebugCommand::EXIT_CODE_MISSING | TranslationDebugCommand::EXIT_CODE_UNUSED,
|
||||
$ret,
|
||||
'Returns appropriate exit code in the event of error'
|
||||
);
|
||||
$this->assertStringContainsString('missing messages hello_from_construct_arg_service', $tester->getDisplay());
|
||||
$this->assertStringContainsString('missing messages hello_from_subscriber_service', $tester->getDisplay());
|
||||
$this->assertStringContainsString('missing messages hello_from_property_service', $tester->getDisplay());
|
||||
|
Reference in New Issue
Block a user