Skip to content

Commit fc4df09

Browse files
authored
refactor: avoid unnecessary allocations from using callbacks in ExecuteExtensions (#601)
Avoid allocations from anonymous functions from using the `ExecuteExtensions`. Add explicit if-conditions instead.
1 parent 3a18916 commit fc4df09

20 files changed

+482
-758
lines changed

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ public IDirectoryInfo CreateTempSubdirectory(string? prefix = null)
8686
_fileSystem.Execute.Path.GetTempPath(),
8787
(prefix ?? "") + _fileSystem.Execute.Path.GetFileNameWithoutExtension(
8888
_fileSystem.Execute.Path.GetRandomFileName()));
89-
_fileSystem.Execute.OnMac(() => localBasePath = "/private" + localBasePath);
89+
if (_fileSystem.Execute.IsMac)
90+
{
91+
localBasePath = "/private" + localBasePath;
92+
}
9093
basePath = localBasePath;
9194
} while (_fileSystem.Directory.Exists(basePath));
9295

@@ -689,39 +692,31 @@ private IDisposable RegisterMethod<T1, T2, T3>(string name, T1 parameter1, T2 pa
689692
private static void ThrowMissingFileCreatedTimeException(MockFileSystem fileSystem, string path)
690693
{
691694
#if NET7_0_OR_GREATER
692-
fileSystem.Execute.OnMac(
693-
() =>
694-
throw ExceptionFactory.DirectoryNotFound(
695-
fileSystem.Execute.Path.GetFullPath(path)),
696-
() =>
697-
throw ExceptionFactory.FileNotFound(
698-
fileSystem.Execute.Path.GetFullPath(path)));
695+
if (!fileSystem.Execute.IsMac)
699696
#else
700-
fileSystem.Execute.OnWindows(
701-
() =>
702-
throw ExceptionFactory.FileNotFound(
703-
fileSystem.Execute.Path.GetFullPath(path)),
704-
() =>
705-
throw ExceptionFactory.DirectoryNotFound(
706-
fileSystem.Execute.Path.GetFullPath(path)));
697+
if (fileSystem.Execute.IsWindows)
707698
#endif
699+
{
700+
throw ExceptionFactory.FileNotFound(
701+
fileSystem.Execute.Path.GetFullPath(path));
702+
}
703+
704+
throw ExceptionFactory.DirectoryNotFound(
705+
fileSystem.Execute.Path.GetFullPath(path));
708706
}
709707

710708
private static void ThrowMissingFileLastAccessOrLastWriteTimeException(
711709
MockFileSystem fileSystem,
712710
string path)
713711
{
714-
#if NET7_0_OR_GREATER
712+
#if !NET7_0_OR_GREATER
713+
if (!fileSystem.Execute.IsWindows)
714+
{
715+
throw ExceptionFactory.DirectoryNotFound(
716+
fileSystem.Execute.Path.GetFullPath(path));
717+
}
718+
#endif
715719
throw ExceptionFactory.FileNotFound(
716720
fileSystem.Execute.Path.GetFullPath(path));
717-
#else
718-
fileSystem.Execute.OnWindows(
719-
() =>
720-
throw ExceptionFactory.FileNotFound(
721-
fileSystem.Execute.Path.GetFullPath(path)),
722-
() =>
723-
throw ExceptionFactory.DirectoryNotFound(
724-
fileSystem.Execute.Path.GetFullPath(path)));
725-
#endif
726721
}
727722
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,10 @@ public string VolumeLabel
185185

186186
// ReSharper disable once ConstantNullCoalescingCondition
187187
_volumeLabel = value ?? _volumeLabel;
188-
_fileSystem.Execute.NotOnWindows(
189-
() => throw ExceptionFactory.OperationNotSupportedOnThisPlatform());
188+
if (!_fileSystem.Execute.IsWindows)
189+
{
190+
throw ExceptionFactory.OperationNotSupportedOnThisPlatform();
191+
}
190192
}
191193
}
192194

