From 807aeefcb1890020fcc0133bb95342b63a0c9171 Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Sun, 25 Nov 2018 17:15:36 +0000 Subject: [PATCH 01/15] update nunit --- .../System.IO.Abstractions.TestingHelpers.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj b/System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj index 00f7fba8d..64d46205c 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj +++ b/System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj @@ -47,7 +47,7 @@ - + From 4fd4cb154c3eea9cb54db138ed0f56ed78dd9105 Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Sun, 25 Nov 2018 17:19:57 +0000 Subject: [PATCH 02/15] return original directory name --- .../MockDirectoryInfoTests.cs | 6 ++++-- System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs index 194dde31b..5090d847d 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs @@ -273,9 +273,11 @@ public void MockDirectoryInfo_Constructor_ShouldThrowArgumentException_IfArgumen } [Test] - public void MockDirectoryInfo_ToString_ShouldReturnDirectoryName() + [TestCase(@"c:\temp\folder\folder")] + [TestCase(@"..\..\..\Desktop")] + public void MockDirectoryInfo_ToString_ShouldReturnDirectoryName(string directoryName) { - var directoryPath = XFS.Path(@"c:\temp\folder\folder"); + var directoryPath = XFS.Path(directoryName); // Arrange var fileSystem = new MockFileSystem(); diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs index 90f2ce9a1..36231d6e5 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs @@ -10,6 +10,7 @@ public class MockDirectoryInfo : DirectoryInfoBase { private readonly IMockFileDataAccessor mockFileDataAccessor; private readonly string directoryPath; + private readonly string originalPath; /// /// Initializes a new instance of the class. @@ -21,6 +22,8 @@ public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string dire { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor)); + originalPath = directoryPath; + directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath); this.directoryPath = directoryPath.TrimSlashes(); @@ -285,7 +288,7 @@ public override DirectoryInfoBase Root public override string ToString() { - return FullName; + return originalPath; } private MockFileData GetMockFileDataForRead() From b62ff2a8dd04790a0fb9fad3f71cbe82e0700c4f Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Wed, 28 Nov 2018 11:04:57 +0000 Subject: [PATCH 03/15] Add new fileInfo test --- .../MockFileInfoTests.cs | 19 +++++++++++++++++-- .../MockDirectoryInfo.cs | 14 +++++++------- .../MockFileInfo.cs | 6 +++--- System.IO.Abstractions/DirectoryInfoBase.cs | 2 +- .../DirectoryInfoWrapper.cs | 2 +- System.IO.Abstractions/FileInfoBase.cs | 2 +- System.IO.Abstractions/FileInfoWrapper.cs | 2 +- System.IO.Abstractions/FileSystemInfoBase.cs | 10 +++++++++- 8 files changed, 40 insertions(+), 17 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index e75cb3cec..bf8f7d71c 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -167,7 +167,7 @@ public void MockFileInfo_IsReadOnly_ShouldSetReadOnlyAttributeOfFileInMemoryFile [Test] public void MockFileInfo_IsReadOnly_ShouldSetNotReadOnlyAttributeOfFileInMemoryFileSystem() { - var fileData = new MockFileData("Demo text content") {Attributes = FileAttributes.ReadOnly}; + var fileData = new MockFileData("Demo text content") { Attributes = FileAttributes.ReadOnly }; var fileSystem = new MockFileSystem(new Dictionary { { XFS.Path(@"c:\a.txt"), fileData } @@ -210,7 +210,7 @@ public void MockFileInfo_OpenWrite_ShouldAddDataToFileInMemoryFileSystem() { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); - var bytesToAdd = new byte[] {65, 66, 67, 68, 69}; + var bytesToAdd = new byte[] { 65, 66, 67, 68, 69 }; using (var file = fileInfo.OpenWrite()) @@ -491,6 +491,21 @@ public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist() Assert.Throws(action); } + [Test] + public void MockFileInfo_ToString_ShouldReturnOriginalFilePath() + { + //Arrange + var filePath = XFS.Path(@"..\..\..\c.txt"); + + //Act + var mockFileInfo = new MockFileInfo(new MockFileSystem(), filePath); + var realFileInfo = new FileInfo(filePath); + + //Assert + Assert.AreEqual(filePath, mockFileInfo.ToString()); + Assert.AreEqual(filePath, realFileInfo.ToString()); + } + #if NET40 [Test] public void MockFileInfo_Replace_ShouldReplaceFileContents() diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs index 36231d6e5..0b650f4a4 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs @@ -10,7 +10,7 @@ public class MockDirectoryInfo : DirectoryInfoBase { private readonly IMockFileDataAccessor mockFileDataAccessor; private readonly string directoryPath; - private readonly string originalPath; + //private readonly string originalPath; /// /// Initializes a new instance of the class. @@ -18,11 +18,11 @@ public class MockDirectoryInfo : DirectoryInfoBase /// The mock file data accessor. /// The directory path. /// Thrown if or is . - public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath) : base(mockFileDataAccessor?.FileSystem) + public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath) : base(mockFileDataAccessor?.FileSystem, directoryPath) { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor)); - originalPath = directoryPath; + //originalPath = directoryPath; directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath); @@ -286,10 +286,10 @@ public override DirectoryInfoBase Root } } - public override string ToString() - { - return originalPath; - } + //public override string ToString() + //{ + // return originalPath; + //} private MockFileData GetMockFileDataForRead() { diff --git a/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs index 298d054be..cd8ac7914 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs @@ -8,7 +8,7 @@ public class MockFileInfo : FileInfoBase private readonly IMockFileDataAccessor mockFileSystem; private string path; - public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mockFileSystem?.FileSystem) + public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mockFileSystem?.FileSystem, path) { this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem)); this.path = path ?? throw new ArgumentNullException(nameof(path)); @@ -242,7 +242,7 @@ public override Stream OpenRead() public override StreamReader OpenText() { - return new StreamReader(OpenRead()); + return new StreamReader(OpenRead()); } public override Stream OpenWrite() @@ -296,7 +296,7 @@ public override bool IsReadOnly set { if (MockFileData == null) throw new FileNotFoundException("File not found", path); - if(value) + if (value) MockFileData.Attributes |= FileAttributes.ReadOnly; else MockFileData.Attributes &= ~FileAttributes.ReadOnly; diff --git a/System.IO.Abstractions/DirectoryInfoBase.cs b/System.IO.Abstractions/DirectoryInfoBase.cs index c5b40ea65..458874336 100644 --- a/System.IO.Abstractions/DirectoryInfoBase.cs +++ b/System.IO.Abstractions/DirectoryInfoBase.cs @@ -7,7 +7,7 @@ namespace System.IO.Abstractions [Serializable] public abstract class DirectoryInfoBase : FileSystemInfoBase { - protected DirectoryInfoBase(IFileSystem fileSystem) : base(fileSystem) + protected DirectoryInfoBase(IFileSystem fileSystem, string path) : base(fileSystem, path) { } diff --git a/System.IO.Abstractions/DirectoryInfoWrapper.cs b/System.IO.Abstractions/DirectoryInfoWrapper.cs index ec7e4d5e8..7a7b7d1cd 100644 --- a/System.IO.Abstractions/DirectoryInfoWrapper.cs +++ b/System.IO.Abstractions/DirectoryInfoWrapper.cs @@ -9,7 +9,7 @@ public class DirectoryInfoWrapper : DirectoryInfoBase { private readonly DirectoryInfo instance; - public DirectoryInfoWrapper(IFileSystem fileSystem, DirectoryInfo instance) : base(fileSystem) + public DirectoryInfoWrapper(IFileSystem fileSystem, DirectoryInfo instance) : base(fileSystem, instance.ToString()) { this.instance = instance ?? throw new ArgumentNullException(nameof(instance)); } diff --git a/System.IO.Abstractions/FileInfoBase.cs b/System.IO.Abstractions/FileInfoBase.cs index cfd1508f4..b16c2c97d 100644 --- a/System.IO.Abstractions/FileInfoBase.cs +++ b/System.IO.Abstractions/FileInfoBase.cs @@ -6,7 +6,7 @@ namespace System.IO.Abstractions [Serializable] public abstract class FileInfoBase : FileSystemInfoBase { - protected FileInfoBase(IFileSystem fileSystem) : base(fileSystem) + protected FileInfoBase(IFileSystem fileSystem, string path) : base(fileSystem, path) { } diff --git a/System.IO.Abstractions/FileInfoWrapper.cs b/System.IO.Abstractions/FileInfoWrapper.cs index 264c2033f..6cd82f883 100644 --- a/System.IO.Abstractions/FileInfoWrapper.cs +++ b/System.IO.Abstractions/FileInfoWrapper.cs @@ -7,7 +7,7 @@ public class FileInfoWrapper : FileInfoBase { private readonly FileInfo instance; - public FileInfoWrapper(IFileSystem fileSystem, FileInfo instance) : base(fileSystem) + public FileInfoWrapper(IFileSystem fileSystem, FileInfo instance) : base(fileSystem, instance.ToString()) { this.instance = instance ?? throw new ArgumentNullException(nameof(instance)); } diff --git a/System.IO.Abstractions/FileSystemInfoBase.cs b/System.IO.Abstractions/FileSystemInfoBase.cs index 586b2f726..2bfc822c3 100644 --- a/System.IO.Abstractions/FileSystemInfoBase.cs +++ b/System.IO.Abstractions/FileSystemInfoBase.cs @@ -4,9 +4,12 @@ [Serializable] public abstract class FileSystemInfoBase { - protected FileSystemInfoBase(IFileSystem fileSystem) + private readonly string originalPath; + + protected FileSystemInfoBase(IFileSystem fileSystem, string path) { FileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); + originalPath = path ?? throw new ArgumentNullException(nameof(path)); } [Obsolete("This constructor only exists to support mocking libraries.", error: true)] @@ -55,5 +58,10 @@ internal FileSystemInfoBase() { } /// public abstract string Name { get; } + + public override string ToString() + { + return originalPath; + } } } From 5e1c0fc0f0b422cef420bd0db0989a42116fb53e Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Wed, 28 Nov 2018 11:45:18 +0000 Subject: [PATCH 04/15] Fix MockDriveInfo and add tests --- .../MockDirectoryInfoTests.cs | 30 +++++++++---------- .../MockDriveInfoTests.cs | 18 +++++++++++ .../MockFileInfoTests.cs | 11 ++++--- ...O.Abstractions.TestingHelpers.Tests.csproj | 4 +-- .../MockDriveInfo.cs | 6 ++-- System.IO.Abstractions/DriveInfoBase.cs | 7 +++++ 6 files changed, 51 insertions(+), 25 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs index 5090d847d..9c7000622 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs @@ -36,8 +36,8 @@ public static IEnumerable MockDirectoryInfo_Exists_Cases { get { - yield return new object[]{ XFS.Path(@"c:\temp\folder"), true }; - yield return new object[]{ XFS.Path(@"c:\temp\folder\notExistant"), false }; + yield return new object[] { XFS.Path(@"c:\temp\folder"), true }; + yield return new object[] { XFS.Path(@"c:\temp\folder\notExistant"), false }; } } @@ -152,8 +152,8 @@ public void MockDirectoryInfo_GetParent_ShouldReturnDirectoriesAndNamesWithSearc [Test] public void MockDirectoryInfo_EnumerateFiles_ShouldReturnAllFiles() { - // Arrange - var fileSystem = new MockFileSystem(new Dictionary + // Arrange + var fileSystem = new MockFileSystem(new Dictionary { //Files "above" in folder we're querying { XFS.Path(@"c:\temp\a.txt"), "" }, @@ -166,11 +166,11 @@ public void MockDirectoryInfo_EnumerateFiles_ShouldReturnAllFiles() { XFS.Path(@"c:\temp\folder\deeper\d.txt"), "" } }); - // Act - var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\temp\folder")); + // Act + var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\temp\folder")); - // Assert - Assert.AreEqual(new[]{"b.txt", "c.txt"}, directoryInfo.EnumerateFiles().ToList().Select(x => x.Name).ToArray()); + // Assert + Assert.AreEqual(new[] { "b.txt", "c.txt" }, directoryInfo.EnumerateFiles().ToList().Select(x => x.Name).ToArray()); } [Test] @@ -238,7 +238,7 @@ public void MockDirectoryInfo_Constructor_ShouldThrowArgumentNullException_IfArg var fileSystem = new MockFileSystem(); // Act - TestDelegate action = () => new MockDirectoryInfo(fileSystem, null); + TestDelegate action = () => new MockDirectoryInfo(fileSystem, null); // Assert var exception = Assert.Throws(action); @@ -272,22 +272,20 @@ public void MockDirectoryInfo_Constructor_ShouldThrowArgumentException_IfArgumen Assert.That(exception.Message, Does.StartWith("The path is not of a legal form.")); } - [Test] [TestCase(@"c:\temp\folder\folder")] [TestCase(@"..\..\..\Desktop")] public void MockDirectoryInfo_ToString_ShouldReturnDirectoryName(string directoryName) { - var directoryPath = XFS.Path(directoryName); - // Arrange - var fileSystem = new MockFileSystem(); - var directoryInfo = new MockDirectoryInfo(fileSystem, directoryPath); + var directoryPath = XFS.Path(directoryName); // Act - var str = directoryInfo.ToString(); + var mockDirectoryInfo = new MockDirectoryInfo(new MockFileSystem(), directoryPath); + var realDirectoryInfo = new DirectoryInfo(directoryPath); // Assert - Assert.AreEqual(directoryPath, str); + Assert.AreEqual(directoryPath, mockDirectoryInfo.ToString()); + Assert.AreEqual(directoryPath, realDirectoryInfo.ToString()); } } } \ No newline at end of file diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs index 8b33fd16f..ca87704be 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs @@ -67,5 +67,23 @@ public void MockDriveInfo_RootDirectory_ShouldReturnTheDirectoryBase() // Assert Assert.AreEqual(expectedDirectory, actualDirectory.FullName); } + + [TestCase("c:","c:\\")] + [TestCase("d:","d:\\")] + [TestCase("e:","e:\\")] + [TestCase("f:","f:\\")] + public void MockDriveInfo_ToString_ShouldReturnTheDrivePath(string path, string expectedPath) + { + // Arrange + var directoryPath = XFS.Path(path); + + // Act + var mockDriveInfo = new MockDriveInfo(new MockFileSystem(), directoryPath); + var realDriveInfo = new DriveInfo(directoryPath); + + // Assert + Assert.AreEqual(expectedPath, mockDriveInfo.ToString()); + Assert.AreEqual(expectedPath, realDriveInfo.ToString()); + } } } diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index bf8f7d71c..6f815c3db 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -491,12 +491,15 @@ public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist() Assert.Throws(action); } - [Test] - public void MockFileInfo_ToString_ShouldReturnOriginalFilePath() + [TestCase(@"..\..\..\c.txt")] + [TestCase(@"c:\a\b\c.txt")] + [TestCase(@"c:\a\c.txt")] + [TestCase(@"c:\c.txt")] + public void MockFileInfo_ToString_ShouldReturnOriginalFilePath(string path) { //Arrange - var filePath = XFS.Path(@"..\..\..\c.txt"); - + var filePath = XFS.Path(path); + //Act var mockFileInfo = new MockFileInfo(new MockFileSystem(), filePath); var realFileInfo = new FileInfo(filePath); diff --git a/System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj b/System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj index 64d46205c..67a48bfdb 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj +++ b/System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj @@ -41,12 +41,12 @@ - + - + diff --git a/System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs b/System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs index 918c147f0..6072cd158 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs @@ -20,15 +20,15 @@ public MockDriveInfo(IMockFileDataAccessor mockFileDataAccessor, string name) : const string DRIVE_SEPARATOR = @":\"; if (name.Length == 1) { - name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR; + name = name[0] + DRIVE_SEPARATOR; } else if (name.Length == 2 && name[1] == ':') { - name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR; + name = name[0] + DRIVE_SEPARATOR; } else if (name.Length == 3 && name.EndsWith(DRIVE_SEPARATOR, StringComparison.Ordinal)) { - name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR; + name = name[0] + DRIVE_SEPARATOR; } else { diff --git a/System.IO.Abstractions/DriveInfoBase.cs b/System.IO.Abstractions/DriveInfoBase.cs index 55e2463d1..5c1357c7a 100644 --- a/System.IO.Abstractions/DriveInfoBase.cs +++ b/System.IO.Abstractions/DriveInfoBase.cs @@ -142,5 +142,12 @@ public static implicit operator DriveInfoBase(DriveInfo driveInfo) return new DriveInfoWrapper(new FileSystem(), driveInfo); } + + /// Returns a drive name as a string. + /// The name of the drive. + public override string ToString() + { + return Name; + } } } From bb8cb680577e402338f3a079395b60038ad5615b Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Wed, 28 Nov 2018 12:10:14 +0000 Subject: [PATCH 05/15] fix tests --- .../MockDriveInfoFactoryTests.cs | 6 +++--- .../MockDriveInfoTests.cs | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoFactoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoFactoryTests.cs index 2b10ec883..c6b7d5900 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoFactoryTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoFactoryTests.cs @@ -26,7 +26,7 @@ public void MockDriveInfoFactory_GetDrives_ShouldReturnDrives() var actualNames = actualResults.Select(d => d.Name); // Assert - Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"D:\" })); + Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"d:\" })); } [Test] @@ -47,7 +47,7 @@ public void MockDriveInfoFactory_GetDrives_ShouldReturnDrivesWithNoDuplicates() var actualNames = actualResults.Select(d => d.Name); // Assert - Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"D:\" })); + Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"d:\" })); } [Test] @@ -67,7 +67,7 @@ public void MockDriveInfoFactory_GetDrives_ShouldReturnOnlyLocalDrives() var actualNames = actualResults.Select(d => d.Name); // Assert - Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"D:\" })); + Assert.That(actualNames, Is.EquivalentTo(new[] { @"C:\", @"Z:\", @"d:\" })); } [Test] diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs index ca87704be..24dae1869 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs @@ -21,7 +21,7 @@ public void MockDriveInfo_Constructor_ShouldInitializeLocalWindowsDrives(string var driveInfo = new MockDriveInfo(fileSystem, path); // Assert - Assert.AreEqual(@"C:\", driveInfo.Name); + Assert.AreEqual(@"c:\", driveInfo.Name); } [Test] @@ -35,7 +35,7 @@ public void MockDriveInfo_Constructor_ShouldInitializeLocalWindowsDrives_Special var driveInfo = new MockDriveInfo(fileSystem, "c"); // Assert - Assert.AreEqual(@"C:\", driveInfo.Name); + Assert.AreEqual(@"c:\", driveInfo.Name); } [TestCase(@"\\unc\share")] @@ -59,7 +59,7 @@ public void MockDriveInfo_RootDirectory_ShouldReturnTheDirectoryBase() var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"c:\Test")); var driveInfo = new MockDriveInfo(fileSystem, "c:"); - var expectedDirectory = XFS.Path(@"C:\"); + var expectedDirectory = XFS.Path(@"c:\"); // Act var actualDirectory = driveInfo.RootDirectory; @@ -69,6 +69,7 @@ public void MockDriveInfo_RootDirectory_ShouldReturnTheDirectoryBase() } [TestCase("c:","c:\\")] + [TestCase("C:","C:\\")] [TestCase("d:","d:\\")] [TestCase("e:","e:\\")] [TestCase("f:","f:\\")] From cb1c8a13424154b7c1a9be451429c17f7dd576c2 Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Wed, 28 Nov 2018 12:13:03 +0000 Subject: [PATCH 06/15] remove commented code --- .../MockDirectoryInfo.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs index 0b650f4a4..e66501972 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs @@ -10,8 +10,7 @@ public class MockDirectoryInfo : DirectoryInfoBase { private readonly IMockFileDataAccessor mockFileDataAccessor; private readonly string directoryPath; - //private readonly string originalPath; - + /// /// Initializes a new instance of the class. /// @@ -22,8 +21,6 @@ public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string dire { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor)); - //originalPath = directoryPath; - directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath); this.directoryPath = directoryPath.TrimSlashes(); @@ -286,11 +283,6 @@ public override DirectoryInfoBase Root } } - //public override string ToString() - //{ - // return originalPath; - //} - private MockFileData GetMockFileDataForRead() { return mockFileDataAccessor.GetFile(directoryPath) ?? MockFileData.NullObject; From 7ad8782cc8d1ea5fbd4636ce51721c2db1326d52 Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Mon, 3 Dec 2018 14:07:25 +0000 Subject: [PATCH 07/15] Move ToString to MockDriveInfo --- .../MockDriveInfoTests.cs | 2 -- .../MockFileInfoTests.cs | 2 -- System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs | 5 +++++ System.IO.Abstractions/DriveInfoBase.cs | 7 ------- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs index 24dae1869..88524b2f4 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs @@ -80,11 +80,9 @@ public void MockDriveInfo_ToString_ShouldReturnTheDrivePath(string path, string // Act var mockDriveInfo = new MockDriveInfo(new MockFileSystem(), directoryPath); - var realDriveInfo = new DriveInfo(directoryPath); // Assert Assert.AreEqual(expectedPath, mockDriveInfo.ToString()); - Assert.AreEqual(expectedPath, realDriveInfo.ToString()); } } } diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index 6f815c3db..c3b2a03b6 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -502,11 +502,9 @@ public void MockFileInfo_ToString_ShouldReturnOriginalFilePath(string path) //Act var mockFileInfo = new MockFileInfo(new MockFileSystem(), filePath); - var realFileInfo = new FileInfo(filePath); //Assert Assert.AreEqual(filePath, mockFileInfo.ToString()); - Assert.AreEqual(filePath, realFileInfo.ToString()); } #if NET40 diff --git a/System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs b/System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs index 6072cd158..e2ee47700 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs @@ -63,6 +63,11 @@ public override DirectoryInfoBase RootDirectory } } + public override string ToString() + { + return Name; + } + public new long TotalFreeSpace { get; protected set; } public new long TotalSize { get; protected set; } public override string VolumeLabel { get; set; } diff --git a/System.IO.Abstractions/DriveInfoBase.cs b/System.IO.Abstractions/DriveInfoBase.cs index 5c1357c7a..55e2463d1 100644 --- a/System.IO.Abstractions/DriveInfoBase.cs +++ b/System.IO.Abstractions/DriveInfoBase.cs @@ -142,12 +142,5 @@ public static implicit operator DriveInfoBase(DriveInfo driveInfo) return new DriveInfoWrapper(new FileSystem(), driveInfo); } - - /// Returns a drive name as a string. - /// The name of the drive. - public override string ToString() - { - return Name; - } } } From 90852de123a1bc915ba71120ea036bf04a7e33f8 Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Mon, 3 Dec 2018 15:01:10 +0000 Subject: [PATCH 08/15] Move ToString to mock classes --- .../MockDirectoryInfo.cs | 12 +++++++++--- .../MockFileInfo.cs | 7 ++++++- System.IO.Abstractions/DirectoryInfoBase.cs | 2 +- System.IO.Abstractions/DirectoryInfoWrapper.cs | 2 +- System.IO.Abstractions/FileInfoBase.cs | 2 +- System.IO.Abstractions/FileInfoWrapper.cs | 2 +- System.IO.Abstractions/FileSystemInfoBase.cs | 10 +--------- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs index e66501972..ed806e493 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Security.AccessControl; @@ -10,17 +9,19 @@ public class MockDirectoryInfo : DirectoryInfoBase { private readonly IMockFileDataAccessor mockFileDataAccessor; private readonly string directoryPath; - + private readonly string originalPath; + /// /// Initializes a new instance of the class. /// /// The mock file data accessor. /// The directory path. /// Thrown if or is . - public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath) : base(mockFileDataAccessor?.FileSystem, directoryPath) + public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath) : base(mockFileDataAccessor?.FileSystem) { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor)); + originalPath = directoryPath; directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath); this.directoryPath = directoryPath.TrimSlashes(); @@ -293,5 +294,10 @@ private MockFileData GetMockFileDataForWrite() return mockFileDataAccessor.GetFile(directoryPath) ?? throw new FileNotFoundException(StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), directoryPath); } + + public override string ToString() + { + return originalPath; + } } } diff --git a/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs index cd8ac7914..0d11fea89 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs @@ -8,7 +8,7 @@ public class MockFileInfo : FileInfoBase private readonly IMockFileDataAccessor mockFileSystem; private string path; - public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mockFileSystem?.FileSystem, path) + public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mockFileSystem?.FileSystem) { this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem)); this.path = path ?? throw new ArgumentNullException(nameof(path)); @@ -311,5 +311,10 @@ public override long Length return MockFileData.Contents.Length; } } + + public override string ToString() + { + return path; + } } } diff --git a/System.IO.Abstractions/DirectoryInfoBase.cs b/System.IO.Abstractions/DirectoryInfoBase.cs index 458874336..c5b40ea65 100644 --- a/System.IO.Abstractions/DirectoryInfoBase.cs +++ b/System.IO.Abstractions/DirectoryInfoBase.cs @@ -7,7 +7,7 @@ namespace System.IO.Abstractions [Serializable] public abstract class DirectoryInfoBase : FileSystemInfoBase { - protected DirectoryInfoBase(IFileSystem fileSystem, string path) : base(fileSystem, path) + protected DirectoryInfoBase(IFileSystem fileSystem) : base(fileSystem) { } diff --git a/System.IO.Abstractions/DirectoryInfoWrapper.cs b/System.IO.Abstractions/DirectoryInfoWrapper.cs index 7a7b7d1cd..ec7e4d5e8 100644 --- a/System.IO.Abstractions/DirectoryInfoWrapper.cs +++ b/System.IO.Abstractions/DirectoryInfoWrapper.cs @@ -9,7 +9,7 @@ public class DirectoryInfoWrapper : DirectoryInfoBase { private readonly DirectoryInfo instance; - public DirectoryInfoWrapper(IFileSystem fileSystem, DirectoryInfo instance) : base(fileSystem, instance.ToString()) + public DirectoryInfoWrapper(IFileSystem fileSystem, DirectoryInfo instance) : base(fileSystem) { this.instance = instance ?? throw new ArgumentNullException(nameof(instance)); } diff --git a/System.IO.Abstractions/FileInfoBase.cs b/System.IO.Abstractions/FileInfoBase.cs index b16c2c97d..cfd1508f4 100644 --- a/System.IO.Abstractions/FileInfoBase.cs +++ b/System.IO.Abstractions/FileInfoBase.cs @@ -6,7 +6,7 @@ namespace System.IO.Abstractions [Serializable] public abstract class FileInfoBase : FileSystemInfoBase { - protected FileInfoBase(IFileSystem fileSystem, string path) : base(fileSystem, path) + protected FileInfoBase(IFileSystem fileSystem) : base(fileSystem) { } diff --git a/System.IO.Abstractions/FileInfoWrapper.cs b/System.IO.Abstractions/FileInfoWrapper.cs index 6cd82f883..264c2033f 100644 --- a/System.IO.Abstractions/FileInfoWrapper.cs +++ b/System.IO.Abstractions/FileInfoWrapper.cs @@ -7,7 +7,7 @@ public class FileInfoWrapper : FileInfoBase { private readonly FileInfo instance; - public FileInfoWrapper(IFileSystem fileSystem, FileInfo instance) : base(fileSystem, instance.ToString()) + public FileInfoWrapper(IFileSystem fileSystem, FileInfo instance) : base(fileSystem) { this.instance = instance ?? throw new ArgumentNullException(nameof(instance)); } diff --git a/System.IO.Abstractions/FileSystemInfoBase.cs b/System.IO.Abstractions/FileSystemInfoBase.cs index 2bfc822c3..586b2f726 100644 --- a/System.IO.Abstractions/FileSystemInfoBase.cs +++ b/System.IO.Abstractions/FileSystemInfoBase.cs @@ -4,12 +4,9 @@ [Serializable] public abstract class FileSystemInfoBase { - private readonly string originalPath; - - protected FileSystemInfoBase(IFileSystem fileSystem, string path) + protected FileSystemInfoBase(IFileSystem fileSystem) { FileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); - originalPath = path ?? throw new ArgumentNullException(nameof(path)); } [Obsolete("This constructor only exists to support mocking libraries.", error: true)] @@ -58,10 +55,5 @@ internal FileSystemInfoBase() { } /// public abstract string Name { get; } - - public override string ToString() - { - return originalPath; - } } } From a42a86646c9e95e79c363515f156d8ee2ac7bf6a Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Mon, 3 Dec 2018 15:04:30 +0000 Subject: [PATCH 09/15] fix directory info test --- .../MockDirectoryInfoTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs index 9c7000622..a850a157d 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs @@ -281,11 +281,9 @@ public void MockDirectoryInfo_ToString_ShouldReturnDirectoryName(string director // Act var mockDirectoryInfo = new MockDirectoryInfo(new MockFileSystem(), directoryPath); - var realDirectoryInfo = new DirectoryInfo(directoryPath); // Assert Assert.AreEqual(directoryPath, mockDirectoryInfo.ToString()); - Assert.AreEqual(directoryPath, realDirectoryInfo.ToString()); } } } \ No newline at end of file From 57cf7c2ac32ba1423127429cc0f413206c40e82e Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Thu, 27 Dec 2018 17:19:59 +0000 Subject: [PATCH 10/15] fix folder delete --- .../MockDirectoryTests.cs | 53 ++++++++++++++----- .../MockDirectory.cs | 22 +++++--- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index 250241dd2..082f1d75f 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,33 @@ public void MockDirectory_Delete_ShouldDeleteDirectory() Assert.IsFalse(fileSystem.Directory.Exists(XFS.Path(@"c:\bar"))); } + [Test] + public void MockDirectory_Delete_ShouldNotDeleteAllDirectories() + { + // Arrange + var folder1Path = @"D:\Test\Program"; + var folder1SubFolderPath = @"D:\Test\Program\Subfolder"; + var folder2Path = @"D:\Test\Program_bak"; + var folder3Path = @"D:\Test\Program_old"; + + var fileSystem = new MockFileSystem(); + + fileSystem.AddDirectory(folder1Path); + fileSystem.AddDirectory(folder2Path); + fileSystem.AddDirectory(folder3Path); + fileSystem.AddDirectory(folder1SubFolderPath); + + var preDeleteCount = fileSystem.AllDirectories.Count(); + Assert.AreEqual(7, preDeleteCount); + + // Act + fileSystem.Directory.Delete(folder1Path, recursive: true); + var postDeleteCount = fileSystem.AllDirectories.Count(); + + // Assert + Assert.AreEqual(5, postDeleteCount); + } + [Test] [WindowsOnly(WindowsSpecifics.CaseInsensitivity)] public void MockDirectory_Delete_ShouldDeleteDirectoryCaseInsensitively() @@ -700,7 +727,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 +856,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 +952,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 +1238,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 +1247,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 +1261,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 +1307,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 +1360,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 c38d0ce4a..b44da7fe0 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -75,11 +75,21 @@ public override void Delete(string path) public override void Delete(string path, bool recursive) { path = mockFileDataAccessor.Path.GetFullPath(path).TrimSlashes(); + //var affectedPaths = mockFileDataAccessor + // .AllPaths + // .Where(p => mockFileDataAccessor.StringOperations.StartsWith(p, path)) + // .ToList(); + + + var pathWithDirectorySeparatorChar = path.Insert(path.Length, Path.DirectorySeparatorChar.ToString()); + var affectedPaths = mockFileDataAccessor .AllPaths - .Where(p => mockFileDataAccessor.StringOperations.StartsWith(p, path)) + .Where(p => p == path || mockFileDataAccessor.StringOperations.StartsWith(p, pathWithDirectorySeparatorChar)) .ToList(); + //Path.DirectorySeparatorChar + if (!affectedPaths.Any()) throw new DirectoryNotFoundException(path + " does not exist or could not be found."); @@ -93,7 +103,7 @@ public override void Delete(string path, bool recursive) public override bool Exists(string path) { - if (path == "/" && XFS.IsUnixPlatform()) + if (path == "/" && XFS.IsUnixPlatform()) { return true; } @@ -111,16 +121,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 new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), path)); } - var directoryData = (MockDirectoryData) mockFileDataAccessor.GetFile(path); + var directoryData = (MockDirectoryData)mockFileDataAccessor.GetFile(path); return directoryData.AccessControl; } @@ -421,7 +431,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) From dd5aa30b5b83a015a1d74839368a4867957470e4 Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Thu, 27 Dec 2018 17:36:08 +0000 Subject: [PATCH 11/15] fix case sensitive --- System.IO.Abstractions.TestingHelpers/MockDirectory.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs index b44da7fe0..2fc1a2739 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -75,21 +75,14 @@ public override void Delete(string path) public override void Delete(string path, bool recursive) { path = mockFileDataAccessor.Path.GetFullPath(path).TrimSlashes(); - //var affectedPaths = mockFileDataAccessor - // .AllPaths - // .Where(p => mockFileDataAccessor.StringOperations.StartsWith(p, path)) - // .ToList(); - var pathWithDirectorySeparatorChar = path.Insert(path.Length, Path.DirectorySeparatorChar.ToString()); var affectedPaths = mockFileDataAccessor .AllPaths - .Where(p => p == path || mockFileDataAccessor.StringOperations.StartsWith(p, pathWithDirectorySeparatorChar)) + .Where(p => mockFileDataAccessor.StringOperations.Equals(p,path)|| mockFileDataAccessor.StringOperations.StartsWith(p, pathWithDirectorySeparatorChar)) .ToList(); - //Path.DirectorySeparatorChar - if (!affectedPaths.Any()) throw new DirectoryNotFoundException(path + " does not exist or could not be found."); From 8768b76a09b2f929d0c8ec8fbef0d209df619d93 Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Thu, 27 Dec 2018 17:50:37 +0000 Subject: [PATCH 12/15] make test windows only --- .../MockDirectoryTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index 082f1d75f..9f65b07a8 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -599,6 +599,7 @@ public void MockDirectory_Delete_ShouldDeleteDirectory() } [Test] + [WindowsOnly(WindowsSpecifics.CaseInsensitivity)] public void MockDirectory_Delete_ShouldNotDeleteAllDirectories() { // Arrange From 46940fda35b50c730349e2d75a96435f46cd4ece Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Sat, 5 Jan 2019 22:58:46 +0100 Subject: [PATCH 13/15] Update MockDirectoryTests.cs --- .../MockDirectoryTests.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index 9f65b07a8..f858a12ea 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -599,14 +599,13 @@ public void MockDirectory_Delete_ShouldDeleteDirectory() } [Test] - [WindowsOnly(WindowsSpecifics.CaseInsensitivity)] public void MockDirectory_Delete_ShouldNotDeleteAllDirectories() { // Arrange - var folder1Path = @"D:\Test\Program"; - var folder1SubFolderPath = @"D:\Test\Program\Subfolder"; - var folder2Path = @"D:\Test\Program_bak"; - var folder3Path = @"D:\Test\Program_old"; + var folder1Path = XFS.Path(@"D:\Test\Program"); + var folder1SubFolderPath = XFS.Path(@"D:\Test\Program\Subfolder"); + var folder2Path = XFS.Path(@"D:\Test\Program_bak"); + var folder3Path = XFS.Path(@"D:\Test\Program_old"); var fileSystem = new MockFileSystem(); From e0448eaa4097be1b35bad2bdbedc7933f0101b68 Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Sat, 5 Jan 2019 23:03:10 +0100 Subject: [PATCH 14/15] Improve readability --- .../MockDirectory.cs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs index 2fc1a2739..cf5a7075a 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) { @@ -76,22 +75,28 @@ public override void Delete(string path, bool recursive) { path = mockFileDataAccessor.Path.GetFullPath(path).TrimSlashes(); - var pathWithDirectorySeparatorChar = path.Insert(path.Length, Path.DirectorySeparatorChar.ToString()); - + var stringOps = mockFileDataAccessor.StringOperations; + var pathWithDirectorySeparatorChar = $"{path}{Path.DirectorySeparatorChar}"; + var affectedPaths = mockFileDataAccessor .AllPaths - .Where(p => mockFileDataAccessor.StringOperations.Equals(p,path)|| mockFileDataAccessor.StringOperations.StartsWith(p, pathWithDirectorySeparatorChar)) + .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) From e832d62a51b713c9e3715d940b93b082e5361b80 Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Sat, 5 Jan 2019 23:06:44 +0100 Subject: [PATCH 15/15] Fix tests --- .../MockDirectoryTests.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index f858a12ea..72cf08c7e 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -605,24 +605,20 @@ public void MockDirectory_Delete_ShouldNotDeleteAllDirectories() var folder1Path = XFS.Path(@"D:\Test\Program"); var folder1SubFolderPath = XFS.Path(@"D:\Test\Program\Subfolder"); var folder2Path = XFS.Path(@"D:\Test\Program_bak"); - var folder3Path = XFS.Path(@"D:\Test\Program_old"); var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(folder1Path); fileSystem.AddDirectory(folder2Path); - fileSystem.AddDirectory(folder3Path); fileSystem.AddDirectory(folder1SubFolderPath); - var preDeleteCount = fileSystem.AllDirectories.Count(); - Assert.AreEqual(7, preDeleteCount); - // Act fileSystem.Directory.Delete(folder1Path, recursive: true); - var postDeleteCount = fileSystem.AllDirectories.Count(); // Assert - Assert.AreEqual(5, postDeleteCount); + Assert.IsFalse(fileSystem.Directory.Exists(folder1Path)); + Assert.IsFalse(fileSystem.Directory.Exists(folder1SubFolderPath)); + Assert.IsTrue(fileSystem.Directory.Exists(folder2Path)); } [Test]