Enhancement: Also validate lock file

main
Andreas Möller 9 years ago
parent 39e6f51bef
commit 5828f6202d

@ -341,9 +341,9 @@ php composer.phar validate
### Options ### Options
* **--no-check-all:** Do not emit a warning if requirements in `composer.json` use unbound version constraints. * **--no-check-all:** Do not emit a warning if requirements in `composer.json` use unbound version constraints.
* **--no-check-lock:** Do not emit an error if `composer.lock` exists and is not up to date.
* **--no-check-publish:** Do not emit an error if `composer.json` is unsuitable for publishing as a package on Packagist but is otherwise valid. * **--no-check-publish:** Do not emit an error if `composer.json` is unsuitable for publishing as a package on Packagist but is otherwise valid.
## status ## status
If you often need to modify the code of your dependencies and they are If you often need to modify the code of your dependencies and they are

@ -12,6 +12,7 @@
namespace Composer\Command; namespace Composer\Command;
use Composer\Factory;
use Composer\Package\Loader\ValidatingArrayLoader; use Composer\Package\Loader\ValidatingArrayLoader;
use Composer\Util\ConfigValidator; use Composer\Util\ConfigValidator;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
@ -34,14 +35,15 @@ class ValidateCommand extends Command
{ {
$this $this
->setName('validate') ->setName('validate')
->setDescription('Validates a composer.json') ->setDescription('Validates a composer.json and composer.lock')
->setDefinition(array( ->setDefinition(array(
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-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 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 The validate command validates a given composer.json and composer.lock
EOT EOT
); );
@ -73,6 +75,15 @@ EOT
$checkPublish = !$input->getOption('no-check-publish'); $checkPublish = !$input->getOption('no-check-publish');
list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll); list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
$checkLock = !$input->getOption('no-check-lock');
$lockErrors = array();
$composer = Factory::create($this->getIO(), $file);
$locker = $composer->getLocker();
if ($locker->isLocked() && !$locker->isFresh()) {
$lockErrors[] = 'The lock file is not up to date with the latest changes in composer.json.';
}
// output errors/warnings // output errors/warnings
if (!$errors && !$publishErrors && !$warnings) { if (!$errors && !$publishErrors && !$warnings) {
$this->getIO()->write('<info>' . $file . ' is valid</info>'); $this->getIO()->write('<info>' . $file . ' is valid</info>');
@ -92,19 +103,26 @@ EOT
'warning' => $warnings, 'warning' => $warnings,
); );
// If checking publish errors, display them 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); $messages['error'] = array_merge($messages['error'], $publishErrors);
} else { } else {
$messages['warning'] = array_merge($messages['warning'], $publishErrors); $messages['warning'] = array_merge($messages['warning'], $publishErrors);
} }
// If checking lock errors, display them as errors, otherwise just show them as warnings
if ($checkLock) {
$messages['error'] = array_merge($messages['error'], $lockErrors);
} else {
$messages['warning'] = array_merge($messages['warning'], $lockErrors);
}
foreach ($messages as $style => $msgs) { foreach ($messages as $style => $msgs) {
foreach ($msgs as $msg) { foreach ($msgs as $msg) {
$this->getIO()->writeError('<' . $style . '>' . $msg . '</' . $style . '>'); $this->getIO()->writeError('<' . $style . '>' . $msg . '</' . $style . '>');
} }
} }
return $errors || ($publishErrors && $checkPublish) ? 1 : 0; return $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 1 : 0;
} }
} }

Loading…
Cancel
Save