Skip to content

Commit e80ce0b

Browse files
committed
explicit the fact that mounting a filesystem may fail
1 parent b42e8dd commit e80ce0b

7 files changed

Lines changed: 40 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `Innmind\OperatingSystem\Config::withHttpHeartbeat()` period is now expressed with a `Innmind\TimeContinuum\Period`
1717
- `Innmind\OperatingSystem\CurrentProcess::id()` now returns an `Innmind\Immutable\Attempt`
1818
- `Innmind\OperatingSystem\CurrentProcess::halt()` now returns `Innmind\Immutable\Attempt<Innmind\Immutable\SideEffect>`
19+
- `Innmind\OperatingSystem\Filesystem::mount()` now returns an `Innmind\Immutable\Attempt`
1920
- `Innmind\OperatingSystem\Filesystem::temporary()` now returns an `Innmind\Immutable\Attempt`
2021
- `Innmind\OperatingSystem\Ports::open()` now returns an `Innmind\Immutable\Attempt`
2122
- `Innmind\OperatingSystem\Remote::socket()` now returns an `Innmind\Immutable\Attempt`

src/Filesystem.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
interface Filesystem
2020
{
21-
public function mount(Path $path): Adapter;
21+
/**
22+
* @return Attempt<Adapter>
23+
*/
24+
public function mount(Path $path): Attempt;
2225
public function contains(Path $path): bool;
2326

2427
/**

src/Filesystem/Generic.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,30 @@ public static function of(Processes $processes, Config $config): self
4848
}
4949

5050
#[\Override]
51-
public function mount(Path $path): Adapter
51+
public function mount(Path $path): Attempt
5252
{
5353
/**
5454
* @var Adapter $adapter
5555
* @var string $mounted
5656
*/
5757
foreach ($this->mounted as $adapter => $mounted) {
5858
if ($path->toString() === $mounted) {
59-
return $adapter;
59+
return Attempt::result($adapter);
6060
}
6161
}
6262

63-
$adapter = Adapter\Filesystem::mount(
64-
$path,
65-
$this->config->io(),
66-
)
67-
->withCaseSensitivity(
68-
$this->config->filesystemCaseSensitivity(),
69-
);
70-
$this->mounted[$adapter] = $path->toString();
71-
72-
return $adapter;
63+
return Attempt::of(function() use ($path) {
64+
$adapter = Adapter\Filesystem::mount(
65+
$path,
66+
$this->config->io(),
67+
)
68+
->withCaseSensitivity(
69+
$this->config->filesystemCaseSensitivity(),
70+
);
71+
$this->mounted[$adapter] = $path->toString();
72+
73+
return $adapter;
74+
});
7375
}
7476

7577
#[\Override]

src/Filesystem/Logger.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ public static function psr(
3838
}
3939

4040
#[\Override]
41-
public function mount(Path $path): Adapter
41+
public function mount(Path $path): Attempt
4242
{
43-
return Adapter\Logger::psr(
44-
$this->filesystem->mount($path),
45-
$this->logger,
43+
return $this->filesystem->mount($path)->map(
44+
fn($adapter) => Adapter\Logger::psr(
45+
$adapter,
46+
$this->logger,
47+
),
4648
);
4749
}
4850

tests/FactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public function testPersistingFileOnCaseInsensitiveFilesystem()
4949
$os = Factory::build(Config::of()->caseInsensitiveFilesystem());
5050
$adapter = $os
5151
->filesystem()
52-
->mount(Path::of($path));
52+
->mount(Path::of($path))
53+
->unwrap();
5354
$adapter->add(
5455
$directory = Directory::named('0')
5556
->add($file = File::named('L', Content::none()))

tests/Filesystem/GenericTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testMount()
4949
Config::of(),
5050
);
5151

52-
$adapter = $filesystem->mount(Path::of('/tmp/'));
52+
$adapter = $filesystem->mount(Path::of('/tmp/'))->unwrap();
5353

5454
$this->assertInstanceOf(FilesystemAdapter::class, $adapter);
5555
}
@@ -61,9 +61,9 @@ public function testMountingTheSamePathTwiceReturnsTheSameAdapter()
6161
Config::of(),
6262
);
6363

64-
$adapter = $filesystem->mount(Path::of('/tmp/'));
64+
$adapter = $filesystem->mount(Path::of('/tmp/'))->unwrap();
6565

66-
$this->assertSame($adapter, $filesystem->mount(Path::of('/tmp/')));
66+
$this->assertSame($adapter, $filesystem->mount(Path::of('/tmp/'))->unwrap());
6767
}
6868

6969
public function testContainsFile()

tests/Filesystem/LoggerTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
FileWatch\Ping,
1515
};
1616
use Innmind\TimeWarp\Halt;
17-
use Innmind\Immutable\Maybe;
17+
use Innmind\Immutable\{
18+
Attempt,
19+
Maybe,
20+
};
1821
use Psr\Log\LoggerInterface;
1922
use PHPUnit\Framework\TestCase;
2023
use Innmind\BlackBox\{
@@ -47,11 +50,15 @@ public function testMount()
4750
$inner
4851
->expects($this->once())
4952
->method('mount')
50-
->with($path);
53+
->with($path)
54+
->willReturn(Attempt::result($this->createMock(Adapter::class)));
5155
$logger = $this->createMock(LoggerInterface::class);
5256
$filesystem = Logger::psr($inner, $logger);
5357

54-
$this->assertInstanceOf(Adapter\Logger::class, $filesystem->mount($path));
58+
$this->assertInstanceOf(
59+
Adapter\Logger::class,
60+
$filesystem->mount($path)->unwrap(),
61+
);
5562
});
5663
}
5764

0 commit comments

Comments
 (0)