Skip to content

Commit

Permalink
Merge pull request #4 from faissaloux/deep-scan
Browse files Browse the repository at this point in the history
Scan deep directories
  • Loading branch information
faissaloux authored Oct 11, 2024
2 parents 89d1df8 + 07f4d75 commit 60a688f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 11 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,23 @@ expect('notlowercase.php')->toReturnLowercase();

### Scan directory

You can scan all directory files at once
```bash
directory
├── file.js
├── file.php
├── subdirectory
├── file.json
├── file1.php
├── file2.php
```

- To scan all the php files in `directory` and all its subdirectories (`file.php`, `file1.php` and `file2.php`), we can use:
```php
expect('directory')->toReturnLowercase();
```
```

- We can also specify the scan depth using `depth`.
```php
expect('directory')->toReturnLowercase(depth:0);
```
In this case it will only scan direct php file which is `file.php`.
10 changes: 2 additions & 8 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@

expect()->extend(
'toReturnLowercase',
function (): PestExpectation {
function (int $depth = -1): PestExpectation {
$files = [$this->value];

if (is_dir($files[0])) {
$directory = $files[0];

if ($files = scandir($files[0])) {
$files = array_diff($files, ['.', '..']);
$files = array_filter($files, 'isPhp');
$files = array_map(fn (string $file): string => $directory.DIRECTORY_SEPARATOR.$file, $files);
}
$files = getFilesIn($files[0], $depth);

if ($files === []) {
expect(true)->toBeTrue();
Expand Down
38 changes: 38 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
<?php

function isDirOrPhp(string $maybeFile): bool
{
return isDir($maybeFile) || isPhp($maybeFile);
}

function isDir(string $maybeDir): bool
{
return ! str_contains($maybeDir, '.');
}

function isPhp(string $file): bool
{
$exploded = explode('.', $file);

return end($exploded) === 'php';
}

/**
* @return array<string>
*/
function getFilesIn(string $directory, int $depth = -1): array
{
$allFiles = [];

if ($files = scandir($directory)) {
$files = array_diff($files, ['.', '..']);
$files = array_filter($files, 'isDirOrPhp');

foreach ($files as $file) {
if (isDir($file)) {
if ($depth !== 0) {
$deeperFiles = getFilesIn($directory.DIRECTORY_SEPARATOR.$file, $depth < 0 ? $depth : $depth - 1);
$allFiles = array_merge($deeperFiles, $allFiles);
}
} else {
$allFiles[] = $directory.DIRECTORY_SEPARATOR.$file;
}
}

return $allFiles;
}

return [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

return [
'',
'f@issa!oux',
'pest',
'plugin',
'inside',
'lowercase',
'loWer',
'case',
];
27 changes: 27 additions & 0 deletions tests/Unit/helpers/getFilesIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

uses()->group('helpers');

it('gets all php files in directory and subdirectories', function (): void {
$files = getFilesIn('tests/Fixtures');

expect($files)->toBeArray()->toHaveCount(7);
});

it('gets all direct php files in directory', function (): void {
$files = getFilesIn('tests/Fixtures', depth: 0);

expect($files)->toBeArray()->toHaveCount(2);
});

it('gets all php files in directory depth 1', function (): void {
$files = getFilesIn('tests/Fixtures', depth: 1);

expect($files)->toBeArray()->toHaveCount(6);
});

it('gets all php files in directory and subdirectories on negative depth', function (): void {
$files = getFilesIn('tests/Fixtures', depth: -4);

expect($files)->toBeArray()->toHaveCount(7);
});
7 changes: 6 additions & 1 deletion tests/toReturnLowercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

it('passes when all directory files content are lowercase', function (): void {
expect('tests/Fixtures/shouldAllBeLowercase')
->toReturnLowercase();
->toReturnLowercase(depth: 0);
});

it('fails', function (): void {
Expand All @@ -46,3 +46,8 @@
expect('tests/Fixtures/shouldAllBeLowercase1')
->toReturnLowercase();
})->throws(ExpectationFailedException::class);

it('fails when not all subdirectories files content are lowercase', function (): void {
expect('tests/Fixtures/shouldAllBeLowercase')
->toReturnLowercase();
})->throws(ExpectationFailedException::class);

0 comments on commit 60a688f

Please sign in to comment.