Skip to content

Commit

Permalink
Update error messaging and enhance sorting in CSV validation
Browse files Browse the repository at this point in the history
This commit refactors the error message output when validating CSVs to better signify invalid files. Additionally, the process of finding files has been improved with clear depth conditions and sorted file results. An overall update to test cases and documentation reflects these changes for consistency.
  • Loading branch information
Denis Smet committed Mar 12, 2024
1 parent 78ec043 commit 17d5e47
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 58 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,16 @@ Default report format is `table`:
Schema: ./tests/schemas/demo_invalid.yml
OK: ./tests/fixtures/batch/sub/demo-3.csv
Error: ./tests/fixtures/batch/demo-2.csv
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 ------------------------------------------+
Invalid file: ./tests/fixtures/batch/demo-2.csv
+------+------------+------------+----- demo-2.csv ---------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------+------------+--------------------------------------------------------+
Expand All @@ -225,15 +233,7 @@ Error: ./tests/fixtures/batch/demo-2.csv
| 7 | 0:Name | min_length | Value "Lois" (length: 4) is too short. Min length is 5 |
+------+------------+------------+----- demo-2.csv ---------------------------------------+
Error: ./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 ------------------------------------------+
OK: ./tests/fixtures/batch/sub/demo-3.csv
Found 7 issues in 2 out of 3 CSV files.
```
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ValidateCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function executeAction(): int
$errorCounter += $errorSuite->count();

if ($this->isTextMode()) {
$this->_('<red>Error</red>: ' . Utils::cutPath($csvFilename->getPathname()), OutLvl::E);
$this->_('<red>Invalid file:</red> ' . Utils::cutPath($csvFilename->getPathname()), OutLvl::E);
}
$output = $errorSuite->render($this->getOptString('report'));
if ($output !== null) {
Expand Down
5 changes: 3 additions & 2 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public static function findFiles(array $paths): array
if (\strpos($path, '*') !== false) {
$finder = (new Finder())
->in(\dirname($path))
// ->depth(self::MAX_DIRECTORY_DEPTH)
->depth('< ' . self::MAX_DIRECTORY_DEPTH)
->ignoreVCSIgnored(true)
->ignoreDotFiles(true)
->followLinks()
->name(\basename($path));
->name(\basename($path))
->sortByName(true);

foreach ($finder as $file) {
if (!$file->isReadable()) {
Expand Down
62 changes: 31 additions & 31 deletions tests/Blueprint/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testCreateValidateNegative(): void
$expected = <<<'TXT'
Schema: ./tests/schemas/demo_invalid.yml
Error: ./tests/fixtures/demo.csv
Invalid file: ./tests/fixtures/demo.csv
+------+------------------+--------------+-- demo.csv --------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+--------------+--------------------------------------------------------+
Expand Down Expand Up @@ -117,8 +117,16 @@ public function testCreateValidateNegativeMultiple(): void
$expected = <<<'TXT'
Schema: ./tests/schemas/demo_invalid.yml
OK: ./tests/fixtures/batch/sub/demo-3.csv
Error: ./tests/fixtures/batch/demo-2.csv
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 ------------------------------------------+
Invalid file: ./tests/fixtures/batch/demo-2.csv
+------+------------+------------+----- demo-2.csv ---------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------+------------+--------------------------------------------------------+
Expand All @@ -132,15 +140,7 @@ public function testCreateValidateNegativeMultiple(): void
| 7 | 0:Name | min_length | Value "Lois" (length: 4) is too short. Min length is 5 |
+------+------------+------------+----- demo-2.csv ---------------------------------------+
Error: ./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 ------------------------------------------+
OK: ./tests/fixtures/batch/sub/demo-3.csv
Found 7 issues in 2 out of 3 CSV files.
TXT;
Expand Down Expand Up @@ -173,7 +173,7 @@ public function testCreateValidateNegativeText(): void
$expected = <<<'TXT'
Schema: ./tests/schemas/demo_invalid.yml
Error: ./tests/fixtures/demo.csv
Invalid file: ./tests/fixtures/demo.csv
"max" at line 5, column "2:Float". Value "74605.944" is greater than "74605".
"allow_values" at line 5, column "4:Favorite color". Value "blue" is not allowed. Allowed values: ["red", "green", "Blue"].
"min_length" at line 6, column "0:Name". Value "Carl" (length: 4) is too short. Min length is 5.
Expand Down Expand Up @@ -204,9 +204,24 @@ public function testCreateValidateNegativeTeamcity(): void

$expected = <<<'TXT'
Schema: ./tests/schemas/demo_invalid.yml
Invalid file: ./tests/fixtures/batch/demo-1.csv
OK: ./tests/fixtures/batch/sub/demo-3.csv
Error: ./tests/fixtures/batch/demo-2.csv
##teamcity[testCount count='2' flowId='42']
##teamcity[testSuiteStarted name='demo-1.csv' flowId='42']
##teamcity[testStarted name='max at column 2:Float' locationHint='php_qn://./tests/fixtures/batch/demo-1.csv' flowId='42']
"max" at line 3, column "2:Float". Value "74605.944" is greater than "74605".
##teamcity[testFinished name='max at column 2:Float' flowId='42']
##teamcity[testStarted name='allow_values at column 4:Favorite color' locationHint='php_qn://./tests/fixtures/batch/demo-1.csv' flowId='42']
"allow_values" at line 3, column "4:Favorite color". Value "blue" is not allowed. Allowed values: ["red", "green", "Blue"].
##teamcity[testFinished name='allow_values at column 4:Favorite color' flowId='42']
##teamcity[testSuiteFinished name='demo-1.csv' flowId='42']
Invalid file: ./tests/fixtures/batch/demo-2.csv
##teamcity[testCount count='5' flowId='42']
Expand Down Expand Up @@ -234,22 +249,7 @@ public function testCreateValidateNegativeTeamcity(): void
##teamcity[testSuiteFinished name='demo-2.csv' flowId='42']
Error: ./tests/fixtures/batch/demo-1.csv
##teamcity[testCount count='2' flowId='42']
##teamcity[testSuiteStarted name='demo-1.csv' flowId='42']
##teamcity[testStarted name='max at column 2:Float' locationHint='php_qn://./tests/fixtures/batch/demo-1.csv' flowId='42']
"max" at line 3, column "2:Float". Value "74605.944" is greater than "74605".
##teamcity[testFinished name='max at column 2:Float' flowId='42']
##teamcity[testStarted name='allow_values at column 4:Favorite color' locationHint='php_qn://./tests/fixtures/batch/demo-1.csv' flowId='42']
"allow_values" at line 3, column "4:Favorite color". Value "blue" is not allowed. Allowed values: ["red", "green", "Blue"].
##teamcity[testFinished name='allow_values at column 4:Favorite color' flowId='42']
##teamcity[testSuiteFinished name='demo-1.csv' flowId='42']
OK: ./tests/fixtures/batch/sub/demo-3.csv
Found 7 issues in 2 out of 3 CSV files.
TXT;
Expand Down
26 changes: 13 additions & 13 deletions tests/Blueprint/MiscTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,42 +139,42 @@ public function testFindFiles(): void

$this->getFileName(Utils::findFiles(['*.qwerty']));

isSame(['demo-3.csv', 'demo-2.csv', 'demo-1.csv'], $this->getFileName(Utils::findFiles([
isSame(['demo-1.csv', 'demo-2.csv', 'demo-3.csv'], $this->getFileName(Utils::findFiles([
PROJECT_ROOT . '/tests/fixtures/batch/*.csv',
])));

isSame(['demo-3.csv', 'demo-2.csv', 'demo-1.csv'], $this->getFileName(Utils::findFiles([
isSame(['demo-1.csv', 'demo-2.csv', 'demo-3.csv'], $this->getFileName(Utils::findFiles([
'tests/fixtures/batch/*.csv',
])));

isSame(['demo-3.csv', 'demo-2.csv', 'demo-1.csv'], $this->getFileName(Utils::findFiles([
isSame(['demo-1.csv', 'demo-2.csv', 'demo-3.csv'], $this->getFileName(Utils::findFiles([
'./tests/fixtures/batch/*.csv',
])));

isSame(['demo-3.csv', 'demo-2.csv', 'demo-1.csv'], $this->getFileName(Utils::findFiles(['**/demo-*.csv'])));
isSame(['demo-1.csv', 'demo-2.csv', 'demo-3.csv'], $this->getFileName(Utils::findFiles(['**/demo-*.csv'])));

isSame(['demo-3.csv', 'demo-2.csv', 'demo-1.csv', 'demo.csv'], $this->getFileName(Utils::findFiles([
isSame(['demo-1.csv', 'demo-2.csv', 'demo-3.csv', 'demo.csv'], $this->getFileName(Utils::findFiles([
PROJECT_ROOT . '/tests/fixtures/batch/*.csv',
PROJECT_ROOT . '/tests/fixtures/demo.csv',
])));

isSame(['demo.csv', 'demo-3.csv', 'demo-2.csv', 'demo-1.csv'], $this->getFileName(Utils::findFiles([
isSame(['demo.csv', 'demo-1.csv', 'demo-2.csv', 'demo-3.csv'], $this->getFileName(Utils::findFiles([
PROJECT_ROOT . '/tests/fixtures/demo.csv',
PROJECT_ROOT . '/tests/fixtures/batch/*.csv',
])));

isSame(
[
'demo-1.csv',
'demo-2.csv',
'demo-3.csv',
'complex_header.csv',
'simple_no_header.csv',
'empty_no_header.csv',
'complex_no_header.csv',
'demo.csv',
'empty_header.csv',
'demo-3.csv',
'demo-2.csv',
'demo-1.csv',
'empty_no_header.csv',
'simple_header.csv',
'demo.csv',
'complex_no_header.csv',
'simple_no_header.csv',
],
$this->getFileName(Utils::findFiles(['tests/**/*.csv'])),
);
Expand Down

0 comments on commit 17d5e47

Please sign in to comment.