{{ message }}
Repository navigation
Expand file tree
/
Copy pathFakeDirectory.cs
More file actions
280 lines (242 loc) · 9 KB
/
Copy pathFakeDirectory.cs
File metadata and controls
280 lines (242 loc) · 9 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Cake.Core.IO;
namespace Cake.Testing
{
/// <summary>
/// Represents a fake directory.
/// </summary>
[DebuggerDisplay("{Path,nq}")]
public sealed class FakeDirectory : IDirectory
{
private readonly FakeFileSystemTree _tree;
/// <inheritdoc/>
public bool Exists { get; internal set; }
/// <inheritdoc/>
public bool Hidden { get; internal set; }
/// <summary>
/// Gets the time when this <see cref="IFileSystemInfo"/> was last written to.
/// </summary>
/// <value>The last write time.</value>
public DateTime LastWriteTime { get; private set; }
/// <summary>
/// Gets the date and time, in Coordinated Universal Time (UTC), that the directory was last written to.
/// </summary>
/// <value>
/// A <see cref="DateTime"/> value that represents the last write time in UTC, or <c>null</c> if not available.
/// </value>
public DateTime? LastWriteTimeUtc { get; private set; }
/// <summary>
/// Gets the date and time, in Coordinated Universal Time (UTC), that the directory was created.
/// </summary>
/// <value>
/// A <see cref="DateTime"/> value that represents the creation time in UTC, or <c>null</c> if not available.
/// </value>
public DateTime? CreationTimeUtc { get; private set; }
/// <summary>
/// Gets the date and time, in Coordinated Universal Time (UTC), that the directory was last accessed.
/// </summary>
/// <value>
/// A <see cref="DateTime"/> value that represents the last access time in UTC, or <c>null</c> if not available.
/// </value>
public DateTime? LastAccessTimeUtc { get; private set; }
/// <summary>
/// Gets the Unix file mode of the entry.
/// </summary>
/// <value>
/// A <see cref="System.IO.UnixFileMode"/> value that represents the Unix file mode of the entry.
/// </value>
public System.IO.UnixFileMode? UnixFileMode { get; internal set; }
/// <inheritdoc/>
public DirectoryPath Path { get; }
/// <inheritdoc/>
Path IFileSystemInfo.Path => Path;
internal FakeDirectory Parent { get; set; }
internal FakeDirectoryContent Content { get; }
internal FakeDirectory(FakeFileSystemTree tree, DirectoryPath path)
{
_tree = tree;
Path = path;
Content = new FakeDirectoryContent(this, tree.Comparer);
CreationTimeUtc = null;
LastAccessTimeUtc = null;
LastWriteTimeUtc = null;
UnixFileMode = null;
}
/// <inheritdoc/>
public void Create()
{
_tree.CreateDirectory(this);
}
/// <inheritdoc/>
public void Move(DirectoryPath destination)
{
_tree.MoveDirectory(this, destination);
}
/// <inheritdoc/>
public void Delete(bool recursive)
{
_tree.DeleteDirectory(this, recursive);
}
/// <inheritdoc/>
public IEnumerable<IDirectory> GetDirectories(string filter, SearchScope scope)
{
var stack = new Stack<FakeDirectory>();
foreach (var child in Content.Directories.Values)
{
if (child.Exists)
{
stack.Push(child);
}
}
// Rewrite the filter to a regex expression.
var expression = CreateRegex(filter);
while (stack.Count > 0)
{
// Pop a directory from the stack.
var current = stack.Pop();
// Is this a match? In that case, add it to the result.
if (expression.IsMatch(current.Path.GetDirectoryName()))
{
yield return current;
}
// Recurse?
if (scope == SearchScope.Recursive)
{
foreach (var child in current.Content.Directories.Values)
{
if (child.Exists)
{
stack.Push(child);
}
}
}
}
}
/// <inheritdoc/>
public IEnumerable<IFile> GetFiles(string filter, SearchScope scope)
{
var stack = new Stack<FakeDirectory>();
// Rewrite the filter to a regex expression.
var expression = CreateRegex(filter);
// Just interested in this directory?
if (scope == SearchScope.Current)
{
foreach (var file in GetFiles(this, expression))
{
yield return file;
}
yield break;
}
stack.Push(this);
while (stack.Count > 0)
{
// Pop a directory from the stack.
var current = stack.Pop();
// Add all files we can find.
foreach (var file in GetFiles(current, expression))
{
yield return file;
}
// Recurse?
if (scope == SearchScope.Recursive)
{
foreach (var child in current.Content.Directories.Values)
{
if (child.Exists)
{
stack.Push(child);
}
}
}
}
}
/// <inheritdoc/>
public IDirectory SetCreationTime(DateTime creationTime)
{
FakeFileSystemTree.ThrowIfNotFound(this);
CreationTimeUtc = creationTime.ToUniversalTime();
return this;
}
/// <inheritdoc/>
public IDirectory SetCreationTimeUtc(DateTime creationTimeUtc)
{
FakeFileSystemTree.ThrowIfNotFound(this);
CreationTimeUtc = creationTimeUtc;
return this;
}
/// <inheritdoc/>
public IDirectory SetLastAccessTime(DateTime lastAccessTime)
{
FakeFileSystemTree.ThrowIfNotFound(this);
LastAccessTimeUtc = lastAccessTime.ToUniversalTime();
return this;
}
/// <inheritdoc/>
public IDirectory SetLastAccessTimeUtc(DateTime lastAccessTimeUtc)
{
FakeFileSystemTree.ThrowIfNotFound(this);
LastAccessTimeUtc = lastAccessTimeUtc;
return this;
}
/// <inheritdoc/>
public IDirectory SetLastWriteTime(DateTime lastWriteTime)
{
FakeFileSystemTree.ThrowIfNotFound(this);
LastWriteTime = lastWriteTime;
LastWriteTimeUtc = lastWriteTime.ToUniversalTime();
return this;
}
/// <inheritdoc/>
public IDirectory SetLastWriteTimeUtc(DateTime lastWriteTimeUtc)
{
FakeFileSystemTree.ThrowIfNotFound(this);
LastWriteTimeUtc = lastWriteTimeUtc;
LastWriteTime = lastWriteTimeUtc.ToLocalTime();
return this;
}
/// <inheritdoc/>
public IDirectory SetUnixFileMode(System.IO.UnixFileMode unixFileMode)
{
if (!_tree.IsUnix)
{
throw new PlatformNotSupportedException("Setting Unix file mode is not supported on Windows platforms.");
}
FakeFileSystemTree.ThrowIfNotFound(this);
UnixFileMode = unixFileMode;
return this;
}
/// <summary>
/// Sets the last write time of the file to the current UTC time.
/// </summary>
/// <returns>The current <see cref="IFile"/> instance.</returns>
public IDirectory SetLastWriteNow()
{
return SetLastWriteTimeUtc(_tree.GetUtcNow());
}
private static IEnumerable<FakeFile> GetFiles(FakeDirectory current, Regex expression)
{
foreach (var file in current.Content.Files)
{
if (file.Value.Exists)
{
// Is this a match? In that case, add it to the result.
if (expression.IsMatch(file.Key.GetFilename().FullPath))
{
yield return current.Content.Files[file.Key];
}
}
}
}
private static Regex CreateRegex(string pattern)
{
pattern = pattern.Replace(".", "\\.").Replace("*", ".*").Replace("?", ".{1}");
return new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled);
}
}
}
You can’t perform that action at this time.
