Skip to content

Commit

Permalink
Allow empty directory values in FileSystemEventArgs/RenamedEventArgs …
Browse files Browse the repository at this point in the history
…ctors (dotnet#68582)

* Allow empty directory values in FileSystemEventArgs/RenamedEventArgs ctors

* Apply PR feedback for simpler implementation, and allow whitespace

* Apply PR feedback
  • Loading branch information
jeffhandley authored Apr 29, 2022
1 parent 095e4d0 commit daec9dc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ public class FileSystemEventArgs : EventArgs
/// </devdoc>
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);
}

/// <devdoc>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <devdoc>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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]
Expand All @@ -80,7 +103,6 @@ public static void FileSystemEventArgs_ctor_When_EmptyFileName_Then_FullPathRetu
public static void FileSystemEventArgs_ctor_Invalid()
{
Assert.Throws<ArgumentNullException>(() => new FileSystemEventArgs((WatcherChangeTypes)0, null, "foo.txt"));
Assert.Throws<ArgumentException>(() => new FileSystemEventArgs((WatcherChangeTypes)0, "", "foo.txt"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -83,7 +89,6 @@ public static void RenamedEventArgs_ctor_When_EmptyOldFileName_Then_OldFullPathR
[Fact]
public static void RenamedEventArgs_ctor_Invalid()
{
Assert.Throws<ArgumentException>(() => new RenamedEventArgs((WatcherChangeTypes)0, "", "foo.txt", "bar.txt"));
Assert.Throws<ArgumentNullException>(() => new RenamedEventArgs((WatcherChangeTypes)0, null, "foo.txt", "bar.txt"));
}
}
Expand Down

0 comments on commit daec9dc

Please sign in to comment.