TestableIO/System.IO.Abstractions

MockFileStreamFactory.Create does not throw correct exceptions

vbreuss opened this issue · 2 comments

Describe the bug
When comparing the behaviour of MockFileSystem.FileStream.Create with the the behaviour of the real file system (constructor of FileStream), some edge cases are not implemented correctly.
The following scenarios fail:
image

To Reproduce
The following system tests compare the behaviour of the MockFileSystem with the real file system and some scenarios fail due to not thrown or incorrect thrown exceptions:

using System.Collections.Generic;
using NUnit.Framework;
using System.Threading;
using System.Linq;

namespace System.IO.Abstractions.TestingHelpers.Tests
{
    [TestFixture]
    public class SystemTests
    {
        public static IEnumerable<object[]> GetSystemTestParameters()
        {
            foreach (var fileMode in Enum.GetValues(typeof(FileMode)).Cast<FileMode>())
            {
                foreach (var fileAccess in Enum.GetValues(typeof(FileAccess)).Cast<FileAccess>())
                {
                    yield return new object[] { true, fileMode, fileAccess };
                    yield return new object[] { false, fileMode, fileAccess };
                }
            }
        }

        [Test, TestCaseSource(nameof(GetSystemTestParameters))]
        public void Stream_Create_ShouldBehaveSameAsRealFileSystem(bool fileExists, FileMode fileMode, FileAccess fileAccess)
        {
            string fileName = @$"c:\test\existing_{fileExists}_{fileMode}_{fileAccess}.txt";
            if (fileExists)
            {
                File.WriteAllText(fileName, "foo");
            }
            else
            {
                File.Delete(fileName);
            }

            var time_before = DateTime.UtcNow.AddDays(-5);
            var time_after = time_before.AddSeconds(1);
            var fs = new MockFileSystem().MockTime(() => time_before);
            fs.Directory.CreateDirectory(@$"c:\test");
            if (fileExists)
            {
                fs.File.WriteAllText(fileName, "foo");
            }

            var realfilesystem_before_accessed = File.GetLastAccessTimeUtc(fileName);
            var realfilesystem_before_written = File.GetLastWriteTimeUtc(fileName);
            var realfilesystem_before_created = File.GetCreationTimeUtc(fileName);
            Thread.Sleep(1000);
            try
            {
                _ = new FileStream(fileName, fileMode, fileAccess);
            }
            catch (Exception ex)
            {
                fs.MockTime(() => time_after);
                Assert.Throws(ex.GetType(), () =>
                {
                    _ = fs.FileStream.Create(fileName, fileMode, fileAccess);
                });
                return;
            }

            var realfilesystem_after_accessed = File.GetLastAccessTimeUtc(fileName);
            var realfilesystem_after_written = File.GetLastWriteTimeUtc(fileName);
            var realfilesystem_after_created = File.GetCreationTimeUtc(fileName);

            bool isRealCreatedChanged = realfilesystem_before_created != realfilesystem_after_created;
            bool isRealAccessedChanged = realfilesystem_before_accessed != realfilesystem_after_accessed;
            bool isRealWrittenChanged = realfilesystem_before_written != realfilesystem_after_written;

            var mockfilesystem_before_accessed = fs.File.GetLastAccessTimeUtc(fileName);
            var mockfilesystem_before_written = fs.File.GetLastWriteTimeUtc(fileName);
            var mockfilesystem_before_created = fs.File.GetCreationTimeUtc(fileName);
            fs.MockTime(() => time_after);
            _ = fs.FileStream.Create(fileName, fileMode, fileAccess);
            var mockfilesystem_after_accessed = fs.File.GetLastAccessTimeUtc(fileName);
            var mockfilesystem_after_written = fs.File.GetLastWriteTimeUtc(fileName);
            var mockfilesystem_after_created = fs.File.GetCreationTimeUtc(fileName);

            if (isRealCreatedChanged)
            {
                Assert.That(mockfilesystem_before_created, Is.Not.EqualTo(mockfilesystem_after_created), message: "Created");
            }
            else
            {
                Assert.That(mockfilesystem_before_created, Is.EqualTo(mockfilesystem_after_created), message: "Created");
            }
            if (isRealAccessedChanged)
            {
                Assert.That(mockfilesystem_before_accessed, Is.Not.EqualTo(mockfilesystem_after_accessed), message: "Accessed");
            }
            else
            {
                Assert.That(mockfilesystem_before_accessed, Is.EqualTo(mockfilesystem_after_accessed), message: "Accessed");
            }
            if (isRealWrittenChanged)
            {
                Assert.That(mockfilesystem_before_written, Is.Not.EqualTo(mockfilesystem_after_written), message: "Written");
            }
            else
            {
                Assert.That(mockfilesystem_before_written, Is.EqualTo(mockfilesystem_after_written), message: "Written");
            }
        }
    }
}

Expected behavior
All tests should pass.

Additional context
See here for some comparison tests.

Thanks for writing the issue and providing the test cases!

This is addressed in release v19.2.16.