Browse Source

Merge pull request #12480 from AlPri78/optionExtract

Add an option on extractTask to avoid the display of marker errors
Mark Story 7 years ago
parent
commit
2856f0aab5

+ 31 - 1
src/Shell/Task/ExtractTask.php

@@ -113,6 +113,18 @@ class ExtractTask extends Shell
     protected $_extractCore = false;
 
     /**
+     * Displays marker error(s) if true
+     * @var bool
+     */
+    protected $_markerError;
+
+    /**
+     * Count number of marker errors found
+     * @var bool
+     */
+    protected $_countMarkerError = 0;
+
+    /**
      * No welcome message.
      *
      * @return void
@@ -240,6 +252,7 @@ class ExtractTask extends Shell
             $this->_merge = strtolower($response) === 'y';
         }
 
+        $this->_markerError = $this->param('marker-error');
         $this->_relativePaths = $this->param('relative-paths');
 
         if (empty($this->_files)) {
@@ -310,6 +323,11 @@ class ExtractTask extends Shell
         $this->_paths = $this->_files = $this->_storage = [];
         $this->_translations = $this->_tokens = [];
         $this->out();
+        if ($this->_countMarkerError) {
+            $this->err("{$this->_countMarkerError} marker error(s) detected.");
+            $this->err(" => Use the --marker-error option to display errors.");
+        }
+
         $this->out('Done.');
     }
 
@@ -366,6 +384,10 @@ class ExtractTask extends Shell
             'boolean' => true,
             'default' => false,
             'help' => 'Do not write file locations for each extracted message.',
+        ])->addOption('marker-error', [
+            'boolean' => true,
+            'default' => false,
+            'help' => 'Do not display marker error.',
         ]);
 
         return $parser;
@@ -470,7 +492,7 @@ class ExtractTask extends Shell
                         $details['msgctxt'] = $context;
                     }
                     $this->_addTranslation($domain, $singular, $details);
-                } elseif (strpos($this->_file, CAKE_CORE_INCLUDE_PATH) === false) {
+                } else {
                     $this->_markerError($this->_file, $line, $functionName, $count);
                 }
             }
@@ -691,6 +713,14 @@ class ExtractTask extends Shell
      */
     protected function _markerError($file, $line, $marker, $count)
     {
+        if (strpos($this->_file, CAKE_CORE_INCLUDE_PATH) === false) {
+            $this->_countMarkerError++;
+        }
+
+        if (!$this->_markerError) {
+            return;
+        }
+
         $this->err(sprintf("Invalid marker content in %s:%s\n* %s(", $file, $line, $marker));
         $count += 2;
         $tokenCount = count($this->_tokens);

+ 35 - 0
tests/TestCase/Shell/Task/ExtractTaskTest.php

@@ -17,9 +17,14 @@ namespace Cake\Test\TestCase\Shell\Task;
 use Cake\Core\Plugin;
 use Cake\Filesystem\Folder;
 use Cake\TestSuite\TestCase;
+use PHPUnit\Framework\MockObject\MockObject;
 
 /**
  * ExtractTaskTest class
+ *
+ * @property \Cake\Shell\Task\ExtractTask|MockObject $Task
+ * @property \Cake\Console\ConsoleIo|MockObject $io
+ * @property string $path
  */
 class ExtractTaskTest extends TestCase
 {
@@ -361,6 +366,36 @@ class ExtractTaskTest extends TestCase
     }
 
     /**
+     * Test when marker-error option is set
+     * When marker-error is unset, it's already test
+     * with other functions like testExecute that not detects error because err never called
+     */
+    public function testMarkerErrorSets()
+    {
+        $this->Task->method('err')
+            ->will($this->returnCallback([$this, 'echoTest']));
+
+        $this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
+        $this->Task->params['output'] = $this->path . DS;
+        $this->Task->params['extract-core'] = 'no';
+        $this->Task->params['merge'] = 'no';
+        $this->Task->params['marker-error'] = true;
+
+        $this->expectOutputRegex('/.*Invalid marker content in .*extract\.ctp.*/');
+        $this->Task->main();
+    }
+
+    /**
+     * A useful function to mock/replace err or out function that allows to use expectOutput
+     * @param string $val
+     * @param int $nbLines
+     */
+    public function echoTest($val = '', $nbLines = 1)
+    {
+        echo $val . str_repeat(PHP_EOL, $nbLines);
+    }
+
+    /**
      * test relative-paths option
      *
      * @return void

+ 3 - 0
tests/test_app/TestApp/Template/Pages/extract.ctp

@@ -18,6 +18,9 @@ echo __('You have %d new message.');
 echo __('double "quoted"');
 echo __("single 'quoted'");
 
+// Contains no string like a variable or a function or ...
+echo __($count);
+
 // Multiline
 __('Hot features!'
     . "\n - No Configuration:"