Skip to content
Permalink
Browse files

colorize result (#158)

This is a part of the changes made in #105.

* colorize result
* remove output
* remove unsed var
  • Loading branch information...
mimmi20 authored and jaydiablo committed Jul 22, 2019
1 parent a0692cf commit 072d4066df8e5ef9f1ccddd95a2de888f066b68f
Showing with 129 additions and 35 deletions.
  1. +111 −20 src/Command/Analyze.php
  2. +2 −5 src/Command/Normalize.php
  3. +16 −10 src/Command/Test.php
@@ -327,14 +327,42 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}
if (array_sum($passFail['browser']) === 0) {
$browserContent = '<fg=white;bg=blue>-</>';
} else {
$browserPercentage = $passFail['browser']['pass'] / array_sum($passFail['browser']) * 100;
$browserContent = $this->colorByPercent($browserPercentage) . $passFail['browser']['pass'] . '/' . array_sum($passFail['browser']) . ' ' . round($browserPercentage, 2, \PHP_ROUND_HALF_DOWN) . '%</>';
}
if (array_sum($passFail['platform']) === 0) {
$platformContent = '<fg=white;bg=blue>-</>';
} else {
$platformPercentage = $passFail['platform']['pass'] / array_sum($passFail['platform']) * 100;
$platformContent = $this->colorByPercent($platformPercentage) . $passFail['platform']['pass'] . '/' . array_sum($passFail['platform']) . ' ' . round($platformPercentage, 2, \PHP_ROUND_HALF_DOWN) . '%</>';
}
if (array_sum($passFail['device']) === 0) {
$deviceContent = '<fg=white;bg=blue>-</>';
} else {
$devicePercentage = $passFail['device']['pass'] / array_sum($passFail['device']) * 100;
$deviceContent = $this->colorByPercent($devicePercentage) . $passFail['device']['pass'] . '/' . array_sum($passFail['device']) . ' ' . round($devicePercentage, 2, \PHP_ROUND_HALF_DOWN) . '%</>';
}
if ($possibleScores[$parserName][$testName] === 0) {
$summaryContent = '<fg=white;bg=blue>-</>';
} else {
$summaryPercentage = $parserScores[$parserName][$testName] / $possibleScores[$parserName][$testName] * 100;
$summaryContent = $this->colorByPercent($summaryPercentage) . $parserScores[$parserName][$testName] . '/' . $possibleScores[$parserName][$testName] . ' ' . round($summaryPercentage, 2, \PHP_ROUND_HALF_DOWN) . '%</>';
}
$rows[] = [
$parserName,
$parserData['metadata']['version'] ?? 'n/a',
$passFail['browser']['pass'] . '/' . array_sum($passFail['browser']) . ' ' . (array_sum($passFail['browser']) === 0 ? '0.0' : round($passFail['browser']['pass'] / array_sum($passFail['browser']) * 100, 2, \PHP_ROUND_HALF_DOWN)) . '%',
$passFail['platform']['pass'] . '/' . array_sum($passFail['platform']) . ' ' . (array_sum($passFail['platform']) === 0 ? '0.0' : round($passFail['platform']['pass'] / array_sum($passFail['platform']) * 100, 2, \PHP_ROUND_HALF_DOWN)) . '%',
$passFail['device']['pass'] . '/' . array_sum($passFail['device']) . ' ' . (array_sum($passFail['device']) === 0 ? '0.0' : round($passFail['device']['pass'] / array_sum($passFail['device']) * 100, 2, \PHP_ROUND_HALF_DOWN)) . '%',
$browserContent,
$platformContent,
$deviceContent,
round($testResult['parse_time'] + $testResult['init_time'], 3) . 's',
$parserScores[$parserName][$testName] . '/' . $possibleScores[$parserName][$testName] . ' ' . ($possibleScores[$parserName][$testName] === 0 ? '0.0' : round($parserScores[$parserName][$testName] / $possibleScores[$parserName][$testName] * 100, 2, \PHP_ROUND_HALF_DOWN)) . '%',
$summaryContent,
];
if (!isset($totals[$parserName])) {
@@ -364,15 +392,44 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (count($this->options['tests']) > 1) {
$rows[] = [new TableCell('<fg=yellow>Total for all Test suites</>', ['colspan' => 6])];
$rows[] = new TableSeparator();
foreach ($totals as $parser => $total) {
if (array_sum($total['browser']) === 0) {
$browserContent = '<fg=white;bg=blue>-</>';
} else {
$browserPercentage = $total['browser']['pass'] / array_sum($total['browser']) * 100;
$browserContent = $this->colorByPercent($browserPercentage) . $total['browser']['pass'] . '/' . array_sum($total['browser']) . ' ' . round($browserPercentage, 2, \PHP_ROUND_HALF_DOWN) . '%</>';
}
if (array_sum($total['platform']) === 0) {
$platformContent = '<fg=white;bg=blue>-</>';
} else {
$platformPercentage = $total['platform']['pass'] / array_sum($total['platform']) * 100;
$platformContent = $this->colorByPercent($platformPercentage) . $total['platform']['pass'] . '/' . array_sum($total['platform']) . ' ' . round($platformPercentage, 2, \PHP_ROUND_HALF_DOWN) . '%</>';
}
if (array_sum($total['device']) === 0) {
$deviceContent = '<fg=white;bg=blue>-</>';
} else {
$devicePercentage = $total['device']['pass'] / array_sum($total['device']) * 100;
$deviceContent = $this->colorByPercent($devicePercentage) . $total['device']['pass'] . '/' . array_sum($total['device']) . ' ' . round($devicePercentage, 2, \PHP_ROUND_HALF_DOWN) . '%</>';
}
if ($total['score']['possible'] === 0) {
$summaryContent = '<fg=white;bg=blue>-</>';
} else {
$summaryPercentage = $total['score']['earned'] / $total['score']['possible'] * 100;
$summaryContent = $this->colorByPercent($summaryPercentage) . $total['score']['earned'] . '/' . $total['score']['possible'] . ' ' . round($summaryPercentage, 2, \PHP_ROUND_HALF_DOWN) . '%</>';
}
$rows[] = [
$parser,
isset($this->options['parsers'][$parser]['metadata']['version']) ? $this->options['parsers'][$parser]['metadata']['version'] : 'n/a',
$total['browser']['pass'] . '/' . array_sum($total['browser']) . ' ' . (array_sum($total['browser']) === 0 ? '0.0' : round($total['browser']['pass'] / array_sum($total['browser']) * 100, 2, \PHP_ROUND_HALF_DOWN)) . '%',
$total['platform']['pass'] . '/' . array_sum($total['platform']) . ' ' . (array_sum($total['platform']) === 0 ? '0.0' : round($total['platform']['pass'] / array_sum($total['platform']) * 100, 2, \PHP_ROUND_HALF_DOWN)) . '%',
$total['device']['pass'] . '/' . array_sum($total['device']) . ' ' . (array_sum($total['device']) === 0 ? '0.0' : round($total['device']['pass'] / array_sum($total['device']) * 100, 2, \PHP_ROUND_HALF_DOWN)) . '%',
$browserContent,
$platformContent,
$deviceContent,
round($total['time'], 3) . 's',
$total['score']['earned'] . '/' . $total['score']['possible'] . ' ' . ($total['score']['possible'] === 0 ? '0.0' : round($total['score']['earned'] / $total['score']['possible'] * 100, 2, \PHP_ROUND_HALF_DOWN)) . '%',
$summaryContent,
];
}
@@ -653,30 +710,39 @@ private function analyzeFailures(string $test, string $parser, bool $justAgents
}
$table = new Table($this->output);
$table->setColumnWidth(0, 50);
$table->setColumnMaxWidth(0, 50);
$table->setColumnWidth(1, 50);
$table->setColumnMaxWidth(1, 50);
$table->setColumnWidth(2, 50);
$table->setColumnMaxWidth(2, 50);
$table->setStyle('box');
$table->setHeaders([
[new TableCell('UserAgent', ['colspan' => 3])],
['Browser', 'Platform', 'Device'],
[new TableCell('Browser'), new TableCell('Platform'), new TableCell('Device')],
]);
$rows = [];
foreach ($this->failures[$test][$parser] as $agent => $failData) {
$rows[] = [new TableCell((string) $agent, ['colspan' => 3])];
$rows[] = [
isset($failData['browser']) ? $this->outputDiff($failData['browser']) : '',
isset($failData['platform']) ? $this->outputDiff($failData['platform']) : '',
isset($failData['device']) ? $this->outputDiff($failData['device']) : '',
];
$rows[] = new TableSeparator();
if ($justAgents === true) {
$this->output->writeln($agent);
} else {
$rows[] = [new TableCell((string) $agent, ['colspan' => 3])];
$rows[] = [
new TableCell(isset($failData['browser']) ? $this->outputDiff($failData['browser']) : ''),
new TableCell(isset($failData['platform']) ? $this->outputDiff($failData['platform']) : ''),
new TableCell(isset($failData['device']) ? $this->outputDiff($failData['device']) : ''),
];
$rows[] = new TableSeparator();
}
}
array_pop($rows);
$table->setRows($rows);
if ($justAgents === false) {
array_pop($rows);
$table->setRows($rows);
$table->render();
}
}
@@ -823,4 +889,29 @@ private function outputDiff(array $diff): string
return $output;
}
private function colorByPercent(float $percent): string
{
if ($percent >= 100.0) {
return '<fg=green;bg=black;options=bold>';
}
if ($percent >= 95.0) {
return '<fg=green;bg=black>';
}
if ($percent >= 90.0) {
return '<fg=yellow;bg=black;options=bold>';
}
if ($percent >= 85.0) {
return '<fg=yellow;bg=black>';
}
if ($percent < 50.0) {
return '<fg=red;bg=black>';
}
return '</>';
}
}
@@ -59,8 +59,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln('<comment>Normalizing data from test run: ' . $run . '</comment>');
$this->options = ['tests' => [], 'parsers' => []];
$this->options = ['tests' => [], 'parsers' => []];
if (file_exists($this->runDir . '/' . $run . '/metadata.json')) {
try {
$contents = file_get_contents($this->runDir . '/' . $run . '/metadata.json');
@@ -150,8 +148,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
$testName = str_replace('.json', '', $resultFile->getFilename());
$message = sprintf('%sProcessing results from the <fg=yellow>%s</> test suite... ', ' ', $testName);
$message = sprintf('%sProcessing results from the <fg=yellow>%s</> test suite... ', ' ', $testName);
$output->write($message . '<info> parsing result</info>');
@@ -174,7 +171,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}
$output->write("\r" . $message . '<info> normalizing result</info>' . "\n");
$output->write("\r" . $message . '<info> normalizing result</info>');
foreach ($data['results'] as $result) {
if (!isset($result['parsed'])) {
@@ -13,6 +13,7 @@
use function Safe\ksort;
use function Safe\mkdir;
use function Safe\sort;
use function Safe\sprintf;
use SplFileInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
@@ -120,33 +121,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$usedTests = [];
foreach ($selectedTests as $testName => $testData) {
$output->write('Generating data for the ' . $testName . ' test suite... ');
$message = sprintf('Generating data for the <fg=yellow>%s</> test suite... ', $testName);
$output->write($message . '<info> building test suite</info>');
$this->results[$testName] = [];
$testOutput = trim((string) shell_exec($testData['path'] . '/build.sh'));
$output->write("\r" . $message . '<info> writing test suite</info> ');
file_put_contents($expectedDir . '/' . $testName . '.json', $testOutput);
try {
$testOutput = json_decode($testOutput, true);
} catch (Exception $e) {
$output->writeln('<error>There was an error with the output from the ' . $testName . ' test suite.</error>');
$output->writeln("\r" . $message . '<error>There was an error with the output from the ' . $testName . ' test suite.</error>');
continue;
}
if ($testOutput['tests'] === null) {
$output->writeln('<error>There was an error with the output from the ' . $testName . ' test suite, no tests were found.</error>');
$output->writeln("\r" . $message . '<error>There was an error with the output from the ' . $testName . ' test suite, no tests were found.</error>');
continue;
}
if (!empty($testOutput['version'])) {
$testData['metadata']['version'] = $testOutput['version'];
}
$output->writeln('<info>done! [' . count($testOutput['tests']) . ' tests]</info>');
$output->write("\r" . $message . '<info> write test data into file...</info>');
$output->write('write test data for the ' . $testName . ' test suite into file... ');
// write our test's file that we'll pass to the parsers
$filename = $testFilesDir . '/' . $testName . '.txt';
@@ -161,15 +165,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
});
file_put_contents($filename, implode(PHP_EOL, $agents));
$output->writeln('<info> done! [' . count($agents) . ' tests found]</info>');
$output->writeln("\r" . $message . '<info> done! [' . count($agents) . ' tests found]</info> ');
foreach ($parsers as $parserName => $parser) {
$output->write(' <info> Testing against the <fg=green;options=bold,underscore>' . $parserName . '</> parser... </info>');
$testMessage = sprintf(' Testing against the <fg=green;options=bold,underscore>%s</> parser...', $parserName);
$output->write($testMessage . ' <info> parsing</info>');
$result = $parser['parse']($filename);
if (empty($result)) {
$output->writeln(
'<error>The <fg=red;options=bold,underscore>' . $parserName . '</> parser did not return any data, there may have been an error</error>'
"\r" . $testMessage . ' <error>The parser did not return any data, there may have been an error</error>'
);
continue;
@@ -189,15 +195,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
} catch (Exception $e) {
$output->writeln('<error> encoding the result failed!</error>');
$output->writeln("\r" . $testMessage . ' <error>encoding the result failed!</error>');
continue;
}
file_put_contents(
$resultsDir . '/' . $parserName . '/' . $testName . '.json',
$encoded
);
$output->writeln('<info> done!</info>');
$output->writeln("\r" . $testMessage . ' <info> done!</info> ');
$usedTests[$testName] = $testData;
}

0 comments on commit 072d406

Please sign in to comment.
You can’t perform that action at this time.