diff --git a/src/Filter.php b/src/Filter.php deleted file mode 100644 index eadb914..0000000 --- a/src/Filter.php +++ /dev/null @@ -1,35 +0,0 @@ -setAccessible(true); - - $this->callback = function () use ($it, $filter, $re) { - return $re->invoke($filter, $it->current()); - }; - } - - public function accept() : bool - { - return ($this->callback)(); - } -} diff --git a/src/Iterator.php b/src/Iterator.php index eefbfd7..fb62020 100644 --- a/src/Iterator.php +++ b/src/Iterator.php @@ -2,15 +2,17 @@ namespace DiffSniffer; -use EmptyIterator; +use function iterator_to_array; use IteratorIterator; use PHP_CodeSniffer\Config; use PHP_CodeSniffer\Files\DummyFile; use PHP_CodeSniffer\Files\File; -use PHP_CodeSniffer\Filters\Filter as BaseFilter; +use PHP_CodeSniffer\Filters\Filter; use PHP_CodeSniffer\Ruleset; use RecursiveArrayIterator; use Traversable; +use const DIRECTORY_SEPARATOR; +use function str_replace; /** * Changeset iterator @@ -47,15 +49,28 @@ public function __construct(Traversable $files, Changeset $changeSet, Ruleset $r public function getIterator() : Traversable { - $it = new IteratorIterator($this->files); - $it = new Filter($it, new BaseFilter( - new RecursiveArrayIterator( - new EmptyIterator() - ), - '', + // PHP_CodeSniffer expects file paths to contain the native directory separator on Windows when matching them + // against the exclude pattern but Git and GitHub REST API will return forward slashes regardless of the OS + if (DIRECTORY_SEPARATOR === '\\') { + $it = (function () : iterable { + foreach ($this->files as $file) { + yield str_replace('/', DIRECTORY_SEPARATOR, $file); + } + })(); + } else { + $it = new IteratorIterator($this->files); + } + + $it = new RecursiveArrayIterator( + iterator_to_array($it) + ); + + $it = new Filter( + $it, + "\0", $this->config, $this->ruleSet - )); + ); foreach ($it as $path) { yield $this->createFile( diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index 613941a..c4e349d 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -7,6 +7,7 @@ use DiffSniffer\Command; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use const DIRECTORY_SEPARATOR; class ApplicationTest extends TestCase { @@ -16,9 +17,9 @@ class ApplicationTest extends TestCase */ public function testUseCase(string $useCase, int $expectedExitCode) { - $dir = __DIR__ . '/fixtures/' . $useCase; + $dir = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . $useCase; $changeset = new FixtureChangeset($dir); - chdir($dir . '/tree'); + chdir($dir . DIRECTORY_SEPARATOR . 'tree'); /** @var Command|MockObject $command */ $command = $this->createMock(Command::class); @@ -27,7 +28,7 @@ public function testUseCase(string $useCase, int $expectedExitCode) ->willReturn($changeset); $app = new Application(); - $expectedOutput = file_get_contents($dir . '/output.txt'); + $expectedOutput = file_get_contents($dir . DIRECTORY_SEPARATOR . 'output.txt'); $expectedOutput = str_replace("\n", PHP_EOL, $expectedOutput); $this->expectOutputString($expectedOutput); @@ -47,6 +48,10 @@ public static function useCaseProvider() 'excluded-rule-cache', 0, ], + 'exclude-pattern' => [ + 'exclude-pattern', + 1, + ], ]; } } diff --git a/tests/FixtureChangeset.php b/tests/FixtureChangeset.php index 1869bf0..552bb83 100644 --- a/tests/FixtureChangeset.php +++ b/tests/FixtureChangeset.php @@ -3,6 +3,7 @@ namespace DiffSniffer\Tests; use DiffSniffer\Changeset; +use const DIRECTORY_SEPARATOR; /** * Fixture changeset @@ -29,7 +30,7 @@ public function __construct(string $dir) */ public function getDiff() : string { - return file_get_contents($this->dir . '/changeset.diff'); + return file_get_contents($this->dir . DIRECTORY_SEPARATOR . 'changeset.diff'); } /** diff --git a/tests/fixtures/exclude-pattern/changeset.diff b/tests/fixtures/exclude-pattern/changeset.diff new file mode 100644 index 0000000..78901f5 --- /dev/null +++ b/tests/fixtures/exclude-pattern/changeset.diff @@ -0,0 +1,18 @@ +diff --git a/main.php b/main.php +`--- /dev/null ++++ b/main.php +@@ -0,0 +1,5 @@ ++ + + excluded/* + +