From d5542fff8ee21b979064e2210ee767a087da7acb Mon Sep 17 00:00:00 2001 From: Patryk Olejniczak Date: Sun, 24 Feb 2019 19:52:42 +0100 Subject: [PATCH] Fix exists and lenght properties in MockFileInfo. --- .../MockFileInfoTests.cs | 29 +++++++++++++++++++ .../MockFileInfo.cs | 4 +-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index c3b2a03b6..876564e71 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -50,6 +50,20 @@ public void MockFileInfo_Exists_ShouldReturnFalseIfFileDoesNotExistInMemoryFileS Assert.IsFalse(result); } + [Test] + public void MockFileInfo_Exists_ShouldRetunFalseIfPathLeadsToDirectory() + { + var fileSystem = new MockFileSystem(new Dictionary + { + { 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() { @@ -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 + { + { XFS.Path(@"c:\a\b\c.txt"), new MockFileData(fileContent) }, + }); + var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a\b")); + + var ex = Assert.Throws(() => fileInfo.Length.ToString(CultureInfo.InvariantCulture)); + + Assert.AreEqual(XFS.Path(@"c:\a\b"), ex.FileName); + } + [Test] public void MockFileInfo_CreationTimeUtc_ShouldReturnCreationTimeUtcOfFileInMemoryFileSystem() { diff --git a/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs index 03d7e809e..34691af01 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs @@ -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 @@ -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; } }