Skip to content

Commit 33b2da2

Browse files
author
Harry Bragg
authored
Filtering (#5)
* filter the output from applications * update diff renderer * add a show output flag
1 parent 05a1360 commit 33b2da2

5 files changed

Lines changed: 74 additions & 12 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ composer-%: ## Run a composer command, `make "composer-<command> [...]"`.
3131
# Testing
3232

3333
test: ## Run the unit and integration testsuites.
34-
test: lint test-unit test-integration
34+
test: lint test-unit
3535

3636
lint: ## Run phpcs against the code.
3737
${DOCKER_RUN} vendor/bin/phpcs -p --warning-severity=0 src/ tests/

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"squizlabs/php_codesniffer": "^2.9",
3131
"graze/standards": "^1.0",
3232
"symfony/console": "^3.1",
33-
"graze/console-diff-renderer": "^0.5",
33+
"graze/console-diff-renderer": "^0.6",
3434
"mockery/mockery": "^0.9.9"
3535
},
3636
"suggest": {

src/Run.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function start()
5757
{
5858
if (!$this->process->isRunning()) {
5959
$this->process->start(function ($type, $data) {
60-
$this->last = trim($data);
60+
$this->last = rtrim($data);
6161
});
6262
$this->started = microtime(true);
6363
$this->completed = false;

src/Table.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Exception;
1717
use Graze\DiffRenderer\DiffConsoleOutput;
18+
use Graze\DiffRenderer\Terminal\TerminalInterface;
1819
use Symfony\Component\Console\Output\OutputInterface;
1920
use Symfony\Component\Process\Exception\ProcessFailedException;
2021
use Symfony\Component\Process\Process;
@@ -33,6 +34,10 @@ class Table
3334
private $maxLengths = [];
3435
/** @var DiffConsoleOutput */
3536
private $output;
37+
/** @var TerminalInterface */
38+
private $terminal;
39+
/** @var bool */
40+
private $showOutput = true;
3641

3742
/**
3843
* Table constructor.
@@ -49,6 +54,7 @@ public function __construct(OutputInterface $output, Pool $pool = null)
4954
} else {
5055
$this->output = $output;
5156
}
57+
$this->terminal = $this->output->getTerminal();
5258
$this->exceptions = [];
5359
}
5460

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

@@ -107,7 +113,7 @@ public function add(Process $process, array $data = [])
107113
$data,
108114
mb_substr(static::SPINNER, $spinner++, 1),
109115
$duration,
110-
$last
116+
($this->showOutput ? $last : '')
111117
);
112118
if ($spinner > mb_strlen(static::SPINNER) - 1) {
113119
$spinner = 0;
@@ -172,4 +178,24 @@ public function run($checkInterval = Pool::CHECK_INTERVAL)
172178

173179
return $output;
174180
}
181+
182+
/**
183+
* @return bool
184+
*/
185+
public function isShowOutput()
186+
{
187+
return $this->showOutput;
188+
}
189+
190+
/**
191+
* @param bool $showOutput
192+
*
193+
* @return $this
194+
*/
195+
public function setShowOutput($showOutput)
196+
{
197+
$this->showOutput = $showOutput;
198+
199+
return $this;
200+
}
175201
}

tests/unit/TableTest.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public function testConstructWithNonBufferedOutput()
3737
$this->assertInstanceOf(Table::class, $table);
3838
}
3939

40+
public function testShowOutput()
41+
{
42+
$output = Mockery::mock(ConsoleOutputInterface::class);
43+
$table = new Table($output);
44+
45+
$this->assertTrue($table->isShowOutput());
46+
47+
$table->setShowOutput(false);
48+
49+
$this->assertFalse($table->isShowOutput());
50+
}
51+
4052
public function testSpinnerLoop()
4153
{
4254
$this->output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
@@ -91,33 +103,37 @@ public function testSpinnerLoop()
91103
* @dataProvider outputData
92104
*
93105
* @param int $verbosity OutputInterface::VERBOSITY_*
106+
* @param bool $showOutput Should it display some output text
94107
* @param bool[] $processStates an entry for each process to run, true = success, false = failure
95108
* @param string[][] $outputs Regex patterns for the output string
96109
*
97110
* @throws \Exception
98-
* @throws mixed
99111
*/
100-
public function testOutput($verbosity, array $processStates, array $outputs)
112+
public function testOutput($verbosity, $showOutput, array $processStates, array $outputs)
101113
{
102114
$this->output->setVerbosity($verbosity);
115+
$this->table->setShowOutput($showOutput);
103116

104117
$oneFails = false;
105118

106119
for ($i = 0; $i < count($processStates); $i++) {
107120
$process = Mockery::mock(Process::class);
108121
$process->shouldReceive('stop');
109-
$process->shouldReceive('start')->once();
122+
$process->shouldReceive('start')->with(Mockery::on(function ($closure) {
123+
call_user_func($closure, Process::OUT, 'some text');
124+
return true;
125+
}))->once();
110126
$process->shouldReceive('isStarted')->andReturn(false, true);
111127
$process->shouldReceive('isRunning')->andReturn(false, true, false); // add, start, check, check
112128
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn($processStates[$i]);
129+
$process->shouldReceive('getOutput')->andReturn('some text');
113130

114131
if (!$processStates[$i]) {
115132
$process->shouldReceive('getCommandLine')->andReturn('test');
116133
$process->shouldReceive('getExitCode')->andReturn(1);
117134
$process->shouldReceive('getExitCodeText')->andReturn('failed');
118135
$process->shouldReceive('getWorkingDirectory')->andReturn('/tmp');
119136
$process->shouldReceive('isOutputDisabled')->andReturn(false);
120-
$process->shouldReceive('getOutput')->andReturn('some text');
121137
$process->shouldReceive('getErrorOutput')->andReturn('some error text');
122138
$oneFails = true;
123139
}
@@ -165,6 +181,7 @@ public function outputData()
165181
return [
166182
[ // verbose with single valid run
167183
OutputInterface::VERBOSITY_VERBOSE,
184+
false,
168185
[true],
169186
[
170187
['%<info>key</info>: value <info>run</info>: 0 \(<comment> 0.00s</comment>\) %'],
@@ -174,13 +191,15 @@ public function outputData()
174191
],
175192
[ // normal verbosity only writes a single line
176193
OutputInterface::VERBOSITY_NORMAL,
194+
false,
177195
[true],
178196
[
179197
['%<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) <info>✓</info>%'],
180198
],
181199
],
182200
[
183201
OutputInterface::VERBOSITY_NORMAL,
202+
false,
184203
[true, true],
185204
[
186205
['%<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) <info>✓</info>%'],
@@ -189,6 +208,7 @@ public function outputData()
189208
],
190209
[ // multiple runs with verbosity will update each item one at a time
191210
OutputInterface::VERBOSITY_VERBOSE,
211+
false,
192212
[true, true],
193213
[
194214
[
@@ -215,6 +235,7 @@ public function outputData()
215235
],
216236
[ // errors will display an error
217237
OutputInterface::VERBOSITY_VERBOSE,
238+
false,
218239
[false],
219240
[
220241
['%<info>key</info>: value <info>run</info>: 0 \(<comment> 0.00s</comment>\) %'],
@@ -236,11 +257,13 @@ public function outputData()
236257
================
237258
some error text%
238259
DOC
239-
]
260+
,
261+
],
240262
],
241263
],
242264
[ // errors will display an error
243265
OutputInterface::VERBOSITY_NORMAL,
266+
false,
244267
[false],
245268
[
246269
['%<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) <error>x</error>%'],
@@ -260,11 +283,13 @@ public function outputData()
260283
================
261284
some error text%
262285
DOC
263-
]
286+
,
287+
],
264288
],
265289
],
266290
[ // multiple runs with verbosity will update each item one at a time
267291
OutputInterface::VERBOSITY_VERBOSE,
292+
false,
268293
[true, false],
269294
[
270295
[
@@ -303,7 +328,18 @@ public function outputData()
303328
================
304329
some error text%
305330
DOC
306-
]
331+
,
332+
],
333+
],
334+
],
335+
[ // include output
336+
OutputInterface::VERBOSITY_VERBOSE,
337+
true,
338+
[true],
339+
[
340+
['%(*UTF8)<info>key</info>: value <info>run</info>: 0 \(<comment> 0.00s</comment>\) %'],
341+
['%(*UTF8)<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) [⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏] some text%'],
342+
['%(*UTF8)<info>key</info>: value <info>run</info>: 0 \(<comment>[ 0-9\.s]+</comment>\) <info>✓</info> some text%'],
307343
],
308344
],
309345
];

0 commit comments

Comments
 (0)