Эх сурвалжийг харах

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

Jordi Boggiano 9 жил өмнө
parent
commit
9f2e562e48

+ 18 - 14
src/Composer/Command/ValidateCommand.php

@@ -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-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('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')
             ))
             ->setHelp(<<<EOT
 The validate command validates a given composer.json and composer.lock
 
 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
 
 EOT
@@ -80,6 +82,7 @@ EOT
         $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL;
         $checkPublish = !$input->getOption('no-check-publish');
         $checkLock = !$input->getOption('no-check-lock');
+        $isStrict = $input->getOption('strict');
         list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
 
         $lockErrors = array();
@@ -91,7 +94,7 @@ EOT
 
         $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')) {
             $localRepo = $composer->getRepositoryManager()->getLocalRepository();
@@ -102,7 +105,8 @@ EOT
                     list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
                     $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;
     }
 
-    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) {
             $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>');
         }
 
-        $messages = array(
-            'error' => $errors,
-            'warning' => $warnings,
-        );
-
         // If checking publish errors, display them as errors, otherwise just show them as warnings
         if ($checkPublish) {
-            $messages['error'] = array_merge($messages['error'], $publishErrors);
+            $errors = array_merge($errors, $publishErrors);
         } 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 ($checkLock) {
-            $messages['error'] = array_merge($messages['error'], $lockErrors);
+            $errors = array_merge($errors, $lockErrors);
         } 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 ($msgs as $msg) {
                 $io->writeError('<' . $style . '>' . $msg . '</' . $style . '>');