@@ -286,12 +288,13 @@ private static string ValidateDriveLetter(string driveName,
286288

287289
if (fileSystem.Execute.Path.IsPathRooted(driveName))
288290
{
289-
return fileSystem.Execute.OnWindows(() =>
290-
{
291-
string rootedPath = fileSystem.Execute.Path.GetPathRoot(driveName)!;
292-
return $"{rootedPath.TrimEnd('\\')}\\";
293-
},
294-
() => fileSystem.Execute.Path.GetPathRoot(driveName)!);
291+
if (fileSystem.Execute.IsWindows)
292+
{
293+
string rootedPath = fileSystem.Execute.Path.GetPathRoot(driveName)!;
294+
return $"{rootedPath.TrimEnd('\\')}\\";
295+
}
296+
297+
return fileSystem.Execute.Path.GetPathRoot(driveName)!;
295298
}
296299

297300
throw ExceptionFactory.InvalidDriveName();

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public long Length
9797
Container.Type != FileSystemTypes.File)
9898
{
9999
throw ExceptionFactory.FileNotFound(
100-
_fileSystem.Execute.OnNetFramework(
101-
() => Location.FriendlyName,
102-
() => Location.FullPath));
100+
_fileSystem.Execute.IsNetFramework
101+
? Location.FriendlyName
102+
: Location.FullPath);
103103
}
104104

105105
return Container.GetBytes().Length;
@@ -165,7 +165,11 @@ public FileSystemStream Create()
165165
{
166166
using IDisposable registration = RegisterMethod(nameof(Create));
167167

168-
_fileSystem.Execute.NotOnNetFramework(Refresh);
168+
if (!_fileSystem.Execute.IsNetFramework)
169+
{
170+
Refresh();
171+
}
172+
169173
return _fileSystem.File.Create(FullName);
170174
}
171175

@@ -234,8 +238,10 @@ public FileSystemStream Open(FileMode mode)
234238
using IDisposable registration = RegisterMethod(nameof(Open),
235239
mode);
236240

237-
_fileSystem.Execute.OnNetFrameworkIf(mode == FileMode.Append,
238-
() => throw ExceptionFactory.AppendAccessOnlyInWriteOnlyMode());
241+
if (_fileSystem.Execute.IsNetFramework && mode == FileMode.Append)
242+
{
243+
throw ExceptionFactory.AppendAccessOnlyInWriteOnlyMode();
244+
}
239245

240246
return new FileStreamMock(
241247
_fileSystem,

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

Lines changed: 84 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#if FEATURE_FILESYSTEM_ASYNC
1414
using System.Threading;
1515
using System.Threading.Tasks;
16+
using System.ComponentModel;
1617
#endif
1718

1819
// ReSharper disable PossibleMultipleEnumeration
@@ -179,12 +180,9 @@ public void Copy(string sourceFileName, string destFileName)
179180
.New(sourceFileName)
180181
.CopyTo(destFileName);
181182
}
182-
catch (UnauthorizedAccessException)
183+
catch (UnauthorizedAccessException) when (_fileSystem.Execute.IsNetFramework)
183184
{
184-
_fileSystem.Execute.OnNetFramework(()
185-
=> throw ExceptionFactory.AccessToPathDenied(sourceFileName));
186-
187-
throw;
185+
throw ExceptionFactory.AccessToPathDenied(sourceFileName);
188186
}
189187
}
190188

@@ -194,29 +192,28 @@ public void Copy(string sourceFileName, string destFileName, bool overwrite)
194192
using IDisposable registration = RegisterMethod(nameof(Copy),
195193
sourceFileName, destFileName, overwrite);
196194

197-
_fileSystem.Execute.OnNetFramework(
198-
() =>
199-
{
200-
try
201-
{
202-
_fileSystem.FileInfo.New(sourceFileName
203-
.EnsureValidFormat(_fileSystem, nameof(sourceFileName)))
204-
.CopyTo(destFileName
205-
.EnsureValidFormat(_fileSystem, nameof(destFileName)),
206-
overwrite);
207-
}
208-
catch (UnauthorizedAccessException)
209-
{
210-
throw ExceptionFactory.AccessToPathDenied(sourceFileName);
211-
}
212-
},
213-
() =>
195+
if (_fileSystem.Execute.IsNetFramework)
196+
{
197+
try
214198
{
215199
_fileSystem.FileInfo.New(sourceFileName
216200
.EnsureValidFormat(_fileSystem, nameof(sourceFileName)))
217201
.CopyTo(destFileName
218-
.EnsureValidFormat(_fileSystem, nameof(destFileName)), overwrite);
219-
});
202+
.EnsureValidFormat(_fileSystem, nameof(destFileName)),
203+
overwrite);
204+
}
205+
catch (UnauthorizedAccessException)
206+
{
207+
throw ExceptionFactory.AccessToPathDenied(sourceFileName);
208+
}
209+
}
210+
else
211+
{
212+
_fileSystem.FileInfo.New(sourceFileName
213+
.EnsureValidFormat(_fileSystem, nameof(sourceFileName)))
214+
.CopyTo(destFileName
215+
.EnsureValidFormat(_fileSystem, nameof(destFileName)), overwrite);
216+
}
220217
}
221218

