1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using Xunit;
6 
7 namespace System.IO.Tests
8 {
9     public class Directory_Move_Tests : FileSystemWatcherTest
10     {
11         [Fact]
12         [PlatformSpecific(TestPlatforms.Windows)]  // Expected WatcherChangeTypes are different based on OS
Directory_Move_To_Same_Directory()13         public void Directory_Move_To_Same_Directory()
14         {
15             DirectoryMove_SameDirectory(WatcherChangeTypes.Renamed);
16         }
17 
18         [Fact]
Directory_Move_From_Watched_To_Unwatched()19         public void Directory_Move_From_Watched_To_Unwatched()
20         {
21             DirectoryMove_FromWatchedToUnwatched(WatcherChangeTypes.Deleted);
22         }
23 
24         [Fact]
25         [PlatformSpecific(TestPlatforms.Windows)]  // Expected WatcherChangeTypes are different based on OS
Windows_Directory_Move_To_Different_Watched_Directory()26         public void Windows_Directory_Move_To_Different_Watched_Directory()
27         {
28             DirectoryMove_DifferentWatchedDirectory(WatcherChangeTypes.Changed);
29         }
30 
31         [Fact]
32         [PlatformSpecific(TestPlatforms.AnyUnix)]  // Expected WatcherChangeTypes are different based on OS
Unix_Directory_Move_To_Different_Watched_Directory()33         public void Unix_Directory_Move_To_Different_Watched_Directory()
34         {
35             DirectoryMove_DifferentWatchedDirectory(0);
36         }
37 
38         [Fact]
Directory_Move_From_Unwatched_To_Watched()39         public void Directory_Move_From_Unwatched_To_Watched()
40         {
41             DirectoryMove_FromUnwatchedToWatched(WatcherChangeTypes.Created);
42         }
43 
44         [Theory]
45         [InlineData(false)]
46         [InlineData(true)]
Directory_Move_In_Nested_Directory(bool includeSubdirectories)47         public void Directory_Move_In_Nested_Directory(bool includeSubdirectories)
48         {
49             DirectoryMove_NestedDirectory(includeSubdirectories ? WatcherChangeTypes.Renamed : 0, includeSubdirectories);
50         }
51 
52         [Fact]
Directory_Move_With_Set_NotifyFilter()53         public void Directory_Move_With_Set_NotifyFilter()
54         {
55             DirectoryMove_WithNotifyFilter(WatcherChangeTypes.Renamed);
56         }
57 
58         #region Test Helpers
59 
DirectoryMove_SameDirectory(WatcherChangeTypes eventType)60         private void DirectoryMove_SameDirectory(WatcherChangeTypes eventType)
61         {
62             using (var testDirectory = new TempDirectory(GetTestFilePath()))
63             using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
64             using (var watcher = new FileSystemWatcher(testDirectory.Path, "*"))
65             {
66                 string sourcePath = dir.Path;
67                 string targetPath = Path.Combine(testDirectory.Path, "target");
68 
69                 Action action = () => Directory.Move(sourcePath, targetPath);
70                 Action cleanup = () => Directory.Move(targetPath, sourcePath);
71 
72                 ExpectEvent(watcher, eventType, action, cleanup, targetPath);
73             }
74         }
75 
DirectoryMove_DifferentWatchedDirectory(WatcherChangeTypes eventType)76         private void DirectoryMove_DifferentWatchedDirectory(WatcherChangeTypes eventType)
77         {
78             using (var testDirectory = new TempDirectory(GetTestFilePath()))
79             using (var sourceDir = new TempDirectory(Path.Combine(testDirectory.Path, "source")))
80             using (var adjacentDir = new TempDirectory(Path.Combine(testDirectory.Path, "adj")))
81             using (var dir = new TempDirectory(Path.Combine(sourceDir.Path, "dir")))
82             using (var watcher = new FileSystemWatcher(testDirectory.Path, "*"))
83             {
84                 string sourcePath = dir.Path;
85                 string targetPath = Path.Combine(adjacentDir.Path, "target");
86 
87                 // Move the dir to a different directory under the Watcher
88                 Action action = () => Directory.Move(sourcePath, targetPath);
89                 Action cleanup = () => Directory.Move(targetPath, sourcePath);
90 
91                 ExpectEvent(watcher, eventType, action, cleanup, new string[] { sourceDir.Path, adjacentDir.Path });
92             }
93         }
94 
DirectoryMove_FromWatchedToUnwatched(WatcherChangeTypes eventType)95         private void DirectoryMove_FromWatchedToUnwatched(WatcherChangeTypes eventType)
96         {
97             using (var watchedTestDirectory = new TempDirectory(GetTestFilePath()))
98             using (var unwatchedTestDirectory = new TempDirectory(GetTestFilePath()))
99             using (var dir = new TempDirectory(Path.Combine(watchedTestDirectory.Path, "dir")))
100             using (var watcher = new FileSystemWatcher(watchedTestDirectory.Path, "*"))
101             {
102                 string sourcePath = dir.Path; // watched
103                 string targetPath = Path.Combine(unwatchedTestDirectory.Path, "target");
104 
105                 Action action = () => Directory.Move(sourcePath, targetPath);
106                 Action cleanup = () => Directory.Move(targetPath, sourcePath);
107 
108                 ExpectEvent(watcher, eventType, action, cleanup, sourcePath);
109             }
110         }
111 
DirectoryMove_FromUnwatchedToWatched(WatcherChangeTypes eventType)112         private void DirectoryMove_FromUnwatchedToWatched(WatcherChangeTypes eventType)
113         {
114             using (var watchedTestDirectory = new TempDirectory(GetTestFilePath()))
115             using (var unwatchedTestDirectory = new TempDirectory(GetTestFilePath()))
116             using (var dir = new TempDirectory(Path.Combine(unwatchedTestDirectory.Path, "dir")))
117             using (var watcher = new FileSystemWatcher(watchedTestDirectory.Path, "*"))
118             {
119                 string sourcePath = dir.Path; // unwatched
120                 string targetPath = Path.Combine(watchedTestDirectory.Path, "target");
121 
122                 Action action = () => Directory.Move(sourcePath, targetPath);
123                 Action cleanup = () => Directory.Move(targetPath, sourcePath);
124 
125                 ExpectEvent(watcher, eventType, action, cleanup, targetPath);
126             }
127         }
128 
DirectoryMove_NestedDirectory(WatcherChangeTypes eventType, bool includeSubdirectories)129         private void DirectoryMove_NestedDirectory(WatcherChangeTypes eventType, bool includeSubdirectories)
130         {
131             using (var dir = new TempDirectory(GetTestFilePath()))
132             using (var firstDir = new TempDirectory(Path.Combine(dir.Path, "first")))
133             using (var secondDir = new TempDirectory(Path.Combine(firstDir.Path, "second")))
134             using (var nestedDir = new TempDirectory(Path.Combine(secondDir.Path, "nested")))
135             using (var watcher = new FileSystemWatcher(dir.Path, "*"))
136             {
137                 watcher.IncludeSubdirectories = includeSubdirectories;
138                 watcher.NotifyFilter = NotifyFilters.DirectoryName;
139 
140                 string sourcePath = nestedDir.Path;
141                 string targetPath = nestedDir.Path + "_2";
142 
143                 // Move the dir to a different directory within the same nested directory
144                 Action action = () => Directory.Move(sourcePath, targetPath);
145                 Action cleanup = () => Directory.Move(targetPath, sourcePath);
146 
147                 ExpectEvent(watcher, eventType, action, cleanup, targetPath);
148             }
149         }
150 
DirectoryMove_WithNotifyFilter(WatcherChangeTypes eventType)151         private void DirectoryMove_WithNotifyFilter(WatcherChangeTypes eventType)
152         {
153             using (var testDirectory = new TempDirectory(GetTestFilePath()))
154             using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
155             using (var watcher = new FileSystemWatcher(testDirectory.Path, "*"))
156             {
157                 watcher.NotifyFilter = NotifyFilters.DirectoryName;
158 
159                 string sourcePath = dir.Path;
160                 string targetPath = dir.Path + "_2";
161 
162                 Action action = () => Directory.Move(sourcePath, targetPath);
163                 Action cleanup = () => Directory.Move(targetPath, sourcePath);
164 
165                 ExpectEvent(watcher, eventType, action, cleanup, targetPath);
166             }
167         }
168 
169         #endregion
170     }
171 }