Skip to content

Commit 514706d

Browse files
refactor: make Execute non-static (#462)
As a prerequisite to #460 refactor how to handle OS-specific use cases: - Make the `Execute` class non-static and a property of the `MockFileSystem` - Replace all calls to the `Execute` methods to use the property on the `MockFileSystem` instead - Move the [`StringComparisonMode`](https://github.com/Testably/Testably.Abstractions/blob/c42bd527e3795677136d1af787f3c5a243ff6c3f/Source/Testably.Abstractions.Testing/Storage/InMemoryLocation.cs#L9) from `InMemoryLocation` to the `Execute` class --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
1 parent c42bd52 commit 514706d

38 files changed

+364
-305
lines changed

Source/Testably.Abstractions.Testing/FileSystem/DirectoryInfoMock.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public IDirectoryInfo? Parent
3434

3535
/// <inheritdoc cref="IDirectoryInfo.Root" />
3636
public IDirectoryInfo Root
37-
=> New(_fileSystem.Storage.GetLocation(string.Empty.PrefixRoot()),
37+
=> New(_fileSystem.Storage.GetLocation(string.Empty.PrefixRoot(_fileSystem)),
3838
_fileSystem);
3939

4040
/// <inheritdoc cref="IDirectoryInfo.Create()" />
@@ -48,10 +48,10 @@ public void Create()
4848

4949
if (Container.Type != FileSystemTypes.Directory)
5050
{
51-
throw ExceptionFactory.CannotCreateFileAsAlreadyExists(FullName);
51+
throw ExceptionFactory.CannotCreateFileAsAlreadyExists(_fileSystem.Execute, FullName);
5252
}
5353

54-
ResetCache(!Execute.IsNetFramework);
54+
ResetCache(!_fileSystem.Execute.IsNetFramework);
5555
}
5656

5757
/// <inheritdoc cref="IDirectoryInfo.CreateSubdirectory(string)" />
@@ -61,7 +61,7 @@ public IDirectoryInfo CreateSubdirectory(string path)
6161
_fileSystem.Storage.GetLocation(
6262
_fileSystem.Path.Combine(FullName, path
6363
.EnsureValidFormat(_fileSystem, nameof(path),
64-
Execute.IsWindows && !Execute.IsNetFramework))),
64+
_fileSystem.Execute.IsWindows && !_fileSystem.Execute.IsNetFramework))),
6565
_fileSystem);
6666
directory.Create();
6767
return directory;
@@ -75,7 +75,7 @@ public override void Delete()
7575
throw ExceptionFactory.DirectoryNotFound(Location.FullPath);
7676
}
7777

78-
ResetCache(!Execute.IsNetFramework);
78+
ResetCache(!_fileSystem.Execute.IsNetFramework);
7979
}
8080

8181
/// <inheritdoc cref="IDirectoryInfo.Delete(bool)" />
@@ -87,7 +87,7 @@ public void Delete(bool recursive)
8787
throw ExceptionFactory.DirectoryNotFound(FullName);
8888
}
8989

90-
ResetCache(!Execute.IsNetFramework);
90+
ResetCache(!_fileSystem.Execute.IsNetFramework);
9191
}
9292