222219
/// <inheritdoc cref="IFile.Create(string)" />
@@ -517,15 +514,18 @@ public DateTime GetLastWriteTimeUtc(SafeFileHandle fileHandle)
517514
public UnixFileMode GetUnixFileMode(string path)
518515
{
519516
using IDisposable registration = RegisterMethod(nameof(GetUnixFileMode),
520-
path);
517+
path);
518+
519+
if (_fileSystem.Execute.IsWindows)
520+
{
521+
throw ExceptionFactory.UnixFileModeNotSupportedOnThisPlatform();
522+
}
521523

522-
return _fileSystem.Execute.OnWindows(
523-
() => throw ExceptionFactory.UnixFileModeNotSupportedOnThisPlatform(),
524-
() => _fileSystem.Storage.GetContainer(
525-
_fileSystem.Storage.GetLocation(
526-
path.EnsureValidFormat(_fileSystem))
527-
.ThrowExceptionIfNotFound(_fileSystem))
528-
.UnixFileMode);
524+
return _fileSystem.Storage.GetContainer(
525+
_fileSystem.Storage.GetLocation(
526+
path.EnsureValidFormat(_fileSystem))
527+
.ThrowExceptionIfNotFound(_fileSystem))
528+
.UnixFileMode;
529529
}
530530
#endif
531531

@@ -537,10 +537,12 @@ public UnixFileMode GetUnixFileMode(SafeFileHandle fileHandle)
537537
using IDisposable registration = RegisterMethod(nameof(GetUnixFileMode),
538538
fileHandle);
539539

540-
return _fileSystem.Execute.OnWindows(
541-
() => throw ExceptionFactory.UnixFileModeNotSupportedOnThisPlatform(),
542-
() => GetContainerFromSafeFileHandle(fileHandle)
543-
.UnixFileMode);
540+
if (_fileSystem.Execute.IsWindows)
541+
{
542+
throw ExceptionFactory.UnixFileModeNotSupportedOnThisPlatform();
543+
}
544+
545+
return GetContainerFromSafeFileHandle(fileHandle).UnixFileMode;
544546
}
545547
#endif
546548

