Skip to content

Commit

Permalink
Filtering (#5)
Browse files Browse the repository at this point in the history
* filter the output from applications

* update diff renderer

* add a show output flag
  • Loading branch information
Harry Bragg authored Jun 26, 2017
1 parent 05a1360 commit 33b2da2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ composer-%: ## Run a composer command, `make "composer-<command> [...]"`.
# 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/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 28 additions & 2 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +34,10 @@ class Table
private $maxLengths = [];
/** @var DiffConsoleOutput */
private $output;
/** @var TerminalInterface */
private $terminal;
/** @var bool */
private $showOutput = true;

/**
* Table constructor.
Expand All @@ -49,6 +54,7 @@ public function __construct(OutputInterface $output, Pool $pool = null)
} else {
$this->output = $output;
}
$this->terminal = $this->output->getTerminal();
$this->exceptions = [];
}

Expand Down Expand Up @@ -87,7 +93,7 @@ private function formatRow(array $data, $status, $duration, $extra = '')
$length = isset($this->maxLengths[$key]) ? '-' . $this->maxLengths[$key] : '';
$info[] = sprintf("<info>%s</info>: %{$length}s", $key, $value);
}
$extra = $extra ? ' ' . $extra : '';
$extra = $extra ? ' ' . $this->terminal->filter($extra) : '';
return sprintf("%s (<comment>%6.2fs</comment>) %s%s", implode(' ', $info), $duration, $status, $extra);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
50 changes: 43 additions & 7 deletions tests/unit/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -91,33 +103,37 @@ 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');
$process->shouldReceive('getExitCode')->andReturn(1);
$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;
}
Expand Down Expand Up @@ -165,6 +181,7 @@ public function outputData()
return [
[ // verbose with single valid run
OutputInterface::VERBOSITY_VERBOSE,
false,
[true],
[
['%<info>key</info>: value <info>run</info>: 0 \(<comment> 0.00s</comment>\) %'],
Expand All @@ -174,13 +191,15 @@ public function outputData()
],
[ // normal verbosity only writes a single line
OutputInterface::VERBOSITY_NORMAL,
false,
[true],
[
['%<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) <info>✓</info>%'],
],
],
[
OutputInterface::VERBOSITY_NORMAL,
false,
[true, true],
[
['%<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) <info>✓</info>%'],
Expand All @@ -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],
[
[
Expand All @@ -215,6 +235,7 @@ public function outputData()
],
[ // errors will display an error
OutputInterface::VERBOSITY_VERBOSE,
false,
[false],
[
['%<info>key</info>: value <info>run</info>: 0 \(<comment> 0.00s</comment>\) %'],
Expand All @@ -236,11 +257,13 @@ public function outputData()
================
some error text%
DOC
]
,
],
],
],
[ // errors will display an error
OutputInterface::VERBOSITY_NORMAL,
false,
[false],
[
['%<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) <error>x</error>%'],
Expand All @@ -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],
[
[
Expand Down Expand Up @@ -303,7 +328,18 @@ public function outputData()
================
some error text%
DOC
]
,
],
],
],
[ // include output
OutputInterface::VERBOSITY_VERBOSE,
true,
[true],
[
['%(*UTF8)<info>key</info>: value <info>run</info>: 0 \(<comment> 0.00s</comment>\) %'],
['%(*UTF8)<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) [⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏] some text%'],
['%(*UTF8)<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) <info>✓</info> some text%'],
],
],
];
Expand Down

0 comments on commit 33b2da2

Please sign in to comment.