9393
/// <inheritdoc cref="IDirectoryInfo.EnumerateDirectories()" />
@@ -276,7 +276,7 @@ private IEnumerable<IStorageLocation> EnumerateInternal(
276276
EnumerationOptions enumerationOptions)
277277
{
278278
StorageExtensions.AdjustedLocation adjustedLocation = _fileSystem.Storage
279-
.AdjustLocationFromSearchPattern(
279+
.AdjustLocationFromSearchPattern(_fileSystem,
280280
path.EnsureValidFormat(_fileSystem),
281281
searchPattern);
282282
return _fileSystem.Storage.EnumerateLocations(

Source/Testably.Abstractions.Testing/FileSystem/DirectoryMock.cs

+20-20
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public IDirectoryInfo CreateTempSubdirectory(string? prefix = null)
7373
_fileSystem.Path.GetTempPath(),
7474
(prefix ?? "") + _fileSystem.Path.GetFileNameWithoutExtension(
7575
_fileSystem.Path.GetRandomFileName()));
76-
Execute.OnMac(() => localBasePath = "/private" + localBasePath);
76+
_fileSystem.Execute.OnMac(() => localBasePath = "/private" + localBasePath);
7777
basePath = localBasePath;
7878
} while (_fileSystem.Directory.Exists(basePath));
7979

@@ -85,13 +85,13 @@ public IDirectoryInfo CreateTempSubdirectory(string? prefix = null)
8585
/// <inheritdoc cref="IDirectory.Delete(string)" />
8686
public void Delete(string path)
8787
=> _fileSystem.DirectoryInfo
88-
.New(path.EnsureValidFormat(FileSystem))
88+
.New(path.EnsureValidFormat(_fileSystem))
8989
.Delete();
9090

9191
/// <inheritdoc cref="IDirectory.Delete(string, bool)" />
9292
public void Delete(string path, bool recursive)
9393
=> _fileSystem.DirectoryInfo
94-
.New(path.EnsureValidFormat(FileSystem))
94+
.New(path.EnsureValidFormat(_fileSystem))
9595
.Delete(recursive);
9696

9797
/// <inheritdoc cref="IDirectory.EnumerateDirectories(string)" />
@@ -199,14 +199,14 @@ public bool Exists([NotNullWhen(true)] string? path)
199199
public DateTime GetCreationTime(string path)
200200
=> _fileSystem.Storage.GetContainer(
201201
_fileSystem.Storage.GetLocation(
202-
path.EnsureValidFormat(FileSystem)))
202+
path.EnsureValidFormat(_fileSystem)))
203203
.CreationTime.Get(DateTimeKind.Local);
204204

205205
/// <inheritdoc cref="IDirectory.GetCreationTimeUtc(string)" />
206206
public DateTime GetCreationTimeUtc(string path)
207207
=> _fileSystem.Storage.GetContainer(
208208
_fileSystem.Storage.GetLocation(
209-
path.EnsureValidFormat(FileSystem)))
209+
path.EnsureValidFormat(_fileSystem)))
210210
.CreationTime.Get(DateTimeKind.Utc);
211211

212212
/// <inheritdoc cref="IDirectory.GetCurrentDirectory()" />
@@ -290,28 +290,28 @@ public string[] GetFileSystemEntries(string path,
290290
public DateTime GetLastAccessTime(string path)
291291
=> _fileSystem.Storage.GetContainer(
292292
_fileSystem.Storage.GetLocation(
293-
path.EnsureValidFormat(FileSystem)))
293+
path.EnsureValidFormat(_fileSystem)))
294294
.LastAccessTime.Get(DateTimeKind.Local);
295295

296296
/// <inheritdoc cref="IDirectory.GetLastAccessTimeUtc(string)" />
297297
public DateTime GetLastAccessTimeUtc(string path)
298298
=> _fileSystem.Storage.GetContainer(
299299
_fileSystem.Storage.GetLocation(
300-
path.EnsureValidFormat(FileSystem)))
300+
path.EnsureValidFormat(_fileSystem)))
301301
.LastAccessTime.Get(DateTimeKind.Utc);
302302

303303
/// <inheritdoc cref="IDirectory.GetLastWriteTime(string)" />
304304
public DateTime GetLastWriteTime(string path)
305305
=> _fileSystem.Storage.GetContainer(
306306
_fileSystem.Storage.GetLocation(
307-
path.EnsureValidFormat(FileSystem)))
307+
path.EnsureValidFormat(_fileSystem)))
308308
.LastWriteTime.Get(DateTimeKind.Local);
309309

310310
/// <inheritdoc cref="IDirectory.GetLastWriteTimeUtc(string)" />
311311
public DateTime GetLastWriteTimeUtc(string path)
312312
=> _fileSystem.Storage.GetContainer(
313313
_fileSystem.Storage.GetLocation(
314-
path.EnsureValidFormat(FileSystem)))
314+
path.EnsureValidFormat(_fileSystem)))
315315
.LastWriteTime.Get(DateTimeKind.Utc);
316316

