From 17d5e478753ef119df85caa2fb7522021c34f535 Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Wed, 13 Mar 2024 01:29:06 +0400 Subject: [PATCH] Update error messaging and enhance sorting in CSV validation 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. --- README.md | 22 ++++++------ src/Commands/ValidateCsv.php | 2 +- src/Utils.php | 5 +-- tests/Blueprint/CommandsTest.php | 62 ++++++++++++++++---------------- tests/Blueprint/MiscTest.php | 26 +++++++------- 5 files changed, 59 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 8ea05894..d48a431a 100644 --- a/README.md +++ b/README.md @@ -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 | +------+------------+------------+--------------------------------------------------------+ @@ -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. ``` diff --git a/src/Commands/ValidateCsv.php b/src/Commands/ValidateCsv.php index ffa8cd1c..3a359db0 100644 --- a/src/Commands/ValidateCsv.php +++ b/src/Commands/ValidateCsv.php @@ -91,7 +91,7 @@ protected function executeAction(): int $errorCounter += $errorSuite->count(); if ($this->isTextMode()) { - $this->_('Error: ' . Utils::cutPath($csvFilename->getPathname()), OutLvl::E); + $this->_('Invalid file: ' . Utils::cutPath($csvFilename->getPathname()), OutLvl::E); } $output = $errorSuite->render($this->getOptString('report')); if ($output !== null) { diff --git a/src/Utils.php b/src/Utils.php index 102f2cd6..bac20b63 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -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()) { diff --git a/tests/Blueprint/CommandsTest.php b/tests/Blueprint/CommandsTest.php index a3554654..fb49c9cf 100644 --- a/tests/Blueprint/CommandsTest.php +++ b/tests/Blueprint/CommandsTest.php @@ -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 | +------+------------------+--------------+--------------------------------------------------------+ @@ -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 | +------+------------+------------+--------------------------------------------------------+ @@ -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; @@ -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. @@ -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'] @@ -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; diff --git a/tests/Blueprint/MiscTest.php b/tests/Blueprint/MiscTest.php index faa90a94..f7197789 100644 --- a/tests/Blueprint/MiscTest.php +++ b/tests/Blueprint/MiscTest.php @@ -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'])), );