-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathTestFileSystem.cs
More file actions
191 lines (158 loc) · 5.67 KB
/
TestFileSystem.cs
File metadata and controls
191 lines (158 loc) · 5.67 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace GitCredentialManager.Tests.Objects
{
public class TestFileSystem : IFileSystem
{
public string UserHomePath { get; set; }
public string UserDataDirectoryPath { get; set; }
public IDictionary<string, byte[]> Files { get; set; } = new Dictionary<string, byte[]>();
public ISet<string> ExecutableFiles { get; } = new HashSet<string>();
public ISet<string> Directories { get; set; } = new HashSet<string>();
public string CurrentDirectory { get; set; } = Path.GetTempPath();
public bool IsCaseSensitive { get; set; } = false;
public TestFileSystem()
{
var gcmTestRoot = Path.Combine(Path.GetTempPath(), $"gcmtest-{Guid.NewGuid():N}");
UserHomePath = Path.Combine(gcmTestRoot, "HOME");
UserDataDirectoryPath = Path.Combine(UserHomePath, ".gcm");
}
#region IFileSystem
bool IFileSystem.IsSamePath(string a, string b)
{
return IsCaseSensitive
? StringComparer.Ordinal.Equals(a, b)
: StringComparer.OrdinalIgnoreCase.Equals(a, b);
}
bool IFileSystem.FileExists(string path)
{
return Files.ContainsKey(path);
}
bool IFileSystem.FileIsExecutable(string path)
{
if (!Files.ContainsKey(path))
throw new FileNotFoundException("File not found", path);
// On Windows, all files are considered executable.
if (!PlatformUtils.IsPosix())
return true;
return ExecutableFiles.Contains(path);
}
bool IFileSystem.DirectoryExists(string path)
{
return Directories.Contains(TrimSlash(path));
}
string IFileSystem.GetCurrentDirectory()
{
return CurrentDirectory;
}
Stream IFileSystem.OpenFileStream(string path, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
{
bool writable = fileAccess != FileAccess.Read;
if (fileMode == FileMode.Create)
{
return new TestFileStream(this, path);
}
return new MemoryStream(Files[path], writable);
}
void IFileSystem.CreateDirectory(string path)
{
Directories.Add(TrimSlash(path));
}
void IFileSystem.DeleteFile(string path)
{
Files.Remove(path);
}
IEnumerable<string> IFileSystem.EnumerateFiles(string path, string searchPattern)
{
bool IsPatternMatch(string s, string p)
{
var options = IsCaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase;
string regex = p
.Replace(".", "\\.")
.Replace("*", ".*");
return Regex.IsMatch(s, regex, options);
}
StringComparison comparer = IsCaseSensitive
? StringComparison.Ordinal
: StringComparison.OrdinalIgnoreCase;
foreach (var filePath in Files.Keys)
{
if (filePath.StartsWith(path, comparer) && IsPatternMatch(filePath, searchPattern))
{
yield return filePath;
}
}
}
IEnumerable<string> IFileSystem.EnumerateDirectories(string path)
{
StringComparison comparer = IsCaseSensitive
? StringComparison.Ordinal
: StringComparison.OrdinalIgnoreCase;
foreach (var dirPath in Directories)
{
if (dirPath.StartsWith(path, comparer))
{
yield return dirPath;
}
}
}
string IFileSystem.ReadAllText(string path)
{
if (Files.TryGetValue(path, out byte[] data))
{
return Encoding.UTF8.GetString(data);
}
throw new IOException("File not found");
}
string[] IFileSystem.ReadAllLines(string path)
{
if (Files.TryGetValue(path, out byte[] data))
{
return Encoding.UTF8.GetString(data).Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
}
throw new IOException("File not found");
}
#endregion
/// <summary>
/// Mark a test file as executable. File must exist in <see cref="Files"/> already.
/// </summary>
public void SetExecutable(string path, bool isExecutable = true)
{
if (!Files.ContainsKey(path))
throw new FileNotFoundException("File not found", path);
if (isExecutable)
ExecutableFiles.Add(path);
else
ExecutableFiles.Remove(path);
}
/// <summary>
/// Trim trailing slashes from a path.
/// </summary>
public static string TrimSlash(string path)
{
if (path.Length > 0 && path[path.Length - 1] == Path.DirectorySeparatorChar)
{
return path.Substring(0, path.Length - 1);
}
return path;
}
}
public class TestFileStream : MemoryStream
{
private readonly TestFileSystem _fs;
private readonly string _path;
public TestFileStream(TestFileSystem fs, string path)
{
_fs = fs;
_path = path;
}
public override void Flush()
{
base.Flush();
_fs.Files[_path] = base.ToArray();
}
}
}