Skip to content

Commit

Permalink
Add patch files filter
Browse files Browse the repository at this point in the history
  • Loading branch information
petk committed Apr 23, 2024
1 parent fb4022a commit 631face
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trim_trailing_whitespace = false
[COMMIT_EDITMSG]
max_line_length = 80

[tests/fixtures/generated/**]
[tests/fixtures/{generated/**,miscellaneous.php}]
trim_trailing_whitespace = false
insert_final_newline = false
indent_style = false
Expand Down
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

$finder = PhpCsFixer\Finder::create()
->exclude('tests/fixtures/generated')
->notPath('tests/fixtures/miscellaneous.php')
->in(__DIR__)
->name(['*.php', 'normalizator', 'build'])
;
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- PHP 8.2 minimum required.
- Project dependencies updated.
- Permissions normalization checks shebang with more patterns.
- Patch files are now ignored in certain normalizations.

## [0.0.4] - 2023-07-03

Expand Down
46 changes: 46 additions & 0 deletions src/Filter/NoPatchFilesFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Normalizator\Filter;

use Normalizator\Attribute\Filter;
use Normalizator\Cache\Cache;
use Normalizator\Finder\File;

use function in_array;
use function strtolower;

/**
* Filter which doesn't pass if the file is *.patch.
*/
#[Filter(
name: 'no_patch_files',
)]
class NoPatchFilesFilter implements NormalizationFilterInterface
{
public function __construct(private Cache $cache) {}

public function filter(File $file): bool
{
$key = static::class . ':' . $file->getPathname();

if ($this->cache->has($key)) {
return (bool) $this->cache->get($key);
}

$filter = $this->check($file);

$this->cache->set($key, $filter);

return $filter;
}

protected function check(File $file): bool
{
return !in_array(strtolower($file->getExtension()), [
'patch',
'diff',
], true);
}
}
1 change: 1 addition & 0 deletions src/Normalization/EolNormalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'plain_text',
'no_git',
'no_node_modules',
'no_patch_files',
'no_svn',
'no_vendor',
],
Expand Down
1 change: 1 addition & 0 deletions src/Normalization/IndentationNormalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'plain_text',
'no_git',
'no_node_modules',
'no_patch_files',
'no_svn',
'no_vendor',
],
Expand Down
1 change: 1 addition & 0 deletions src/Normalization/SpaceBeforeTabNormalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'plain_text',
'no_git',
'no_node_modules',
'no_patch_files',
'no_svn',
'no_vendor',
],
Expand Down
1 change: 1 addition & 0 deletions src/Normalization/TrailingWhitespaceNormalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'plain_text',
'no_git',
'no_node_modules',
'no_patch_files',
'no_svn',
'no_vendor',
],
Expand Down
1 change: 1 addition & 0 deletions tests/NormalizatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected function setUp(): void
'indentation.php',
'leadingEol.php',
'middleEol.php',
'miscellaneous.php',
'name.php',
'permissions.php',
'spaceBeforeTab.php',
Expand Down
58 changes: 58 additions & 0 deletions tests/Unit/Filter/NoPatchFilesFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Normalizator\Tests\Unit\Filter;

use Normalizator\Finder\Finder;
use Normalizator\Tests\NormalizatorTestCase;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\CoversNothing;

/**
* @internal
*/
#[CoversNothing]
final class NoPatchFilesFilterTest extends NormalizatorTestCase
{
public function testFilter(): void
{
$structure = [
'testing-project' => [
'patches' => [
'patch_1.patch' => '',
'patch_2.PATCH' => '',
'patch_3.PaTcH' => '',
'patch_4.diff' => '',
'patch_5.DIFF' => '',
'patch_6.DifF' => '',
],
'src' => [],
'file.php' => '',
],
];

vfsStream::create($structure);

$valid = [
'vfs://virtual/testing-project' => true,
'vfs://virtual/testing-project/patches' => true,
'vfs://virtual/testing-project/patches/patch_1.patch' => false,
'vfs://virtual/testing-project/patches/patch_2.PATCH' => false,
'vfs://virtual/testing-project/patches/patch_3.PaTcH' => false,
'vfs://virtual/testing-project/patches/patch_4.diff' => false,
'vfs://virtual/testing-project/patches/patch_5.DIFF' => false,
'vfs://virtual/testing-project/patches/patch_6.DifF' => false,
'vfs://virtual/testing-project/src' => true,
'vfs://virtual/testing-project/file.php' => true,
];

$filter = $this->createFilter('no_patch_files');

$finder = new Finder();

foreach ($finder->getTree('vfs://virtual/testing-project') as $key => $file) {
$this->assertSame($valid[$key], $filter->filter($file));
}
}
}
2 changes: 2 additions & 0 deletions tests/Unit/FilterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Normalizator\Filter\NoGitFilter;
use Normalizator\Filter\NoLinksFilter;
use Normalizator\Filter\NoNodeModulesFilter;
use Normalizator\Filter\NoPatchFilesFilter;
use Normalizator\Filter\NoSvnFilter;
use Normalizator\Filter\NoVendorFilter;
use Normalizator\Filter\PlainTextFilter;
Expand Down Expand Up @@ -44,6 +45,7 @@ public static function dataProvider(): array
['no_git', NoGitFilter::class],
['no_links', NoLinksFilter::class],
['no_node_modules', NoNodeModulesFilter::class],
['no_patch_files', NoPatchFilesFilter::class],
['no_svn', NoSvnFilter::class],
['no_vendor', NoVendorFilter::class],
['plain_text', PlainTextFilter::class],
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Normalization/SpaceBeforeTabNormalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ final class SpaceBeforeTabNormalizationTest extends NormalizatorTestCase
public function testNormalize(string $filename): void
{
$normalization = $this->createNormalization('space_before_tab');
$file = new File('vfs://' . $this->virtualRoot->getChild('initial/space-before-tab/' . $filename)->path());
$file = new File('vfs://' . $this->virtualRoot->getChild('initial/' . $filename)->path());
$file = $normalization->normalize($file);
$file->save();

$this->assertFileEquals('vfs://' . $this->virtualRoot->getChild('fixed/space-before-tab/' . $filename)->path(), $file->getPathname());
$this->assertFileEquals('vfs://' . $this->virtualRoot->getChild('fixed/' . $filename)->path(), $file->getPathname());
}

