Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/console-command.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Console command `validate-translations`

```bash
composer validate-translations [<path>...] [--dry-run] [--strict] [--format|-f <cli|json>] [--skip|-s <VALIDATOR>...] [--only|-o <VALIDATOR>...] [--recursive|-r] [--verbose|-v] [--config|-c <CONFIG>]
composer validate-translations [<path>...] [--dry-run] [--strict] [--format|-f <cli|json>] [--skip|-s <VALIDATOR>...] [--only|-o <VALIDATOR>...] [--recursive|-r] [--exclude|-e <PATTERN>] [--verbose|-v] [--config|-c <CONFIG>]
```

| Argument / Option | Shortcut | Description |
Expand All @@ -11,6 +11,7 @@ composer validate-translations [<path>...] [--dry-run] [--strict] [--format|-f <
| `--skip` | `-s` | Skips specific validators (can be used multiple times). |
| `--only` | `-o` | Runs only the specified validators (can be used multiple times). |
| `--recursive` | `-r` | Search for translation files recursively in subdirectories |
| `--exclude` | `-e` | Exclude files matching glob patterns, comma-separated (e.g., `"**/backup/**,**/*.bak"`). |
| `--verbose` | `-v` | Shows additional output for detailed information. |
| `--strict` | | Enables strict mode, treating warnings as errors. |
| `--dry-run` | | Runs the validation in test mode without saving changes. |
Expand Down
14 changes: 14 additions & 0 deletions src/Command/ValidateTranslationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ protected function configure(): void
InputOption::VALUE_NONE,
'Search for translation files recursively in subdirectories',
)
->addOption(
'exclude',
'e',
InputOption::VALUE_OPTIONAL,
'Exclude files matching glob patterns, comma-separated (e.g., "**/backup/**,**/*.bak")',
)
->setHelp(
<<<HELP
The <info>validate-translations</info> command validates translation files (XLIFF, YAML, JSON and PHP)
Expand All @@ -114,6 +120,7 @@ protected function configure(): void
<info>composer validate-translations translations/ --format github</info>
<info>composer validate-translations translations/ --dry-run</info>
<info>composer validate-translations translations/ --strict</info>
<info>composer validate-translations translations/ --exclude "**/backup/**,**/*.bak"</info>
<info>composer validate-translations translations/ --only \</info>
<info>"MoveElevator\ComposerTranslationValidator\Validator\DuplicateKeysValidator"</info>

Expand Down Expand Up @@ -169,7 +176,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->dryRun = $config->getDryRun() || $input->getOption('dry-run');
$this->strict = $config->getStrict() || $input->getOption('strict');
$recursive = (bool) $input->getOption('recursive');

// Merge exclude patterns from config and CLI
$excludePatterns = $config->getExclude();
$cliExcludeOption = $input->getOption('exclude');
if ($cliExcludeOption) {
$cliExcludePatterns = array_map(trim(...), explode(',', $cliExcludeOption));
$excludePatterns = array_merge($excludePatterns, $cliExcludePatterns);
}

$fileDetector = $this->orchestrationService->resolveFileDetector($config);

Expand Down
57 changes: 57 additions & 0 deletions tests/src/Command/ValidateTranslationCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,61 @@ public function testRecursiveOptionCanBeUsedWithOtherOptions(): void

$this->assertSame(0, $commandTester->getStatusCode());
}

public function testExecuteWithExcludeOption(): void
{
$application = new Application();
$application->add(new ValidateTranslationCommand());

$command = $application->find('validate-translations');
$commandTester = new CommandTester($command);

$commandTester->execute([
'path' => [__DIR__.'/../Fixtures/translations/xliff/success'],
'--exclude' => '**/nonexistent/**',
'--dry-run' => true,
]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('Language validation', $output);
$this->assertSame(0, $commandTester->getStatusCode());
}

public function testExecuteWithMultipleExcludePatterns(): void
{
$application = new Application();
$application->add(new ValidateTranslationCommand());

$command = $application->find('validate-translations');
$commandTester = new CommandTester($command);

$commandTester->execute([
'path' => [__DIR__.'/../Fixtures/translations/xliff/success'],
'--exclude' => '**/backup/**,**/*.bak',
'--dry-run' => true,
]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('Language validation', $output);
$this->assertSame(0, $commandTester->getStatusCode());
}

public function testExecuteWithExcludeShortOption(): void
{
$application = new Application();
$application->add(new ValidateTranslationCommand());

$command = $application->find('validate-translations');
$commandTester = new CommandTester($command);

$commandTester->execute([
'path' => [__DIR__.'/../Fixtures/translations/xliff/success'],
'-e' => '**/temp/**',
'--dry-run' => true,
]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('Language validation', $output);
$this->assertSame(0, $commandTester->getStatusCode());
}
}