Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void MockDirectory_GetFiles_ShouldFilterByExtensionBasedSearchPatternWith
var result = fileSystem.Directory.GetFiles(XFS.Path(@"c:\"), "*.gif", SearchOption.AllDirectories);

// Assert
Assert.That(result, Is.EquivalentTo( expected));
Assert.That(result, Is.EquivalentTo(expected));
}

[Test]
Expand Down Expand Up @@ -598,6 +598,29 @@ public void MockDirectory_Delete_ShouldDeleteDirectory()
Assert.IsFalse(fileSystem.Directory.Exists(XFS.Path(@"c:\bar")));
}

[Test]
public void MockDirectory_Delete_ShouldNotDeleteAllDirectories()
{
// Arrange
var folder1Path = XFS.Path(@"D:\Test\Program");
var folder1SubFolderPath = XFS.Path(@"D:\Test\Program\Subfolder");
var folder2Path = XFS.Path(@"D:\Test\Program_bak");

var fileSystem = new MockFileSystem();

fileSystem.AddDirectory(folder1Path);
fileSystem.AddDirectory(folder2Path);
fileSystem.AddDirectory(folder1SubFolderPath);

// Act
fileSystem.Directory.Delete(folder1Path, recursive: true);

// Assert
Assert.IsFalse(fileSystem.Directory.Exists(folder1Path));
Assert.IsFalse(fileSystem.Directory.Exists(folder1SubFolderPath));
Assert.IsTrue(fileSystem.Directory.Exists(folder2Path));
}

