Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readme added #8

Merged
merged 27 commits into from
Mar 11, 2024
Binary file added .github/assets/github-actions-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/github-actions-termintal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ build: ##@Project Install all 3rd party dependencies
@make build-phar
@rm -f `pwd`/ci-report-converter


build-install: ##@Project Install all 3rd party dependencies as prod
$(call title,"Install/Update all 3rd party dependencies as prod")
@composer install --no-dev --no-progress --no-interaction --no-suggest
@rm -f `pwd`/ci-report-converter


update: ##@Project Install/Update all 3rd party dependencies
@echo "Composer flags: $(JBZOO_COMPOSER_UPDATE_FLAGS)"
@composer update $(JBZOO_COMPOSER_UPDATE_FLAGS)
Expand Down
352 changes: 331 additions & 21 deletions README.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# @see https://github.com/JBZoo/Csv-Blueprint
#

name: 'CSV Validator'
name: 'CSV Validator by schemas'
description: 'Strict and flexible schema-based CSV file validation with the ability to report as GitHub Annotations in your PRs.'
author: 'Denis Smetannikov <admin@jbzoo.com>'

Expand Down Expand Up @@ -41,4 +41,5 @@ runs:
- ${{ inputs.schema }}
- '--output'
- ${{ inputs.output }}
- '--ansi'
- '-vvv'
2 changes: 1 addition & 1 deletion src/Csv/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(string $csvFilename, null|array|string $csvSchemaFil

public function getCsvFilename(): string
{
return \pathinfo((string)\realpath($this->csvFilename), \PATHINFO_BASENAME);
return $this->csvFilename;
}

public function getCsvStructure(): ParseConfig
Expand Down
11 changes: 8 additions & 3 deletions src/Validators/ErrorSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ private function renderTable(): string
{
$buffer = new BufferedOutput();
$table = (new Table($buffer))
->setHeaderTitle($this->csvFilename)
->setFooterTitle($this->csvFilename)
->setHeaderTitle($this->getTestcaseName())
->setFooterTitle($this->getTestcaseName())
->setHeaders(['Line', 'id:Column', 'Rule', 'Message'])
->setColumnMaxWidth(0, 10)
->setColumnMaxWidth(1, 20)
Expand All @@ -154,7 +154,7 @@ private function renderTable(): string

private function prepareSourceSuite(): SourceSuite
{
$suite = new SourceSuite($this->csvFilename);
$suite = new SourceSuite($this->getTestcaseName());

foreach ($this->errors as $error) {
$caseName = $error->getRuleCode() . ' at column ' . $error->getColumnName();
Expand All @@ -166,4 +166,9 @@ private function prepareSourceSuite(): SourceSuite

return $suite;
}

private function getTestcaseName(): string
{
return \pathinfo((string)\realpath((string)$this->csvFilename), \PATHINFO_BASENAME);
}
}
4 changes: 2 additions & 2 deletions tests/Blueprint/CsvReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class CsvReaderTest extends PHPUnit
public function testReadCsvFileWithoutHeader(): void
{
$csv = new CsvFile(self::CSV_SIMPLE_NO_HEADER, self::SCHEMA_SIMPLE_NO_HEADER);
isSame('simple_no_header.csv', $csv->getCsvFilename());
isSame(self::CSV_SIMPLE_NO_HEADER, $csv->getCsvFilename());

isSame([], $csv->getHeader());

Expand All @@ -50,7 +50,7 @@ public function testReadCsvFileWithoutHeader(): void
public function testReadCsvFileWithHeader(): void
{
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, self::SCHEMA_SIMPLE_HEADER);
isSame('simple_header.csv', $csv->getCsvFilename());
isSame(self::CSV_SIMPLE_HEADER, $csv->getCsvFilename());

isSame(['seq', 'bool', 'exact'], $csv->getHeader());

Expand Down
4 changes: 3 additions & 1 deletion tests/Blueprint/MiscTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ private function testCheckExampleInReadme(
$tmpl = \implode("\n", ["```{$type}", $filepath, '```']);
}

$tmpl = $this->getSpoiler("Click to see: {$title}", $tmpl);
if ($type !== 'yml') {
$tmpl = $this->getSpoiler("Click to see: {$title}", $tmpl);
}

isFileContains($tmpl, PROJECT_ROOT . '/README.md');
}
Expand Down
46 changes: 27 additions & 19 deletions tests/Blueprint/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,13 @@ public function testRenderTable(): void

public function testRenderTeamCity(): void
{
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, $this->getRule('seq', 'min', 3));
$out = $csv->validate()->render(ErrorSuite::RENDER_TEAMCITY);
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, $this->getRule('seq', 'min', 3));
$out = $csv->validate()->render(ErrorSuite::RENDER_TEAMCITY);
$path = self::CSV_SIMPLE_HEADER;

isContain("##teamcity[testCount count='2' ", $out);
isContain("##teamcity[testSuiteStarted name='simple_header.csv' ", $out);
isContain("##teamcity[testStarted name='min at column 0:seq' locationHint='php_qn://simple_header.csv'", $out);
isContain("##teamcity[testStarted name='min at column 0:seq' locationHint='php_qn://{$path}'", $out);
isContain("##teamcity[testFinished name='min at column 0:seq' timestamp", $out);
isContain('Value "1" is less than "3"', $out);
isContain('Value "2" is less than "3"', $out);
Expand All @@ -475,12 +476,13 @@ public function testRenderTeamCity(): void

public function testRenderGithub(): void
{
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, $this->getRule('seq', 'min', 3));
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, $this->getRule('seq', 'min', 3));
$path = self::CSV_SIMPLE_HEADER;
isSame(
\implode("\n", [
'::error file=simple_header.csv,line=2::min at column 0:seq%0AValue "1" is less than "3"',
"::error file={$path},line=2::min at column 0:seq%0AValue \"1\" is less than \"3\"",
'',
'::error file=simple_header.csv,line=3::min at column 0:seq%0AValue "2" is less than "3"',
"::error file={$path},line=3::min at column 0:seq%0AValue \"2\" is less than \"3\"",
'',
]),
$csv->validate()->render(ErrorSuite::RENDER_GITHUB),
Expand All @@ -489,44 +491,50 @@ public function testRenderGithub(): void

public function testRenderGitlab(): void
{
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, $this->getRule('seq', 'min', 3));
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, $this->getRule('seq', 'min', 3));
$path = self::CSV_SIMPLE_HEADER;