317317
/// <inheritdoc cref="IDirectory.GetLogicalDrives()" />
@@ -346,7 +346,7 @@ public void Move(string sourceDirName, string destDirName)
346346
when (ex.HResult != -2147024773)
347347
{
348348
throw ExceptionFactory.FileNameCannotBeResolved(linkPath,
349-
Execute.IsWindows ? -2147022975 : -2146232800);
349+
_fileSystem.Execute.IsWindows ? -2147022975 : -2146232800);
350350
}
351351
}
352352
#endif
@@ -402,30 +402,30 @@ public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
402402
#endregion
403403

404404
private IDirectoryInfo LoadDirectoryInfoOrThrowNotFoundException(
405-
string path, Action<IFileSystem, string> onMissingCallback)
405+
string path, Action<MockFileSystem, string> onMissingCallback)
406406
{
407407
IDirectoryInfo directoryInfo =
408-
_fileSystem.DirectoryInfo.New(path.EnsureValidFormat(FileSystem));
408+
_fileSystem.DirectoryInfo.New(path.EnsureValidFormat(_fileSystem));
409409
if (!directoryInfo.Exists)
410410
{
411-
onMissingCallback.Invoke(FileSystem, path);
411+
onMissingCallback.Invoke(_fileSystem, path);
412412
}
413413

414414
return directoryInfo;
415415
}
416416

