Skip to content

MockFileStreamFactory.Create does not throw correct exceptions #884

Description

@vbreuss

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:

usingSystem.Collections.Generic;usingNUnit.Framework;usingSystem.Threading;usingSystem.Linq;namespaceSystem.IO.Abstractions.TestingHelpers.Tests{[TestFixture]publicclassSystemTests{publicstaticIEnumerable<object[]>GetSystemTestParameters(){foreach(varfileModeinEnum.GetValues(typeof(FileMode)).Cast<FileMode>()){foreach(varfileAccessinEnum.GetValues(typeof(FileAccess)).Cast<FileAccess>()){yieldreturnnewobject[]{true,fileMode,fileAccess};yieldreturnnewobject[]{false,fileMode,fileAccess};}}}[Test,TestCaseSource(nameof(GetSystemTestParameters))]publicvoidStream_Create_ShouldBehaveSameAsRealFileSystem(boolfileExists,FileModefileMode,FileAccessfileAccess){stringfileName=@$"c:\test\existing_{fileExists}_{fileMode}_{fileAccess}.txt";if(fileExists){File.WriteAllText(fileName,"foo");}else{File.Delete(fileName);}vartime_before=DateTime.UtcNow.AddDays(-5);vartime_after=time_before.AddSeconds(1);varfs=newMockFileSystem().MockTime(()=>time_before);fs.Directory.CreateDirectory(@$"c:\test");if(fileExists){fs.File.WriteAllText(fileName,"foo");}varrealfilesystem_before_accessed=File.GetLastAccessTimeUtc(fileName);varrealfilesystem_before_written=File.GetLastWriteTimeUtc(fileName);varrealfilesystem_before_created=File.GetCreationTimeUtc(fileName);Thread.Sleep(1000);try{_=newFileStream(fileName,fileMode,fileAccess);}catch(Exceptionex){fs.MockTime(()=>time_after);Assert.Throws(ex.GetType(),()=>{_=fs.FileStream.Create(fileName,fileMode,fileAccess);});return;}varrealfilesystem_after_accessed=File.GetLastAccessTimeUtc(fileName);varrealfilesystem_after_written=File.GetLastWriteTimeUtc(fileName);varrealfilesystem_after_created=File.GetCreationTimeUtc(fileName);boolisRealCreatedChanged=realfilesystem_before_created!=realfilesystem_after_created;boolisRealAccessedChanged=realfilesystem_before_accessed!=realfilesystem_after_accessed;boolisRealWrittenChanged=realfilesystem_before_written!=realfilesystem_after_written;varmockfilesystem_before_accessed=fs.File.GetLastAccessTimeUtc(fileName);varmockfilesystem_before_written=fs.File.GetLastWriteTimeUtc(fileName);varmockfilesystem_before_created=fs.File.GetCreationTimeUtc(fileName);fs.MockTime(()=>time_after);_=fs.FileStream.Create(fileName,fileMode,fileAccess);varmockfilesystem_after_accessed=fs.File.GetLastAccessTimeUtc(fileName);varmockfilesystem_after_written=fs.File.GetLastWriteTimeUtc(fileName);varmockfilesystem_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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testinghelpersIssues that address the testing helpersstate: ready to pickIssues that are ready for being worked onstate: releasedIssues that are releasedtype: bugIssues that describe misbehaving functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions