-
-
Notifications
You must be signed in to change notification settings - Fork 891
Expand file tree
/
Copy pathTestFileSystem.cs
More file actions
58 lines (48 loc) · 1.62 KB
/
TestFileSystem.cs
File metadata and controls
58 lines (48 loc) · 1.62 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
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable enable
namespace SixLabors.ImageSharp.Tests;
/// <summary>
/// A test image file.
/// </summary>
public class TestFileSystem : ImageSharp.IO.IFileSystem
{
private readonly Dictionary<string, Func<Stream>> fileSystem = new(StringComparer.OrdinalIgnoreCase);
public void AddFile(string path, Func<Stream> data)
{
lock (this.fileSystem)
{
this.fileSystem.Add(path, data);
}
}
public Stream Create(string path) => this.GetStream(path) ?? File.Create(path);
public Stream CreateAsynchronous(string path) => this.GetStream(path) ?? File.Open(path, new FileStreamOptions
{
Mode = FileMode.Create,
Access = FileAccess.ReadWrite,
Share = FileShare.None,
Options = FileOptions.Asynchronous,
});
public Stream OpenRead(string path) => this.GetStream(path) ?? File.OpenRead(path);
public Stream OpenReadAsynchronous(string path) => this.GetStream(path) ?? File.Open(path, new FileStreamOptions
{
Mode = FileMode.Open,
Access = FileAccess.Read,
Share = FileShare.Read,
Options = FileOptions.Asynchronous,
});
private Stream? GetStream(string path)
{
// if we have injected a fake file use it instead
lock (this.fileSystem)
{
if (this.fileSystem.TryGetValue(path, out Func<Stream>? streamFactory))
{
Stream stream = streamFactory();
stream.Position = 0;
return stream;
}
}
return null;
}
}