Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<Compile Include="$(CommonPath)System\IO\PathInternal.CaseSensitivity.cs"
Link="Common\System\IO\PathInternal.CaseSensitivity.cs" />
<Compile Include="$(CommonPath)System\IO\PathInternal.cs"
Link="Common\System\IO\PathInternal.cs" />
Link="Common\System\IO\PathInternal.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
Link="Common\System\Text\ValueStringBuilder.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@ public FileSystemEventArgs(WatcherChangeTypes changeType, string directory, stri

_changeType = changeType;
_name = name;
_fullPath = Combine(directory, name);
}

if (directory.Length == 0) // empty directory means current working dir
{
directory = ".";
}
/// <summary>Combines a directory path and a relative file name into a single path.</summary>
/// <param name="directory">The directory path.</param>
/// <param name="name">The file name.</param>
/// <returns>The combined name.</returns>
/// <remarks>
/// This is like Path.Combine, except without argument validation,
/// and a separator is used even if the name argument is empty.
/// </remarks>
internal static string Combine(string directory, string? name)
{
bool hasSeparator = directory.Length != 0 && PathInternal.IsDirectorySeparator(directory[^1]);

string fullDirectoryPath = Path.GetFullPath(directory);
_fullPath = string.IsNullOrEmpty(name) ? PathInternal.EnsureTrailingSeparator(fullDirectoryPath) : Path.Join(fullDirectoryPath, name);
return hasSeparator ?
directory + name :
directory + Path.DirectorySeparatorChar + name;
}

/// <devdoc>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ public RenamedEventArgs(WatcherChangeTypes changeType, string directory, string?
: base(changeType, directory, name)
{
_oldName = oldName;

if (directory.Length == 0) // empty directory means current working dir
{
directory = ".";
}

string fullDirectoryPath = Path.GetFullPath(directory);
_oldFullPath = string.IsNullOrEmpty(oldName) ? PathInternal.EnsureTrailingSeparator(fullDirectoryPath) : Path.Join(fullDirectoryPath, oldName);
_oldFullPath = Combine(directory, oldName);
}

