forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFileSystem.cs
More file actions
130 lines (111 loc) · 4 KB
/
TestFileSystem.cs
File metadata and controls
130 lines (111 loc) · 4 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using GitVersion;
using GitVersion.Helpers;
namespace GitVersionCore.Tests.Helpers
{
public class TestFileSystem : IFileSystem
{
private readonly Dictionary<string, byte[]> fileSystem = new Dictionary<string, byte[]>(StringComparerUtils.OsDependentComparer);
public void Copy(string from, string to, bool overwrite)
{
var fromPath = Path.GetFullPath(from);
var toPath = Path.GetFullPath(to);
if (fileSystem.ContainsKey(toPath))
{
if (overwrite)
fileSystem.Remove(toPath);
else
throw new IOException("File already exists");
}
if (!fileSystem.TryGetValue(fromPath, out var source))
throw new FileNotFoundException($"The source file '{fromPath}' was not found", from);
fileSystem.Add(toPath, source);
}
public void Move(string from, string to)
{
var fromPath = Path.GetFullPath(from);
Copy(from, to, false);
fileSystem.Remove(fromPath);
}
public bool Exists(string file)
{
var path = Path.GetFullPath(file);
return fileSystem.ContainsKey(path);
}
public void Delete(string path)
{
var fullPath = Path.GetFullPath(path);
fileSystem.Remove(fullPath);
}
public string ReadAllText(string file)
{
var path = Path.GetFullPath(file);
if (!fileSystem.TryGetValue(path, out var content))
throw new FileNotFoundException($"The file '{path}' was not found", path);
var encoding = EncodingHelper.DetectEncoding(content) ?? Encoding.UTF8;
return encoding.GetString(content);
}
public void WriteAllText(string file, string fileContents)
{
var path = Path.GetFullPath(file);
var encoding = fileSystem.ContainsKey(path)
? EncodingHelper.DetectEncoding(fileSystem[path]) ?? Encoding.UTF8
: Encoding.UTF8;
WriteAllText(path, fileContents, encoding);
}
public void WriteAllText(string file, string fileContents, Encoding encoding)
{
var path = Path.GetFullPath(file);
fileSystem[path] = encoding.GetBytes(fileContents);
}
public IEnumerable<string> DirectoryEnumerateFiles(string directory, string searchPattern, SearchOption searchOption)
{
throw new NotImplementedException();
}
public Stream OpenWrite(string path)
{
return new TestStream(path, this);
}
public Stream OpenRead(string file)
{
var path = Path.GetFullPath(file);
if (fileSystem.ContainsKey(path))
{
var content = fileSystem[path];
return new MemoryStream(content);
}
throw new FileNotFoundException("File not found.", path);
}
public void CreateDirectory(string directory)
{
var path = Path.GetFullPath(directory);
if (fileSystem.ContainsKey(path))
{
fileSystem[path] = new byte[0];
}
else
{
fileSystem.Add(path, new byte[0]);
}
}
public bool DirectoryExists(string directory)
{
var path = Path.GetFullPath(directory);
return fileSystem.ContainsKey(path);
}
public long GetLastDirectoryWrite(string path)
{
return 1;
}
public bool PathsEqual(string path, string otherPath)
{
return string.Equals(
Path.GetFullPath(path).TrimEnd('\\').TrimEnd('/'),
Path.GetFullPath(otherPath).TrimEnd('\\').TrimEnd('/'),
StringComparerUtils.OsDependentComparison);
}
}
}