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
192
193
194
195
196
197
198
199
200
201
202
203
|
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace AggregateConfigBuildTask.Tests.Unit
{
internal sealed class VirtualFileSystem(bool isWindowsMode = true) : IFileSystem
{
private readonly bool isWindowsMode = isWindowsMode;
private readonly ConcurrentDictionary<string, string> fileSystem = new(
isWindowsMode ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
private RegexOptions RegexOptions => isWindowsMode ? RegexOptions.IgnoreCase : RegexOptions.None;
private StringComparison StringComparison => isWindowsMode ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
private string EnvironmentLineBreak => isWindowsMode ? "\r\n" : "\n";
/// <inheritdoc/>
public string[] GetFiles(string path, string searchPattern)
{
path = NormalizePath(path);
var files = new List<string>();
string regexPattern = ConvertPatternToRegex(searchPattern);
foreach (var file in fileSystem.Keys)
{
// Ensure the file starts with the given path
if (file.StartsWith(path, StringComparison))
{
// Perform a regex match using the translated pattern with timeout
if (Regex.IsMatch(file, regexPattern, RegexOptions, TimeSpan.FromMilliseconds(500)))
{
files.Add(file);
}
}
}
return [.. files];
}
/// <inheritdoc/>
public string[] ReadAllLines(string path)
{
path = NormalizePath(path);
var content = ReadAllText(path);
return content.Split(EnvironmentLineBreak);
}
/// <inheritdoc/>
public string ReadAllText(string path)
{
path = NormalizePath(path);
if (fileSystem.TryGetValue(path, out var content))
{
return content;
}
throw new FileNotFoundException($"The file '{path}' was not found in the virtual file system.");
}
/// <inheritdoc/>
public void WriteAllText(string path, string text)
{
path = NormalizePath(path);
string directoryPath = Path.GetDirectoryName(path);
if (!DirectoryExists(directoryPath))
{
throw new IOException($"Directory not found '{directoryPath}'.");
}
// As a part of the testing, we want to ensure we only write once to each file.
if (FileExists(path))
{
throw new IOException($"Tried to write to existing file '{path}'!");
}
fileSystem[path] = text;
}
/// <inheritdoc/>
public bool FileExists(string path)
{
path = NormalizePath(path);
foreach (var file in fileSystem.Keys)
{
// Ensure the file starts with the given path
if (file.Equals(path, StringComparison))
{
return true;
}
}
return false;
}
/// <inheritdoc/>
public bool DirectoryExists(string directoryPath)
{
directoryPath = NormalizePath(directoryPath);
directoryPath = EnsureTrailingDirectorySeparator(directoryPath);
foreach (var file in fileSystem.Keys)
{
if (file.Equals(directoryPath, StringComparison))
{
return true;
}
}
return false;
}
/// <inheritdoc/>
public void CreateDirectory(string directoryPath)
{
directoryPath = NormalizePath(directoryPath);
directoryPath = EnsureTrailingDirectorySeparator(directoryPath);
fileSystem[directoryPath] = string.Empty;
}
/// <inheritdoc />
public TextReader OpenText(string path)
{
return new StringReader(ReadAllText(path));
}
/// <inheritdoc />
public Stream OpenRead(string inputPath)
{
var byteArray = Encoding.UTF8.GetBytes(ReadAllText(inputPath));
return new MemoryStream(byteArray);
}
/// <summary>
/// Ensures that the provided directory path ends with a directory separator character.
/// </summary>
/// <param name="directoryPath">The directory path to normalize.</param>
private string EnsureTrailingDirectorySeparator(string directoryPath)
{
if (string.IsNullOrEmpty(directoryPath))
{
return directoryPath;
}
char directorySeparator = isWindowsMode ? '\\' : '/';
// Ensure the directory path ends with the correct directory separator
if (!directoryPath.EndsWith(directorySeparator.ToString(), StringComparison.Ordinal))
{
directoryPath += directorySeparator;
}
return directoryPath;
}
/// <summary>
/// Converts a file search pattern (with * and ?) to a regex pattern.
/// </summary>
/// <param name="searchPattern">The wild card path expression to convert to a regex pattern.</param>
private static string ConvertPatternToRegex(string searchPattern)
{
// Escape special regex characters except for * and ?
string escapedPattern = Regex.Escape(searchPattern);
// Replace escaped * and ? with their regex equivalents
escapedPattern = escapedPattern.Replace("\\*", ".*", StringComparison.Ordinal).Replace("\\?", ".", StringComparison.Ordinal);
// Add start and end anchors to ensure full-string match
return "^" + escapedPattern + "$";
}
/// <summary>
/// Normalizes file paths by replacing slashes with the correct directory separator for the platform.
/// </summary>
/// <param name="path">The file or directory path to normalize.</param>
/// <returns>A normalized path string.</returns>
private string NormalizePath(string path)
{
if (string.IsNullOrEmpty(path)) return path;
char directorySeparator = isWindowsMode ? '\\' : '/';
char alternativeSeparator = isWindowsMode ? '/' : '\\';
// Normalize slashes to the correct directory separator
string normalizedPath = path.Replace(alternativeSeparator, directorySeparator);
// Trim any redundant trailing slashes except for root directory ("/" or "C:\\")
if (normalizedPath.Length > 1 && normalizedPath.EndsWith(directorySeparator.ToString(), StringComparison.Ordinal))
{
normalizedPath = normalizedPath.TrimEnd(directorySeparator);
}
return normalizedPath;
}
}
}
|