diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemEventArgs.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemEventArgs.cs index 19e6b9b3443576..5795394e13dfe7 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemEventArgs.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemEventArgs.cs @@ -17,14 +17,18 @@ public class FileSystemEventArgs : EventArgs /// public FileSystemEventArgs(WatcherChangeTypes changeType, string directory, string? name) { + ArgumentNullException.ThrowIfNull(directory); + _changeType = changeType; _name = name; - _fullPath = Path.Join(Path.GetFullPath(directory), name); - if (string.IsNullOrWhiteSpace(name)) + if (directory.Length == 0) // empty directory means current working dir { - _fullPath = PathInternal.EnsureTrailingSeparator(_fullPath); + directory = "."; } + + string fullDirectoryPath = Path.GetFullPath(directory); + _fullPath = string.IsNullOrEmpty(name) ? PathInternal.EnsureTrailingSeparator(fullDirectoryPath) : Path.Join(fullDirectoryPath, name); } /// diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/RenamedEventArgs.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/RenamedEventArgs.cs index 72dc9765f05fb4..17221c568dc32a 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/RenamedEventArgs.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/RenamedEventArgs.cs @@ -18,12 +18,14 @@ public RenamedEventArgs(WatcherChangeTypes changeType, string directory, string? : base(changeType, directory, name) { _oldName = oldName; - _oldFullPath = Path.Join(Path.GetFullPath(directory), oldName); - if (string.IsNullOrWhiteSpace(oldName)) + if (directory.Length == 0) // empty directory means current working dir { - _oldFullPath = PathInternal.EnsureTrailingSeparator(_oldFullPath); + directory = "."; } + + string fullDirectoryPath = Path.GetFullPath(directory); + _oldFullPath = string.IsNullOrEmpty(oldName) ? PathInternal.EnsureTrailingSeparator(fullDirectoryPath) : Path.Join(fullDirectoryPath, oldName); } /// diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/Args.FileSystemEventArgs.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/Args.FileSystemEventArgs.cs index 8b63e13e5a825b..d3abb21d9a2d3f 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/Args.FileSystemEventArgs.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/Args.FileSystemEventArgs.cs @@ -22,8 +22,15 @@ public static void FileSystemEventArgs_ctor_NonPathPropertiesAreSetCorrectly(Wat [Theory] [PlatformSpecific(TestPlatforms.Windows)] + [InlineData("D:\\", null, "D:\\")] + [InlineData("D:\\", "", "D:\\")] [InlineData("D:\\", "foo.txt", "D:\\foo.txt")] + [InlineData("E:\\bar", null, "E:\\bar\\")] + [InlineData("E:\\bar", "", "E:\\bar\\")] [InlineData("E:\\bar", "foo.txt", "E:\\bar\\foo.txt")] + [InlineData("E:\\bar\\", null, "E:\\bar\\")] + [InlineData("E:\\bar\\", "", "E:\\bar\\")] + [InlineData("E:\\bar\\", "foo.txt", "E:\\bar\\foo.txt")] public static void FileSystemEventArgs_ctor_DirectoryIsAbsolutePath_Windows(string directory, string name, string expectedFullPath) { FileSystemEventArgs args = new FileSystemEventArgs(WatcherChangeTypes.All, directory, name); @@ -33,8 +40,16 @@ public static void FileSystemEventArgs_ctor_DirectoryIsAbsolutePath_Windows(stri [Theory] [PlatformSpecific(TestPlatforms.AnyUnix)] + [InlineData("/", null, "/")] + [InlineData("/", "", "/")] + [InlineData("/", " ", "/ ")] [InlineData("/", "foo.txt", "/foo.txt")] + [InlineData("/bar", null, "/bar/")] + [InlineData("/bar", "", "/bar/")] [InlineData("/bar", "foo.txt", "/bar/foo.txt")] + [InlineData("/bar/", null, "/bar/")] + [InlineData("/bar/", "", "/bar/")] + [InlineData("/bar/", "foo.txt", "/bar/foo.txt")] public static void FileSystemEventArgs_ctor_DirectoryIsAbsolutePath_Unix(string directory, string name, string expectedFullPath) { FileSystemEventArgs args = new FileSystemEventArgs(WatcherChangeTypes.All, directory, name); @@ -44,24 +59,32 @@ public static void FileSystemEventArgs_ctor_DirectoryIsAbsolutePath_Unix(string [Theory] [PlatformSpecific(TestPlatforms.Windows)] + [InlineData("", "")] + [InlineData("", "foo.txt")] [InlineData("bar", "foo.txt")] [InlineData("bar\\baz", "foo.txt")] public static void FileSystemEventArgs_ctor_DirectoryIsRelativePath_Windows(string directory, string name) { FileSystemEventArgs args = new FileSystemEventArgs(WatcherChangeTypes.All, directory, name); - Assert.Equal(Path.Combine(Directory.GetCurrentDirectory(), directory, name), args.FullPath); + var expectedDirectory = PathInternal.EnsureTrailingSeparator(Path.Combine(Directory.GetCurrentDirectory(), directory)); + Assert.Equal(Path.Combine(expectedDirectory, name), args.FullPath); } [Theory] [PlatformSpecific(TestPlatforms.AnyUnix)] + [InlineData("", "")] + [InlineData("", "foo.txt")] + [InlineData(" ", " ")] + [InlineData(" ", "foo.txt")] [InlineData("bar", "foo.txt")] [InlineData("bar/baz", "foo.txt")] public static void FileSystemEventArgs_ctor_DirectoryIsRelativePath_Unix(string directory, string name) { FileSystemEventArgs args = new FileSystemEventArgs(WatcherChangeTypes.All, directory, name); - Assert.Equal(Path.Combine(Directory.GetCurrentDirectory(), directory, name), args.FullPath); + var expectedDirectory = PathInternal.EnsureTrailingSeparator(Path.Combine(Directory.GetCurrentDirectory(), directory)); + Assert.Equal(Path.Combine(expectedDirectory, name), args.FullPath); } [Theory] @@ -80,7 +103,6 @@ public static void FileSystemEventArgs_ctor_When_EmptyFileName_Then_FullPathRetu public static void FileSystemEventArgs_ctor_Invalid() { Assert.Throws(() => new FileSystemEventArgs((WatcherChangeTypes)0, null, "foo.txt")); - Assert.Throws(() => new FileSystemEventArgs((WatcherChangeTypes)0, "", "foo.txt")); } } } 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 2084c6cd31d7dc..3a504a668b0872 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/Args.RenamedEventArgs.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/Args.RenamedEventArgs.cs @@ -47,24 +47,30 @@ public static void RenamedEventArgs_ctor_OldFullPath_DirectoryIsAnAbsolutePath_U [Theory] [PlatformSpecific(TestPlatforms.Windows)] + [InlineData("", "", "")] + [InlineData("", "foo.txt", "bar.txt")] [InlineData("bar", "foo.txt", "bar.txt")] [InlineData("bar\\baz", "foo.txt", "bar.txt")] public static void RenamedEventArgs_ctor_OldFullPath_DirectoryIsRelativePath_Windows(string directory, string name, string oldName) { RenamedEventArgs args = new RenamedEventArgs(WatcherChangeTypes.All, directory, name, oldName); - Assert.Equal(Path.Combine(Directory.GetCurrentDirectory(), directory, oldName), args.OldFullPath); + var expectedDirectory = PathInternal.EnsureTrailingSeparator(Path.Combine(Directory.GetCurrentDirectory(), directory)); + Assert.Equal(Path.Combine(expectedDirectory, oldName), args.OldFullPath); } [Theory] [PlatformSpecific(TestPlatforms.AnyUnix)] + [InlineData("", "", "")] + [InlineData("", "foo.txt", "bar.txt")] [InlineData("bar", "foo.txt", "bar.txt")] [InlineData("bar/baz", "foo.txt", "bar.txt")] public static void RenamedEventArgs_ctor_OldFullPath_DirectoryIsRelativePath_Unix(string directory, string name, string oldName) { RenamedEventArgs args = new RenamedEventArgs(WatcherChangeTypes.All, directory, name, oldName); - Assert.Equal(Path.Combine(Directory.GetCurrentDirectory(), directory, oldName), args.OldFullPath); + var expectedDirectory = PathInternal.EnsureTrailingSeparator(Path.Combine(Directory.GetCurrentDirectory(), directory)); + Assert.Equal(Path.Combine(expectedDirectory, oldName), args.OldFullPath); } [Theory] @@ -83,7 +89,6 @@ public static void RenamedEventArgs_ctor_When_EmptyOldFileName_Then_OldFullPathR [Fact] public static void RenamedEventArgs_ctor_Invalid() { - Assert.Throws(() => new RenamedEventArgs((WatcherChangeTypes)0, "", "foo.txt", "bar.txt")); Assert.Throws(() => new RenamedEventArgs((WatcherChangeTypes)0, null, "foo.txt", "bar.txt")); } }