From 9242718a96b45eb6effce5d570f1138c2611da0e Mon Sep 17 00:00:00 2001 From: Denis Smetannikov Date: Thu, 14 Mar 2024 13:51:58 +0400 Subject: [PATCH] Update terminal width for GitHub actions and adjust schema examples (#24) This commit adjusts the terminal width configuration in ErrorSuite.php specifically for GitHub actions by adding a condition to set the maximum width to 150. Furthermore, the order of "str_ends_with" and "str_starts_with" properties in full.yml schema example file have been switched. Relevant changes are also echoed in the README.md file. --- Dockerfile | 3 -- README.md | 10 +++--- action.yml | 2 ++ schema-examples/full.json | 4 +-- schema-examples/full.php | 4 +-- schema-examples/full.yml | 4 +-- src/Rules/{StrEndsWith.php => EndsWith.php} | 2 +- .../{StrStartsWith.php => StartsWith.php} | 2 +- src/Utils.php | 34 +++++++++++++++++++ src/Validators/ErrorSuite.php | 13 +++---- tests/Blueprint/GithubActionsTest.php | 2 +- tests/Blueprint/RulesTest.php | 24 ++++++------- 12 files changed, 67 insertions(+), 37 deletions(-) rename src/Rules/{StrEndsWith.php => EndsWith.php} (94%) rename src/Rules/{StrStartsWith.php => StartsWith.php} (94%) diff --git a/Dockerfile b/Dockerfile index 078b0ff8..929daa31 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,7 +25,4 @@ RUN cd /app \ && composer clear-cache RUN chmod +x /app/csv-blueprint -# Color output by default -ENV TERM_PROGRAM=Hyper - ENTRYPOINT ["/app/csv-blueprint"] diff --git a/README.md b/README.md index cbd1252c..85e6367f 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Also see demo in the [GitHub Actions](https://github.com/JBZoo/Csv-Blueprint/act ### As GitHub Action ```yml -- uses: jbzoo/csv-blueprint # See the specific version on releases page +- uses: jbzoo/csv-blueprint@master # See the specific version on releases page with: # Path(s) to validate. You can specify path in which CSV files will be searched. Feel free to use glob pattrens. Usage examples: /full/path/file.csv, p/file.csv, p/*.csv, p/**/*.csv, p/**/name-*.csv, **/*.csv, etc. # Required: true @@ -330,8 +330,8 @@ columns: max_word_count: 5 # Integer only. Max count of words in the string Example: "Hello World! 123" - 2 words only (123 is not a word) at_least_contains: [ a, b ] # At least one of the string must be in the CSV value. Case-sensitive. all_must_contain: [ a, b, c ] # All the strings must be part of a CSV value. Case-sensitive. - str_ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix" - str_starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World" + starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World" + ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix" # Decimal and integer numbers min: 10 # Can be integer or float, negative and positive @@ -380,7 +380,7 @@ Batch processing Validation * [x] ~~`filename_pattern` validation with regex (like "all files in the folder should be in the format `/^[\d]{4}-[\d]{2}-[\d]{2}\.csv$/`").~~ * [ ] Flag to ignore file name pattern. It's useful when you have a lot of files and you don't want to validate the file name. -* [ ] Keyword for null value. Configurable. By default, it's an empty string. But you can use `null`, `nil`, `none`, `empty`, etc. +* [ ] Keyword for null value. Configurable. By default, it's an empty string. But you can use `null`, `nil`, `none`, `empty`, etc. Overridable on the column level. * [ ] Agregate rules (like "at least one of the fields should be not empty" or "all values must be unique"). * [ ] Handle empty files and files with only a header row, or only with one line of data. One column wthout header is also possible. * [ ] Using multiple schemas for one csv file. @@ -409,6 +409,8 @@ Mock data generation * [ ] Use [Faker](https://github.com/FakerPHP/Faker) for random data generation. Reporting +* [ ] Fix auto width of tables in Githu terminal. +* [ ] * [ ] More report formats (like JSON, XML, etc). Any ideas? * [ ] Gitlab and JUnit reports must be as one structure. It's not so easy to implement. But it's a good idea. * [ ] Merge reports from multiple CSV files into one report. It's useful when you have a lot of files and you want to see all errors in one place. Especially for GitLab and JUnit reports. diff --git a/action.yml b/action.yml index 2f38105e..b69869f6 100644 --- a/action.yml +++ b/action.yml @@ -40,6 +40,8 @@ inputs: runs: using: 'docker' image: 'docker://jbzoo/csv-blueprint' + env: + GITHUB_ACTIONS: 'true' args: - validate:csv - '--csv' diff --git a/schema-examples/full.json b/schema-examples/full.json index fe841bf1..6106829b 100644 --- a/schema-examples/full.json +++ b/schema-examples/full.json @@ -28,8 +28,8 @@ "max_word_count" : 5, "at_least_contains" : ["a", "b"], "all_must_contain" : ["a", "b", "c"], - "str_ends_with" : " suffix", - "str_starts_with" : "prefix ", + "starts_with" : "prefix ", + "ends_with" : " suffix", "min" : 10, "max" : 100.5, "precision" : 3, diff --git a/schema-examples/full.php b/schema-examples/full.php index f336b00f..c2afbac0 100644 --- a/schema-examples/full.php +++ b/schema-examples/full.php @@ -46,8 +46,8 @@ 'max_word_count' => 5, 'at_least_contains' => ['a', 'b'], 'all_must_contain' => ['a', 'b', 'c'], - 'str_ends_with' => ' suffix', - 'str_starts_with' => 'prefix ', + 'starts_with' => 'prefix ', + 'ends_with' => ' suffix', 'min' => 10, 'max' => 100.5, 'precision' => 3, diff --git a/schema-examples/full.yml b/schema-examples/full.yml index 136e73cb..7ba41d13 100644 --- a/schema-examples/full.yml +++ b/schema-examples/full.yml @@ -52,8 +52,8 @@ columns: max_word_count: 5 # Integer only. Max count of words in the string Example: "Hello World! 123" - 2 words only (123 is not a word) at_least_contains: [ a, b ] # At least one of the string must be in the CSV value. Case-sensitive. all_must_contain: [ a, b, c ] # All the strings must be part of a CSV value. Case-sensitive. - str_ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix" - str_starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World" + starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World" + ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix" # Decimal and integer numbers min: 10 # Can be integer or float, negative and positive diff --git a/src/Rules/StrEndsWith.php b/src/Rules/EndsWith.php similarity index 94% rename from src/Rules/StrEndsWith.php rename to src/Rules/EndsWith.php index aa5dd3ad..3d205112 100644 --- a/src/Rules/StrEndsWith.php +++ b/src/Rules/EndsWith.php @@ -16,7 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules; -final class StrEndsWith extends AbstarctRule +final class EndsWith extends AbstarctRule { public function validateRule(string $cellValue): ?string { diff --git a/src/Rules/StrStartsWith.php b/src/Rules/StartsWith.php similarity index 94% rename from src/Rules/StrStartsWith.php rename to src/Rules/StartsWith.php index a16e8f32..f2f37214 100644 --- a/src/Rules/StrStartsWith.php +++ b/src/Rules/StartsWith.php @@ -16,7 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules; -final class StrStartsWith extends AbstarctRule +final class StartsWith extends AbstarctRule { public function validateRule(string $cellValue): ?string { diff --git a/src/Utils.php b/src/Utils.php index 57beb1cc..c22a980e 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -16,6 +16,8 @@ namespace JBZoo\CsvBlueprint; +use JBZoo\Utils\Cli; +use JBZoo\Utils\Env; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; @@ -103,4 +105,36 @@ public static function cutPath(string $fullpath): string return \str_replace($pwd, '.', $fullpath); } + + public static function isDocker(): bool + { + return \file_exists('/app/csv-blueprint'); + } + + public static function isGithubActions(): bool + { + return self::isDocker() && Env::bool('GITHUB_ACTIONS'); + } + + /** + * Autodetect the width of the terminal. + */ + public static function autoDetectTerminalWidth(): int + { + static $maxAutoDetected; // Execution optimization + + if ($maxAutoDetected === null) { + if (self::isGithubActions()) { + $maxAutoDetected = 200; // GitHub Actions has a wide terminal + } elseif (self::isDocker()) { + $maxAutoDetected = 140; + } else { + // Fallback to 80 if the terminal width cannot be determined. + // env.COLUMNS_TEST usually not defined, so we use it only for testing purposes. + $maxAutoDetected = Env::int('COLUMNS_TEST', Cli::getNumberOfColumns()); + } + } + + return $maxAutoDetected; + } } diff --git a/src/Validators/ErrorSuite.php b/src/Validators/ErrorSuite.php index 4a625448..fdcc2157 100644 --- a/src/Validators/ErrorSuite.php +++ b/src/Validators/ErrorSuite.php @@ -21,8 +21,7 @@ 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\CsvBlueprint\Utils; use JBZoo\Utils\Vars; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Output\BufferedOutput; @@ -199,17 +198,13 @@ private static function getTableSize(): array 'line' => 10, 'column' => 20, 'rule' => 20, - 'min' => 90, + 'min' => 120, 'max' => 150, - 'reserve' => 5, // So that the table does not rest on the very edge of the terminal. Just in case. + 'reserve' => 3, // 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. - // env.COLUMNS_TEST usually not defined so we use it only for testing purposes. - $maxAutoDetected = Env::int('COLUMNS_TEST', Cli::getNumberOfColumns()); - $maxWindowWidth = Vars::limit( - $maxAutoDetected, + Utils::autoDetectTerminalWidth(), $floatingSizes['min'], $floatingSizes['max'], ) - $floatingSizes['reserve']; diff --git a/tests/Blueprint/GithubActionsTest.php b/tests/Blueprint/GithubActionsTest.php index 2b6f4776..52321b67 100644 --- a/tests/Blueprint/GithubActionsTest.php +++ b/tests/Blueprint/GithubActionsTest.php @@ -61,7 +61,7 @@ public function testGitHubActionsReadMe(): void $expectedMessage = [ '```yml', - '- uses: jbzoo/csv-blueprint # See the specific version on releases page', + '- uses: jbzoo/csv-blueprint@master # See the specific version on releases page', ' with:', ]; diff --git a/tests/Blueprint/RulesTest.php b/tests/Blueprint/RulesTest.php index d7f741d3..ad6fa0ca 100644 --- a/tests/Blueprint/RulesTest.php +++ b/tests/Blueprint/RulesTest.php @@ -21,6 +21,7 @@ use JBZoo\CsvBlueprint\Rules\AtLeastContains; use JBZoo\CsvBlueprint\Rules\CardinalDirection; use JBZoo\CsvBlueprint\Rules\DateFormat; +use JBZoo\CsvBlueprint\Rules\EndsWith; use JBZoo\CsvBlueprint\Rules\ExactValue; use JBZoo\CsvBlueprint\Rules\IsAlias; use JBZoo\CsvBlueprint\Rules\IsBool; @@ -49,8 +50,7 @@ use JBZoo\CsvBlueprint\Rules\OnlyUppercase; use JBZoo\CsvBlueprint\Rules\Precision; use JBZoo\CsvBlueprint\Rules\Regex; -use JBZoo\CsvBlueprint\Rules\StrEndsWith; -use JBZoo\CsvBlueprint\Rules\StrStartsWith; +use JBZoo\CsvBlueprint\Rules\StartsWith; use JBZoo\CsvBlueprint\Rules\UsaMarketName; use JBZoo\CsvBlueprint\Rules\WordCount; use JBZoo\PHPUnit\PHPUnit; @@ -767,46 +767,46 @@ public function testAllMustContain(): void public function testStrStartsWith(): void { - $rule = new StrStartsWith('prop', 'a'); + $rule = new StartsWith('prop', 'a'); isSame(null, $rule->validate('a')); isSame(null, $rule->validate('abc')); isSame( - '"str_starts_with" at line 0, column "prop". Value "" must start with "a".', + '"starts_with" at line 0, column "prop". Value "" must start with "a".', \strip_tags((string)$rule->validate('')), ); isSame( - '"str_starts_with" at line 0, column "prop". Value " a" must start with "a".', + '"starts_with" at line 0, column "prop". Value " a" must start with "a".', \strip_tags((string)$rule->validate(' a')), ); - $rule = new StrStartsWith('prop', ''); + $rule = new StartsWith('prop', ''); isSame( - '"str_starts_with" at line 0, column "prop". Rule must contain a prefix value in schema file.', + '"starts_with" at line 0, column "prop". Rule must contain a prefix value in schema file.', \strip_tags((string)$rule->validate('a ')), ); } public function testStrEndsWith(): void { - $rule = new StrEndsWith('prop', 'a'); + $rule = new EndsWith('prop', 'a'); isSame(null, $rule->validate('a')); isSame(null, $rule->validate('cba')); isSame( - '"str_ends_with" at line 0, column "prop". Value "" must end with "a".', + '"ends_with" at line 0, column "prop". Value "" must end with "a".', \strip_tags((string)$rule->validate('')), ); isSame( - '"str_ends_with" at line 0, column "prop". Value "a " must end with "a".', + '"ends_with" at line 0, column "prop". Value "a " must end with "a".', \strip_tags((string)$rule->validate('a ')), ); - $rule = new StrEndsWith('prop', ''); + $rule = new EndsWith('prop', ''); isSame( - '"str_ends_with" at line 0, column "prop". Rule must contain a suffix value in schema file.', + '"ends_with" at line 0, column "prop". Rule must contain a suffix value in schema file.', \strip_tags((string)$rule->validate('a ')), ); }