Skip to content

Commit

Permalink
Return original path from Mock*Info.ToString() (#406)
Browse files Browse the repository at this point in the history
Contributes to #400
  • Loading branch information
updateaman authored and fgreinacher committed Dec 3, 2018
1 parent c34ca37 commit c0da0c0
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static IEnumerable<object[]> 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 };
}
}

Expand Down Expand Up @@ -152,8 +152,8 @@ public void MockDirectoryInfo_GetParent_ShouldReturnDirectoriesAndNamesWithSearc
[Test]
public void MockDirectoryInfo_EnumerateFiles_ShouldReturnAllFiles()
{
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
//Files "above" in folder we're querying
{ XFS.Path(@"c:\temp\a.txt"), "" },
Expand All @@ -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]
Expand Down Expand Up @@ -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<ArgumentNullException>(action);
Expand Down Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
23 changes: 20 additions & 3 deletions System.IO.Abstractions.TestingHelpers.Tests/MockDriveInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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")]
Expand All @@ -59,13 +59,30 @@ 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;

// 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());
}
}
}
20 changes: 18 additions & 2 deletions System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, MockFileData>
{
{ XFS.Path(@"c:\a.txt"), fileData }
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -491,6 +491,22 @@ public void MockFileInfo_CopyTo_ThrowsExceptionIfSourceDoesntExist()
Assert.Throws<FileNotFoundException>(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()
Expand Down
13 changes: 7 additions & 6 deletions System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.AccessControl;

Expand All @@ -10,6 +9,7 @@ public class MockDirectoryInfo : DirectoryInfoBase
{
private readonly IMockFileDataAccessor mockFileDataAccessor;
private readonly string directoryPath;
private readonly string originalPath;

/// <summary>
/// Initializes a new instance of the <see cref="MockDirectoryInfo"/> class.
Expand All @@ -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();
Expand Down Expand Up @@ -283,11 +284,6 @@ public override DirectoryInfoBase Root
}
}

public override string ToString()
{
return FullName;
}

private MockFileData GetMockFileDataForRead()
{
return mockFileDataAccessor.GetFile(directoryPath) ?? MockFileData.NullObject;
Expand All @@ -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;
}
}
}
11 changes: 8 additions & 3 deletions System.IO.Abstractions.TestingHelpers/MockDriveInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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; }
Expand Down
9 changes: 7 additions & 2 deletions System.IO.Abstractions.TestingHelpers/MockFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public override Stream OpenRead()

public override StreamReader OpenText()
{
return new StreamReader(OpenRead());
return new StreamReader(OpenRead());
}

public override Stream OpenWrite()
Expand Down Expand Up @@ -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;
Expand All @@ -311,5 +311,10 @@ public override long Length
return MockFileData.Contents.Length;
}
}

public override string ToString()
{
return path;
}
}
}

0 comments on commit c0da0c0

Please sign in to comment.