From 0331a87be172c1e4d59264318abfab4ca80d71b4 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sun, 14 Jul 2024 23:16:04 +0100 Subject: [PATCH] feat: adds `toHaveFileSystemPermissions` expectation --- Features.md | 1 + composer.json | 2 +- src/ArchPresets/Base.php | 2 +- src/ArchPresets/Security.php | 6 ++++++ src/Expectation.php | 13 +++++++++++++ src/Expectations/OppositeExpectation.php | 13 +++++++++++++ tests/.snapshots/success.txt | 7 ++++++- .../Expect/toHaveFileSystemPermissions.php | 16 ++++++++++++++++ tests/Visual/Parallel.php | 2 +- 9 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 tests/Features/Expect/toHaveFileSystemPermissions.php diff --git a/Features.md b/Features.md index f7555867..0cf36e84 100644 --- a/Features.md +++ b/Features.md @@ -3,4 +3,5 @@ 3. `pest()->` 4. `->notes()`, `->issue`, `->pr`, etc 5. `arch()->preset` +6. `toHaveFileSystemPermissions` diff --git a/composer.json b/composer.json index 5675fd0e..9ca86360 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "nunomaduro/termwind": "^2.0.1", "pestphp/pest-plugin": "^3.0.0", "pestphp/pest-plugin-arch": "^3.0.0", - "phpunit/phpunit": "^11.2.6" + "phpunit/phpunit": "^11.2.7" }, "conflict": { "sebastian/exporter": "<6.0.0", diff --git a/src/ArchPresets/Base.php b/src/ArchPresets/Base.php index e616fcb7..6723904b 100644 --- a/src/ArchPresets/Base.php +++ b/src/ArchPresets/Base.php @@ -20,7 +20,7 @@ final class Base extends AbstractPreset 'debug_print_backtrace', 'dump', 'ray', - 'ds', + 'ds', 'die', 'goto', 'global', diff --git a/src/ArchPresets/Security.php b/src/ArchPresets/Security.php index c7142748..57532736 100644 --- a/src/ArchPresets/Security.php +++ b/src/ArchPresets/Security.php @@ -37,5 +37,11 @@ final class Security extends AbstractPreset 'dl', 'assert', ])->not->toBeUsed(); + + foreach ($this->userNamespaces as $namespace) { + $this->expectations[] = expect($namespace) + ->not + ->toHaveFileSystemPermissions('0777'); + } } } diff --git a/src/Expectation.php b/src/Expectation.php index 2d1e7b8b..ce108428 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -434,6 +434,19 @@ final class Expectation return ToUse::make($this, $targets); } + /** + * Asserts that the given expectation target does have the given permissions + */ + public function toHaveFileSystemPermissions(string $permissions): ArchExpectation + { + return Targeted::make( + $this, + fn (ObjectDescription $object): bool => substr(sprintf('%o', fileperms($object->path)), -4) === $permissions, + sprintf('permissions to be [%s]', $permissions), + FileLineFinder::where(fn (string $line): bool => str_contains($line, 'original, + fn (ObjectDescription $object): bool => substr(sprintf('%o', fileperms($object->path)), -4) !== $permissions, + sprintf('permissions not to be [%s]', $permissions), + FileLineFinder::where(fn (string $line): bool => str_contains($line, 'toHaveFileSystemPermissions('0644') + ->and('Pest')->not->toHaveFileSystemPermissions('0777'); +}); + +test('failures', function () { + expect(Pest\Preset::class)->toHaveFileSystemPermissions('0755'); +})->throws(ArchExpectationFailedException::class, "Expecting 'src/Preset.php' permissions to be [0755]."); + +test('not failures', function () { + expect(Pest\Preset::class)->not->toHaveFileSystemPermissions('0644'); +})->throws(ArchExpectationFailedException::class, "Expecting 'src/Preset.php' permissions not to be [0644]."); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 52eebf40..f29178c0 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -16,7 +16,7 @@ $run = function () { test('parallel', function () use ($run) { expect($run('--exclude-group=integration')) - ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1049 passed (2570 assertions)') + ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1052 passed (2579 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows();