diff --git a/Makefile b/Makefile index 4c79328..b9e94df 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ composer-%: ## Run a composer command, `make "composer- [...]"`. # Testing test: ## Run the unit and integration testsuites. -test: lint test-unit test-integration +test: lint test-unit lint: ## Run phpcs against the code. ${DOCKER_RUN} vendor/bin/phpcs -p --warning-severity=0 src/ tests/ diff --git a/composer.json b/composer.json index 62f3700..0d178e6 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "squizlabs/php_codesniffer": "^2.9", "graze/standards": "^1.0", "symfony/console": "^3.1", - "graze/console-diff-renderer": "^0.5", + "graze/console-diff-renderer": "^0.6", "mockery/mockery": "^0.9.9" }, "suggest": { diff --git a/src/Run.php b/src/Run.php index c28a981..5fd5607 100644 --- a/src/Run.php +++ b/src/Run.php @@ -57,7 +57,7 @@ public function start() { if (!$this->process->isRunning()) { $this->process->start(function ($type, $data) { - $this->last = trim($data); + $this->last = rtrim($data); }); $this->started = microtime(true); $this->completed = false; diff --git a/src/Table.php b/src/Table.php index b5bb817..8413d42 100644 --- a/src/Table.php +++ b/src/Table.php @@ -15,6 +15,7 @@ use Exception; use Graze\DiffRenderer\DiffConsoleOutput; +use Graze\DiffRenderer\Terminal\TerminalInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; @@ -33,6 +34,10 @@ class Table private $maxLengths = []; /** @var DiffConsoleOutput */ private $output; + /** @var TerminalInterface */ + private $terminal; + /** @var bool */ + private $showOutput = true; /** * Table constructor. @@ -49,6 +54,7 @@ public function __construct(OutputInterface $output, Pool $pool = null) } else { $this->output = $output; } + $this->terminal = $this->output->getTerminal(); $this->exceptions = []; } @@ -87,7 +93,7 @@ private function formatRow(array $data, $status, $duration, $extra = '') $length = isset($this->maxLengths[$key]) ? '-' . $this->maxLengths[$key] : ''; $info[] = sprintf("%s: %{$length}s", $key, $value); } - $extra = $extra ? ' ' . $extra : ''; + $extra = $extra ? ' ' . $this->terminal->filter($extra) : ''; return sprintf("%s (%6.2fs) %s%s", implode(' ', $info), $duration, $status, $extra); } @@ -107,7 +113,7 @@ public function add(Process $process, array $data = []) $data, mb_substr(static::SPINNER, $spinner++, 1), $duration, - $last + ($this->showOutput ? $last : '') ); if ($spinner > mb_strlen(static::SPINNER) - 1) { $spinner = 0; @@ -172,4 +178,24 @@ public function run($checkInterval = Pool::CHECK_INTERVAL) return $output; } + + /** + * @return bool + */ + public function isShowOutput() + { + return $this->showOutput; + } + + /** + * @param bool $showOutput + * + * @return $this + */ + public function setShowOutput($showOutput) + { + $this->showOutput = $showOutput; + + return $this; + } } diff --git a/tests/unit/TableTest.php b/tests/unit/TableTest.php index dae32d2..4e9f3af 100644 --- a/tests/unit/TableTest.php +++ b/tests/unit/TableTest.php @@ -37,6 +37,18 @@ public function testConstructWithNonBufferedOutput() $this->assertInstanceOf(Table::class, $table); } + public function testShowOutput() + { + $output = Mockery::mock(ConsoleOutputInterface::class); + $table = new Table($output); + + $this->assertTrue($table->isShowOutput()); + + $table->setShowOutput(false); + + $this->assertFalse($table->isShowOutput()); + } + public function testSpinnerLoop() { $this->output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); @@ -91,25 +103,30 @@ public function testSpinnerLoop() * @dataProvider outputData * * @param int $verbosity OutputInterface::VERBOSITY_* + * @param bool $showOutput Should it display some output text * @param bool[] $processStates an entry for each process to run, true = success, false = failure * @param string[][] $outputs Regex patterns for the output string * * @throws \Exception - * @throws mixed */ - public function testOutput($verbosity, array $processStates, array $outputs) + public function testOutput($verbosity, $showOutput, array $processStates, array $outputs) { $this->output->setVerbosity($verbosity); + $this->table->setShowOutput($showOutput); $oneFails = false; for ($i = 0; $i < count($processStates); $i++) { $process = Mockery::mock(Process::class); $process->shouldReceive('stop'); - $process->shouldReceive('start')->once(); + $process->shouldReceive('start')->with(Mockery::on(function ($closure) { + call_user_func($closure, Process::OUT, 'some text'); + return true; + }))->once(); $process->shouldReceive('isStarted')->andReturn(false, true); $process->shouldReceive('isRunning')->andReturn(false, true, false); // add, start, check, check $process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn($processStates[$i]); + $process->shouldReceive('getOutput')->andReturn('some text'); if (!$processStates[$i]) { $process->shouldReceive('getCommandLine')->andReturn('test'); @@ -117,7 +134,6 @@ public function testOutput($verbosity, array $processStates, array $outputs) $process->shouldReceive('getExitCodeText')->andReturn('failed'); $process->shouldReceive('getWorkingDirectory')->andReturn('/tmp'); $process->shouldReceive('isOutputDisabled')->andReturn(false); - $process->shouldReceive('getOutput')->andReturn('some text'); $process->shouldReceive('getErrorOutput')->andReturn('some error text'); $oneFails = true; } @@ -165,6 +181,7 @@ public function outputData() return [ [ // verbose with single valid run OutputInterface::VERBOSITY_VERBOSE, + false, [true], [ ['%key: value run: 0 \( 0.00s\) %'], @@ -174,6 +191,7 @@ public function outputData() ], [ // normal verbosity only writes a single line OutputInterface::VERBOSITY_NORMAL, + false, [true], [ ['%key: value run: 0 \([ 0-9\.s]+\) %'], @@ -181,6 +199,7 @@ public function outputData() ], [ OutputInterface::VERBOSITY_NORMAL, + false, [true, true], [ ['%key: value run: 0 \([ 0-9\.s]+\) %'], @@ -189,6 +208,7 @@ public function outputData() ], [ // multiple runs with verbosity will update each item one at a time OutputInterface::VERBOSITY_VERBOSE, + false, [true, true], [ [ @@ -215,6 +235,7 @@ public function outputData() ], [ // errors will display an error OutputInterface::VERBOSITY_VERBOSE, + false, [false], [ ['%key: value run: 0 \( 0.00s\) %'], @@ -236,11 +257,13 @@ public function outputData() ================ some error text% DOC - ] + , + ], ], ], [ // errors will display an error OutputInterface::VERBOSITY_NORMAL, + false, [false], [ ['%key: value run: 0 \([ 0-9\.s]+\) x%'], @@ -260,11 +283,13 @@ public function outputData() ================ some error text% DOC - ] + , + ], ], ], [ // multiple runs with verbosity will update each item one at a time OutputInterface::VERBOSITY_VERBOSE, + false, [true, false], [ [ @@ -303,7 +328,18 @@ public function outputData() ================ some error text% DOC - ] + , + ], + ], + ], + [ // include output + OutputInterface::VERBOSITY_VERBOSE, + true, + [true], + [ + ['%(*UTF8)key: value run: 0 \( 0.00s\) %'], + ['%(*UTF8)key: value run: 0 \([ 0-9\.s]+\) [⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏] some text%'], + ['%(*UTF8)key: value run: 0 \([ 0-9\.s]+\) some text%'], ], ], ];