@@ -683,8 +685,10 @@ public byte[] ReadAllBytes(string path)
683685
FileAccess.Read,
684686
FileStreamFactoryMock.DefaultShare))
685687
{
686-
_fileSystem.Execute.NotOnWindows(() =>
687-
container.AdjustTimes(TimeAdjustments.LastAccessTime));
688+
if (!_fileSystem.Execute.IsWindows)
689+
{
690+
container.AdjustTimes(TimeAdjustments.LastAccessTime);
691+
}
688692

689693
return container.GetBytes().ToArray();
690694
}
@@ -769,8 +773,10 @@ public string ReadAllText(string path, Encoding encoding)
769773
FileAccess.Read,
770774
FileStreamFactoryMock.DefaultShare))
771775
{
772-
_fileSystem.Execute.NotOnWindows(() =>
773-
container.AdjustTimes(TimeAdjustments.LastAccessTime));
776+
if (!_fileSystem.Execute.IsWindows)
777+
{
778+
container.AdjustTimes(TimeAdjustments.LastAccessTime);
779+
}
774780

775781
using (MemoryStream ms = new(container.GetBytes()))
776782
using (StreamReader sr = new(ms, encoding))
@@ -895,10 +901,16 @@ public void Replace(string sourceFileName,
895901
IStorageLocation location =
896902
_fileSystem.Storage.GetLocation(linkPath
897903
.EnsureValidFormat(_fileSystem, nameof(linkPath)));
898-
_fileSystem.Execute.OnWindows(
899-
() => location.ThrowExceptionIfNotFound(_fileSystem),
900-
() => location.ThrowExceptionIfNotFound(_fileSystem,
901-
onDirectoryNotFound: ExceptionFactory.FileNotFound));
904+
if (_fileSystem.Execute.IsWindows)
905+
{
906+
location.ThrowExceptionIfNotFound(_fileSystem);
907+
}
908+
else
909+
{
910+
location.ThrowExceptionIfNotFound(_fileSystem,
911+
onDirectoryNotFound: ExceptionFactory.FileNotFound);
912+
}
913+
902914
try
903915
{
904916
IStorageLocation? targetLocation =
@@ -1087,8 +1099,10 @@ public void SetUnixFileMode(string path, UnixFileMode mode)
10871099
using IDisposable registration = RegisterMethod(nameof(SetUnixFileMode),
10881100
path, mode);
10891101

1090-
_fileSystem.Execute.OnWindows(
1091-
() => throw ExceptionFactory.UnixFileModeNotSupportedOnThisPlatform());
1102+
if (_fileSystem.Execute.IsWindows)
1103+
{
1104+
throw ExceptionFactory.UnixFileModeNotSupportedOnThisPlatform();
1105+
}
10921106

10931107
IStorageContainer container = GetContainerFromPath(path);
10941108
container.UnixFileMode = mode;
@@ -1103,8 +1117,10 @@ public void SetUnixFileMode(SafeFileHandle fileHandle, UnixFileMode mode)
11031117
using IDisposable registration = RegisterMethod(nameof(SetUnixFileMode),
11041118
fileHandle, mode);
11051119

1106-
_fileSystem.Execute.OnWindows(
1107-
() => throw ExceptionFactory.UnixFileModeNotSupportedOnThisPlatform());
1120+
if (_fileSystem.Execute.IsWindows)
1121+
{
1122+
throw ExceptionFactory.UnixFileModeNotSupportedOnThisPlatform();
1123+
}
11081124

11091125
IStorageContainer container = GetContainerFromSafeFileHandle(fileHandle);
11101126
container.UnixFileMode = mode;
@@ -1134,9 +1150,11 @@ public void WriteAllBytes(string path, byte[] bytes)
11341150
throw ExceptionFactory.AccessToPathDenied(path);
11351151
}
11361152

1137-
_fileSystem.Execute.OnWindowsIf(
1138-
container.Attributes.HasFlag(FileAttributes.Hidden),
1139-
() => throw ExceptionFactory.AccessToPathDenied());
1153+
if (_fileSystem.Execute.IsWindows && container.Attributes.HasFlag(FileAttributes.Hidden))
1154+
{
1155+
throw ExceptionFactory.AccessToPathDenied();
1156+
}
1157+
11401158
using (container.RequestAccess(
11411159
FileAccess.Write,
11421160
FileStreamFactoryMock.DefaultShare))
@@ -1267,9 +1285,12 @@ public void WriteAllText(string path, string? contents, Encoding encoding)
12671285

12681286
if (contents != null)
12691287
{
1270-
_fileSystem.Execute.OnWindowsIf(
1271-
container.Attributes.HasFlag(FileAttributes.Hidden),
1272-
() => throw ExceptionFactory.AccessToPathDenied());
1288+
1289+
if (_fileSystem.Execute.IsWindows && container.Attributes.HasFlag(FileAttributes.Hidden))
1290+
{
1291+
throw ExceptionFactory.AccessToPathDenied();
1292+
}
1293+
12731294
using (container.RequestAccess(
12741295
FileAccess.Write,
12751296
FileStreamFactoryMock.DefaultShare))
@@ -1326,10 +1347,15 @@ private IStorageContainer GetContainerFromPath(string path,
13261347
IStorageLocation location = _fileSystem.Storage.GetLocation(path);
13271348
if (exceptionMode == ExceptionMode.FileNotFoundExceptionOnLinuxAndMac)
13281349
{
1329-
_fileSystem.Execute.OnWindows(
1330-
() => location.ThrowExceptionIfNotFound(_fileSystem),
1331-
() => location.ThrowExceptionIfNotFound(_fileSystem,
1332-
onDirectoryNotFound: ExceptionFactory.FileNotFound));
1350+
if (_fileSystem.Execute.IsWindows)
1351+
{
1352+
location.ThrowExceptionIfNotFound(_fileSystem);
1353+
}
1354+
else
1355+
{
1356+
location.ThrowExceptionIfNotFound(_fileSystem,
1357+
onDirectoryNotFound: ExceptionFactory.FileNotFound);
1358+
}
13331359
}
13341360

13351361
if (exceptionMode == ExceptionMode.Default)

0 commit comments

Comments
 (0)