From 401ef69ae3bcd94511d3abab7da7fea710545e31 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 8 Apr 2017 13:37:25 +0200 Subject: [PATCH 1/2] Remove ANSI control characters from suggested packages output. --- .../Installer/SuggestedPackagesReporter.php | 17 +++++++++++++++-- .../Installer/SuggestedPackagesReporterTest.php | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Composer/Installer/SuggestedPackagesReporter.php b/src/Composer/Installer/SuggestedPackagesReporter.php index 935ec50cf..e9d66f6a6 100644 --- a/src/Composer/Installer/SuggestedPackagesReporter.php +++ b/src/Composer/Installer/SuggestedPackagesReporter.php @@ -116,11 +116,24 @@ class SuggestedPackagesReporter $this->io->writeError(sprintf( '%s suggests installing %s (%s)', $suggestion['source'], - $suggestion['target'], - $suggestion['reason'] + $this->removeControlCharacters($suggestion['target']), + $this->removeControlCharacters($suggestion['reason']) )); } return $this; } + + /** + * @param string $string + * @return string + */ + private function removeControlCharacters($string) + { + return preg_replace( + '/[[:cntrl:]]/', + '', + str_replace("\n", ' ', $string) + ); + } } diff --git a/tests/Composer/Test/Installer/SuggestedPackagesReporterTest.php b/tests/Composer/Test/Installer/SuggestedPackagesReporterTest.php index c4cbd72c5..fb88ec840 100644 --- a/tests/Composer/Test/Installer/SuggestedPackagesReporterTest.php +++ b/tests/Composer/Test/Installer/SuggestedPackagesReporterTest.php @@ -144,6 +144,20 @@ class SuggestedPackagesReporterTest extends \PHPUnit_Framework_TestCase $this->suggestedPackagesReporter->output(); } + /** + * @covers ::output + */ + public function testOutputIgnoresFormatting() + { + $this->suggestedPackagesReporter->addPackage('source', 'target', "\x1b[1;37;42m Like us\r\non Facebook \x1b[0m"); + + $this->io->expects($this->once()) + ->method('writeError') + ->with("source suggests installing target ([1;37;42m Like us on Facebook [0m)"); + + $this->suggestedPackagesReporter->output(); + } + /** * @covers ::output */ From 20050dd9844191304967ed2b667facb82d9412f3 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 19 Apr 2017 17:24:47 +0200 Subject: [PATCH 2/2] Remove console formatting as well. --- .../Installer/SuggestedPackagesReporter.php | 16 ++++++++++++++-- .../Installer/SuggestedPackagesReporterTest.php | 11 ++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Composer/Installer/SuggestedPackagesReporter.php b/src/Composer/Installer/SuggestedPackagesReporter.php index e9d66f6a6..7f4230017 100644 --- a/src/Composer/Installer/SuggestedPackagesReporter.php +++ b/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,14 +117,25 @@ class SuggestedPackagesReporter $this->io->writeError(sprintf( '%s suggests installing %s (%s)', $suggestion['source'], - $this->removeControlCharacters($suggestion['target']), - $this->removeControlCharacters($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 diff --git a/tests/Composer/Test/Installer/SuggestedPackagesReporterTest.php b/tests/Composer/Test/Installer/SuggestedPackagesReporterTest.php index fb88ec840..0edb1b2a4 100644 --- a/tests/Composer/Test/Installer/SuggestedPackagesReporterTest.php +++ b/tests/Composer/Test/Installer/SuggestedPackagesReporterTest.php @@ -149,11 +149,16 @@ class SuggestedPackagesReporterTest extends \PHPUnit_Framework_TestCase */ public function testOutputIgnoresFormatting() { - $this->suggestedPackagesReporter->addPackage('source', 'target', "\x1b[1;37;42m Like us\r\non Facebook \x1b[0m"); + $this->suggestedPackagesReporter->addPackage('source', 'target1', "\x1b[1;37;42m Like us\r\non Facebook \x1b[0m"); + $this->suggestedPackagesReporter->addPackage('source', 'target2', "Like us on Facebook"); - $this->io->expects($this->once()) + $this->io->expects($this->at(0)) ->method('writeError') - ->with("source suggests installing target ([1;37;42m Like us on Facebook [0m)"); + ->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 (\\Like us on Facebook\\)'); $this->suggestedPackagesReporter->output(); }