1
0
Fork 0

Add --strict flag to validate command to treat warnings as failures as well, refs #3202

pull/4432/head
Jordi Boggiano 2015-09-20 19:44:55 +01:00
parent ae23979dfa
commit 9f2e562e48
1 changed files with 18 additions and 14 deletions

View File

@ -40,14 +40,16 @@ class ValidateCommand extends Command
new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not make a complete validation'), new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not make a complete validation'),
new InputOption('no-check-lock', null, InputOption::VALUE_NONE, 'Do not check if lock file is up to date'), new InputOption('no-check-lock', null, InputOption::VALUE_NONE, 'Do not check if lock file is up to date'),
new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'), new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'),
new InputOption('with-dependencies', 'A', InputOption::VALUE_NONE, 'Also validate the composer.json of all installed dependencies.'), new InputOption('with-dependencies', 'A', InputOption::VALUE_NONE, 'Also validate the composer.json of all installed dependencies'),
new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code for warnings as well as errors'),
new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json') new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json')
)) ))
->setHelp(<<<EOT ->setHelp(<<<EOT
The validate command validates a given composer.json and composer.lock The validate command validates a given composer.json and composer.lock
Exit codes in case of errors are: Exit codes in case of errors are:
1 validation error(s) 1 validation warning(s), only when --strict is given
2 validation error(s)
3 file unreadable or missing 3 file unreadable or missing
EOT EOT
@ -80,6 +82,7 @@ EOT
$checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL; $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL;
$checkPublish = !$input->getOption('no-check-publish'); $checkPublish = !$input->getOption('no-check-publish');
$checkLock = !$input->getOption('no-check-lock'); $checkLock = !$input->getOption('no-check-lock');
$isStrict = $input->getOption('strict');
list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll); list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
$lockErrors = array(); $lockErrors = array();
@ -91,7 +94,7 @@ EOT
$this->outputResult($io, $file, $errors, $warnings, $checkPublish, $publishErrors, $checkLock, $lockErrors, true); $this->outputResult($io, $file, $errors, $warnings, $checkPublish, $publishErrors, $checkLock, $lockErrors, true);
$exitCode = $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 1 : 0; $exitCode = $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 2 : ($isStrict && $warnings ? 1 : 0);
if ($input->getOption('with-dependencies')) { if ($input->getOption('with-dependencies')) {
$localRepo = $composer->getRepositoryManager()->getLocalRepository(); $localRepo = $composer->getRepositoryManager()->getLocalRepository();
@ -102,7 +105,8 @@ EOT
list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll); list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
$this->outputResult($io, $package->getPrettyName(), $errors, $warnings, $checkPublish, $publishErrors); $this->outputResult($io, $package->getPrettyName(), $errors, $warnings, $checkPublish, $publishErrors);
$exitCode = $exitCode === 1 || $errors || ($publishErrors && $checkPublish) ? 1 : 0; $depCode = $errors || ($publishErrors && $checkPublish) ? 2 : ($isStrict && $warnings ? 1 : 0);
$exitCode = max($depCode, $exitCode);
} }
} }
} }
@ -110,7 +114,7 @@ EOT
return $exitCode; return $exitCode;
} }
private function outputResult($io, $name, $errors, $warnings, $checkPublish = false, $publishErrors = array(), $checkLock = false, $lockErrors = array(), $printSchemaUrl = false) private function outputResult($io, $name, &$errors, &$warnings, $checkPublish = false, $publishErrors = array(), $checkLock = false, $lockErrors = array(), $printSchemaUrl = false)
{ {
if (!$errors && !$publishErrors && !$warnings) { if (!$errors && !$publishErrors && !$warnings) {
$io->write('<info>' . $name . ' is valid</info>'); $io->write('<info>' . $name . ' is valid</info>');
@ -129,25 +133,25 @@ EOT
$io->writeError('<error>' . $name . ' is invalid, the following errors/warnings were found:</error>'); $io->writeError('<error>' . $name . ' is invalid, the following errors/warnings were found:</error>');
} }
$messages = array(
'error' => $errors,
'warning' => $warnings,
);
// If checking publish errors, display them as errors, otherwise just show them as warnings // If checking publish errors, display them as errors, otherwise just show them as warnings
if ($checkPublish) { if ($checkPublish) {
$messages['error'] = array_merge($messages['error'], $publishErrors); $errors = array_merge($errors, $publishErrors);
} else { } else {
$messages['warning'] = array_merge($messages['warning'], $publishErrors); $warnings = array_merge($warnings, $publishErrors);
} }
// If checking lock errors, display them as errors, otherwise just show them as warnings // If checking lock errors, display them as errors, otherwise just show them as warnings
if ($checkLock) { if ($checkLock) {
$messages['error'] = array_merge($messages['error'], $lockErrors); $errors = array_merge($errors, $lockErrors);
} else { } else {
$messages['warning'] = array_merge($messages['warning'], $lockErrors); $warnings = array_merge($warnings, $lockErrors);
} }
$messages = array(
'error' => $errors,
'warning' => $warnings,
);
foreach ($messages as $style => $msgs) { foreach ($messages as $style => $msgs) {
foreach ($msgs as $msg) { foreach ($msgs as $msg) {
$io->writeError('<' . $style . '>' . $msg . '</' . $style . '>'); $io->writeError('<' . $style . '>' . $msg . '</' . $style . '>');