PageRenderTime 170ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/0072016/0072016-corefx-
C# | 238 lines | 167 code | 30 blank | 41 comment | 4 complexity | b9ff9490ee9099b84049fe30d949395a 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;
  5. using System.IO;
  6. using System.Threading;
  7. using Xunit;
  8. public class MoveFileTests
  9. {
  10. #region WindowsTests
  11. [Theory]
  12. [InlineData(WatcherChangeTypes.Changed, false)]
  13. [InlineData(WatcherChangeTypes.Created, false)]
  14. [InlineData(WatcherChangeTypes.Deleted, false)]
  15. [InlineData(WatcherChangeTypes.Renamed, true)]
  16. [PlatformSpecific(PlatformID.Windows)]
  17. public static void Windows_File_Move_To_Same_Directory_Triggers_Event(WatcherChangeTypes eventType, bool moveRaisesEvent)
  18. {
  19. MoveAndCheck_SameDirectory(eventType, moveRaisesEvent);
  20. }
  21. [Theory]
  22. [OuterLoop]
  23. [InlineData(WatcherChangeTypes.Changed, false)]
  24. [InlineData(WatcherChangeTypes.Created, false)]
  25. [InlineData(WatcherChangeTypes.Deleted, false)]
  26. [InlineData(WatcherChangeTypes.Renamed, true)]
  27. [PlatformSpecific(PlatformID.Windows)]
  28. public static void Windows_File_Move_To_Different_Directory_Triggers_Event(WatcherChangeTypes eventType, bool moveRaisesEvent)
  29. {
  30. MoveAndCheck_DifferentDirectory(eventType, moveRaisesEvent);
  31. }
  32. [Theory]
  33. [InlineData(WatcherChangeTypes.Changed, true)]
  34. [InlineData(WatcherChangeTypes.Created, true)]
  35. [InlineData(WatcherChangeTypes.Deleted, false)]
  36. [InlineData(WatcherChangeTypes.Renamed, true)]
  37. [PlatformSpecific(PlatformID.Windows)]
  38. public static void Windows_File_Move_In_Nested_Directory_Triggers_Event(WatcherChangeTypes eventType, bool moveRaisesEvent)
  39. {
  40. MoveAndCheck_NestedDirectory(eventType, moveRaisesEvent);
  41. }
  42. [Theory]
  43. [InlineData(WatcherChangeTypes.Changed, false)]
  44. [InlineData(WatcherChangeTypes.Created, false)]
  45. [InlineData(WatcherChangeTypes.Deleted, false)]
  46. [InlineData(WatcherChangeTypes.Renamed, true)]
  47. [PlatformSpecific(PlatformID.Windows)]
  48. public static void Windows_File_Move_With_Set_NotifyFilter_Triggers_Event(WatcherChangeTypes eventType, bool moveRaisesEvent)
  49. {
  50. MoveAndCheck_WithNotifyFilter(eventType, moveRaisesEvent);
  51. }
  52. #endregion
  53. #region UnixTests
  54. [Theory]
  55. [InlineData(WatcherChangeTypes.Changed, false)]
  56. [InlineData(WatcherChangeTypes.Created, true)]
  57. [InlineData(WatcherChangeTypes.Deleted, true)]
  58. [InlineData(WatcherChangeTypes.Renamed, false)]
  59. [PlatformSpecific(PlatformID.AnyUnix)]
  60. public static void Unix_File_Move_To_Same_Directory_Triggers_Event(WatcherChangeTypes eventType, bool moveRaisesEvent)
  61. {
  62. MoveAndCheck_SameDirectory(eventType, moveRaisesEvent);
  63. }
  64. [Theory]
  65. [OuterLoop]
  66. [InlineData(WatcherChangeTypes.Changed, false)]
  67. [InlineData(WatcherChangeTypes.Created, true)]
  68. [InlineData(WatcherChangeTypes.Deleted, true)]
  69. [InlineData(WatcherChangeTypes.Renamed, false)]
  70. [PlatformSpecific(PlatformID.AnyUnix)]
  71. public static void Unix_File_Move_To_Different_Directory_Triggers_Event(WatcherChangeTypes eventType, bool moveRaisesEvent)
  72. {
  73. MoveAndCheck_DifferentDirectory(eventType, moveRaisesEvent);
  74. }
  75. [Theory, OuterLoop]
  76. [InlineData(WatcherChangeTypes.Changed, false)]
  77. [InlineData(WatcherChangeTypes.Created, true)]
  78. [InlineData(WatcherChangeTypes.Deleted, true)]
  79. [InlineData(WatcherChangeTypes.Renamed, false)]
  80. [PlatformSpecific(PlatformID.AnyUnix)]
  81. [ActiveIssue(3215, PlatformID.OSX)] // failing for Changed, false
  82. public static void Unix_File_Move_In_Nested_Directory_Triggers_Event(WatcherChangeTypes eventType, bool moveRaisesEvent)
  83. {
  84. MoveAndCheck_NestedDirectory(eventType, moveRaisesEvent);
  85. }
  86. [Theory]
  87. [InlineData(WatcherChangeTypes.Changed, false)]
  88. [InlineData(WatcherChangeTypes.Created, false)]
  89. [InlineData(WatcherChangeTypes.Deleted, true)]
  90. [InlineData(WatcherChangeTypes.Renamed, false)]
  91. [PlatformSpecific(PlatformID.AnyUnix)]
  92. public static void Unix_File_Move_With_Set_NotifyFilter_Triggers_Event(WatcherChangeTypes eventType, bool moveRaisesEvent)
  93. {
  94. MoveAndCheck_WithNotifyFilter(eventType, moveRaisesEvent);
  95. }
  96. #endregion
  97. #region TestHelpers
  98. /// <summary>
  99. /// Sets up watchers for the type given before performing a File.Move operation and checking for
  100. /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
  101. /// we ensure that it is not observed.
  102. ///
  103. /// This test will move the source file to a destination file in the same directory i.e. rename it
  104. /// </summary>
  105. private static void MoveAndCheck_SameDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
  106. {
  107. using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString()))
  108. using (var watcher = new FileSystemWatcher())
  109. {
  110. // put everything in our own directory to avoid collisions
  111. watcher.Path = Path.GetFullPath(dir.Path);
  112. watcher.Filter = "*.*";
  113. // create a file
  114. using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file")))
  115. {
  116. watcher.EnableRaisingEvents = true;
  117. AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, eventType);
  118. // Move the testFile to a different name in the same directory
  119. testFile.Move(testFile.Path + "_" + eventType.ToString());
  120. // Test that the event is observed or not observed
  121. if (moveRaisesEvent)
  122. Utility.ExpectEvent(eventOccurred, eventType.ToString());
  123. else
  124. Utility.ExpectNoEvent(eventOccurred, eventType.ToString());
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// Sets up watchers for the type given before performing a File.Move operation and checking for
  130. /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
  131. /// we ensure that it is not observed.
  132. ///
  133. /// This test checks for when the file being moved has a destination directory that is outside of
  134. /// the path of the FileSystemWatcher.
  135. /// </summary>
  136. private static void MoveAndCheck_DifferentDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
  137. {
  138. using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString()))
  139. using (var dir_unwatched = new TemporaryTestDirectory(Path.GetRandomFileName()))
  140. using (var watcher = new FileSystemWatcher())
  141. {
  142. // put everything in our own directory to avoid collisions
  143. watcher.Path = Path.GetFullPath(dir.Path);
  144. watcher.Filter = "*.*";
  145. // create a file
  146. using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file")))
  147. {
  148. watcher.EnableRaisingEvents = true;
  149. AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, eventType);
  150. // Move the testFile to a different name in the same directory
  151. testFile.Move(Path.Combine(dir_unwatched.Path, testFile.Name + "_" + eventType.ToString()));
  152. // Test which events are thrown
  153. if (moveRaisesEvent)
  154. Utility.ExpectEvent(eventOccurred, eventType.ToString());
  155. else
  156. Utility.ExpectNoEvent(eventOccurred, eventType.ToString());
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// Sets up watchers for the type given before performing a File.Move operation and checking for
  162. /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
  163. /// we ensure that it is not observed.
  164. ///
  165. /// This test will move the source file of a file within a nested directory
  166. /// </summary>
  167. private static void MoveAndCheck_NestedDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent)
  168. {
  169. Utility.TestNestedDirectoriesHelper(eventType, (AutoResetEvent eventOccurred, TemporaryTestDirectory ttd) =>
  170. {
  171. using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile" + eventType.ToString())))
  172. {
  173. nestedFile.Move(nestedFile.Path + "_2");
  174. if (moveRaisesEvent)
  175. Utility.ExpectEvent(eventOccurred, eventType.ToString());
  176. else
  177. Utility.ExpectNoEvent(eventOccurred, eventType.ToString());
  178. }
  179. });
  180. }
  181. /// <summary>
  182. /// Sets up watchers for the type given before performing a File.Move operation and checking for
  183. /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false,
  184. /// we ensure that it is not observed.
  185. ///
  186. /// This test will use the NotifyFilter attribute of the FileSystemWatcher before the move and subsequent
  187. /// checks are made.
  188. /// </summary>
  189. private static void MoveAndCheck_WithNotifyFilter(WatcherChangeTypes eventType, bool moveRaisesEvent)
  190. {
  191. using (var file = Utility.CreateTestFile(Guid.NewGuid().ToString()))
  192. using (var watcher = new FileSystemWatcher("."))
  193. {
  194. watcher.NotifyFilter = NotifyFilters.FileName;
  195. watcher.Filter = Path.GetFileName(file.Path);
  196. AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, eventType);
  197. string newName = file.Path + "_" + eventType.ToString();
  198. Utility.EnsureDelete(newName);
  199. watcher.EnableRaisingEvents = true;
  200. file.Move(newName);
  201. if (moveRaisesEvent)
  202. Utility.ExpectEvent(eventOccurred, eventType.ToString());
  203. else
  204. Utility.ExpectNoEvent(eventOccurred, eventType.ToString());
  205. }
  206. }
  207. #endregion
  208. }