Skip to content

Commit

Permalink
Update table rendering in ErrorSuite and related tests
Browse files Browse the repository at this point in the history
The table rendering in ErrorSuite.php has been refactored to dynamically set column sizes based on terminal width rather than using fixed widths. A new method getTableSize() has been added to calculate and return the sizes. The relevant tests and documentation in README.md, have been adjusted to reflect these changes.
  • Loading branch information
Denis Smet committed Mar 13, 2024
1 parent c45e619 commit aaf53ad
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 78 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

.PHONY: build

REPORT ?= table
COLUMNS ?= 150

ifneq (, $(wildcard ./vendor/jbzoo/codestyle/src/init.Makefile))
include ./vendor/jbzoo/codestyle/src/init.Makefile
endif

REPORT ?= table

build: ##@Project Install all 3rd party dependencies
$(call title,"Install/Update all 3rd party dependencies")
Expand Down
45 changes: 20 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,35 +211,30 @@ Schema: ./tests/schemas/demo_invalid.yml
Found CSV files: 3
(1/3) Invalid file: ./tests/fixtures/batch/demo-1.csv
+------+------------------+--------------+ demo-1.csv ------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+--------------+------------------------------------------------------+
| 3 | 2:Float | max | Value "74605.944" is greater than "74605" |
| 3 | 4:Favorite color | allow_values | Value "blue" is not allowed. Allowed values: ["red", |
| | | | "green", "Blue"] |
+------+------------------+--------------+ demo-1.csv ------------------------------------------+
+------+------------------+--------------+--------- demo-1.csv --------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+--------------+-----------------------------------------------------------------------+
| 3 | 2:Float | max | Value "74605.944" is greater than "74605" |
| 3 | 4:Favorite color | allow_values | Value "blue" is not allowed. Allowed values: ["red", "green", "Blue"] |
+------+------------------+--------------+--------- demo-1.csv --------------------------------------------------+
(2/3) Invalid file: ./tests/fixtures/batch/demo-2.csv
+------+------------+------------+----- demo-2.csv ---------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------+------------+--------------------------------------------------------+
| 2 | 0:Name | min_length | Value "Carl" (length: 4) is too short. Min length is 5 |
| 2 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date |
| | | | "1955-05-15T00:00:00.000+00:00" |
| 4 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date |
| | | | "1955-05-15T00:00:00.000+00:00" |
| 5 | 3:Birthday | max_date | Value "2010-07-20" is more than the maximum date |
| | | | "2009-01-01T00:00:00.000+00:00" |
| 7 | 0:Name | min_length | Value "Lois" (length: 4) is too short. Min length is 5 |
+------+------------+------------+----- demo-2.csv ---------------------------------------+
+------+------------+------------+------------------ demo-2.csv ----------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------+------------+----------------------------------------------------------------------------------+
| 2 | 0:Name | min_length | Value "Carl" (length: 4) is too short. Min length is 5 |
| 2 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date "1955-05-15T00:00:00.000+00:00" |
| 4 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date "1955-05-15T00:00:00.000+00:00" |
| 5 | 3:Birthday | max_date | Value "2010-07-20" is more than the maximum date "2009-01-01T00:00:00.000+00:00" |
| 7 | 0:Name | min_length | Value "Lois" (length: 4) is too short. Min length is 5 |
+------+------------+------------+------------------ demo-2.csv ----------------------------------------------------+
(3/3) Invalid file: ./tests/fixtures/batch/sub/demo-3.csv
+------+-----------+------------------+---- demo-3.csv -------------------------------------------+
| Line | id:Column | Rule | Message |
+------+-----------+------------------+-----------------------------------------------------------+
| 0 | | filename_pattern | Filename "./tests/fixtures/batch/sub/demo-3.csv" does not |
| | | | match pattern: "/demo-[12].csv$/i" |
+------+-----------+------------------+---- demo-3.csv -------------------------------------------+
+------+-----------+------------------+---------------------- demo-3.csv ------------------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+-----------+------------------+----------------------------------------------------------------------------------------------+
| 0 | | filename_pattern | Filename "./tests/fixtures/batch/sub/demo-3.csv" does not match pattern: "/demo-[12].csv$/i" |
+------+-----------+------------------+---------------------- demo-3.csv ------------------------------------------------------------+
Found 8 issues in 3 out of 3 CSV files.
Expand Down
5 changes: 0 additions & 5 deletions csv-blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace JBZoo\CsvBlueprint;

use JBZoo\Cli\CliApplication;
use JBZoo\Utils\Cli;

\define('PATH_ROOT', __DIR__);

