Skip to content

Commit e1d605c

Browse files
authored
MockDirectory.Move should throw when target cannot be parsed (#573)
Fixes #551
1 parent 1378f58 commit e1d605c

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,5 +1903,69 @@ public void MockDirectory_SetCreationTime_ShouldNotThrowWithoutTrailingBackslash
19031903
fs.Directory.SetCreationTime(path, DateTime.Now);
19041904
fs.Directory.Delete(path);
19051905
}
1906+
1907+
[Test]
1908+
[TestCase(@"c:\temp2\fd\df")]
1909+
[TestCase(@"c:\temp2\fd\")]
1910+
[TestCase(@"c:\temp2\fd\..\fd")]
1911+
[TestCase(@"c:\temp2\fd")]
1912+
[TestCase(@".\..\temp2\fd\df")]
1913+
[TestCase(@".\..\temp2\fd\df\..")]
1914+
[TestCase(@".\..\temp2\fd\df\..\")]
1915+
[TestCase(@"..\temp2\fd\")]
1916+
[TestCase(@".\temp2\fd")]
1917+
[TestCase(@"\temp2\fd")]
1918+
[TestCase(@"temp2\fd")]
1919+
public void Move_Directory_Throws_When_Target_Directory_Without_First_Order_Path_Is_Missing(
1920+
string targetDirName)
1921+
{
1922+
// Arange
1923+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
1924+
{
1925+
{XFS.Path(@"c:\temp\exists\foldertomove"), new MockDirectoryData()}
1926+
});
1927+
1928+
string folderToMove = XFS.Path(@"c:\temp\exists\foldertomove");
1929+
targetDirName = XFS.Path(targetDirName);
1930+
1931+
// Act
1932+
Assert.Throws<DirectoryNotFoundException>(() =>
1933+
fileSystem.Directory.Move(folderToMove, targetDirName));
1934+
1935+
// Assert
1936+
Assert.IsFalse(fileSystem.Directory.Exists(targetDirName));
1937+
}
1938+
1939+
[Test]
1940+
[TestCase(@"c:\temp2\")]
1941+
[TestCase(@"c:\temp2")]
1942+
[TestCase(@"c:\temp2\..\temp2")]
1943+
[TestCase(@".\..\temp2")]
1944+
[TestCase(@".\..\temp2\..\temp2")]
1945+
[TestCase(@".\..\temp2\fd\df\..\..")]
1946+
[TestCase(@".\..\temp2\fd\df\..\..\")]
1947+
[TestCase(@"..\temp2")]
1948+
[TestCase(@".\temp2")]
1949+
[TestCase(@"\temp2")]
1950+
[TestCase(@"temp2")]
1951+
public void Move_Directory_DoesNotThrow_When_Target_Directory_With_First_Order_Path_Is_Missing(
1952+
string targetDirName)
1953+
{
1954+
// Arange
1955+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
1956+
{
1957+
{XFS.Path(@"c:\temp\exists\foldertomove"), new MockDirectoryData()}
1958+
});
1959+
1960+
string folderToMove = XFS.Path(@"c:\temp\exists\foldertomove");
1961+
targetDirName = XFS.Path(targetDirName);
1962+
1963+
// Act
1964+
Assert.DoesNotThrow(() =>
1965+
fileSystem.Directory.Move(folderToMove, targetDirName));
1966+
1967+
// Assert
1968+
Assert.IsTrue(fileSystem.Directory.Exists(targetDirName));
1969+
}
19061970
}
19071971
}

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 (!Win32FileSystemBehavior.MoveFileCanParsePath(fullDestPath))
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.");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace System.IO.Abstractions.TestingHelpers
2+
{
3+
internal static class Win32FileSystemBehavior
4+
{
5+
public static bool MoveFileCanParsePath(string fullPath)
6+
{
7+
fullPath = fullPath.Trim(Path.DirectorySeparatorChar);
8+
var dir = Path.IsPathRooted(fullPath)
9+
? fullPath.Replace(Path.GetPathRoot(fullPath), string.Empty)
10+
: fullPath.Replace($".{Path.DirectorySeparatorChar}", string.Empty);
11+
12+
const int DirectorySegmentsAllowed = 1;
13+
var isSupported = dir.Split(Path.DirectorySeparatorChar).Length <= DirectorySegmentsAllowed;
14+
return isSupported;
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)