Skip to content

Commit

Permalink
Directory.Move behaves differently on Unix vs. Windows (dotnet#37121)
Browse files Browse the repository at this point in the history
* Directory.Move behaves differently on Unix vs. Windows

We will now throw an exception in Unix platforms like in Windows, if the destination path already exists.

* Modifying ExistingDirectory test method that verifies Move overwrite so its called by all platforms.

We already had a test method that tests Move when the destination path exists. I simply removed the platform attribute that restricted it to Windows and renamed the test method so its universal (as opposed to platform specific).
  • Loading branch information
carlossanlop committed Apr 23, 2019
1 parent 17382de commit f9c85c0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@ public static void MoveDirectory(string sourceFullPath, string destFullPath)
destFullPath = PathInternal.TrimEndingDirectorySeparator(destFullPath);
}

if (FileExists(destFullPath))
{
// Some Unix distros will overwrite the destination file if it already exists.
// Throwing IOException to match Windows behavior.
throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destFullPath));
}

if (Interop.Sys.Rename(sourceFullPath, destFullPath) < 0)
{
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
Expand Down
4 changes: 2 additions & 2 deletions src/System.IO.FileSystem/tests/Directory/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ public void UnixWhitespacePath()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Moving to existing directory causes IOException
public void WindowsExistingDirectory()
// Moving to existing directory causes IOException
public void ExistingDirectory()
{
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
string testDirSource = Path.Combine(testDir.FullName, GetTestFileName());
Expand Down

0 comments on commit f9c85c0

Please sign in to comment.