/**
Expand All @@ -32,7 +32,8 @@ public function testNormalize(string $filename): void
public static function dataProvider(): array
{
return [
['spaceBeforeTab.php'],
['space-before-tab/spaceBeforeTab.php'],
['miscellaneous/file_1.patch'],
];
}
}
51 changes: 26 additions & 25 deletions tests/Unit/Normalization/TrailingWhitespaceNormalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ final class TrailingWhitespaceNormalizationTest extends NormalizatorTestCase
public function testNormalize(string $filename): void
{
$normalization = $this->createNormalization('trailing_whitespace');
$file = new File('vfs://' . $this->virtualRoot->getChild('initial/trailing-whitespace/' . $filename)->path());
$file = new File('vfs://' . $this->virtualRoot->getChild('initial/' . $filename)->path());
$file = $normalization->normalize($file);
$file->save();

$this->assertFileEquals('vfs://virtual/fixed/trailing-whitespace/' . $filename, $file->getPathname());
$this->assertFileEquals('vfs://virtual/fixed/' . $filename, $file->getPathname());
}

/**
Expand All @@ -32,29 +32,30 @@ public function testNormalize(string $filename): void
public static function dataProvider(): array
{
return [
['fileWithTrailingWhitespace.php'],
['no-break-space.txt'],
['mongolian-vowel-separator.txt'],
['en-quad.txt'],
['em-quad.txt'],
['en-space.txt'],
['em-space.txt'],
['three-per-em-space.txt'],
['four-per-em-space.txt'],
['six-per-em-space.txt'],
['figure-space.txt'],
['punctuation-space.txt'],
['thin-space.txt'],
['hair-space.txt'],
['narrow-no-break-space.txt'],
['medium-mathematical-space.txt'],
['ideographic-space.txt'],
['zero-width-space.txt'],
['zero-width-no-break-space.txt'],
['various.txt'],
['various-crlf.txt'],
['various-cr.txt'],
['various-mixed-eol.txt'],
['trailing-whitespace/fileWithTrailingWhitespace.php'],
['trailing-whitespace/no-break-space.txt'],
['trailing-whitespace/mongolian-vowel-separator.txt'],
['trailing-whitespace/en-quad.txt'],
['trailing-whitespace/em-quad.txt'],
['trailing-whitespace/en-space.txt'],
['trailing-whitespace/em-space.txt'],
['trailing-whitespace/three-per-em-space.txt'],
['trailing-whitespace/four-per-em-space.txt'],
['trailing-whitespace/six-per-em-space.txt'],
['trailing-whitespace/figure-space.txt'],
['trailing-whitespace/punctuation-space.txt'],
['trailing-whitespace/thin-space.txt'],
['trailing-whitespace/hair-space.txt'],
['trailing-whitespace/narrow-no-break-space.txt'],
['trailing-whitespace/medium-mathematical-space.txt'],
['trailing-whitespace/ideographic-space.txt'],
['trailing-whitespace/zero-width-space.txt'],
['trailing-whitespace/zero-width-no-break-space.txt'],
['trailing-whitespace/various.txt'],
['trailing-whitespace/various-crlf.txt'],
['trailing-whitespace/various-cr.txt'],
['trailing-whitespace/various-mixed-eol.txt'],
['miscellaneous/file_1.patch'],
];
}
}
55 changes: 55 additions & 0 deletions tests/fixtures/miscellaneous.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Miscellaneous fixtures.
*/

declare(strict_types=1);

$structure = [
'initial' => [
'miscellaneous' => [],
],
'fixed' => [
'miscellaneous' => [],
],
];

$patch_1 = <<<'EOL'
diff --git a/README.md b/README.md
index 802d433..dde8419 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,25 @@
# Hello, World
-Lorem ipsum dolor sit amet.
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pharetra dui
+euismod posuere fermentum. Etiam vitae neque ut justo venenatis interdum sit
+amet sed nulla. Etiam a auctor quam. Vivamus cursus augue purus, vitae euismod
+nunc porta nec. Praesent condimentum arcu ac facilisis sodales. In tincidunt
+pretium malesuada. Nunc ut erat quis dolor aliquet tristique in ut nisi.
+
+
+Suspendisse a ultrices neque. Pellentesque faucibus, sapien non lobortis
+venenatis, diam turpis eleifend orci, ut congue odio ligula a lectus. In
+finibus, quam non volutpat sagittis, arcu sem sodales ligula, a aliquam turpis
+urna eget sapien.
```sh
echo "Hello, World
```
+
+Proin congue, augue auctor maximus facilisis, libero purus bibendum dolor, non
+pulvinar sapien metus condimentum ante. Nulla rhoncus molestie condimentum.
+
+```Makefile
+build:
+ gcc src.c
+```
EOL;

$structure['initial']['miscellaneous']['file_1.patch'] = $patch_1;
$structure['fixed']['miscellaneous']['file_1.patch'] = $patch_1;

return $structure;

0 comments on commit 631face

Please sign in to comment.