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
29 changes: 29 additions & 0 deletions System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public void MockFileInfo_Exists_ShouldReturnFalseIfFileDoesNotExistInMemoryFileS
Assert.IsFalse(result);
}

[Test]
public void MockFileInfo_Exists_ShouldRetunFalseIfPathLeadsToDirectory()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\a\b\c.txt"), new MockFileData("Demo text content") },
});
var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a\b"));

var result = fileInfo.Exists;

Assert.IsFalse(result);
}

[Test]
public void MockFileInfo_Length_ShouldReturnLengthOfFileInMemoryFileSystem()
{
Expand Down Expand Up @@ -82,6 +96,21 @@ public void MockFileInfo_Length_ShouldThrowFileNotFoundExceptionIfFileDoesNotExi
Assert.AreEqual(XFS.Path(@"c:\foo.txt"), ex.FileName);
}

[Test]
public void MockFileInfo_Length_ShouldThrowFileNotFoundExceptionIfPathLeadsToDirectory()
{
const string fileContent = "Demo text content";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"c:\a\b\c.txt"), new MockFileData(fileContent) },
});
var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a\b"));

var ex = Assert.Throws<FileNotFoundException>(() => fileInfo.Length.ToString(CultureInfo.InvariantCulture));

Assert.AreEqual(XFS.Path(@"c:\a\b"), ex.FileName);
}

[Test]
public void MockFileInfo_CreationTimeUtc_ShouldReturnCreationTimeUtcOfFileInMemoryFileSystem()
{
Expand Down
4 changes: 2 additions & 2 deletions System.IO.Abstractions.TestingHelpers/MockFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override DateTime CreationTimeUtc

public override bool Exists
{
get { return MockFileData != null; }
get { return MockFileData != null && !MockFileData.IsDirectory; }
}

public override string Extension
Expand Down Expand Up @@ -307,7 +307,7 @@ public override long Length
{
get
{
if (MockFileData == null) throw CommonExceptions.FileNotFound(path);
if (MockFileData == null || MockFileData.IsDirectory) throw CommonExceptions.FileNotFound(path);
return MockFileData.Contents.Length;
}
}
Expand Down