Expand All @@ -36,10 +35,6 @@

require_once JBZOO_AUTOLOAD_FILE;

var_dump(Cli::getNumberOfColumns());
exit(1);


\date_default_timezone_set('UTC');

(new CliApplication('CSV Blueprint', '@git-version@'))
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@
<logging>
<junit outputFile="build/coverage_junit/main.xml"/>
</logging>

<php>
<env name="COLUMNS" value="150"/>
</php>
</phpunit>
46 changes: 42 additions & 4 deletions src/Validators/ErrorSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use JBZoo\CIReportConverter\Converters\JUnitConverter;
use JBZoo\CIReportConverter\Converters\TeamCityTestsConverter;
use JBZoo\CIReportConverter\Formats\Source\SourceSuite;
use JBZoo\Utils\Cli;
use JBZoo\Utils\Env;
use JBZoo\Utils\Vars;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\BufferedOutput;

Expand Down Expand Up @@ -139,15 +142,17 @@ private function renderPlainText(): string

private function renderTable(): string
{
$floatingSizes = self::getTableSize();

$buffer = new BufferedOutput();
$table = (new Table($buffer))
->setHeaderTitle($this->getTestcaseName())
->setFooterTitle($this->getTestcaseName())
->setHeaders(['Line', 'id:Column', 'Rule', 'Message'])
->setColumnMaxWidth(0, 10)
->setColumnMaxWidth(1, 20)
->setColumnMaxWidth(2, 20)
->setColumnMaxWidth(3, 60);
->setColumnMaxWidth(0, $floatingSizes['line'])
->setColumnMaxWidth(1, $floatingSizes['column'])
->setColumnMaxWidth(2, $floatingSizes['rule'])
->setColumnMaxWidth(3, $floatingSizes['message']);

foreach ($this->errors as $error) {
$table->addRow([
Expand Down Expand Up @@ -182,4 +187,37 @@ private function getTestcaseName(): string
{
return \pathinfo((string)\realpath((string)$this->csvFilename), \PATHINFO_BASENAME);
}

/**
* Retrieves the size configuration for a table.
*
* @return int[]
*/
private static function getTableSize(): array
{
$floatingSizes = [
'line' => 10,
'column' => 20,
'rule' => 20,
'min' => 90,
'max' => 150,
'reserve' => 5, // So that the table does not rest on the very edge of the terminal. Just in case.
];

// Fallback to 80 if the terminal width cannot be determined.
$maxAutoDetected = Env::int('COLUMNS', Cli::getNumberOfColumns());

$maxWindowWidth = Vars::limit(
$maxAutoDetected,
$floatingSizes['min'],
$floatingSizes['max'],
) - $floatingSizes['reserve'];

$floatingSizes['message'] = $maxWindowWidth
- $floatingSizes['line']
- $floatingSizes['column']
- $floatingSizes['rule'];

return $floatingSizes;
}
}
1 change: 0 additions & 1 deletion tests/Blueprint/ReadmeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use JBZoo\PHPUnit\PHPUnit;
use JBZoo\PHPUnit\TestTools;
use JBZoo\Utils\Cli;
use JBZoo\Utils\Sys;
use Symfony\Component\Console\Input\StringInput;

use function JBZoo\PHPUnit\isFileContains;
Expand Down
74 changes: 32 additions & 42 deletions tests/Blueprint/ValidateCsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,18 @@ public function testValidateOneFileNegativeTable(): void
Found CSV files: 1
(1/1) Invalid file: ./tests/fixtures/demo.csv
+------+------------------+------------------+--- demo.csv -------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+------------------+--------------------------------------------------------------+
| 0 | | filename_pattern | Filename "./tests/fixtures/demo.csv" does not match pattern: |
| | | | "/demo-[12].csv$/i" |
| 5 | 2:Float | max | Value "74605.944" is greater than "74605" |
| 5 | 4:Favorite color | allow_values | Value "blue" is not allowed. Allowed values: ["red", |
| | | | "green", "Blue"] |
| 6 | 0:Name | min_length | Value "Carl" (length: 4) is too short. Min length is 5 |
| 6 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date |
| | | | "1955-05-15T00:00:00.000+00:00" |
| 8 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date |
| | | | "1955-05-15T00:00:00.000+00:00" |
| 9 | 3:Birthday | max_date | Value "2010-07-20" is more than the maximum date |
| | | | "2009-01-01T00:00:00.000+00:00" |
| 11 | 0:Name | min_length | Value "Lois" (length: 4) is too short. Min length is 5 |
+------+------------------+------------------+--- demo.csv -------------------------------------------------+
+------+------------------+------------------+------------- demo.csv -----------------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+------------------+----------------------------------------------------------------------------------+
| 0 | | filename_pattern | Filename "./tests/fixtures/demo.csv" does not match pattern: "/demo-[12].csv$/i" |
| 5 | 2:Float | max | Value "74605.944" is greater than "74605" |
| 5 | 4:Favorite color | allow_values | Value "blue" is not allowed. Allowed values: ["red", "green", "Blue"] |
| 6 | 0:Name | min_length | Value "Carl" (length: 4) is too short. Min length is 5 |
| 6 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date "1955-05-15T00:00:00.000+00:00" |
| 8 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date "1955-05-15T00:00:00.000+00:00" |
| 9 | 3:Birthday | max_date | Value "2010-07-20" is more than the maximum date "2009-01-01T00:00:00.000+00:00" |
| 11 | 0:Name | min_length | Value "Lois" (length: 4) is too short. Min length is 5 |
+------+------------------+------------------+------------- demo.csv -----------------------------------------------------------+
Found 8 issues in CSV file.
Expand All @@ -107,35 +102,30 @@ public function testValidateManyFileNegativeTable(): void
Found CSV files: 3
(1/3) Invalid file: ./tests/fixtures/batch/demo-1.csv
+------+------------------+--------------+ demo-1.csv ------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+--------------+------------------------------------------------------+
| 3 | 2:Float | max | Value "74605.944" is greater than "74605" |
| 3 | 4:Favorite color | allow_values | Value "blue" is not allowed. Allowed values: ["red", |
| | | | "green", "Blue"] |
+------+------------------+--------------+ demo-1.csv ------------------------------------------+
+------+------------------+--------------+--------- demo-1.csv --------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+--------------+-----------------------------------------------------------------------+
| 3 | 2:Float | max | Value "74605.944" is greater than "74605" |
| 3 | 4:Favorite color | allow_values | Value "blue" is not allowed. Allowed values: ["red", "green", "Blue"] |
+------+------------------+--------------+--------- demo-1.csv --------------------------------------------------+
(2/3) Invalid file: ./tests/fixtures/batch/demo-2.csv
+------+------------+------------+----- demo-2.csv ---------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------+------------+--------------------------------------------------------+
| 2 | 0:Name | min_length | Value "Carl" (length: 4) is too short. Min length is 5 |
| 2 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date |
| | | | "1955-05-15T00:00:00.000+00:00" |
| 4 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date |
| | | | "1955-05-15T00:00:00.000+00:00" |
| 5 | 3:Birthday | max_date | Value "2010-07-20" is more than the maximum date |
| | | | "2009-01-01T00:00:00.000+00:00" |
| 7 | 0:Name | min_length | Value "Lois" (length: 4) is too short. Min length is 5 |
+------+------------+------------+----- demo-2.csv ---------------------------------------+
+------+------------+------------+------------------ demo-2.csv ----------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------+------------+----------------------------------------------------------------------------------+
| 2 | 0:Name | min_length | Value "Carl" (length: 4) is too short. Min length is 5 |
| 2 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date "1955-05-15T00:00:00.000+00:00" |
| 4 | 3:Birthday | min_date | Value "1955-05-14" is less than the minimum date "1955-05-15T00:00:00.000+00:00" |
| 5 | 3:Birthday | max_date | Value "2010-07-20" is more than the maximum date "2009-01-01T00:00:00.000+00:00" |
| 7 | 0:Name | min_length | Value "Lois" (length: 4) is too short. Min length is 5 |
+------+------------+------------+------------------ demo-2.csv ----------------------------------------------------+
(3/3) Invalid file: ./tests/fixtures/batch/sub/demo-3.csv
+------+-----------+------------------+---- demo-3.csv -------------------------------------------+
| Line | id:Column | Rule | Message |
+------+-----------+------------------+-----------------------------------------------------------+
| 0 | | filename_pattern | Filename "./tests/fixtures/batch/sub/demo-3.csv" does not |
| | | | match pattern: "/demo-[12].csv$/i" |
+------+-----------+------------------+---- demo-3.csv -------------------------------------------+
+------+-----------+------------------+---------------------- demo-3.csv ------------------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+-----------+------------------+----------------------------------------------------------------------------------------------+
| 0 | | filename_pattern | Filename "./tests/fixtures/batch/sub/demo-3.csv" does not match pattern: "/demo-[12].csv$/i" |
+------+-----------+------------------+---------------------- demo-3.csv ------------------------------------------------------------+
Found 8 issues in 3 out of 3 CSV files.
Expand Down

0 comments on commit aaf53ad

Please sign in to comment.