Skip to content

Commit 413cd6a

Browse files
PatrykOlejniczakfgreinacher
authored andcommitted
Fix exists and lenght properties in MockFileInfo.
1 parent d8be8f2 commit 413cd6a

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ public void MockFileInfo_Exists_ShouldReturnFalseIfFileDoesNotExistInMemoryFileS
5050
Assert.IsFalse(result);
5151
}
5252

53+
[Test]
54+
public void MockFileInfo_Exists_ShouldRetunFalseIfPathLeadsToDirectory()
55+
{
56+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
57+
{
58+
{ XFS.Path(@"c:\a\b\c.txt"), new MockFileData("Demo text content") },
59+
});
60+
var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a\b"));
61+
62+
var result = fileInfo.Exists;
63+
64+
Assert.IsFalse(result);
65+
}
66+
5367
[Test]
5468
public void MockFileInfo_Length_ShouldReturnLengthOfFileInMemoryFileSystem()
5569
{
@@ -82,6 +96,21 @@ public void MockFileInfo_Length_ShouldThrowFileNotFoundExceptionIfFileDoesNotExi
8296
Assert.AreEqual(XFS.Path(@"c:\foo.txt"), ex.FileName);
8397
}
8498

99+
[Test]
100+
public void MockFileInfo_Length_ShouldThrowFileNotFoundExceptionIfPathLeadsToDirectory()
101+
{
102+
const string fileContent = "Demo text content";
103+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
104+
{
105+
{ XFS.Path(@"c:\a\b\c.txt"), new MockFileData(fileContent) },
106+
});
107+
var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a\b"));
108+
109+
var ex = Assert.Throws<FileNotFoundException>(() => fileInfo.Length.ToString(CultureInfo.InvariantCulture));
110+
111+
Assert.AreEqual(XFS.Path(@"c:\a\b"), ex.FileName);
112+
}
113+
85114
[Test]
86115
public void MockFileInfo_CreationTimeUtc_ShouldReturnCreationTimeUtcOfFileInMemoryFileSystem()
87116
{

System.IO.Abstractions.TestingHelpers/MockFileInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public override DateTime CreationTimeUtc
6868

6969
public override bool Exists
7070
{
71-
get { return MockFileData != null; }
71+
get { return MockFileData != null && !MockFileData.IsDirectory; }
7272
}
7373

7474
public override string Extension
@@ -307,7 +307,7 @@ public override long Length
307307
{
308308
get
309309
{
310-
if (MockFileData == null) throw CommonExceptions.FileNotFound(path);
310+
if (MockFileData == null || MockFileData.IsDirectory) throw CommonExceptions.FileNotFound(path);
311311
return MockFileData.Contents.Length;
312312
}
313313
}

0 commit comments

Comments
 (0)