forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestFileSystem.cs
104 lines (87 loc) · 2.32 KB
/
TestFileSystem.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using GitVersion.Helpers;
public class TestFileSystem : IFileSystem
{
Dictionary<string, string> fileSystem = new Dictionary<string, string>();
public void Copy(string @from, string to, bool overwrite)
{
if (fileSystem.ContainsKey(to))
{
if (overwrite)
fileSystem.Remove(to);
else
throw new IOException("File already exists");
}
string source;
if (!fileSystem.TryGetValue(from, out source))
throw new FileNotFoundException(string.Format("The source file '{0}' was not found", from), from);
fileSystem.Add(to, source);
}
public void Move(string @from, string to)
{
Copy(from, to, false);
fileSystem.Remove(from);
}
public bool Exists(string file)
{
return fileSystem.ContainsKey(file);
}
public void Delete(string path)
{
fileSystem.Remove(path);
}
public string ReadAllText(string path)
{
return fileSystem[path];
}
public void WriteAllText(string file, string fileContents)
{
if (fileSystem.ContainsKey(file))
{
fileSystem[file] = fileContents;
}
else
{
fileSystem.Add(file, fileContents);
}
}
public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
{
throw new NotImplementedException();
}
public Stream OpenWrite(string path)
{
return new TestStream(path, this);
}
public Stream OpenRead(string path)
{
if (fileSystem.ContainsKey(path))
{
var content = fileSystem[path];
return new MemoryStream(Encoding.UTF8.GetBytes(content));
}
throw new FileNotFoundException("File not found.", path);
}
public void CreateDirectory(string path)
{
if (fileSystem.ContainsKey(path))
{
fileSystem[path] = "";
}
else
{
fileSystem.Add(path, "");
}
}
public bool DirectoryExists(string path)
{
return fileSystem.ContainsKey(path);
}
public long GetLastDirectoryWrite(string path)
{
return 1;
}
}