Skip to content

Commit

Permalink
fix removing files in a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Jul 14, 2023
1 parent fc9f763 commit a78a3c6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 6.5.1 - 2023-07-14

### Fixed

- Deleting a file in a directory for the adapter `InMemory::emulateFilesystem()` wasn't applied

## 6.5.0 - 2023-07-09

### Changed
Expand Down
2 changes: 2 additions & 0 deletions properties/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static function all(): array
Adapter\AddDirectoryFromAnotherAdapterWithFileRemoved::class,
Adapter\RemoveUnknownFile::class,
Adapter\RemoveFile::class,
Adapter\RemoveFileInDirectory::class,
Adapter\AllRootFilesAreAccessible::class,
Adapter\AccessingUnknownFileReturnsNothing::class,
Adapter\AddDirectory::class,
Expand All @@ -61,6 +62,7 @@ public static function alwaysApplicable(): array
return [
Adapter\RemoveUnknownFile::class,
Adapter\RemoveFile::class,
Adapter\RemoveFileInDirectory::class,
Adapter\AllRootFilesAreAccessible::class,
Adapter\ReAddingFilesHasNoSideEffect::class,
Adapter\RootDirectoryIsNamedRoot::class,
Expand Down
65 changes: 65 additions & 0 deletions properties/Adapter/RemoveFileInDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
declare(strict_types = 1);

namespace Properties\Innmind\Filesystem\Adapter;

use Innmind\Filesystem\{
Adapter,
File,
Name,
Directory\Directory,
};
use Innmind\BlackBox\{
Property,
Set,
Runner\Assert,
};
use Fixtures\Innmind\Filesystem\{
File as FFile,
Name as FName,
};

/**
* @implements Property<Adapter>
*/
final class RemoveFileInDirectory implements Property
{
private File $file;
private Name $name;

public function __construct(File $file, Name $name)
{
$this->file = $file;
$this->name = $name;
}

public static function any(): Set
{
return Set\Composite::immutable(
static fn(...$args) => new self(...$args),
FFile::any(),
FName::any(),
);
}

public function applicableTo(object $adapter): bool
{
return true;
}

public function ensureHeldBy(Assert $assert, object $adapter): object
{
$assert->null($adapter->add(Directory::of($this->name)->add($this->file)));
$assert->null($adapter->add(Directory::of($this->name)->remove($this->file->name())));
$assert->false(
$adapter
->get($this->name)
->match(
fn($directory) => $directory->contains($this->file->name()),
static fn() => null,
),
);

return $adapter;
}
}
8 changes: 8 additions & 0 deletions src/Adapter/InMemory/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function __invoke(Directory $parent, File $file): Directory

private function merge(Directory $existing, Directory $new): Directory
{
$existing = $new
->removed()
->filter(static fn($name) => !$new->contains($name))
->reduce(
$existing,
static fn(Directory $existing, $name) => $existing->remove($name),
);

return $new->reduce(
$existing,
fn(Directory $directory, $file) => $this($directory, $file),
Expand Down

0 comments on commit a78a3c6

Please sign in to comment.