Browse Source

Merge branch '1.4'

Jordi Boggiano 8 years ago
parent
commit
e3a23c0047

+ 27 - 2
src/Composer/Installer/SuggestedPackagesReporter.php

@@ -15,6 +15,7 @@ namespace Composer\Installer;
 use Composer\IO\IOInterface;
 use Composer\Package\PackageInterface;
 use Composer\Repository\RepositoryInterface;
+use Symfony\Component\Console\Formatter\OutputFormatter;
 
 /**
  * Add suggested packages from different places to output them in the end.
@@ -116,11 +117,35 @@ class SuggestedPackagesReporter
             $this->io->writeError(sprintf(
                 '%s suggests installing %s (%s)',
                 $suggestion['source'],
-                $suggestion['target'],
-                $suggestion['reason']
+                $this->escapeOutput($suggestion['target']),
+                $this->escapeOutput($suggestion['reason'])
             ));
         }
 
         return $this;
     }
+
+    /**
+     * @param string $string
+     * @return string
+     */
+    private function escapeOutput($string)
+    {
+        return OutputFormatter::escape(
+            $this->removeControlCharacters($string)
+        );
+    }
+
+    /**
+     * @param string $string
+     * @return string
+     */
+    private function removeControlCharacters($string)
+    {
+        return preg_replace(
+            '/[[:cntrl:]]/',
+            '',
+            str_replace("\n", ' ', $string)
+        );
+    }
 }

+ 19 - 0
tests/Composer/Test/Installer/SuggestedPackagesReporterTest.php

@@ -144,6 +144,25 @@ class SuggestedPackagesReporterTest extends \PHPUnit_Framework_TestCase
         $this->suggestedPackagesReporter->output();
     }
 
+    /**
+     * @covers ::output
+     */
+    public function testOutputIgnoresFormatting()
+    {
+        $this->suggestedPackagesReporter->addPackage('source', 'target1', "\x1b[1;37;42m Like us\r\non Facebook \x1b[0m");
+        $this->suggestedPackagesReporter->addPackage('source', 'target2', "<bg=green>Like us on Facebook</>");
+
+        $this->io->expects($this->at(0))
+            ->method('writeError')
+            ->with("source suggests installing target1 ([1;37;42m Like us on Facebook [0m)");
+
+        $this->io->expects($this->at(1))
+            ->method('writeError')
+            ->with('source suggests installing target2 (\\<bg=green>Like us on Facebook\\</>)');
+
+        $this->suggestedPackagesReporter->output();
+    }
+
     /**
      * @covers ::output
      */