$cleanJson = json($csv->validate()->render(ErrorSuite::RENDER_GITLAB))->getArrayCopy();
unset($cleanJson[0]['fingerprint'], $cleanJson[1]['fingerprint']);

isSame(
[
[
'description' => "min at column 0:seq\nValue \"1\" is less than \"3\"",
'fingerprint' => 'ec0612c9f1610d440b558fff51bbceed086a0212cdeb14d79d09c8a9bd108487',
'severity' => 'major',
'location' => [
'path' => 'simple_header.csv',
// 'fingerprint' => '2c2639beb20e2e9ea13a414ce91865522f6e1885abcf1f99ada44de007cdb01f',
'severity' => 'major',
'location' => [
'path' => $path,
'lines' => ['begin' => 2],
],
],
[
'description' => "min at column 0:seq\nValue \"2\" is less than \"3\"",
'fingerprint' => '51f82750d029c395dec5f2f5c1c4eb841e43c1ea6b8ece9ee31126a3e22620cb',
'severity' => 'major',
'location' => [
'path' => 'simple_header.csv',
// 'fingerprint' => '0cda6e2df28be9033542ab504e315d070951a206446eb7005d2060d44cfa0e45',
'severity' => 'major',
'location' => [
'path' => $path,
'lines' => ['begin' => 3],
],
],
],
json($csv->validate()->render(ErrorSuite::RENDER_GITLAB))->getArrayCopy(),
$cleanJson,
);
}

public function testRenderJUnit(): void
{
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, $this->getRule('seq', 'min', 3));
$csv = new CsvFile(self::CSV_SIMPLE_HEADER, $this->getRule('seq', 'min', 3));
$path = self::CSV_SIMPLE_HEADER;
isSame(
\implode("\n", [
'<?xml version="1.0" encoding="UTF-8"?>',
'<testsuites>',
' <testsuite name="simple_header.csv" tests="2">',
' <testcase name="min at column 0:seq" file="simple_header.csv" line="2">',
" <testcase name=\"min at column 0:seq\" file=\"{$path}\" line=\"2\">",
' <system-out>Value "1" is less than "3"</system-out>',
' </testcase>',
' <testcase name="min at column 0:seq" file="simple_header.csv" line="3">',
" <testcase name=\"min at column 0:seq\" file=\"{$path}\" line=\"3\">",
' <system-out>Value "2" is less than "3"</system-out>',
' </testcase>',
' </testsuite>',
Expand Down
85 changes: 84 additions & 1 deletion tests/CsvBlueprintPackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,93 @@ final class CsvBlueprintPackageTest extends \JBZoo\Codestyle\PHPUnit\AbstractPac
{
protected string $packageName = 'Csv-Blueprint';

protected array $params = [
// Packagist
'packagist_latest_stable_version' => true,
'packagist_latest_unstable_version' => true,
'packagist_license' => true,
'packagist_version' => true,

'packagist_dependents' => true,
'packagist_suggesters' => true,

'packagist_downloads_total' => true,
'packagist_downloads_daily' => true,
'packagist_downloads_monthly' => true,

'packagist_composerlock' => true,
'packagist_gitattributes' => true,

'github_issues' => true,
'github_license' => true,
'github_forks' => true,
'github_stars' => true,
'github_actions' => true,

'github_actions_demo' => true,
'github_actions_release_docker' => true,

'docker_pulls' => true,

'psalm_coverage' => true,
'psalm_level' => true,
'codacy' => true,
'codefactor' => true,
'sonarcloud' => true,
'coveralls' => true,
'circle_ci' => true,
];

protected array $badgesTemplate = [
'github_actions',
'github_actions_demo',
'github_actions_release_docker',
'docker_build',
'codecov',
'coveralls',
'psalm_coverage',
'psalm_level',
'codefactor',
'scrutinizer',
'__BR__',
'packagist_latest_stable_version',
'packagist_downloads_total',
'docker_pulls',
'packagist_dependents',
'visitors',
'github_license',
];

protected function setUp(): void
{
$this->params['docker_pulls'] = true;
$this->excludePaths[] = 'assets';

parent::setUp();
}

protected function checkBadgeGithubActionsDemo(): ?string
{
$path = 'https://github.com/__VENDOR_ORIG__/__PACKAGE_ORIG__/actions/workflows';

return $this->getPreparedBadge(
$this->getBadge(
'CI',
$path . '/demo.yml/badge.svg?branch=master',
$path . '/demo.yml?query=branch%3Amaster',
),
);
}

protected function checkBadgeGithubActionsReleaseDocker(): ?string
{
$path = 'https://github.com/__VENDOR_ORIG__/__PACKAGE_ORIG__/actions/workflows';

return $this->getPreparedBadge(
$this->getBadge(
'CI',
$path . '/release-docker.yml/badge.svg?branch=master',
$path . '/release-docker.yml?query=branch%3Amaster',
),
);
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/demo.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Name,City,Float,Birthday,Favorite color

Check failure on line 1 in tests/fixtures/demo.csv

View workflow job for this annotation

GitHub Actions / All Report Types

csv.header at column 1: Property "name" is not defined in schema: "./tests/schemas/demo_invalid.yml"
Clyde,Rivsikgo,4825.1856,2000-01-01,red
Clyde,Rivsikgo,4825.1856,2000-01-01,green
Elsie,Vonavka,03.6544,2000-12-01,green
Derek,Sarefunaw,-177.9088,2000-01-31,green
Dylan,Wufolu,74605.944,1998-02-28,blue

Check failure on line 5 in tests/fixtures/demo.csv

View workflow job for this annotation

GitHub Actions / All Report Types

max at column 2:Float Value "74605.944" is greater than "74605"

Check failure on line 5 in tests/fixtures/demo.csv

View workflow job for this annotation

GitHub Actions / All Report Types

allow_values at column 4:Favorite color Value "blue" is not allowed. Allowed values: ["red", "green", "Blue"]
Carl,Gorriju,0.8431,1955-05-14,red

Check failure on line 6 in tests/fixtures/demo.csv

View workflow job for this annotation

GitHub Actions / All Report Types

min_length at column 0:Name Value "Carl" (legth: 4) is too short. Min length is 5

Check failure on line 6 in tests/fixtures/demo.csv

View workflow job for this annotation

GitHub Actions / All Report Types

min_date at column 3:Birthday Value "1955-05-14" is less than the minimum date "1955-05-15T00:00:00.000+00:00"
Landon,Mojebol,123.64,1989-05-15,red
Olive,Pebiogu,0,1955-05-14,green

Check failure on line 8 in tests/fixtures/demo.csv

View workflow job for this annotation

GitHub Actions / All Report Types

min_date at column 3:Birthday Value "1955-05-14" is less than the minimum date "1955-05-15T00:00:00.000+00:00"
Willie,Sowaah,0.001,2010-07-20,red

Check failure on line 9 in tests/fixtures/demo.csv

View workflow job for this annotation

GitHub Actions / All Report Types

max_date at column 3:Birthday Value "2010-07-20" is more than the maximum date "2009-01-01T00:00:00.000+00:00"
Derrick,Rakufag,42,1990-09-10,green
Lois,Mofninle,-19366059127.6032,1988-08-24,blue
Lois,Mofninle,-19366059127.6032,1988-08-24,green

Check failure on line 11 in tests/fixtures/demo.csv

View workflow job for this annotation

GitHub Actions / All Report Types

min_length at column 0:Name Value "Lois" (legth: 4) is too short. Min length is 5
2 changes: 2 additions & 0 deletions tests/schemas/demo_invalid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# @see https://github.com/JBZoo/Csv-Blueprint
#

# This schema is invalid because does not match the CSV file (tests/fixtures/demo.csv).

columns:
- name: Name
rules:
Expand Down
2 changes: 2 additions & 0 deletions tests/schemas/demo_valid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# @see https://github.com/JBZoo/Csv-Blueprint
#

# This schema is valid because match the CSV file (tests/fixtures/demo.csv) perfectly.

columns:
- name: Name
rules:
Expand Down
Loading