diff options
author | Alex Perovich <alperovi@microsoft.com> | 2017-04-05 07:20:51 +0300 |
---|---|---|
committer | Dan Moseley <danmose@microsoft.com> | 2017-04-05 07:20:51 +0300 |
commit | 074bf02532c4c4496e7c79e03b4801e848848509 (patch) | |
tree | 36297267bb6475644e9395e89c7b2437a7808392 /src | |
parent | debd8e917e26b40f76e47400cdd02db38472a8d5 (diff) |
Check if destination exists in Directory.Move (#17913)
* Check if destination exists in Directory.Move
Fixes #12396
* Fix tests
* Update DirectoryInfo.MoveTo as well
Diffstat (limited to 'src')
-rw-r--r-- | src/System.IO.FileSystem/src/System/IO/Directory.cs | 6 | ||||
-rw-r--r-- | src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs | 6 | ||||
-rw-r--r-- | src/System.IO.FileSystem/tests/Directory/Move.cs | 44 |
3 files changed, 30 insertions, 26 deletions
diff --git a/src/System.IO.FileSystem/src/System/IO/Directory.cs b/src/System.IO.FileSystem/src/System/IO/Directory.cs index 3187d75e36..44271c579b 100644 --- a/src/System.IO.FileSystem/src/System/IO/Directory.cs +++ b/src/System.IO.FileSystem/src/System/IO/Directory.cs @@ -563,6 +563,12 @@ namespace System.IO String destinationRoot = Path.GetPathRoot(destPath); if (!String.Equals(sourceRoot, destinationRoot, pathComparison)) throw new IOException(SR.IO_SourceDestMustHaveSameRoot); + + if (!FileSystem.Current.DirectoryExists(fullsourceDirName)) + throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, fullsourceDirName)); + + if (FileSystem.Current.DirectoryExists(fulldestDirName)) + throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, fulldestDirName)); FileSystem.Current.MoveDirectory(fullsourceDirName, fulldestDirName); } diff --git a/src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs b/src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs index 95b448412c..177177d9c4 100644 --- a/src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs +++ b/src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs @@ -413,6 +413,12 @@ namespace System.IO if (!String.Equals(sourceRoot, destinationRoot, pathComparison)) throw new IOException(SR.IO_SourceDestMustHaveSameRoot); + if (!Exists) + throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, FullPath)); + + if (FileSystem.Current.DirectoryExists(fullDestDirName)) + throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, fullDestDirName)); + FileSystem.Current.MoveDirectory(FullPath, fullDestDirName); FullPath = fullDestDirName; diff --git a/src/System.IO.FileSystem/tests/Directory/Move.cs b/src/System.IO.FileSystem/tests/Directory/Move.cs index 63588f5cce..586e1e4db0 100644 --- a/src/System.IO.FileSystem/tests/Directory/Move.cs +++ b/src/System.IO.FileSystem/tests/Directory/Move.cs @@ -42,13 +42,30 @@ namespace System.IO.Tests } [Fact] - public void MoveOntoExistingDirectory() + public void MoveOntoSameDirectory() { DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath()); Assert.Throws<IOException>(() => Move(testDir.FullName, testDir.FullName)); } [Fact] + public void MoveOntoExistingDirectory() + { + DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath()); + DirectoryInfo secondDir = Directory.CreateDirectory(GetTestFilePath()); + Assert.Throws<IOException>(() => Move(testDir.FullName, secondDir.FullName)); + } + + [Fact] + public void MoveOntoFile() + { + DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath()); + string testFile = GetTestFilePath(); + File.WriteAllText(testFile, ""); + Assert.Throws<IOException>(() => Move(testDir.FullName, testFile)); + } + + [Fact] public void MoveIntoCurrentDirectory() { DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath()); @@ -261,31 +278,6 @@ namespace System.IO.Tests Assert.Throws<IOException>(() => Move(path, "D:\\DoesntExist")); } - [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/Microsoft/BashOnWindows/issues/1008 - [PlatformSpecific(TestPlatforms.AnyUnix)] // Moving to existing directory allowed for empty directory, but causes IOException for non-empty directory - public void UnixExistingDirectory() - { - // Moving to an-empty directory is supported on Unix, but moving to a non-empty directory is not - string testDirSource = GetTestFilePath(); - string testDirDestEmpty = GetTestFilePath(); - string testDirDestNonEmpty = GetTestFilePath(); - - Directory.CreateDirectory(testDirSource); - Directory.CreateDirectory(testDirDestEmpty); - Directory.CreateDirectory(testDirDestNonEmpty); - - using (File.Create(Path.Combine(testDirDestNonEmpty, GetTestFileName()))) - { - Assert.Throws<IOException>(() => Move(testDirSource, testDirDestNonEmpty)); - Assert.True(Directory.Exists(testDirDestNonEmpty)); - Assert.True(Directory.Exists(testDirSource)); - } - - Move(testDirSource, testDirDestEmpty); - Assert.True(Directory.Exists(testDirDestEmpty)); - Assert.False(Directory.Exists(testDirSource)); - } - #endregion } } |