-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathVirtualFileSystemManagerTests.cs
More file actions
85 lines (70 loc) · 2.79 KB
/
Copy pathVirtualFileSystemManagerTests.cs
File metadata and controls
85 lines (70 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.IO;
using System.Threading.Tasks;
using Shouldly;
using ManagedCode.Storage.Core;
using ManagedCode.Storage.FileSystem;
using ManagedCode.Storage.FileSystem.Options;
using ManagedCode.Storage.VirtualFileSystem.Core;
using ManagedCode.Storage.VirtualFileSystem.Extensions;
using ManagedCode.Storage.VirtualFileSystem.Options;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace ManagedCode.Storage.Tests.VirtualFileSystem;
public class VirtualFileSystemManagerTests : IAsyncLifetime
{
private readonly string _basePath = Path.Combine(Directory.GetCurrentDirectory(), "managedcode-vfs-manager", Guid.NewGuid().ToString());
private ServiceProvider _serviceProvider = null!;
private IStorage _storage = null!;
public async Task InitializeAsync()
{
Directory.CreateDirectory(_basePath);
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IStorage>(_ => new FileSystemStorage(new FileSystemStorageOptions
{
BaseFolder = _basePath,
CreateContainerIfNotExists = true
}));
services.AddVirtualFileSystem(options =>
{
options.DefaultContainer = string.Empty;
options.EnableCache = true;
});
_serviceProvider = services.BuildServiceProvider();
_storage = _serviceProvider.GetRequiredService<IStorage>();
await _storage.CreateContainerAsync();
}
public async Task DisposeAsync()
{
if (_serviceProvider.GetService<IVirtualFileSystemManager>() is IAsyncDisposable asyncManager)
{
await asyncManager.DisposeAsync();
}
(_storage as IDisposable)?.Dispose();
await _serviceProvider.DisposeAsync();
if (Directory.Exists(_basePath))
{
Directory.Delete(_basePath, recursive: true);
}
}
[Fact]
public async Task MountAndResolvePaths_ShouldWork()
{
var manager = _serviceProvider.GetRequiredService<IVirtualFileSystemManager>();
await manager.MountAsync("/fs", _storage, new VfsOptions { DefaultContainer = string.Empty });
var vfs = manager.GetMount("/fs");
var file = await vfs.GetFileAsync(new VfsPath("/sample.txt"));
await file.WriteAllTextAsync("manager-test");
var (mountPoint, relativePath) = manager.ResolvePath("/fs/sample.txt");
mountPoint.ShouldBe("/fs");
relativePath.Value.ShouldBe("/sample.txt");
var mounts = manager.GetMounts();
mounts.ShouldContainKey("/fs");
await manager.UnmountAsync("/fs");
mounts = manager.GetMounts();
mounts.ShouldBeEmpty();
Func<IVirtualFileSystem> action = () => manager.GetMount("/fs");
Should.Throw<ArgumentException>(action);
}
}