From c0da0c0e57bb75ed65f51f75f1e4b256b1b2fc7e Mon Sep 17 00:00:00 2001 From: Aman Baloch Date: Mon, 3 Dec 2018 19:15:27 +0000 Subject: [PATCH] Return original path from Mock*Info.ToString() (#406) Contributes to #400 --- .../MockDirectoryInfoTests.cs | 32 +++++++++---------- .../MockDriveInfoFactoryTests.cs | 6 ++-- .../MockDriveInfoTests.cs | 23 +++++++++++-- .../MockFileInfoTests.cs | 20 ++++++++++-- .../MockDirectoryInfo.cs | 13 ++++---- .../MockDriveInfo.cs | 11 +++++-- .../MockFileInfo.cs | 9 ++++-- 7 files changed, 78 insertions(+), 36 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs index 194dde31b..a850a157d 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,20 +272,18 @@ public void MockDirectoryInfo_Constructor_ShouldThrowArgumentException_IfArgumen Assert.That(exception.Message, Does.StartWith("The path is not of a legal form.")); } - [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"); - // 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); // Assert - Assert.AreEqual(directoryPath, str); + Assert.AreEqual(directoryPath, mockDirectoryInfo.ToString()); } } } \ No newline at end of file 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 8b33fd16f..88524b2f4 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; @@ -67,5 +67,22 @@ public void MockDriveInfo_RootDirectory_ShouldReturnTheDirectoryBase() // Assert Assert.AreEqual(expectedDirectory, actualDirectory.FullName); } + + [TestCase("c:","c:\\")] + [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); + + // Assert + Assert.AreEqual(expectedPath, mockDriveInfo.ToString()); + } } } diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index e75cb3cec..c3b2a03b6 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,22 @@ public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist() Assert.Throws(action); } + [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(path); + + //Act + var mockFileInfo = new MockFileInfo(new MockFileSystem(), filePath); + + //Assert + Assert.AreEqual(filePath, mockFileInfo.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 90f2ce9a1..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,6 +9,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 +21,7 @@ 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(); @@ -283,11 +284,6 @@ public override DirectoryInfoBase Root } } - public override string ToString() - { - return FullName; - } - private MockFileData GetMockFileDataForRead() { return mockFileDataAccessor.GetFile(directoryPath) ?? MockFileData.NullObject; @@ -298,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/MockDriveInfo.cs b/System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs index 918c147f0..e2ee47700 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 { @@ -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.TestingHelpers/MockFileInfo.cs b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs index 298d054be..0d11fea89 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs @@ -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; @@ -311,5 +311,10 @@ public override long Length return MockFileData.Contents.Length; } } + + public override string ToString() + { + return path; + } } }