From 5828f6202d332c1c3afe7eb309b3eae994c483a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Mo=CC=88ller?= Date: Tue, 7 Jul 2015 20:12:59 -0400 Subject: [PATCH] Enhancement: Also validate lock file --- doc/03-cli.md | 2 +- src/Composer/Command/ValidateCommand.php | 26 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/doc/03-cli.md b/doc/03-cli.md index efa21e692..57e1d08c5 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -341,9 +341,9 @@ php composer.phar validate ### Options * **--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. - ## status If you often need to modify the code of your dependencies and they are diff --git a/src/Composer/Command/ValidateCommand.php b/src/Composer/Command/ValidateCommand.php index 320032984..8a1595a04 100644 --- a/src/Composer/Command/ValidateCommand.php +++ b/src/Composer/Command/ValidateCommand.php @@ -12,6 +12,7 @@ namespace Composer\Command; +use Composer\Factory; use Composer\Package\Loader\ValidatingArrayLoader; use Composer\Util\ConfigValidator; use Symfony\Component\Console\Input\InputArgument; @@ -34,14 +35,15 @@ class ValidateCommand extends Command { $this ->setName('validate') - ->setDescription('Validates a composer.json') + ->setDescription('Validates a composer.json and composer.lock') ->setDefinition(array( 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 InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json') )) ->setHelp(<<getOption('no-check-publish'); 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 if (!$errors && !$publishErrors && !$warnings) { $this->getIO()->write('' . $file . ' is valid'); @@ -92,19 +103,26 @@ EOT '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) { $messages['error'] = array_merge($messages['error'], $publishErrors); } else { $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 ($msgs as $msg) { $this->getIO()->writeError('<' . $style . '>' . $msg . ''); } } - return $errors || ($publishErrors && $checkPublish) ? 1 : 0; + return $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 1 : 0; } }