forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemWatcher.Directory.Move.cs
More file actions
273 lines (217 loc) · 11.7 KB
/
FileSystemWatcher.Directory.Move.cs
File metadata and controls
273 lines (217 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
// 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 Xunit;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace System.IO.Tests
{
public class Directory_Move_Tests : FileSystemWatcherTest
{
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Expected WatcherChangeTypes are different based on OS
public void Directory_Move_To_Same_Directory()
{
DirectoryMove_SameDirectory(WatcherChangeTypes.Renamed);
}
[Fact]
[PlatformSpecific(TestPlatforms.OSX)]
public void Directory_Move_From_Watched_To_Unwatched()
{
DirectoryMove_FromWatchedToUnwatched(WatcherChangeTypes.Deleted);
}
[Theory]
[PlatformSpecific(TestPlatforms.OSX)]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void Directory_Move_Multiple_From_Watched_To_Unwatched_Mac(int filesCount)
{
// 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.
DirectoryMove_Multiple_FromWatchedToUnwatched(filesCount, skipOldEvents: true);
}
[Theory]
[PlatformSpecific(~TestPlatforms.OSX)]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void Directory_Move_Multiple_From_Watched_To_Unwatched(int filesCount)
{
DirectoryMove_Multiple_FromWatchedToUnwatched(filesCount, skipOldEvents: false);
}
[Theory]
[PlatformSpecific(~TestPlatforms.FreeBSD)]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void Directory_Move_Multiple_From_Unatched_To_Watched(int filesCount)
{
DirectoryMove_Multiple_FromUnwatchedToWatched(filesCount);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Expected WatcherChangeTypes are different based on OS
public void Windows_Directory_Move_To_Different_Watched_Directory()
{
DirectoryMove_DifferentWatchedDirectory(WatcherChangeTypes.Changed);
}
[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Expected WatcherChangeTypes are different based on OS
public void Unix_Directory_Move_To_Different_Watched_Directory()
{
DirectoryMove_DifferentWatchedDirectory(0);
}
[Fact]
public void Directory_Move_From_Unwatched_To_Watched()
{
DirectoryMove_FromUnwatchedToWatched(WatcherChangeTypes.Created);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void Directory_Move_In_Nested_Directory(bool includeSubdirectories)
{
DirectoryMove_NestedDirectory(includeSubdirectories ? WatcherChangeTypes.Renamed : 0, includeSubdirectories);
}
[Fact]
public void Directory_Move_With_Set_NotifyFilter()
{
DirectoryMove_WithNotifyFilter(WatcherChangeTypes.Renamed);
}
#region Test Helpers
private void DirectoryMove_SameDirectory(WatcherChangeTypes eventType)
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
using (var watcher = new FileSystemWatcher(testDirectory.Path, "*"))
{
string sourcePath = dir.Path;
string targetPath = Path.Combine(testDirectory.Path, "target");
Action action = () => Directory.Move(sourcePath, targetPath);
Action cleanup = () => Directory.Move(targetPath, sourcePath);
ExpectEvent(watcher, eventType, action, cleanup, targetPath);
}
}
private void DirectoryMove_DifferentWatchedDirectory(WatcherChangeTypes eventType)
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var sourceDir = new TempDirectory(Path.Combine(testDirectory.Path, "source")))
using (var adjacentDir = new TempDirectory(Path.Combine(testDirectory.Path, "adj")))
using (var dir = new TempDirectory(Path.Combine(sourceDir.Path, "dir")))
using (var watcher = new FileSystemWatcher(testDirectory.Path, "*"))
{
string sourcePath = dir.Path;
string targetPath = Path.Combine(adjacentDir.Path, "target");
// Move the dir to a different directory under the Watcher
Action action = () => Directory.Move(sourcePath, targetPath);
Action cleanup = () => Directory.Move(targetPath, sourcePath);
ExpectEvent(watcher, eventType, action, cleanup, new string[] { sourceDir.Path, adjacentDir.Path });
}
}
private void DirectoryMove_Multiple_FromWatchedToUnwatched(int filesCount, bool skipOldEvents)
{
Assert.InRange(filesCount, 0, int.MaxValue);
using var watchedTestDirectory = new TempDirectory(GetTestFilePath());
using var unwatchedTestDirectory = new TempDirectory(GetTestFilePath());
var dirs = Enumerable.Range(0, filesCount)
.Select(i => new
{
DirecoryInWatchedDir = Path.Combine(watchedTestDirectory.Path, $"dir{i}"),
DirecoryInUnwatchedDir = Path.Combine(unwatchedTestDirectory.Path, $"dir{i}")
}).ToArray();
Array.ForEach(dirs, (dir) => Directory.CreateDirectory(dir.DirecoryInWatchedDir));
using var watcher = new FileSystemWatcher(watchedTestDirectory.Path, "*");
Action action = () => Array.ForEach(dirs, dir => Directory.Move(dir.DirecoryInWatchedDir, dir.DirecoryInUnwatchedDir));
// On macOS, for each file we receive two events as describe in comment below.
int expectEvents = filesCount;
if (skipOldEvents)
expectEvents = expectEvents * 2;
IEnumerable<FiredEvent> events = ExpectEvents(watcher, expectEvents, action);
if (skipOldEvents)
events = events.Where(x => x.EventType != WatcherChangeTypes.Created);
var expectedEvents = dirs.Select(dir => new FiredEvent(WatcherChangeTypes.Deleted, dir.DirecoryInWatchedDir));
// Remove Created events as there is racecondition when create dir and then observe parent folder. It receives Create event altought Watcher is not registered yet.
Assert.Equal(expectedEvents, events.Where(x => x.EventType != WatcherChangeTypes.Created));
}
private void DirectoryMove_Multiple_FromUnwatchedToWatched(int filesCount)
{
Assert.InRange(filesCount, 0, int.MaxValue);
using var watchedTestDirectory = new TempDirectory(GetTestFilePath());
using var unwatchedTestDirectory = new TempDirectory(GetTestFilePath());
var dirs = Enumerable.Range(0, filesCount)
.Select(i => new
{
DirecoryInWatchedDir = Path.Combine(watchedTestDirectory.Path, $"dir{i}"),
DirecoryInUnwatchedDir = Path.Combine(unwatchedTestDirectory.Path, $"dir{i}")
}).ToArray();
Array.ForEach(dirs, (dir) => Directory.CreateDirectory(dir.DirecoryInUnwatchedDir));
using var watcher = new FileSystemWatcher(watchedTestDirectory.Path, "*");
Action action = () => Array.ForEach(dirs, dir => Directory.Move(dir.DirecoryInUnwatchedDir, dir.DirecoryInWatchedDir));
List<FiredEvent> events = ExpectEvents(watcher, filesCount, action);
var expectedEvents = dirs.Select(dir => new FiredEvent(WatcherChangeTypes.Created, dir.DirecoryInWatchedDir));
Assert.Equal(expectedEvents, events);
}
private void DirectoryMove_FromWatchedToUnwatched(WatcherChangeTypes eventType)
{
using (var watchedTestDirectory = new TempDirectory(GetTestFilePath()))
using (var unwatchedTestDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(watchedTestDirectory.Path, "dir")))
using (var watcher = new FileSystemWatcher(watchedTestDirectory.Path, "*"))
{
string sourcePath = dir.Path; // watched
string targetPath = Path.Combine(unwatchedTestDirectory.Path, "target");
Action action = () => Directory.Move(sourcePath, targetPath);
Action cleanup = () => Directory.Move(targetPath, sourcePath);
ExpectEvent(watcher, eventType, action, cleanup, sourcePath);
}
}
private void DirectoryMove_FromUnwatchedToWatched(WatcherChangeTypes eventType)
{
using (var watchedTestDirectory = new TempDirectory(GetTestFilePath()))
using (var unwatchedTestDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(unwatchedTestDirectory.Path, "dir")))
using (var watcher = new FileSystemWatcher(watchedTestDirectory.Path, "*"))
{
string sourcePath = dir.Path; // unwatched
string targetPath = Path.Combine(watchedTestDirectory.Path, "target");
Action action = () => Directory.Move(sourcePath, targetPath);
Action cleanup = () => Directory.Move(targetPath, sourcePath);
ExpectEvent(watcher, eventType, action, cleanup, targetPath);
}
}
private void DirectoryMove_NestedDirectory(WatcherChangeTypes eventType, bool includeSubdirectories)
{
using (var dir = new TempDirectory(GetTestFilePath()))
using (var firstDir = new TempDirectory(Path.Combine(dir.Path, "first")))
using (var secondDir = new TempDirectory(Path.Combine(firstDir.Path, "second")))
using (var nestedDir = new TempDirectory(Path.Combine(secondDir.Path, "nested")))
using (var watcher = new FileSystemWatcher(dir.Path, "*"))
{
watcher.IncludeSubdirectories = includeSubdirectories;
watcher.NotifyFilter = NotifyFilters.DirectoryName;
string sourcePath = nestedDir.Path;
string targetPath = nestedDir.Path + "_2";
// Move the dir to a different directory within the same nested directory
Action action = () => Directory.Move(sourcePath, targetPath);
Action cleanup = () => Directory.Move(targetPath, sourcePath);
ExpectEvent(watcher, eventType, action, cleanup, targetPath);
}
}
private void DirectoryMove_WithNotifyFilter(WatcherChangeTypes eventType)
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
using (var watcher = new FileSystemWatcher(testDirectory.Path, "*"))
{
watcher.NotifyFilter = NotifyFilters.DirectoryName;
string sourcePath = dir.Path;
string targetPath = dir.Path + "_2";
Action action = () => Directory.Move(sourcePath, targetPath);
Action cleanup = () => Directory.Move(targetPath, sourcePath);
ExpectEvent(watcher, eventType, action, cleanup, targetPath);
}
}
#endregion
}
}