417-
private static void ThrowMissingFileCreatedTimeException(IFileSystem fileSystem, string path)
417+
private static void ThrowMissingFileCreatedTimeException(MockFileSystem fileSystem, string path)
418418
{
419419
#if NET7_0_OR_GREATER
420-
Execute.OnMac(
420+
fileSystem.Execute.OnMac(
421421
() =>
422422
throw ExceptionFactory.DirectoryNotFound(
423423
fileSystem.Path.GetFullPath(path)),
424424
() =>
425425
throw ExceptionFactory.FileNotFound(
426426
fileSystem.Path.GetFullPath(path)));
427427
#else
428-
Execute.OnWindows(
428+
fileSystem.Execute.OnWindows(
429429
() =>
430430
throw ExceptionFactory.FileNotFound(
431431
fileSystem.Path.GetFullPath(path)),
@@ -435,14 +435,14 @@ private static void ThrowMissingFileCreatedTimeException(IFileSystem fileSystem,
435435
#endif
436436
}
437437

438-
private static void ThrowMissingFileLastAccessOrLastWriteTimeException(IFileSystem fileSystem,
438+
private static void ThrowMissingFileLastAccessOrLastWriteTimeException(MockFileSystem fileSystem,
439439
string path)
440440
{
441441
#if NET7_0_OR_GREATER
442442
throw ExceptionFactory.FileNotFound(
443443
fileSystem.Path.GetFullPath(path));
444444
#else
445-
Execute.OnWindows(
445+
fileSystem.Execute.OnWindows(
446446
() =>
447447
throw ExceptionFactory.FileNotFound(
448448
fileSystem.Path.GetFullPath(path)),
@@ -458,8 +458,8 @@ private IEnumerable<string> EnumerateInternal(FileSystemTypes fileSystemTypes,
458458
EnumerationOptions enumerationOptions)
459459
{
460460
StorageExtensions.AdjustedLocation adjustedLocation = _fileSystem.Storage
461-
.AdjustLocationFromSearchPattern(
462-
path.EnsureValidFormat(FileSystem),
461+
.AdjustLocationFromSearchPattern(_fileSystem,
462+
path.EnsureValidFormat(_fileSystem),
463463
searchPattern);
464464
return _fileSystem.Storage.EnumerateLocations(
465465
adjustedLocation.Location,

Source/Testably.Abstractions.Testing/FileSystem/DriveInfoMock.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private DriveInfoMock(string driveName, MockFileSystem fileSystem)
3737
{
3838
_fileSystem = fileSystem;
3939

40-
if (driveName.IsUncPath())
40+
if (driveName.IsUncPath(_fileSystem))
4141
{
4242
IsUncPath = true;
4343
driveName = PathHelper.UncPrefix +
@@ -102,7 +102,7 @@ public string VolumeLabel
102102
set
103103
{
104104
_volumeLabel = value ?? _volumeLabel;
105-
Execute.NotOnWindows(
105+
_fileSystem.Execute.NotOnWindows(
106106
() => throw ExceptionFactory.OperationNotSupportedOnThisPlatform());
107107
}
108108
}
@@ -176,7 +176,7 @@ private string GetTopmostParentDirectory(string path)
176176
}
177177

178178
private static string ValidateDriveLetter(string driveName,
179-
IFileSystem fileSystem)
179+
MockFileSystem fileSystem)
180180
{
181181
if (driveName.Length == 1 &&
182182
char.IsLetter(driveName, 0))
@@ -186,7 +186,7 @@ private static string ValidateDriveLetter(string driveName,
186186

187187
if (fileSystem.Path.IsPathRooted(driveName))
188188
{
189-
return Execute.OnWindows(() =>
189+
return fileSystem.Execute.OnWindows(() =>
190190
{
191191
string rootedPath = fileSystem.Path.GetPathRoot(driveName)!;
192192
return $"{rootedPath.TrimEnd('\\')}\\";

Source/Testably.Abstractions.Testing/FileSystem/FileInfoFactoryMock.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public IFileInfo New(string fileName)
3333
throw new ArgumentNullException(nameof(fileName));
3434
}
3535

36-
if (fileName.IsEffectivelyEmpty())
36+
if (fileName.IsEffectivelyEmpty(_fileSystem))
3737
{
3838
throw ExceptionFactory.PathIsEmpty("path");
3939
}

Source/Testably.Abstractions.Testing/FileSystem/FileInfoMock.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public long Length
6161
Container.Type != FileSystemTypes.File)
6262
{
6363
throw ExceptionFactory.FileNotFound(
64-
Execute.OnNetFramework(
64+
_fileSystem.Execute.OnNetFramework(
6565
() => Location.FriendlyName,
6666
() => Location.FullPath));
6767
}
@@ -115,7 +115,7 @@ public IFileInfo CopyTo(string destFileName, bool overwrite)
115115
/// <inheritdoc cref="IFileInfo.Create()" />
116116
public FileSystemStream Create()
117117
{
118-
Execute.NotOnNetFramework(Refresh);
118+
_fileSystem.Execute.NotOnNetFramework(Refresh);
119119
return _fileSystem.File.Create(FullName);
120120
}
121121

@@ -165,7 +165,7 @@ public void MoveTo(string destFileName, bool overwrite)
165165
/// <inheritdoc cref="IFileInfo.Open(FileMode)" />
166166
public FileSystemStream Open(FileMode mode)
167167
{
168-
Execute.OnNetFrameworkIf(mode == FileMode.Append,
168+
_fileSystem.Execute.OnNetFrameworkIf(mode == FileMode.Append,
169169
() => throw ExceptionFactory.AppendAccessOnlyInWriteOnlyMode());
170170

171171
return new FileStreamMock(
@@ -233,7 +233,7 @@ public IFileInfo Replace(string destinationFileName,
233233
() => { },
234234
() =>
235235
{
236-
if (Execute.IsWindows)
236+
if (_fileSystem.Execute.IsWindows)
237237
{
238238
throw ExceptionFactory.FileNotFound(FullName);
239239
}
@@ -247,7 +247,7 @@ public IFileInfo Replace(string destinationFileName,
247247
() => { },
248248
() =>
249249
{
250-
if (Execute.IsWindows)
250+
if (_fileSystem.Execute.IsWindows)
251251
{
252252
throw ExceptionFactory.DirectoryNotFound(FullName);
253253
}
@@ -260,14 +260,14 @@ public IFileInfo Replace(string destinationFileName,
260260
() => { },
261261
() =>
262262
{
263-
if (Execute.IsWindows)
263+
if (_fileSystem.Execute.IsWindows)
264264
{
265265
throw ExceptionFactory.FileNotFound(FullName);
266266
}
267267

268268
throw ExceptionFactory.DirectoryNotFound(FullName);
269269
}),
270-
!Execute.IsWindows)
270+
!_fileSystem.Execute.IsWindows)
271271
?? throw ExceptionFactory.FileNotFound(FullName);
272272
return _fileSystem.FileInfo.New(location.FullPath);
273273
}

0 commit comments

Comments
 (0)