{{ message }}
Repository navigation
Expand file tree
/
Copy pathFakeFile.cs
More file actions
280 lines (247 loc) · 8.72 KB
/
Copy pathFakeFile.cs
File metadata and controls
280 lines (247 loc) · 8.72 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.IO;
using Cake.Core.IO;
using Path = Cake.Core.IO.Path;
namespace Cake.Testing
{
/// <summary>
/// Represents a fake file.
/// </summary>
public sealed class FakeFile : IFile
{
private readonly FakeFileSystemTree _tree;
/// <inheritdoc/>
public FilePath Path { get; }
/// <inheritdoc/>
Path IFileSystemInfo.Path => Path;
/// <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 file 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 file 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 file 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="UnixFileMode"/> value that represents the Unix file mode of the entry.
/// </value>
public UnixFileMode? UnixFileMode { get; private set; }
/// <inheritdoc/>
public long Length { get; private set; }
/// <inheritdoc/>
public FileAttributes Attributes { get; set; }
internal object ContentLock { get; } = new object();
/// <summary>
/// Gets the length of the content.
/// </summary>
/// <value>
/// The length of the content.
/// </value>
public long ContentLength
{
get { return Length; }
internal set { Length = value; }
}
/// <summary>
/// Gets the content.
/// </summary>
/// <value>The content.</value>
public byte[] Content { get; internal set; }
internal FakeFile(FakeFileSystemTree tree, FilePath path)
{
_tree = tree;
Path = path;
Reset();
}
/// <inheritdoc/>
public void Copy(FilePath destination, bool overwrite)
{
_tree.CopyFile(this, destination, overwrite);
}
/// <inheritdoc/>
public void Move(FilePath destination)
{
_tree.MoveFile(this, destination);
}
/// <inheritdoc/>
public Stream Open(FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
{
var position = GetPosition(fileMode, out bool fileWasCreated);
if (fileWasCreated)
{
_tree.CreateFile(this);
}
return new FakeFileStream(this) { Position = position };
}
/// <inheritdoc/>
public void Delete()
{
_tree.DeleteFile(this);
}
/// <summary>
/// Resizes the file.
/// </summary>
/// <param name="offset">The offset.</param>
public void Resize(long offset)
{
if (Length < offset)
{
Length = offset;
}
if (Content.Length >= Length)
{
return;
}
var buffer = new byte[Length * 2];
Buffer.BlockCopy(Content, 0, buffer, 0, Content.Length);
Content = buffer;
}
private long GetPosition(FileMode fileMode, out bool fileWasCreated)
{
fileWasCreated = false;
if (Exists)
{
switch (fileMode)
{
case FileMode.CreateNew:
throw new IOException();
case FileMode.Create:
case FileMode.Truncate:
Length = 0;
return 0;
case FileMode.Open:
case FileMode.OpenOrCreate:
return 0;
case FileMode.Append:
return Length;
}
}
else
{
switch (fileMode)
{
case FileMode.Create:
case FileMode.Append:
case FileMode.CreateNew:
case FileMode.OpenOrCreate:
fileWasCreated = true;
Exists = true;
var now = _tree.GetUtcNow();
SetLastWriteTimeUtc(now);
SetLastAccessTimeUtc(now);
SetCreationTimeUtc(now);
UnixFileMode = _tree.DefaultUnixCreateFileMode;
return Length;
case FileMode.Open:
case FileMode.Truncate:
throw FakeFileSystemTree.ThrowIfNotFound(this);
}
}
throw new NotSupportedException();
}
/// <inheritdoc/>
public IFile SetCreationTime(DateTime creationTime)
{
FakeFileSystemTree.ThrowIfNotFound(this);
CreationTimeUtc = creationTime.ToUniversalTime();
return this;
}
/// <inheritdoc/>
public IFile SetCreationTimeUtc(DateTime creationTimeUtc)
{
FakeFileSystemTree.ThrowIfNotFound(this);
CreationTimeUtc = creationTimeUtc;
return this;
}
/// <inheritdoc/>
public IFile SetLastAccessTime(DateTime lastAccessTime)
{
FakeFileSystemTree.ThrowIfNotFound(this);
LastAccessTimeUtc = lastAccessTime.ToUniversalTime();
return this;
}
/// <inheritdoc/>
public IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc)
{
FakeFileSystemTree.ThrowIfNotFound(this);
LastAccessTimeUtc = lastAccessTimeUtc;
return this;
}
/// <inheritdoc/>
public IFile SetLastWriteTime(DateTime lastWriteTime)
{
FakeFileSystemTree.ThrowIfNotFound(this);
LastWriteTime = lastWriteTime;
LastWriteTimeUtc = lastWriteTime.ToUniversalTime();
return this;
}
/// <inheritdoc/>
public IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc)
{
FakeFileSystemTree.ThrowIfNotFound(this);
LastWriteTimeUtc = lastWriteTimeUtc;
LastWriteTime = lastWriteTimeUtc.ToLocalTime();
return this;
}
/// <inheritdoc/>
public IFile SetUnixFileMode(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 IFile SetLastWriteNow()
{
return SetLastWriteTimeUtc(_tree.GetUtcNow());
}
internal void Reset()
{
Exists = false;
Hidden = false;
LastWriteTime = default;
CreationTimeUtc = null;
LastAccessTimeUtc = null;
LastWriteTimeUtc = null;
UnixFileMode = null;
ContentLength = 0;
Attributes = default;
Content = new byte[4096];
}
}
}
You can’t perform that action at this time.
