8000 [FrameworkBundle][TranslationDebug] add strict option · symfony/symfony@990dbc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 990dbc0

Browse files
committed
[FrameworkBundle][TranslationDebug] add strict option
1 parent 66924db commit 990dbc0

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
*/
4040
class TranslationDebugCommand extends Command
4141
{
42+
const DISPLAY_MESSAGE_MISSING = 'missing';
43+
const DISPLAY_MESSAGE_UNUSED = 'unused';
44+
const DISPLAY_MESSAGE_FALLBACK = 'fallback';
4245
const MESSAGE_MISSING = 0;
4346
const MESSAGE_UNUSED = 1;
4447
const MESSAGE_EQUALS_FALLBACK = 2;
@@ -85,6 +88,7 @@ protected function configure()
8588
new InputOption('only-missing', null, InputOption::VALUE_NONE, 'Displays only missing messages'),
8689
new InputOption('only-unused', null, InputOption::VALUE_NONE, 'Displays only unused messages'),
8790
new InputOption('all', null, InputOption::VALUE_NONE, 'Load messages from all registered bundles'),
91+
new InputOption('strict', null, InputOption::VALUE_OPTIONAL, 'Returns a non-zero exit code upon failure', false),
8892
])
8993
->setDescription('Displays translation messages information')
9094
->setHelp(<<<'EOF'
@@ -130,6 +134,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
130134

131135
$locale = $input->getArgument('locale');
132136
$domain = $input->getOption('domain');
137+
138+
$strictOption = $input->getOption('strict');
139+
$strictnessLevel = $this->getStrictnessLevel($strictOption);
140+
$exitCode = 0;
141+
133142
/** @var KernelInterface $kernel */
134143
$kernel = $this->getApplication()->getKernel();
135144
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
@@ -244,7 +253,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
244253

245254
$io->getErrorStyle()->warning($outputMessage);
246255

247-
return;
256+
if (null !== $strictnessLevel) {
257+
$exitCode = 1;
258+
}
259+
260+
return $exitCode;
248261
}
249262

250263
// Load the fallback catalogues
@@ -265,9 +278,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
265278
if ($extractedCatalogue->defines($messageId, $domain)) {
266279
if (!$currentCatalogue->defines($messageId, $domain)) {
267280
$states[] = self::MESSAGE_MISSING;
281+
282+
if (null !== $strictnessLevel && $strictnessLevel <= self::MESSAGE_MISSING) {
283+
$exitCode = $exitCode | 1;
284+
}
268285
}
269286
} elseif ($currentCatalogue->defines($messageId, $domain)) {
270287
$states[] = self::MESSAGE_UNUSED;
288+
289+
if (null !== $strictnessLevel && $strictnessLevel <= self::MESSAGE_UNUSED) {
290+
$exitCode = $exitCode | 1;
291+
}
271292
}
272293

273294
if (!\in_array(self::MESSAGE_UNUSED, $states) && true === $input->getOption('only-unused')
@@ -279,6 +300,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
279300
if ($fallbackCatalogue->defines($messageId, $domain) && $value === $fallbackCatalogue->get($messageId, $domain)) {
280301
$states[] = self::MESSAGE_EQUALS_FALLBACK;
281302

303+
if (null !== $strictnessLevel && $strictnessLevel <= self::MESSAGE_EQUALS_FALLBACK) {
304+
$exitCode = $exitCode | 1;
305+
}
306+
282307
break;
283308
}
284309
}
@@ -293,20 +318,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
293318
}
294319

295320
$io->table($headers, $rows);
321+
322+
return $exitCode;
296323
}
297324

