move ci/check_keep_fixtures.php to ValidateFixtureCommand

This commit is contained in:
TomasVotruba 2020-03-29 15:49:12 +02:00
parent f823903560
commit be4b34302f
5 changed files with 107 additions and 61 deletions

View File

@ -16,4 +16,4 @@ jobs:
php-version: 7.2
coverage: none # disable xdebug, pcov
- run: composer install --no-progress
- run: composer check-fixtures
- run: bin/rector validate-fixture

View File

@ -1,57 +0,0 @@
<?php
declare(strict_types=1);
use Nette\Utils\Strings;
use Rector\Core\Testing\ValueObject\SplitLine;
use Symfony\Component\Finder\Finder;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
use Symplify\SmartFileSystem\Finder\FinderSanitizer;
use Symplify\SmartFileSystem\SmartFileInfo;
require __DIR__ . '/../vendor/autoload.php';
$finder = new Finder();
$finder->files();
$finder->name('*.php.inc');
$finder->in(__DIR__ . '/../tests');
$finder->in(__DIR__ . '/../packages/*/tests');
$finder->in(__DIR__ . '/../rules/*/tests');
$finderSanitizer = new FinderSanitizer();
$smartFileInfos = $finderSanitizer->sanitize($finder);
$symfonyStyleFactory = new SymfonyStyleFactory();
$symfonyStyle = $symfonyStyleFactory->create();
$errors = [];
/** @var SmartFileInfo $smartFileInfo */
foreach ($smartFileInfos as $smartFileInfo) {
if (! Strings::match($smartFileInfo->getContents(), SplitLine::SPLIT_LINE)) {
continue;
}
// original → expected
[$originalContent, $expectedContent] = Strings::split($smartFileInfo->getContents(), SplitLine::SPLIT_LINE);
if ($originalContent !== $expectedContent) {
continue;
}
$errors[] = $smartFileInfo->getRelativeFilePathFromCwd();
}
if ($errors === []) {
exit(ShellCode::SUCCESS);
}
$symfonyStyle->warning(sprintf(
'These files have same content before "%s" and after it. Remove the content after "%s"',
SplitLine::SPLIT_LINE,
SplitLine::SPLIT_LINE
));
$symfonyStyle->listing($errors);
exit(ShellCode::ERROR);

View File

@ -242,9 +242,9 @@
"bin/rector check-static-type-mappers",
"@check-sets"
],
"check-sets": "bin/rector validate-sets",
"check-sets": "bin/rector validate-sets --ansi",
"check-cs": "vendor/bin/ecs check --ansi",
"check-fixtures": "php ci/check_keep_fixtures.php",
"check-fixtures": "bin/rector validate-fixture --ansi",
"check-services": "php ci/check_services_in_yaml_configs.php",
"fix-cs": [
"vendor/bin/ecs check --fix --ansi",

View File

@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
namespace Rector\Utils\SetRunner\Command;
use Nette\Utils\Strings;
use Rector\Core\Testing\ValueObject\SplitLine;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\SmartFileSystem\Finder\FinderSanitizer;
use Symplify\SmartFileSystem\SmartFileInfo;
final class ValidateFixtureCommand extends Command
{
/**
* @var FinderSanitizer
*/
private $finderSanitizer;
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
public function __construct(FinderSanitizer $finderSanitizer, SymfonyStyle $symfonyStyle)
{
$this->finderSanitizer = $finderSanitizer;
$this->symfonyStyle = $symfonyStyle;
parent::__construct();
}
protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
$this->setDescription('[CI] Validate tests fixtures');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$hasError = false;
foreach ($this->getFixtureFileInfos() as $fixtureFileInfo) {
if (! $this->hasFileIdenticalCodeBeforeAndAfter($fixtureFileInfo)) {
continue;
}
// files content is equal, but it should not
$message = sprintf(
'The "%s" file has same content before "%s" and after it.%sRemove the content after "%s"',
$fixtureFileInfo->getRelativeFilePathFromCwd(),
SplitLine::SPLIT_LINE,
PHP_EOL,
SplitLine::SPLIT_LINE
);
$this->symfonyStyle->error($message);
$hasError = true;
}
if ($hasError) {
return ShellCode::ERROR;
}
$this->symfonyStyle->success('All fixtures are correct');
return ShellCode::SUCCESS;
}
/**
* @return SmartFileInfo[]
*/
private function getFixtureFileInfos(): array
{
$finder = (new Finder())
->files()
->name('*.php.inc')
->in(__DIR__ . '/../../../../tests')
->in(__DIR__ . '/../../../../packages/*/tests')
->in(__DIR__ . '/../../../../rules/*/tests');
return $this->finderSanitizer->sanitize($finder);
}
private function hasFileIdenticalCodeBeforeAndAfter(SmartFileInfo $smartFileInfo): bool
{
$fileContent = $smartFileInfo->getContents();
if (! Strings::match($fileContent, SplitLine::SPLIT_LINE)) {
return false;
}
// original → expected
[$originalContent, $expectedContent] = Strings::split($fileContent, SplitLine::SPLIT_LINE);
return $originalContent === $expectedContent;
}
}

View File

@ -68,7 +68,7 @@ final class ValidateSetsCommand extends Command
protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
$this->setDescription('Validate each sets by running it on simple file');
$this->setDescription('[CI] Validate each sets by running it on simple file');
}
protected function execute(InputInterface $input, OutputInterface $output): int