diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/Args.RenamedEventArgs.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/Args.RenamedEventArgs.cs index 888ac6342bb..8fe29265ca1 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/Args.RenamedEventArgs.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/Args.RenamedEventArgs.cs @@ -16,20 +16,24 @@ namespace System.IO.Tests public static void RenamedEventArgs_ctor(WatcherChangeTypes changeType, string directory, string name, string oldName) { RenamedEventArgs args = new RenamedEventArgs(changeType, directory, name, oldName); - - if (!directory.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)) - { - directory += Path.DirectorySeparatorChar; - } - Assert.Equal(changeType, args.ChangeType); - Assert.Equal(directory + name, args.FullPath); + Assert.Equal(directory + Path.DirectorySeparatorChar + name, args.FullPath); Assert.Equal(name, args.Name); - - Assert.Equal(directory + oldName, args.OldFullPath); Assert.Equal(oldName, args.OldName); } + [Theory] + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "OldFullPath on Desktop demands FileIOPermissions which causes failures for invalid paths.")] + [InlineData(WatcherChangeTypes.Changed, "C:", "foo.txt", "bar.txt")] + [InlineData(WatcherChangeTypes.All, "C:", "foo.txt", "bar.txt")] + [InlineData(0, "", "", "")] + [InlineData(0, "", null, null)] + public static void RenamedEventArgs_ctor_OldFullPath(WatcherChangeTypes changeType, string directory, string name, string oldName) + { + RenamedEventArgs args = new RenamedEventArgs(changeType, directory, name, oldName); + Assert.Equal(directory + Path.DirectorySeparatorChar + oldName, args.OldFullPath); + } + [Fact] public static void RenamedEventArgs_ctor_Invalid() { diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Changed.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Changed.cs index 960af3a0fc1..892b71da9a1 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Changed.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Changed.cs @@ -24,6 +24,7 @@ namespace System.IO.Tests } [Fact] + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Desktop FSW disallows modification of the watched folder.")] public void FileSystemWatcher_Directory_Changed_WatchedFolder() { using (var testDirectory = new TempDirectory(GetTestFilePath())) diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs index dd569210a7c..32809e3b363 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs @@ -92,7 +92,7 @@ namespace System.IO.Tests } [Fact] - public void FileSystemWatcher_ctor_InvalidStrings() + public void FileSystemWatcher_ctor_NullStrings() { using (var testDirectory = new TempDirectory(GetTestFilePath())) { @@ -102,7 +102,15 @@ namespace System.IO.Tests // Null path Assert.Throws("path", () => new FileSystemWatcher(null)); Assert.Throws("path", () => new FileSystemWatcher(null, "*")); + } + } + [Fact] + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "On Desktop, these exceptions don't have a parameter")] + public void FileSystemWatcher_ctor_InvalidStrings() + { + using (var testDirectory = new TempDirectory(GetTestFilePath())) + { // Empty path Assert.Throws("path", () => new FileSystemWatcher(string.Empty)); Assert.Throws("path", () => new FileSystemWatcher(string.Empty, "*")); @@ -305,13 +313,13 @@ namespace System.IO.Tests Assert.Equal((NotifyFilters)0, watcher.NotifyFilter); // These throw InvalidEnumException on desktop, but ArgumentException on K - Assert.Throws(() => watcher.NotifyFilter = (NotifyFilters)(-1)); - Assert.Throws(() => watcher.NotifyFilter = (NotifyFilters)int.MinValue); - Assert.Throws(() => watcher.NotifyFilter = (NotifyFilters)int.MaxValue); - Assert.Throws(() => watcher.NotifyFilter = allFilters + 1); + Assert.ThrowsAny(() => watcher.NotifyFilter = (NotifyFilters)(-1)); + Assert.ThrowsAny(() => watcher.NotifyFilter = (NotifyFilters)int.MinValue); + Assert.ThrowsAny(() => watcher.NotifyFilter = (NotifyFilters)int.MaxValue); + Assert.ThrowsAny(() => watcher.NotifyFilter = allFilters + 1); // Simulate a bit added to the flags - Assert.Throws(() => watcher.NotifyFilter = allFilters | (NotifyFilters)((int)notifyFilters.Max() << 1)); + Assert.ThrowsAny(() => watcher.NotifyFilter = allFilters | (NotifyFilters)((int)notifyFilters.Max() << 1)); } [Fact] @@ -584,32 +592,21 @@ namespace System.IO.Tests // a FSW event callback and make sure we don't Thread.Join to deadlock using (var dir = new TempDirectory(GetTestFilePath())) { - FileSystemWatcher watcher = new FileSystemWatcher(); + string filePath = Path.Combine(dir.Path, "testfile.txt"); + File.Create(filePath).Dispose(); AutoResetEvent are = new AutoResetEvent(false); - + FileSystemWatcher watcher = new FileSystemWatcher(Path.GetFullPath(dir.Path), "*"); FileSystemEventHandler callback = (sender, arg) => { watcher.Dispose(); are.Set(); }; - - // Attach the FSW to the existing structure - watcher.Path = Path.GetFullPath(dir.Path); - watcher.Filter = "*"; - watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; + watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite; watcher.Changed += callback; - - using (var file = File.Create(Path.Combine(dir.Path, "testfile.txt"))) - { - watcher.EnableRaisingEvents = true; - - // Change the nested file and verify we get the changed event - byte[] bt = new byte[4096]; - file.Write(bt, 0, bt.Length); - file.Flush(); - } - - ExpectEvent(are, "deleted"); + watcher.EnableRaisingEvents = true; + File.SetLastWriteTime(filePath, File.GetLastWriteTime(filePath).AddDays(1)); + Assert.True(are.WaitOne(10000)); + Assert.Throws(() => watcher.EnableRaisingEvents = true); } } diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs index 0efc21eaa34..6f31a1e0737 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs @@ -20,7 +20,7 @@ namespace System.IO.Tests // to keep the timeout short, as in a successful run we'll end up waiting for // the entire timeout specified. public const int WaitForExpectedEventTimeout = 500; // ms to wait for an event to happen - public const int LongWaitTimeout = 10000; // ms to wait for an event that takes a longer time than the average operation + public const int LongWaitTimeout = 50000; // ms to wait for an event that takes a longer time than the average operation public const int SubsequentExpectedWait = 10; // ms to wait for checks that occur after the first. public const int WaitForExpectedEventTimeout_NoRetry = 3000;// ms to wait for an event that isn't surrounded by a retry. public const int DefaultAttemptsForExpectedEvent = 3; // Number of times an expected event should be retried if failing. @@ -196,7 +196,7 @@ namespace System.IO.Tests /// True if results should be asserted. Used if there is no retry. /// Adds path verification to all expected events. /// True if the events raised correctly; else, false. - private static bool ExecuteAndVerifyEvents(FileSystemWatcher watcher, WatcherChangeTypes expectedEvents, Action action, bool assertExpected, string[] expectedPaths, int timeout) + public static bool ExecuteAndVerifyEvents(FileSystemWatcher watcher, WatcherChangeTypes expectedEvents, Action action, bool assertExpected, string[] expectedPaths, int timeout) { bool result = true, verifyChanged = true, verifyCreated = true, verifyDeleted = true, verifyRenamed = true; AutoResetEvent changed = null, created = null, deleted = null, renamed = null;