/// <devdoc>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace System.IO.Tests
public class FileSystemEventArgsTests
{
[Theory]
[InlineData(WatcherChangeTypes.Changed, "bar", "foo.txt")]
[InlineData(WatcherChangeTypes.All, "bar", "foo.txt")]
[InlineData((WatcherChangeTypes)0, "bar", "")]
[InlineData((WatcherChangeTypes)0, "bar", null)]
[InlineData(WatcherChangeTypes.Changed, "C:", "foo.txt")]
[InlineData(WatcherChangeTypes.All, "C:", "foo.txt")]
[InlineData((WatcherChangeTypes)0, "", "")]
[InlineData((WatcherChangeTypes)0, "", null)]
public static void FileSystemEventArgs_ctor_NonPathPropertiesAreSetCorrectly(WatcherChangeTypes changeType, string directory, string name)
{
FileSystemEventArgs args = new FileSystemEventArgs(changeType, directory, name);
Expand Down Expand Up @@ -59,44 +59,52 @@ 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)
[InlineData("", "", "\\")]
[InlineData("", "foo.txt", "\\foo.txt")]
[InlineData("bar", "foo.txt", "bar\\foo.txt")]
[InlineData("bar\\baz", "foo.txt", "bar\\baz\\foo.txt")]
public static void FileSystemEventArgs_ctor_DirectoryIsRelativePath_Windows(string directory, string name, string expectedFullPath)
{
FileSystemEventArgs args = new FileSystemEventArgs(WatcherChangeTypes.All, directory, name);

var expectedDirectory = PathInternal.EnsureTrailingSeparator(Path.Combine(Directory.GetCurrentDirectory(), directory));
Assert.Equal(Path.Combine(expectedDirectory, name), args.FullPath);
Assert.Equal(expectedFullPath, 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)
[InlineData("", "", "/")]
[InlineData("", "foo.txt", "/foo.txt")]
[InlineData(" ", " ", " / ")]
[InlineData(" ", "foo.txt", " /foo.txt")]
[InlineData("bar", "foo.txt", "bar/foo.txt")]
[InlineData("bar/baz", "foo.txt", "bar/baz/foo.txt")]
public static void FileSystemEventArgs_ctor_DirectoryIsRelativePath_Unix(string directory, string name, string expectedFullPath)
{
FileSystemEventArgs args = new FileSystemEventArgs(WatcherChangeTypes.All, directory, name);

var expectedDirectory = PathInternal.EnsureTrailingSeparator(Path.Combine(Directory.GetCurrentDirectory(), directory));
Assert.Equal(Path.Combine(expectedDirectory, name), args.FullPath);
Assert.Equal(expectedFullPath, args.FullPath);
}

[Theory]
[InlineData("bar", "")]
[InlineData("bar", null)]
public static void FileSystemEventArgs_ctor_When_EmptyFileName_Then_FullPathReturnsTheDirectoryFullPath_WithTrailingSeparator(string directory, string name)
[PlatformSpecific(TestPlatforms.Windows)]
[InlineData("bar", "", "bar\\")]
[InlineData("bar", null, "bar\\")]
public static void FileSystemEventArgs_ctor_EmptyFileName_Windows(string directory, string name, string expectedFullPath)
{
FileSystemEventArgs args = new FileSystemEventArgs(WatcherChangeTypes.All, directory, name);

directory = PathInternal.EnsureTrailingSeparator(directory);
Assert.Equal(expectedFullPath, args.FullPath);
}

Assert.Equal(PathInternal.EnsureTrailingSeparator(Directory.GetCurrentDirectory()) + directory, args.FullPath);
[Theory]
[PlatformSpecific(TestPlatforms.AnyUnix)]
[InlineData("bar", "", "bar/")]
[InlineData("bar", null, "bar/")]
public static void FileSystemEventArgs_ctor_EmptyFileName_Unix(string directory, string name, string expectedFullPath)
{
FileSystemEventArgs args = new FileSystemEventArgs(WatcherChangeTypes.All, directory, name);

Assert.Equal(expectedFullPath, args.FullPath);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,52 @@ 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)
[InlineData("", "", "", "\\")]
[InlineData("", "foo.txt", "bar.txt", "\\bar.txt")]
[InlineData("bar", "foo.txt", "bar.txt", "bar\\bar.txt")]
[InlineData("bar\\baz", "foo.txt", "bar.txt", "bar\\baz\\bar.txt")]
public static void RenamedEventArgs_ctor_OldFullPath_DirectoryIsRelativePath_Windows(string directory, string name, string oldName, string expectedOldFullPath)
{
RenamedEventArgs args = new RenamedEventArgs(WatcherChangeTypes.All, directory, name, oldName);

var expectedDirectory = PathInternal.EnsureTrailingSeparator(Path.Combine(Directory.GetCurrentDirectory(), directory));
Assert.Equal(Path.Combine(expectedDirectory, oldName), args.OldFullPath);
Assert.Equal(expectedOldFullPath, 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)
[InlineData("", "", "", "/")]
[InlineData("", "foo.txt", "bar.txt", "/bar.txt")]
[InlineData("bar", "foo.txt", "bar.txt", "bar/bar.txt")]
[InlineData("bar/baz", "foo.txt", "bar.txt", "bar/baz/bar.txt")]
public static void RenamedEventArgs_ctor_OldFullPath_DirectoryIsRelativePath_Unix(string directory, string name, string oldName, string expectedOldFullPath)
{
RenamedEventArgs args = new RenamedEventArgs(WatcherChangeTypes.All, directory, name, oldName);

var expectedDirectory = PathInternal.EnsureTrailingSeparator(Path.Combine(Directory.GetCurrentDirectory(), directory));
Assert.Equal(Path.Combine(expectedDirectory, oldName), args.OldFullPath);
Assert.Equal(expectedOldFullPath, args.OldFullPath);
}

[Theory]
[InlineData("bar", "", "")]
[InlineData("bar", null, null)]
[InlineData("bar", "foo.txt", null)]
public static void RenamedEventArgs_ctor_When_EmptyOldFileName_Then_OldFullPathReturnsTheDirectoryFullPath_WithTrailingSeparator(string directory, string name, string oldName)
[PlatformSpecific(TestPlatforms.Windows)]
[InlineData("bar", "", "", "bar\\")]
[InlineData("bar", null, null, "bar\\")]
[InlineData("bar", "foo.txt", null, "bar\\")]
public static void RenamedEventArgs_ctor_EmptyOldFileName_Windows(string directory, string name, string oldName, string expectedOldFullPath)
{
RenamedEventArgs args = new RenamedEventArgs(WatcherChangeTypes.All, directory, name, oldName);

directory = PathInternal.EnsureTrailingSeparator(directory);
Assert.Equal(expectedOldFullPath, args.OldFullPath);
}

Assert.Equal(PathInternal.EnsureTrailingSeparator(Directory.GetCurrentDirectory()) + directory, args.OldFullPath);
[Theory]
[PlatformSpecific(TestPlatforms.AnyUnix)]
[InlineData("bar", "", "", "bar/")]
[InlineData("bar", null, null, "bar/")]
[InlineData("bar", "foo.txt", null, "bar/")]
public static void RenamedEventArgs_ctor_EmptyOldFileName_Unix(string directory, string name, string oldName, string expectedOldFullPath)
{
RenamedEventArgs args = new RenamedEventArgs(WatcherChangeTypes.All, directory, name, oldName);

Assert.Equal(expectedOldFullPath, args.OldFullPath);
}

[Fact]
Expand Down