forked from dotnet/msbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTouch_Tests.cs
More file actions
411 lines (335 loc) · 11.7 KB
/
Copy pathTouch_Tests.cs
File metadata and controls
411 lines (335 loc) · 11.7 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using Xunit;
#nullable disable
namespace Microsoft.Build.UnitTests
{
public sealed class Touch_Tests
{
internal static Microsoft.Build.Framework.FileExists fileExists = new Microsoft.Build.Framework.FileExists(FileExists);
internal static Microsoft.Build.Framework.FileCreate fileCreate = new Microsoft.Build.Framework.FileCreate(FileCreate);
internal static Microsoft.Build.Tasks.GetAttributes fileGetAttributes = new Microsoft.Build.Tasks.GetAttributes(GetAttributes);
internal static Microsoft.Build.Tasks.SetAttributes fileSetAttributes = new Microsoft.Build.Tasks.SetAttributes(SetAttributes);
internal static Microsoft.Build.Tasks.SetLastAccessTime setLastAccessTime = new Microsoft.Build.Tasks.SetLastAccessTime(SetLastAccessTime);
internal static Microsoft.Build.Tasks.SetLastWriteTime setLastWriteTime = new Microsoft.Build.Tasks.SetLastWriteTime(SetLastWriteTime);
internal static string myexisting_txt = NativeMethodsShared.IsWindows ? @"c:\touch\myexisting.txt" : @"/touch/myexisting.txt";
internal static string mynonexisting_txt = NativeMethodsShared.IsWindows ? @"c:\touch\mynonexisting.txt" : @"/touch/mynonexisting.txt";
internal static string nonexisting_txt = NativeMethodsShared.IsWindows ? @"c:\touch-nonexistent\file.txt" : @"/touch-nonexistent/file.txt";
internal static string myreadonly_txt = NativeMethodsShared.IsWindows ? @"c:\touch\myreadonly.txt" : @"/touch/myreadonly.txt";
private bool Execute(Touch t)
{
return t.ExecuteImpl(
fileExists,
fileCreate,
fileGetAttributes,
fileSetAttributes,
setLastAccessTime,
setLastWriteTime);
}
/// <summary>
/// Mock file exists.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static bool FileExists(string path)
{
if (path == myexisting_txt)
{
return true;
}
if (path == mynonexisting_txt)
{
return false;
}
if (path == nonexisting_txt)
{
return false;
}
if (path == myreadonly_txt)
{
return true;
}
Assert.Fail("Unexpected file exists: " + path);
return true;
}
/// <summary>
/// Mock file create.
/// </summary>
/// <param name="path"></param>
private static FileStream FileCreate(string path)
{
if (path == mynonexisting_txt)
{
return null;
}
if (path == nonexisting_txt)
{
throw new DirectoryNotFoundException();
}
Assert.Fail("Unexpected file create: " + path);
return null;
}
/// <summary>
/// Mock get attributes.
/// </summary>
/// <param name="path"></param>
private static FileAttributes GetAttributes(string path)
{
FileAttributes a = new FileAttributes();
if (path == myexisting_txt)
{
return a;
}
if (path == mynonexisting_txt)
{
// Has attributes because Touch created it.
return a;
}
if (path == myreadonly_txt)
{
return System.IO.FileAttributes.ReadOnly;
}
Assert.Fail("Unexpected file attributes: " + path);
return a;
}
/// <summary>
/// Mock get attributes.
/// </summary>
/// <param name="path"></param>
private static void SetAttributes(string path, FileAttributes attributes)
{
if (path == myreadonly_txt)
{
return;
}
Assert.Fail("Unexpected set file attributes: " + path);
}
/// <summary>
/// Mock SetLastAccessTime.
/// </summary>
/// <param name="path"></param>
private static void SetLastAccessTime(string path, DateTime timestamp)
{
if (path == myexisting_txt)
{
return;
}
if (path == mynonexisting_txt)
{
return;
}
if (path == myreadonly_txt)
{
// Read-only so throw an exception
throw new IOException();
}
Assert.Fail("Unexpected set last access time: " + path);
}
/// <summary>
/// Mock SetLastWriteTime.
/// </summary>
/// <param name="path"></param>
private static void SetLastWriteTime(string path, DateTime timestamp)
{
if (path == myexisting_txt)
{
return;
}
if (path == mynonexisting_txt)
{
return;
}
if (path == myreadonly_txt)
{
return;
}
Assert.Fail("Unexpected set last write time: " + path);
}
[Fact]
public void TouchExisting()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.Files = new ITaskItem[]
{
new TaskItem(myexisting_txt)
};
bool success = Execute(t);
Assert.True(success);
Assert.Single(t.TouchedFiles);
Assert.Contains(
String.Format(AssemblyResources.GetString("Touch.Touching"), myexisting_txt),
engine.Log);
}
[Fact]
public void TouchNonExisting()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.Files = new ITaskItem[]
{
new TaskItem(mynonexisting_txt)
};
bool success = Execute(t);
// Not success because the file doesn't exist
Assert.False(success);
Assert.Contains(
String.Format(AssemblyResources.GetString("Touch.FileDoesNotExist"), mynonexisting_txt),
engine.Log);
}
[Fact]
public void TouchNonExistingAlwaysCreate()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.AlwaysCreate = true;
t.Files = new ITaskItem[]
{
new TaskItem(mynonexisting_txt)
};
bool success = Execute(t);
// Success because the file was created.
Assert.True(success);
Assert.Contains(
String.Format(AssemblyResources.GetString("Touch.CreatingFile"), mynonexisting_txt, "AlwaysCreate"),
engine.Log);
}
[Fact]
public void TouchNonExistingAlwaysCreateAndBadlyFormedTimestamp()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.AlwaysCreate = true;
t.ForceTouch = false;
t.Time = "Badly formed time String.";
t.Files = new ITaskItem[]
{
new TaskItem(mynonexisting_txt)
};
bool success = Execute(t);
// Failed because of badly formed time string.
Assert.False(success);
Assert.Contains("MSB3376", engine.Log);
}
[Fact]
public void TouchReadonly()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.AlwaysCreate = true;
t.Files = new ITaskItem[]
{
new TaskItem(myreadonly_txt)
};
bool success = Execute(t);
// Failed because file is readonly.
Assert.False(success);
Assert.Contains("MSB3374", engine.Log);
Assert.Contains(myreadonly_txt, engine.Log);
}
[Fact]
public void TouchReadonlyForce()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.ForceTouch = true;
t.AlwaysCreate = true;
t.Files = new ITaskItem[]
{
new TaskItem(myreadonly_txt)
};
Execute(t);
}
[Fact]
public void TouchNonExistingDirectoryDoesntExist()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.AlwaysCreate = true;
t.Files = new ITaskItem[]
{
new TaskItem(nonexisting_txt)
};
bool success = Execute(t);
// Failed because the target directory didn't exist.
Assert.False(success);
Assert.Contains("MSB3371", engine.Log);
Assert.Contains(nonexisting_txt, engine.Log);
}
/// <summary>
/// Question touch on non-existing file should return false.
/// </summary>
[Fact]
public void QuestionTouchNonExisting()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.FailIfNotIncremental = true;
t.Files = new ITaskItem[]
{
new TaskItem(mynonexisting_txt)
};
bool success = Execute(t);
// Not success because the file doesn't exist
Assert.False(success);
Assert.Contains(
String.Format(AssemblyResources.GetString("Touch.FileDoesNotExist"), mynonexisting_txt),
engine.Log);
}
/// <summary>
/// Question touch on a non-existing file with AlwaysCreate property should return false.
/// </summary>
[Fact]
public void QuestionTouchNonExistingAlwaysCreate()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.FailIfNotIncremental = true;
t.AlwaysCreate = true;
t.Files = new ITaskItem[]
{
new TaskItem(mynonexisting_txt)
};
bool success = Execute(t);
Assert.True(success);
Assert.Contains(
String.Format(AssemblyResources.GetString("Touch.CreatingFile"), mynonexisting_txt, "AlwaysCreate"),
engine.Log);
}
/// <summary>
/// Question touch should return true and the file is not touched.
/// </summary>
[Fact]
public void QuestionTouchExisting()
{
Touch t = new Touch();
MockEngine engine = new MockEngine();
t.BuildEngine = engine;
t.FailIfNotIncremental = true;
t.Files = new ITaskItem[]
{
new TaskItem(myexisting_txt)
};
bool success = Execute(t);
Assert.True(success);
Assert.Contains(
String.Format(AssemblyResources.GetString("Touch.Touching"), myexisting_txt),
engine.Log);
}
}
}