-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathFileSystem.cs
55 lines (45 loc) · 1.28 KB
/
FileSystem.cs
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
namespace k8s
{
internal static class FileSystem
{
public interface IFileSystem
{
Stream OpenRead(string path);
bool Exists(string path);
string ReadAllText(string path);
}
public static IFileSystem Current { get; private set; } = new RealFileSystem();
public static IDisposable With(IFileSystem fileSystem)
{
return new InjectedFileSystem(fileSystem);
}
private class InjectedFileSystem : IDisposable
{
private readonly IFileSystem _original;
public InjectedFileSystem(IFileSystem fileSystem)
{
_original = Current;
Current = fileSystem;
}
public void Dispose()
{
Current = _original;
}
}
private class RealFileSystem : IFileSystem
{
public bool Exists(string path)
{
return File.Exists(path);
}
public Stream OpenRead(string path)
{
return File.OpenRead(path);
}
public string ReadAllText(string path)
{
return File.ReadAllText(path);
}
}
}
}