Skip to content

Commit

Permalink
Revert FileSystemEventArgs/RenamedEventArgs FullPath/OldFullPath chan…
Browse files Browse the repository at this point in the history
…ges (dotnet#68883)

* Revert daec9dc (PR dotnet#68582)

* Revert 2a1b15d (PR dotnet#63051)

* Restore additional tests adapted to match prior behavior

* Remove unnecessary PathInternal inclusion

* PR feedback

* Fix unix tests (copy/paste error)
  • Loading branch information
jeffhandley authored May 5, 2022
1 parent 475c8a8 commit 45f15a5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 60 deletions.
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

0 comments on commit 45f15a5

Please sign in to comment.