@@ -8,45 +8,49 @@ namespace SixLabors.ImageSharp.Drawing.Tests;
88/// </summary>
99public class TestFileSystem : IO . IFileSystem
1010{
11- private readonly Dictionary < string , Stream > fileSystem = new ( StringComparer . OrdinalIgnoreCase ) ;
11+ private readonly Dictionary < string , Func < Stream > > fileSystem = new ( StringComparer . OrdinalIgnoreCase ) ;
1212
13- public void AddFile ( string path , Stream data )
13+ public void AddFile ( string path , Func < Stream > data )
1414 {
1515 lock ( this . fileSystem )
1616 {
1717 this . fileSystem . Add ( path , data ) ;
1818 }
1919 }
2020
21- public Stream Create ( string path )
21+ public Stream Create ( string path ) => this . GetStream ( path ) ?? File . Create ( path ) ;
22+
23+ public Stream CreateAsynchronous ( string path ) => this . GetStream ( path ) ?? File . Open ( path , new FileStreamOptions
2224 {
23- // if we have injected a fake file use it instead
24- lock ( this . fileSystem )
25- {
26- if ( this . fileSystem . ContainsKey ( path ) )
27- {
28- Stream stream = this . fileSystem [ path ] ;
29- stream . Position = 0 ;
30- return stream ;
31- }
32- }
25+ Mode = FileMode . Create ,
26+ Access = FileAccess . ReadWrite ,
27+ Share = FileShare . None ,
28+ Options = FileOptions . Asynchronous ,
29+ } ) ;
3330
34- return File . Create ( path ) ;
35- }
31+ public Stream OpenRead ( string path ) => this . GetStream ( path ) ?? File . OpenRead ( path ) ;
32+
33+ public Stream OpenReadAsynchronous ( string path ) => this . GetStream ( path ) ?? File . Open ( path , new FileStreamOptions
34+ {
35+ Mode = FileMode . Open ,
36+ Access = FileAccess . Read ,
37+ Share = FileShare . Read ,
38+ Options = FileOptions . Asynchronous ,
39+ } ) ;
3640
37- public Stream OpenRead ( string path )
41+ private Stream ? GetStream ( string path )
3842 {
3943 // if we have injected a fake file use it instead
4044 lock ( this . fileSystem )
4145 {
42- if ( this . fileSystem . ContainsKey ( path ) )
46+ if ( this . fileSystem . TryGetValue ( path , out Func < Stream > ? streamFactory ) )
4347 {
44- Stream stream = this . fileSystem [ path ] ;
48+ Stream stream = streamFactory ( ) ;
4549 stream . Position = 0 ;
4650 return stream ;
4751 }
4852 }
4953
50- return File . OpenRead ( path ) ;
54+ return null ;
5155 }
5256}
0 commit comments