[Test]
[WindowsOnly(WindowsSpecifics.CaseInsensitivity)]
public void MockDirectory_Delete_ShouldDeleteDirectoryCaseInsensitively()
Expand Down Expand Up @@ -700,7 +723,7 @@ public void MockDirectory_Delete_ShouldDeleteDirectoryRecursively()
public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
{
string testPath = XFS.Path(@"c:\foo\bar.txt");
string testDir = XFS.Path(@"c:\foo\bar");
string testDir = XFS.Path(@"c:\foo\bar");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ testPath, new MockFileData("Demo text content") },
Expand Down Expand Up @@ -829,7 +852,7 @@ public void MockDirectory_GetFiles_ShouldFindFilesContainingTwoOrMoreDots()
var actualResult = fileSystem.Directory.GetFiles(XFS.Path(@"c:\"), XFS.Path(@"foo..r\*"));

// Assert
Assert.That(actualResult, Is.EquivalentTo(new [] { testPath }));
Assert.That(actualResult, Is.EquivalentTo(new[] { testPath }));
}

#if NET40
Expand Down Expand Up @@ -925,7 +948,7 @@ public void MockDirectory_GetDirectories_WithTopDirectories_ShouldOnlyReturnTopD
var actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\Folder\"), "*.foo");

// Assert
Assert.That(actualResult, Is.EquivalentTo(new []{XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo")}));
Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo") }));
}

[Test]
Expand Down Expand Up @@ -1211,17 +1234,18 @@ public void MockDirectory_Move_ShouldMoveDirectoryWithReadOnlySubDirectory()
}

[Test]
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor() {
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor()
{
string directory = XFS.Path(@"D:\folder1\folder2");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>(), directory);

var actual = fileSystem.Directory.GetCurrentDirectory();

Assert.AreEqual(directory, actual);
}

[Test]
public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet()
public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet()
{
string directory = XFS.Path(@"C:\");

Expand All @@ -1233,7 +1257,8 @@ public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet(
}

[Test]
public void MockDirectory_SetCurrentDirectory_ShouldChangeCurrentDirectory() {
public void MockDirectory_SetCurrentDirectory_ShouldChangeCurrentDirectory()
{
string directory = XFS.Path(@"D:\folder1\folder2");
var fileSystem = new MockFileSystem();

Expand Down Expand Up @@ -1278,7 +1303,7 @@ public void MockDirectory_GetParent_ShouldReturnADirectoryInfoIfPathDoesNotExist
var fileSystem = new MockFileSystem();

// Act
var actualResult = fileSystem.Directory.GetParent(XFS.Path(@"c:\directory\does\not\exist"));
var actualResult = fileSystem.Directory.GetParent(XFS.Path(@"c:\directory\does\not\exist"));

// Assert
Assert.IsNotNull(actualResult);
Expand Down Expand Up @@ -1331,9 +1356,9 @@ public static IEnumerable<string[]> MockDirectory_GetParent_Cases
{
get
{
yield return new [] { XFS.Path(@"c:\a"), XFS.Path(@"c:\") };
yield return new [] { XFS.Path(@"c:\a\b\c\d"), XFS.Path(@"c:\a\b\c") };
yield return new [] { XFS.Path(@"c:\a\b\c\d\"), XFS.Path(@"c:\a\b\c") };
yield return new[] { XFS.Path(@"c:\a"), XFS.Path(@"c:\") };
yield return new[] { XFS.Path(@"c:\a\b\c\d"), XFS.Path(@"c:\a\b\c") };
yield return new[] { XFS.Path(@"c:\a\b\c\d\"), XFS.Path(@"c:\a\b\c") };
}
}

Expand Down
30 changes: 19 additions & 11 deletions System.IO.Abstractions.TestingHelpers/MockDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class MockDirectory : DirectoryBase
private readonly IMockFileDataAccessor mockFileDataAccessor;
private string currentDirectory;

// This constructor is retained to avoid breaking change
public MockDirectory(IMockFileDataAccessor mockFileDataAccessor, FileBase fileBase, string currentDirectory) :
this(mockFileDataAccessor, currentDirectory)
{
Expand Down Expand Up @@ -75,25 +74,34 @@ public override void Delete(string path)
public override void Delete(string path, bool recursive)
{
path = mockFileDataAccessor.Path.GetFullPath(path).TrimSlashes();

var stringOps = mockFileDataAccessor.StringOperations;
var pathWithDirectorySeparatorChar = $"{path}{Path.DirectorySeparatorChar}";

var affectedPaths = mockFileDataAccessor
.AllPaths
.Where(p => mockFileDataAccessor.StringOperations.StartsWith(p, path))
.Where(p => stringOps.Equals(p, path) || stringOps.StartsWith(p, pathWithDirectorySeparatorChar))
.ToList();

if (!affectedPaths.Any())
{
throw new DirectoryNotFoundException(path + " does not exist or could not be found.");

if (!recursive &&
affectedPaths.Count > 1)
}

if (!recursive && affectedPaths.Count > 1)
{
throw new IOException("The directory specified by " + path + " is read-only, or recursive is false and " + path + " is not an empty directory.");

}

foreach (var affectedPath in affectedPaths)
{
mockFileDataAccessor.RemoveFile(affectedPath);
}
}

public override bool Exists(string path)
{
if (path == "/" && XFS.IsUnixPlatform())
if (path == "/" && XFS.IsUnixPlatform())
{
return true;
}
Expand All @@ -111,16 +119,16 @@ public override bool Exists(string path)
}

public override DirectorySecurity GetAccessControl(string path)
{
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
path = path.TrimSlashes();

if (!mockFileDataAccessor.Directory.Exists(path))
{
throw CommonExceptions.CouldNotFindPartOfPath(path);
}

var directoryData = (MockDirectoryData) mockFileDataAccessor.GetFile(path);
var directoryData = (MockDirectoryData)mockFileDataAccessor.GetFile(path);
return directoryData.AccessControl;
}

Expand Down Expand Up @@ -417,7 +425,7 @@ public override void SetCreationTimeUtc(string path, DateTime creationTimeUtc)

public override void SetCurrentDirectory(string path)
{
currentDirectory = path;
currentDirectory = path;
}

public override void SetLastAccessTime(string path, DateTime lastAccessTime)
Expand Down