Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5439bf2

Browse files
gndknicolas-grekas
authored andcommitted
[FrameworkBundle] Fix exit codes in debug:translation command
The `--only-missing` and `--only-unused` options should be independent of each other. When using the `--only-missing` option, only **missing** messages should be relevant to the outcome of the execution. If there are no missing messages, but some unused messages, the execution of the command was still successful and no non-zero exit code should be returned. The same applies when using the `--only-unused` option. In this case, only **unused** messages should be relevant to the execution result, even if there are some missing messages.
1 parent 326944d commit 5439bf2

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
133133
$locale = $input->getArgument('locale');
134134
$domain = $input->getOption('domain');
135135

136-
$exitCode = 0;
136+
$exitCode = self::SUCCESS;
137137

138138
/** @var KernelInterface $kernel */
139139
$kernel = $this->getApplication()->getKernel();
@@ -219,16 +219,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
219219
if (!$currentCatalogue->defines($messageId, $domain)) {
220220
$states[] = self::MESSAGE_MISSING;
221221

222-
$exitCode = $exitCode | self::EXIT_CODE_MISSING;
222+
if (!$input->getOption('only-unused')) {
223+
$exitCode = $exitCode | self::EXIT_CODE_MISSING;
224+
}
223225
}
224226
} elseif ($currentCatalogue->defines($messageId, $domain)) {
225227
$states[] = self::MESSAGE_UNUSED;
226228

227-
$exitCode = $exitCode | self::EXIT_CODE_UNUSED;
229+
if (!$input->getOption('only-missing')) {
230+
$exitCode = $exitCode | self::EXIT_CODE_UNUSED;
231+
}
228232
}
229233

230-
if (!\in_array(self::MESSAGE_UNUSED, $states) && true === $input->getOption('only-unused')
231-
|| !\in_array(self::MESSAGE_MISSING, $states) && true === $input->getOption('only-missing')) {
234+
if (!\in_array(self::MESSAGE_UNUSED, $states) && $input->getOption('only-unused')
235+
|| !\in_array(self::MESSAGE_MISSING, $states) && $input->getOption('only-missing')
236+
) {
232237
continue;
233238
}
234239

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
1616
use Symfony\Bundle\FrameworkBundle\Console\Application;
1717
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ExtensionWithoutConfigTestBundle\ExtensionWithoutConfigTestBundle;
18+
use Symfony\Component\Console\Command\Command;
1819
use Symfony\Component\Console\Tester\CommandCompletionTester;
1920
use Symfony\Component\Console\Tester\CommandTester;
2021
use Symfony\Component\DependencyInjection\Container;
@@ -36,7 +37,7 @@ public function testDebugMissingMessages()
3637
$res = $tester->execute(['locale' => 'en', 'bundle' => 'foo']);
3738

3839
$this->assertMatchesRegularExpression('/missing/', $tester->getDisplay());
39-
$this->assertEquals(TranslationDebugCommand::EXIT_CODE_MISSING, $res);
40+
$this->assertSame(TranslationDebugCommand::EXIT_CODE_MISSING, $res);
4041
}
4142

4243
public function testDebugUnusedMessages()
@@ -45,7 +46,7 @@ public function testDebugUnusedMessages()
4546
$res = $tester->execute(['locale' => 'en', 'bundle' => 'foo']);
4647

4748
$this->assertMatchesRegularExpression('/unused/', $tester->getDisplay());
48-
$this->assertEquals(TranslationDebugCommand::EXIT_CODE_UNUSED, $res);
49+
$this->assertSame(TranslationDebugCommand::EXIT_CODE_UNUSED, $res);
4950
}
5051

5152
public function testDebugFallbackMessages()
@@ -54,7 +55,7 @@ public function testDebugFallbackMessages()
5455
$res = $tester->execute(['locale' => 'fr', 'bundle' => 'foo']);
5556

5657
$this->assertMatchesRegularExpression('/fallback/', $tester->getDisplay());
57-
$this->assertEquals(TranslationDebugCommand::EXIT_CODE_FALLBACK, $res);
58+
$this->assertSame(TranslationDebugCommand::EXIT_CODE_FALLBACK, $res);
5859
}
5960

6061
public function testNoDefinedMessages()
@@ -63,7 +64,7 @@ public function testNoDefinedMessages()
6364
$res = $tester->execute(['locale' => 'fr', 'bundle' => 'test']);
6465

6566
$this->assertMatchesRegularExpression('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
66-
$this->assertEquals(TranslationDebugCommand::EXIT_CODE_GENERAL_ERROR, $res);
67+
$this->assertSame(TranslationDebugCommand::EXIT_CODE_GENERAL_ERROR, $res);
6768
}
6869

6970
public function testDebugDefaultDirectory()
@@ -74,7 +75,7 @@ public function testDebugDefaultDirectory()
7475

7576
$this->assertMatchesRegularExpression('/missing/', $tester->getDisplay());
7677
$this->assertMatchesRegularExpression('/unused/', $tester->getDisplay());
77-
$this->assertEquals($expectedExitStatus, $res);
78+
$this->assertSame($expectedExitStatus, $res);
7879
}
7980

8081
public function testDebugDefaultRootDirectory()
@@ -92,7 +93,7 @@ public function testDebugDefaultRootDirectory()
9293

9394
$this->assertMatchesRegularExpression('/missing/', $tester->getDisplay());
9495
$this->assertMatchesRegularExpression('/unused/', $tester->getDisplay());
95-
$this->assertEquals($expectedExitStatus, $res);
96+
$this->assertSame($expectedExitStatus, $res);
9697
}
9798

9899
public function testDebugCustomDirectory()
@@ -112,7 +113,7 @@ public function testDebugCustomDirectory()
112113

113114
$this->assertMatchesRegularExpression('/missing/', $tester->getDisplay());
114115
$this->assertMatchesRegularExpression('/unused/', $tester->getDisplay());
115-
$this->assertEquals($expectedExitStatus, $res);
116+
$this->assertSame($expectedExitStatus, $res);
116117
}
117118

118119
public function testDebugInvalidDirectory()
@@ -128,6 +129,22 @@ public function testDebugInvalidDirectory()
128129
$tester->execute(['locale' => 'en', 'bundle' => 'dir']);
129130
}
130131

132+
public function testNoErrorWithOnlyMissingOptionAndNoResults()
133+
{
134+
$tester = $this->createCommandTester([], ['foo' => 'foo']);
135+
$res = $tester->execute(['locale' => 'en', '--only-missing' => true]);
136+
137+
$this->assertSame(Command::SUCCESS, $res);
138+
}
139+
140+
public function testNoErrorWithOnlyUnusedOptionAndNoResults()
141+
{
142+
$tester = $this->createCommandTester(['foo' => 'foo']);
143+
$res = $tester->execute(['locale' => 'en', '--only-unused' => true]);
144+
145+
$this->assertSame(Command::SUCCESS, $res);
146+
}
147+
131148
protected function setUp(): void
132149
{
133150
$this->fs = new Filesystem();

0 commit comments

Comments
 (0)