diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index 250241dd2..72cf08c7e 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -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] @@ -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() @@ -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 { { testPath, new MockFileData("Demo text content") }, @@ -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 @@ -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] @@ -1211,7 +1234,8 @@ 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(), directory); @@ -1219,9 +1243,9 @@ public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemCon Assert.AreEqual(directory, actual); } - + [Test] - public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet() + public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet() { string directory = XFS.Path(@"C:\"); @@ -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(); @@ -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); @@ -1331,9 +1356,9 @@ public static IEnumerable 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") }; } } diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs index bf370ce78..280d88948 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -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) { @@ -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; } @@ -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; } @@ -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)