-
-
Notifications
You must be signed in to change notification settings - Fork 432
[ci] check extra after part in fixture, that show no change + check no PHP files in /Fixture dirs #7134
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
Merged
Merged
[ci] check extra after part in fixture, that show no change + check no PHP files in /Fixture dirs #7134
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Nette\Utils\Strings; | ||
|
|
||
| require __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\ArgvInput; | ||
| use Symfony\Component\Console\Output\ConsoleOutput; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
| use Symfony\Component\Finder\Finder; | ||
| use Symfony\Component\Finder\SplFileInfo; | ||
| use Webmozart\Assert\Assert; | ||
|
|
||
| final readonly class SameBeforeAfterFixtureDetector | ||
| { | ||
| private SymfonyStyle $symfonyStyle; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->symfonyStyle = new SymfonyStyle(new ArgvInput(), new ConsoleOutput()); | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $testDirectories | ||
| * @return Command::SUCCESS|Command::FAILURE | ||
| */ | ||
| public function run(array $testDirectories): int | ||
| { | ||
| $fixtureFiles = $this->findFixtureFiles($testDirectories); | ||
|
|
||
| $invalidFixturePaths = []; | ||
| foreach ($fixtureFiles as $fixtureFile) { | ||
| if (! $this->hasFileSameBeforeAndAfterPart($fixtureFile)) { | ||
| continue; | ||
| } | ||
|
|
||
| $invalidFixturePaths[] = substr($fixtureFile->getRealPath(), strlen(getcwd()) + 1); | ||
| } | ||
|
|
||
| if ($invalidFixturePaths === []) { | ||
| $this->symfonyStyle->success('All fixtures are valid'); | ||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| $this->symfonyStyle->error( | ||
| 'The following fixtures have the same before and after content. Remove the part after "-----" to fix them' | ||
| ); | ||
| $this->symfonyStyle->listing($invalidFixturePaths); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $directories | ||
| * @return SplFileInfo[] | ||
| */ | ||
| private function findFixtureFiles(array $directories): array | ||
| { | ||
| Assert::allDirectory($directories); | ||
|
|
||
| $finder = (new Finder()) | ||
| ->files() | ||
| ->in($directories) | ||
| ->name('*.php.inc') | ||
| ->sortByName(); | ||
|
|
||
| return iterator_to_array($finder->getIterator()); | ||
| } | ||
|
|
||
| private function hasFileSameBeforeAndAfterPart(SplFileInfo $fixtureFile): bool | ||
| { | ||
| $parts = Strings::split($fixtureFile->getContents(), '#^\s*-----\s*$#m'); | ||
| if (count($parts) !== 2) { | ||
| return false; | ||
| } | ||
|
|
||
| return trim((string) $parts[0]) === trim((string) $parts[1]); | ||
| } | ||
| } | ||
|
|
||
| $sameBeforeAfterFixtureDetector = new SameBeforeAfterFixtureDetector(); | ||
| exit($sameBeforeAfterFixtureDetector->run([__DIR__ . '/../tests', __DIR__ . '/../rules-tests'])); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\ArgvInput; | ||
| use Symfony\Component\Console\Output\ConsoleOutput; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
| use Symfony\Component\Finder\Finder; | ||
| use Symfony\Component\Finder\SplFileInfo; | ||
| use Webmozart\Assert\Assert; | ||
|
|
||
| final readonly class NoPhpFileInFixturesDetector | ||
| { | ||
| /** | ||
| * @var string[] | ||
| */ | ||
| private const EXCLUDED_FILES = [ | ||
| // on-purpose as same namespace text | ||
| 'rules-tests/Renaming/Rector/Name/RenameClassRector/FixtureAutoImportNames/SomeShort.php', | ||
| ]; | ||
|
|
||
| private SymfonyStyle $symfonyStyle; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->symfonyStyle = new SymfonyStyle(new ArgvInput(), new ConsoleOutput()); | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $testDirectories | ||
| * @return Command::SUCCESS|Command::FAILURE | ||
| */ | ||
| public function run(array $testDirectories): int | ||
| { | ||
| $phpFiles = $this->findPhpFiles($testDirectories); | ||
|
|
||
| $allFixtureFiles = $this->findFixtureFiles($testDirectories); | ||
|
|
||
| $relativePhpFiles = []; | ||
| foreach ($phpFiles as $phpFile) { | ||
| $relativeFilePath = substr($phpFile->getRealPath(), strlen(getcwd()) + 1); | ||
|
|
||
| // should skip? | ||
| if (in_array($relativeFilePath, self::EXCLUDED_FILES, true)) { | ||
| continue; | ||
| } | ||
|
|
||
| $relativePhpFiles[] = $relativeFilePath; | ||
| } | ||
|
|
||
| if ($relativePhpFiles === []) { | ||
| $this->symfonyStyle->success(sprintf('All %d fixtures are valid', count($allFixtureFiles))); | ||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| $this->symfonyStyle->error( | ||
| 'The following "*.php* files were found in /Fixtures directory, but only "*.php.inc" files are picked up and allowed. Rename their suffix or remove them' | ||
| ); | ||
| $this->symfonyStyle->listing($relativePhpFiles); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $directories | ||
| * @return SplFileInfo[] | ||
| */ | ||
| private function findPhpFiles(array $directories): array | ||
| { | ||
| Assert::allDirectory($directories); | ||
|
|
||
| $finder = (new Finder()) | ||
| ->files() | ||
| ->in($directories) | ||
| ->path('/Fixture') | ||
| ->path('/Fixture*') | ||
| ->notPath('Source') | ||
| ->name('*.php') | ||
| ->sortByName(); | ||
|
|
||
| return iterator_to_array($finder->getIterator()); | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $directories | ||
| * @return SplFileInfo[] | ||
| */ | ||
| private function findFixtureFiles(array $directories): array | ||
| { | ||
| Assert::allDirectory($directories); | ||
|
|
||
| $finder = (new Finder()) | ||
| ->files() | ||
| ->in($directories) | ||
| ->path('Fixture') | ||
| ->path('Fixture*') | ||
| ->notPath('Source') | ||
| ->sortByName(); | ||
|
|
||
| return iterator_to_array($finder->getIterator()); | ||
| } | ||
| } | ||
|
|
||
| $noPhpFileInFixturesDetector = new NoPhpFileInFixturesDetector(); | ||
|
|
||
| exit($noPhpFileInFixturesDetector->run([__DIR__ . '/../rules-tests'])); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
rules-tests/Renaming/Rector/Cast/RenameCastRector/RenameCastRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samsonasik This might help with code reviews 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TomasVotruba the files seems commited on scoped vendor, should it be removed before scoped?
rectorphp/rector@d2d451e#diff-6a943cc1e536167118f5fb8160e164027ae139c4986d1e324a1faabbd1f39b36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Indeed, should be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I removed at PR: