Skip to content

Commit 7337796

Browse files
authored
On Directory.Move, first check to ensure the parent directory for the indicated target folder exists (#580)
Fixes #551
1 parent 2b3700f commit 7337796

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,5 +1903,82 @@ public void MockDirectory_SetCreationTime_ShouldNotThrowWithoutTrailingBackslash
19031903
fs.Directory.SetCreationTime(path, DateTime.Now);
19041904
fs.Directory.Delete(path);
19051905
}
1906+
1907+
private static IEnumerable<TestCaseData> Failing_DirectoryMoveFromToPaths
1908+
{
1909+
get
1910+
{
1911+
var testTargetDirs = new[]
1912+
{
1913+
@"c:\temp2\fd\df", @"c:\temp2\fd\", @"c:\temp2\fd\..\fd", @"c:\temp2\fd", @".\..\temp2\fd\df",
1914+
@".\..\temp2\fd\df\..", @".\..\temp2\fd\df\..\", @"..\temp2\fd\", @".\temp2\fd", @"temp2\fd",
1915+
@"c:\temp3\exists2\d3", @"c:\temp4\exists"
1916+
};
1917+
1918+
var testSourceDirs = new[] {@"c:\temp\exists\foldertomove", @"c:\temp3\exists", @"c:\temp3"};
1919+
1920+
return
1921+
from s in testSourceDirs
1922+
from t in testTargetDirs
1923+
select new TestCaseData(XFS.Path(s), XFS.Path(t));
1924+
}
1925+
}
1926+
1927+
[Test]
1928+
[TestCaseSource(nameof(Failing_DirectoryMoveFromToPaths))]
1929+
public void Move_Directory_Throws_When_Target_Directory_Parent_Does_Not_Exist(
1930+
string sourceDirName,
1931+
string targetDirName)
1932+
{
1933+
// Arange
1934+
var fileSystem = new MockFileSystem();
1935+
fileSystem.Directory.CreateDirectory(sourceDirName);
1936+
1937+
// Act
1938+
Assert.Throws<DirectoryNotFoundException>(() =>
1939+
fileSystem.Directory.Move(sourceDirName, targetDirName));
1940+
1941+
// Assert
1942+
Assert.IsFalse(fileSystem.Directory.Exists(targetDirName));
1943+
Assert.IsTrue(fileSystem.Directory.Exists(sourceDirName));
1944+
}
1945+
1946+
private static IEnumerable<TestCaseData> Success_DirectoryMoveFromToPaths
1947+
{
1948+
get
1949+
{
1950+
var testTargetDirs = new[]
1951+
{
1952+
@"c:\temp2\", @"c:\temp2", @"c:\temp2\..\temp2", @".\..\temp2", @".\..\temp2\..\temp2",
1953+
@".\..\temp2\fd\df\..\..", @".\..\temp2\fd\df\..\..\", @"..\temp2", @".\temp2", @"\temp2", @"temp2",
1954+
};
1955+
1956+
var testSourceDirs = new[] { @"c:\temp3\exists\foldertomove",@"c:\temp3\exists", @"c:\temp4" };
1957+
1958+
return
1959+
from s in testSourceDirs
1960+
from t in testTargetDirs
1961+
select new TestCaseData(XFS.Path(s), XFS.Path(t));
1962+
}
1963+
}
1964+
1965+
[Test]
1966+
[TestCaseSource(nameof(Success_DirectoryMoveFromToPaths))]
1967+
public void Move_Directory_DoesNotThrow_When_Target_Directory_Parent_Exists(
1968+
string sourceDirName,
1969+
string targetDirName)
1970+
{
1971+
// Arange
1972+
var fileSystem = new MockFileSystem();
1973+
fileSystem.Directory.CreateDirectory(sourceDirName);
1974+
1975+
// Act
1976+
Assert.DoesNotThrow(() =>
1977+
fileSystem.Directory.Move(sourceDirName, targetDirName));
1978+
1979+
// Assert
1980+
Assert.IsTrue(fileSystem.Directory.Exists(targetDirName));
1981+
Assert.IsFalse(fileSystem.Directory.Exists(sourceDirName));
1982+
}
19061983
}
19071984
}

System.IO.Abstractions.TestingHelpers/MockDirectory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ public override void Move(string sourceDirName, string destDirName)
415415
throw new DirectoryNotFoundException($"Could not find a part of the path '{sourceDirName}'.");
416416
}
417417

418+
if (!mockFileDataAccessor.Directory.GetParent(fullDestPath).Exists)
419+
{
420+
throw new DirectoryNotFoundException($"Could not find a part of the path.");
421+
}
422+
418423
if (mockFileDataAccessor.Directory.Exists(fullDestPath) || mockFileDataAccessor.File.Exists(fullDestPath))
419424
{
420425
throw new IOException($"Cannot create '{fullDestPath}' because a file or directory with the same name already exists.");

0 commit comments

Comments
 (0)