2
2
using System ;
3
3
using System . Collections . Generic ;
4
4
using System . IO ;
5
+ using System . Runtime . InteropServices ;
5
6
using Testably . Abstractions . Testing . FileSystem ;
6
7
using Testably . Abstractions . Testing . Helpers ;
7
8
using Testably . Abstractions . Testing . Statistics ;
@@ -82,9 +83,19 @@ internal IReadOnlyList<IStorageContainer> StorageContainers
82
83
/// <summary>
83
84
/// Initializes the <see cref="MockFileSystem" />.
84
85
/// </summary>
85
- public MockFileSystem ( )
86
+ public MockFileSystem ( ) : this ( _ => { } ) { }
87
+
88
+ /// <summary>
89
+ /// Initializes the <see cref="MockFileSystem" /> with the <paramref name="initializationCallback" />.
90
+ /// </summary>
91
+ internal MockFileSystem ( Action < Initialization > initializationCallback )
86
92
{
87
- Execute = new Execute ( this ) ;
93
+ Initialization initialization = new ( ) ;
94
+ initializationCallback ( initialization ) ;
95
+
96
+ Execute = initialization . OperatingSystem == null
97
+ ? new Execute ( this )
98
+ : new Execute ( this , initialization . OperatingSystem . Value ) ;
88
99
StatisticsRegistration = new FileSystemStatistics ( this ) ;
89
100
using IDisposable release = StatisticsRegistration . Ignore ( ) ;
90
101
RandomSystem = new MockRandomSystem ( ) ;
@@ -101,7 +112,7 @@ public MockFileSystem()
101
112
FileSystemWatcher = new FileSystemWatcherFactoryMock ( this ) ;
102
113
SafeFileHandleStrategy = new NullSafeFileHandleStrategy ( ) ;
103
114
AccessControlStrategy = new NullAccessControlStrategy ( ) ;
104
- AddDriveFromCurrentDirectory ( ) ;
115
+ InitializeFileSystem ( initialization ) ;
105
116
}
106
117
107
118
#region IFileSystem Members
@@ -181,11 +192,17 @@ public MockFileSystem WithSafeFileHandleStrategy(
181
192
return this ;
182
193
}
183
194
184
- private void AddDriveFromCurrentDirectory ( )
195
+ private void InitializeFileSystem ( Initialization initialization )
185
196
{
186
197
try
187
198
{
188
- string ? root = Path . GetPathRoot ( System . IO . Directory . GetCurrentDirectory ( ) ) ;
199
+ if ( initialization . CurrentDirectory != null )
200
+ {
201
+ IDirectoryInfo directoryInfo = DirectoryInfo . New ( initialization . CurrentDirectory ) ;
202
+ Storage . CurrentDirectory = directoryInfo . FullName ;
203
+ }
204
+
205
+ string ? root = Execute . Path . GetPathRoot ( Directory . GetCurrentDirectory ( ) ) ;
189
206
if ( root != null &&
190
207
root [ 0 ] != _storage . MainDrive . Name [ 0 ] )
191
208
{
@@ -198,4 +215,61 @@ private void AddDriveFromCurrentDirectory()
198
215
// due to brittle tests on MacOS
199
216
}
200
217
}
218
+
219
+ /// <summary>
220
+ /// The initialization options for the <see cref="MockFileSystem" />.
221
+ /// </summary>
222
+ internal class Initialization
223
+ {
224
+ /// <summary>
225
+ /// The current directory.
226
+ /// </summary>
227
+ internal string ? CurrentDirectory { get ; private set ; }
228
+
229
+ /// <summary>
230
+ /// The simulated operating system.
231
+ /// </summary>
232
+ internal OSPlatform ? OperatingSystem { get ; private set ; }
233
+
234
+ /// <summary>
235
+ /// Specify the operating system that should be simulated.
236
+ /// </summary>
237
+ /// <remarks>
238
+ /// Supported values are<br />
239
+ /// - <see cref="OSPlatform.Linux" /><br />
240
+ /// - <see cref="OSPlatform.OSX" /><br />
241
+ /// - <see cref="OSPlatform.Windows" />
242
+ /// </remarks>
243
+ internal Initialization SimulatingOperatingSystem ( OSPlatform operatingSystem )
244
+ {
245
+ if ( operatingSystem != OSPlatform . Linux &&
246
+ operatingSystem != OSPlatform . OSX &&
247
+ operatingSystem != OSPlatform . Windows )
248
+ {
249
+ throw new NotSupportedException (
250
+ "Only Linux, OSX and Windows are supported operating systems." ) ;
251
+ }
252
+
253
+ OperatingSystem = operatingSystem ;
254
+ return this ;
255
+ }
256
+
257
+ /// <summary>
258
+ /// Use the provided <paramref name="path" /> as current directory.
259
+ /// </summary>
260
+ internal Initialization UseCurrentDirectory ( string path )
261
+ {
262
+ CurrentDirectory = path ;
263
+ return this ;
264
+ }
265
+
266
+ /// <summary>
267
+ /// Use <see cref="Directory.GetCurrentDirectory()" /> as current directory.
268
+ /// </summary>
269
+ internal Initialization UseCurrentDirectory ( )
270
+ {
271
+ CurrentDirectory = System . IO . Directory . GetCurrentDirectory ( ) ;
272
+ return this ;
273
+ }
274
+ }
201
275
}
0 commit comments