-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
FileSystemWatcher.File.Move.cs
333 lines (275 loc) · 14.3 KB
/
FileSystemWatcher.File.Move.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.XUnitExtensions;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace System.IO.Tests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/103584", TestPlatforms.Windows)]
public class File_Move_Tests : FileSystemWatcherTest
{
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Expected WatcherChangeTypes are different based on OS
public void Windows_File_Move_To_Same_Directory()
{
FileMove_SameDirectory(WatcherChangeTypes.Renamed);
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Expected WatcherChangeTypes are different based on OS
public void Unix_File_Move_To_Same_Directory()
{
FileMove_SameDirectory(WatcherChangeTypes.Renamed);
}
[Fact]
public void File_Move_From_Watched_To_Unwatched()
{
FileMove_FromWatchedToUnwatched(WatcherChangeTypes.Deleted);
}
[ConditionalTheory]
[PlatformSpecific(TestPlatforms.OSX)]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void File_Move_Multiple_From_Watched_To_Unwatched_Mac(int filesCount)
{
if (Environment.OSVersion.Version.Major == 12)
{
throw new SkipTestException("Unreliable on Monterey"); // https://github.com/dotnet/runtime/issues/70164
}
// On Mac, the FSStream aggregate old events caused by the test setup.
// There is no option how to get rid of it but skip it.
FileMove_Multiple_FromWatchedToUnwatched(filesCount, skipOldEvents: true);
}
[Theory]
[SkipOnPlatform(TestPlatforms.OSX | TestPlatforms.MacCatalyst, "Not supported on OSX/MacCatalyst.")]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void File_Move_Multiple_From_Watched_To_Unwatched(int filesCount)
{
FileMove_Multiple_FromWatchedToUnwatched(filesCount, skipOldEvents: false);
}
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void File_Move_Multiple_From_Unwatched_To_WatchedMac(int filesCount)
{
FileMove_Multiple_FromUnwatchedToWatched(filesCount);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Expected WatcherChangeTypes are different based on OS
public void Windows_File_Move_To_Different_Watched_Directory()
{
FileMove_DifferentWatchedDirectory(WatcherChangeTypes.Changed);
}
[Fact]
[PlatformSpecific(TestPlatforms.OSX)] // Expected WatcherChangeTypes are different based on OS
public void OSX_File_Move_To_Different_Watched_Directory()
{
FileMove_DifferentWatchedDirectory(0);
}
[Fact]
[PlatformSpecific(TestPlatforms.Linux)] // Expected WatcherChangeTypes are different based on OS
public void Linux_File_Move_To_Different_Watched_Directory()
{
FileMove_DifferentWatchedDirectory(0);
}
[Fact]
public void File_Move_From_Unwatched_To_Watched()
{
FileMove_FromUnwatchedToWatched(WatcherChangeTypes.Created);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
[PlatformSpecific(TestPlatforms.Windows)] // Expected WatcherChangeTypes are different based on OS
public void Windows_File_Move_In_Nested_Directory(bool includeSubdirectories)
{
FileMove_NestedDirectory(includeSubdirectories ? WatcherChangeTypes.Renamed : 0, includeSubdirectories);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Expected WatcherChangeTypes are different based on OS
public void Unix_File_Move_In_Nested_Directory(bool includeSubdirectories)
{
FileMove_NestedDirectory(includeSubdirectories ? WatcherChangeTypes.Renamed : 0, includeSubdirectories);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Expected WatcherChangeTypes are different based on OS
public void Windows_File_Move_With_Set_NotifyFilter()
{
FileMove_WithNotifyFilter(WatcherChangeTypes.Renamed);
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Expected WatcherChangeTypes are different based on OS
public void Unix_File_Move_With_Set_NotifyFilter()
{
FileMove_WithNotifyFilter(WatcherChangeTypes.Renamed);
}
[Fact]
public void File_Move_SynchronizingObject()
{
string dir = CreateTestDirectory(TestDirectory, "dir");
string testFile = CreateTestFile(dir, "file");
using (var watcher = new FileSystemWatcher(dir, "*"))
{
TestISynchronizeInvoke invoker = new TestISynchronizeInvoke();
watcher.SynchronizingObject = invoker;
string sourcePath = testFile;
string targetPath = testFile + "_Renamed";
// Move the testFile to a different name in the same directory
Action action = () => File.Move(sourcePath, targetPath);
Action cleanup = () => File.Move(targetPath, sourcePath);
ExpectEvent(watcher, WatcherChangeTypes.Renamed, action, cleanup, targetPath);
Assert.True(invoker.BeginInvoke_Called);
}
}
#region Test Helpers
private void FileMove_SameDirectory(WatcherChangeTypes eventType)
{
string dir = CreateTestDirectory(TestDirectory, "dir");
string testFile = CreateTestFile(dir, "file");
using (var watcher = new FileSystemWatcher(dir, "*"))
{
string sourcePath = testFile;
string targetPath = testFile + "_" + eventType.ToString();
// Move the testFile to a different name in the same directory
Action action = () => File.Move(sourcePath, targetPath);
Action cleanup = () => File.Move(targetPath, sourcePath);
if ((eventType & WatcherChangeTypes.Deleted) > 0)
ExpectEvent(watcher, eventType, action, cleanup, new string[] { sourcePath, targetPath });
else
ExpectEvent(watcher, eventType, action, cleanup, targetPath);
}
}
private void FileMove_DifferentWatchedDirectory(WatcherChangeTypes eventType)
{
string dir = CreateTestDirectory(TestDirectory, "dir");
string dir_adjacent = CreateTestDirectory(TestDirectory, "dir_adj");
string testFile = CreateTestFile(dir, "file");
using (var watcher = new FileSystemWatcher(TestDirectory, "*"))
{
string sourcePath = testFile;
string targetPath = Path.Combine(dir_adjacent, Path.GetFileName(testFile) + "_" + eventType.ToString());
// Move the testFile to a different directory under the Watcher
Action action = () => File.Move(sourcePath, targetPath);
Action cleanup = () => File.Move(targetPath, sourcePath);
ExpectEvent(watcher, eventType, action, cleanup, new string[] { dir, dir_adjacent });
}
}
[ActiveIssue("https://github.com/dotnet/runtime/issues/96728", typeof(PlatformDetection), nameof(PlatformDetection.IsReadyToRunCompiled))]
private void FileMove_FromWatchedToUnwatched(WatcherChangeTypes eventType)
{
string dir_watched = CreateTestDirectory(TestDirectory, "dir_watched");
string dir_unwatched = CreateTestDirectory(TestDirectory, "dir_unwatched");
string testFile = CreateTestFile(dir_watched, "file");
using (var watcher = new FileSystemWatcher(dir_watched, "*"))
{
string sourcePath = testFile; // watched
string targetPath = Path.Combine(dir_unwatched, "file");
Action action = () => File.Move(sourcePath, targetPath);
Action cleanup = () => File.Move(targetPath, sourcePath);
ExpectEvent(watcher, eventType, action, cleanup, sourcePath);
}
}
private void FileMove_Multiple_FromWatchedToUnwatched(int filesCount, bool skipOldEvents)
{
Assert.InRange(filesCount, 0, int.MaxValue);
string watchedTestDirectory = CreateTestDirectory(TestDirectory, "dir_watched");
string unwatchedTestDirectory = CreateTestDirectory(TestDirectory, "dir_unwatched");
var files = Enumerable.Range(0, filesCount)
.Select(i => new
{
FileInWatchedDir = Path.Combine(watchedTestDirectory, $"file{i}"),
FileInUnwatchedDir = Path.Combine(unwatchedTestDirectory, $"file{i}")
}).ToArray();
Array.ForEach(files, (file) => File.Create(file.FileInWatchedDir).Dispose());
using var watcher = new FileSystemWatcher(watchedTestDirectory, "*");
Action action = () => Array.ForEach(files, file => File.Move(file.FileInWatchedDir, file.FileInUnwatchedDir));
// On macOS, for each file we receive two events as describe in comment below.
int expectEvents = filesCount;
if (skipOldEvents)
{
expectEvents = expectEvents * 3;
}
IEnumerable<FiredEvent> events = ExpectEvents(watcher, expectEvents, action);
// Remove Created and Changed events as there is racecondition when create file and then observe parent folder. It receives Create and Changed event altought Watcher is not registered yet.
if (skipOldEvents)
{
events = events.Where(x => (x.EventType & (WatcherChangeTypes.Created | WatcherChangeTypes.Changed)) == 0);
}
var expectedEvents = files.Select(file => new FiredEvent(WatcherChangeTypes.Deleted, file.FileInWatchedDir));
Assert.Equal(expectedEvents, events);
}
private void FileMove_Multiple_FromUnwatchedToWatched(int filesCount)
{
Assert.InRange(filesCount, 0, int.MaxValue);
string watchedTestDirectory = CreateTestDirectory(TestDirectory, "dir_watched");
string unwatchedTestDirectory = CreateTestDirectory(TestDirectory, "dir_unwatched");
var files = Enumerable.Range(0, filesCount)
.Select(i => new
{
FileInWatchedDir = Path.Combine(watchedTestDirectory, $"file{i}"),
FileInUnwatchedDir = Path.Combine(unwatchedTestDirectory, $"file{i}")
}).ToArray();
Array.ForEach(files, (file) => File.Create(file.FileInUnwatchedDir).Dispose());
using var watcher = new FileSystemWatcher(watchedTestDirectory, "*");
Action action = () => Array.ForEach(files, file => File.Move(file.FileInUnwatchedDir, file.FileInWatchedDir));
List<FiredEvent> events = ExpectEvents(watcher, filesCount, action);
var expectedEvents = files.Select(file => new FiredEvent(WatcherChangeTypes.Created, file.FileInWatchedDir));
Assert.Equal(expectedEvents, events);
}
private void FileMove_FromUnwatchedToWatched(WatcherChangeTypes eventType)
{
string dir_watched = CreateTestDirectory(TestDirectory, "dir_watched");
string testFile = CreateTestFile(TestDirectory, "dir_unwatched", "file");
using (var watcher = new FileSystemWatcher(dir_watched, "*"))
{
string sourcePath = testFile; // unwatched
string targetPath = Path.Combine(dir_watched, "file");
Action action = () => File.Move(sourcePath, targetPath);
Action cleanup = () => File.Move(targetPath, sourcePath);
ExpectEvent(watcher, eventType, action, cleanup, targetPath);
}
}
private void FileMove_NestedDirectory(WatcherChangeTypes eventType, bool includeSubdirectories)
{
string nestedFile = CreateTestFile(TestDirectory, "dir1", "nested", "nestedFile" + eventType.ToString());
using (var watcher = new FileSystemWatcher(TestDirectory, "*"))
{
watcher.NotifyFilter = NotifyFilters.FileName;
watcher.IncludeSubdirectories = includeSubdirectories;
string sourcePath = nestedFile;
string targetPath = nestedFile + "_2";
// Move the testFile to a different name within the same nested directory
Action action = () => File.Move(sourcePath, targetPath);
Action cleanup = () => File.Move(targetPath, sourcePath);
if ((eventType & WatcherChangeTypes.Deleted) > 0)
ExpectEvent(watcher, eventType, action, cleanup, new string[] { targetPath, sourcePath });
else
ExpectEvent(watcher, eventType, action, cleanup, targetPath);
}
}
private void FileMove_WithNotifyFilter(WatcherChangeTypes eventType)
{
string file = CreateTestFile(TestDirectory, "file");
using (var watcher = new FileSystemWatcher(TestDirectory, Path.GetFileName(file)))
{
watcher.NotifyFilter = NotifyFilters.FileName;
string sourcePath = file;
string targetPath = Path.Combine(TestDirectory, "target");
// Move the testFile to a different name under the same directory with active notifyfilters
Action action = () => File.Move(sourcePath, targetPath);
Action cleanup = () => File.Move(targetPath, sourcePath);
if ((eventType & WatcherChangeTypes.Deleted) > 0)
ExpectEvent(watcher, eventType, action, cleanup, sourcePath);
else
ExpectEvent(watcher, eventType, action, cleanup, targetPath);
}
}
#endregion
}
}