298325
private function formatState($state): string
299326
{
300327
if (self::MESSAGE_MISSING === $state) {
301-
return '<error> missing </error>';
328+
return '<error> '.self::DISPLAY_MESSAGE_MISSING.' </error>';
302329
}
303330

304331
if (self::MESSAGE_UNUSED === $state) {
305-
return '<comment> unused </comment>';
332+
return '<comment> '.self::DISPLAY_MESSAGE_UNUSED.' </comment>';
306333
}
307334

308335
if (self::MESSAGE_EQUALS_FALLBACK === $state) {
309-
return '<info> fallback </info>';
336+
return '<info> '.self::DISPLAY_MESSAGE_FALLBACK.' </info>';
310337
}
311338

312339
return $state;
@@ -390,4 +417,25 @@ private function loadFallbackCatalogues(string $locale, array $transPaths): arra
390417

391418
return $fallbackCatalogues;
392419
}
420+
421+
/**
422+
* @param string|false $strictArg
423+
*
424+
* @return int|false|null strictness level, using MESSAGE_* values
425+
*/
426+
private function getStrictnessLevel($strictArg)
427+
{
428+
switch ($strictArg) {
429+
case self::DISPLAY_MESSAGE_FALLBACK:
430+
return self::MESSAGE_EQUALS_FALLBACK;
431+
case self::DISPLAY_MESSAGE_UNUSED:
432+
return self::MESSAGE_UNUSED;
433+
case self::DISPLAY_MESSAGE_MISSING:
434+
return self::MESSAGE_MISSING;
435+
case null:
436+
return null;
437+
default:
438+
return false;
439+
}
440+
}
393441
}

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ public function testDebugMissingMessages()
3232
$this->assertRegExp('/missing/', $tester->getDisplay());
3333
}
3434

35+
public function testDebugMissingMessagesWithMissingStrictMode()
36+
{
37+
$tester = $this->createCommandTester(['foo' => 'foo']);
38+
$res = $tester->execute(['locale' => 'en', 'bundle' => 'foo', '--strict' => 'missing']);
39+
40+
$this->assertRegExp('/missing/', $tester->getDisplay());
41+
$this->assertNotEquals(0, $res);
42+
}
43+
44+
public function testDebugMissingMessagesWithUnusedStrictMode()
45+
{
46+
$tester = $this->createCommandTester(['foo' => 'foo']);
47+
$res = $tester->execute(['locale' => 'en', 'bundle' => 'foo', '--strict' => 'unused']);
48+
49+
$this->assertRegExp('/missing/', $tester->getDisplay());
50+
$this->assertEquals(0, $res);
51+
}
52+
3553
public function testDebugUnusedMessages()
3654
{
3755
$tester = $this->createCommandTester([], ['foo' => 'foo']);
@@ -40,6 +58,24 @@ public function testDebugUnusedMessages()
4058
$this->assertRegExp('/unused/', $tester->getDisplay());
4159
}
4260

61+
public function testDebugUnusedMessagesWithUnusedStrictMode()
62+
{
63+
$tester = $this->createCommandTester([], ['foo' => 'foo']);
64+
$res = $tester->execute(['locale' => 'en', 'bundle' => 'foo', '--strict' => 'unused']);
65+
66+
$this->assertRegExp('/unused/', $tester->getDisplay());
67+
$this->assertNotEquals(0, $res);
68+
}
69+
70+
public function testDebugUnusedMessagesWithMissingStrictMode()
71+
{
72+
$tester = $this->createCommandTester([], ['foo' => 'foo']);
73+
$res = $tester->execute(['locale' => 'en', 'bundle' => 'foo', '--strict' => 'missing']);
74+
75+
$this->assertRegExp('/unused/', $tester->getDisplay());
76+
$this->assertNotEquals(0, $res);
77+
}
78+
4379
public function testDebugFallbackMessages()
4480
{
4581
$tester = $this->createCommandTester([], ['foo' => 'foo']);
@@ -56,6 +92,15 @@ public function testNoDefinedMessages()
5692
$this->assertRegExp('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
5793
}
5894

95+
public function testNoDefinedMessagesWithStrictMode()
96+
{
97+
$tester = $this->createCommandTester();
98+
$res = $tester->execute(['locale' => 'fr', 'bundle' => 'test', '--strict' => 'missing']);
99+
100+
$this->assertRegExp('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
101+
$this->assertNotEquals(0, $res);
102+
}
103+
59104
public function testDebugDefaultDirectory()
60105
{
61106
$tester = $this->createCommandTester(['foo' => 'foo'], ['bar' => 'bar']);

0 commit comments

Comments
 (0)
0