PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Renamed.cs

https://gitlab.com/0072016/0072016-corefx-
C# | 219 lines | 162 code | 34 blank | 23 comment | 0 complexity | b2baf9770a346ab07ed36462359a92cb MD5 | raw file
  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. using System.IO;
  5. using System.Threading;
  6. using Xunit;
  7. public partial class RenamedTests
  8. {
  9. [Fact]
  10. public static void FileSystemWatcher_Renamed_Directory()
  11. {
  12. using (var dir = Utility.CreateTestDirectory())
  13. using (var watcher = new FileSystemWatcher("."))
  14. {
  15. watcher.Filter = Path.GetFileName(dir.Path);
  16. AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Renamed);
  17. string newName = dir.Path + "_rename";
  18. Utility.EnsureDelete(newName);
  19. watcher.EnableRaisingEvents = true;
  20. dir.Move(newName);
  21. Utility.ExpectEvent(eventOccurred, "renamed");
  22. }
  23. }
  24. [Fact]
  25. public static void FileSystemWatcher_Renamed_Negative()
  26. {
  27. using (var dir = Utility.CreateTestDirectory())
  28. using (var watcher = new FileSystemWatcher())
  29. {
  30. // put everything in our own directory to avoid collisions
  31. watcher.Path = Path.GetFullPath(dir.Path);
  32. watcher.Filter = "*.*";
  33. AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Renamed);
  34. watcher.EnableRaisingEvents = true;
  35. // run all scenarios together to avoid unnecessary waits,
  36. // assert information is verbose enough to trace to failure cause
  37. // create a file
  38. using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file")))
  39. using (var testDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir")))
  40. {
  41. // change a file
  42. testFile.WriteByte(0xFF);
  43. testFile.Flush();
  44. // deleting a file & directory by leaving the using block
  45. }
  46. Utility.ExpectNoEvent(eventOccurred, "created");
  47. }
  48. }
  49. [Fact]
  50. public static void FileSystemWatcher_Renamed_NestedDirectory()
  51. {
  52. Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Renamed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
  53. {
  54. ttd.Move(ttd.Path + "_2");
  55. Utility.ExpectEvent(are, "renamed");
  56. });
  57. }
  58. [Fact]
  59. public static void FileSystemWatcher_Renamed_FileInNestedDirectory()
  60. {
  61. Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Renamed | WatcherChangeTypes.Created, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
  62. {
  63. using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile")))
  64. {
  65. Utility.ExpectEvent(are, "file created");
  66. nestedFile.Move(nestedFile.Path + "_2");
  67. Utility.ExpectEvent(are, "renamed");
  68. }
  69. });
  70. }
  71. [Fact, OuterLoop]
  72. // Note: Can't use the TestNestedDirectoriesHelper since we need access to the root
  73. public static void FileSystemWatcher_Moved_NestedDirectoryRoot()
  74. {
  75. // Create a test root with our watch dir and a temp directory since, on the default Ubuntu install, the system
  76. // temp directory is on a different mount point and Directory.Move does not work across mount points.
  77. using (var root = Utility.CreateTestDirectory())
  78. using (var dir = Utility.CreateTestDirectory(Path.Combine(root.Path, "test_root")))
  79. using (var temp = Utility.CreateTestDirectory(Path.Combine(root.Path, "temp")))
  80. using (var watcher = new FileSystemWatcher())
  81. {
  82. AutoResetEvent createdOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks
  83. AutoResetEvent deletedOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted);
  84. watcher.Path = Path.GetFullPath(dir.Path);
  85. watcher.Filter = "*";
  86. watcher.IncludeSubdirectories = true;
  87. watcher.EnableRaisingEvents = true;
  88. using (var dir1 = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
  89. {
  90. Utility.ExpectEvent(createdOccurred, "dir1 created");
  91. using (var dir2 = new TemporaryTestDirectory(Path.Combine(dir1.Path, "dir2")))
  92. {
  93. Utility.ExpectEvent(createdOccurred, "dir2 created");
  94. using (var file = Utility.CreateTestFile(Path.Combine(dir2.Path, "test file"))) { };
  95. // Move the directory out of the watched folder and expect that we get a deleted event
  96. string original = dir1.Path;
  97. string target = Path.Combine(temp.Path, Path.GetFileName(dir1.Path));
  98. dir1.Move(target);
  99. Utility.ExpectEvent(deletedOccurred, "dir1 moved out");
  100. // Move the directory back and expect a created event
  101. dir1.Move(original);
  102. Utility.ExpectEvent(createdOccurred, "dir1 moved back");
  103. }
  104. }
  105. }
  106. }
  107. [Fact]
  108. // Note: Can't use the TestNestedDirectoriesHelper since we need access to the root
  109. public static void FileSystemWatcher_Moved_NestedDirectoryRootWithoutSubdirectoriesFlag()
  110. {
  111. // Create a test root with our watch dir and a temp directory since, on the default Ubuntu install, the system
  112. // temp directory is on a different mount point and Directory.Move does not work across mount points.
  113. using (var root = Utility.CreateTestDirectory())
  114. using (var dir = Utility.CreateTestDirectory(Path.Combine(root.Path, "test_root")))
  115. using (var temp = Utility.CreateTestDirectory(Path.Combine(root.Path, "temp")))
  116. using (var watcher = new FileSystemWatcher())
  117. {
  118. AutoResetEvent createdOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks
  119. AutoResetEvent deletedOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted);
  120. watcher.Path = Path.GetFullPath(dir.Path);
  121. watcher.Filter = "*";
  122. watcher.IncludeSubdirectories = false;
  123. watcher.EnableRaisingEvents = true;
  124. using (var dir1 = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
  125. {
  126. Utility.ExpectEvent(createdOccurred, "dir1 created");
  127. using (var dir2 = new TemporaryTestDirectory(Path.Combine(dir1.Path, "dir2")))
  128. {
  129. Utility.ExpectNoEvent(createdOccurred, "dir2 created");
  130. using (var file = Utility.CreateTestFile(Path.Combine(dir2.Path, "test file"))) { };
  131. // Move the directory out of the watched folder and expect that we get a deleted event
  132. string original = dir1.Path;
  133. string target = Path.Combine(temp.Path, Path.GetFileName(dir1.Path));
  134. dir1.Move(target);
  135. Utility.ExpectEvent(deletedOccurred, "dir1 moved out");
  136. // Move the directory back and expect a created event
  137. dir1.Move(original);
  138. Utility.ExpectEvent(createdOccurred, "dir1 moved back");
  139. }
  140. }
  141. }
  142. }
  143. [Fact]
  144. // Note: Can't use the TestNestedDirectoriesHelper since we need access to the root
  145. public static void FileSystemWatcher_Moved_NestedDirectoryTreeMoveFileAndFolder()
  146. {
  147. using (var root = Utility.CreateTestDirectory())
  148. using (var dir = Utility.CreateTestDirectory(Path.Combine(root.Path, "test_root")))
  149. using (var temp = Utility.CreateTestDirectory(Path.Combine(root.Path, "temp")))
  150. using (var dir1 = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
  151. using (var watcher = new FileSystemWatcher())
  152. {
  153. AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created | WatcherChangeTypes.Deleted | WatcherChangeTypes.Changed);
  154. watcher.Path = Path.GetFullPath(dir.Path);
  155. watcher.Filter = "*";
  156. watcher.IncludeSubdirectories = true;
  157. watcher.EnableRaisingEvents = true;
  158. string filePath = Path.Combine(dir1.Path, "test_file");
  159. using (var file = File.Create(filePath))
  160. {
  161. // Wait for the file to be created then make a change to validate that we get a change
  162. Utility.ExpectEvent(eventOccurred, "test file created");
  163. byte[] buffer = new byte[4096];
  164. file.Write(buffer, 0, buffer.Length);
  165. file.Flush();
  166. }
  167. Utility.ExpectEvent(eventOccurred, "test file changed");
  168. // Move the nested dir out of scope and validate that we get a single deleted event
  169. string original = dir1.Path;
  170. string target = Path.Combine(temp.Path, "dir1");
  171. dir1.Move(target);
  172. Utility.ExpectEvent(eventOccurred, "nested dir deleted");
  173. // Move the dir (and child file) back into scope and validate that we get a created event
  174. dir1.Move(original);
  175. Utility.ExpectEvent(eventOccurred, "nested dir created");
  176. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write))
  177. {
  178. byte[] buffer = new byte[4096];
  179. fs.Write(buffer, 0, buffer.Length);
  180. fs.Flush();
  181. }
  182. Utility.ExpectEvent(eventOccurred, "test file changed");
  183. }
  184. }
  185. }