Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make Execute non-static #462

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public IDirectoryInfo? Parent

/// <inheritdoc cref="IDirectoryInfo.Root" />
public IDirectoryInfo Root
=> New(_fileSystem.Storage.GetLocation(string.Empty.PrefixRoot()),
=> New(_fileSystem.Storage.GetLocation(string.Empty.PrefixRoot(_fileSystem)),
_fileSystem);

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

if (Container.Type != FileSystemTypes.Directory)
{
throw ExceptionFactory.CannotCreateFileAsAlreadyExists(FullName);
throw ExceptionFactory.CannotCreateFileAsAlreadyExists(_fileSystem.Execute, FullName);
}

ResetCache(!Execute.IsNetFramework);
ResetCache(!_fileSystem.Execute.IsNetFramework);
}

/// <inheritdoc cref="IDirectoryInfo.CreateSubdirectory(string)" />
Expand All @@ -61,7 +61,7 @@ public IDirectoryInfo CreateSubdirectory(string path)
_fileSystem.Storage.GetLocation(
_fileSystem.Path.Combine(FullName, path
.EnsureValidFormat(_fileSystem, nameof(path),
Execute.IsWindows && !Execute.IsNetFramework))),
_fileSystem.Execute.IsWindows && !_fileSystem.Execute.IsNetFramework))),
_fileSystem);
directory.Create();
return directory;
Expand All @@ -75,7 +75,7 @@ public override void Delete()
throw ExceptionFactory.DirectoryNotFound(Location.FullPath);
}

ResetCache(!Execute.IsNetFramework);
ResetCache(!_fileSystem.Execute.IsNetFramework);
}

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

ResetCache(!Execute.IsNetFramework);
ResetCache(!_fileSystem.Execute.IsNetFramework);
}

/// <inheritdoc cref="IDirectoryInfo.EnumerateDirectories()" />
Expand Down Expand Up @@ -276,7 +276,7 @@ private IEnumerable<IStorageLocation> EnumerateInternal(
EnumerationOptions enumerationOptions)
{
StorageExtensions.AdjustedLocation adjustedLocation = _fileSystem.Storage
.AdjustLocationFromSearchPattern(
.AdjustLocationFromSearchPattern(_fileSystem,
path.EnsureValidFormat(_fileSystem),
searchPattern);
return _fileSystem.Storage.EnumerateLocations(
Expand Down
40 changes: 20 additions & 20 deletions Source/Testably.Abstractions.Testing/FileSystem/DirectoryMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public IDirectoryInfo CreateTempSubdirectory(string? prefix = null)
_fileSystem.Path.GetTempPath(),
(prefix ?? "") + _fileSystem.Path.GetFileNameWithoutExtension(
_fileSystem.Path.GetRandomFileName()));
Execute.OnMac(() => localBasePath = "/private" + localBasePath);
_fileSystem.Execute.OnMac(() => localBasePath = "/private" + localBasePath);
basePath = localBasePath;
} while (_fileSystem.Directory.Exists(basePath));

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

/// <inheritdoc cref="IDirectory.Delete(string, bool)" />
public void Delete(string path, bool recursive)
=> _fileSystem.DirectoryInfo
.New(path.EnsureValidFormat(FileSystem))
.New(path.EnsureValidFormat(_fileSystem))
.Delete(recursive);

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

/// <inheritdoc cref="IDirectory.GetCreationTimeUtc(string)" />
public DateTime GetCreationTimeUtc(string path)
=> _fileSystem.Storage.GetContainer(
_fileSystem.Storage.GetLocation(
path.EnsureValidFormat(FileSystem)))
path.EnsureValidFormat(_fileSystem)))
.CreationTime.Get(DateTimeKind.Utc);

/// <inheritdoc cref="IDirectory.GetCurrentDirectory()" />
Expand Down Expand Up @@ -290,28 +290,28 @@ public string[] GetFileSystemEntries(string path,
public DateTime GetLastAccessTime(string path)
=> _fileSystem.Storage.GetContainer(
_fileSystem.Storage.GetLocation(
path.EnsureValidFormat(FileSystem)))
path.EnsureValidFormat(_fileSystem)))
.LastAccessTime.Get(DateTimeKind.Local);

/// <inheritdoc cref="IDirectory.GetLastAccessTimeUtc(string)" />
public DateTime GetLastAccessTimeUtc(string path)
=> _fileSystem.Storage.GetContainer(
_fileSystem.Storage.GetLocation(
path.EnsureValidFormat(FileSystem)))
path.EnsureValidFormat(_fileSystem)))
.LastAccessTime.Get(DateTimeKind.Utc);

/// <inheritdoc cref="IDirectory.GetLastWriteTime(string)" />
public DateTime GetLastWriteTime(string path)
=> _fileSystem.Storage.GetContainer(
_fileSystem.Storage.GetLocation(
path.EnsureValidFormat(FileSystem)))
path.EnsureValidFormat(_fileSystem)))
.LastWriteTime.Get(DateTimeKind.Local);

/// <inheritdoc cref="IDirectory.GetLastWriteTimeUtc(string)" />
public DateTime GetLastWriteTimeUtc(string path)
=> _fileSystem.Storage.GetContainer(
_fileSystem.Storage.GetLocation(
path.EnsureValidFormat(FileSystem)))
path.EnsureValidFormat(_fileSystem)))
.LastWriteTime.Get(DateTimeKind.Utc);

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

private IDirectoryInfo LoadDirectoryInfoOrThrowNotFoundException(
string path, Action<IFileSystem, string> onMissingCallback)
string path, Action<MockFileSystem, string> onMissingCallback)
{
IDirectoryInfo directoryInfo =
_fileSystem.DirectoryInfo.New(path.EnsureValidFormat(FileSystem));
_fileSystem.DirectoryInfo.New(path.EnsureValidFormat(_fileSystem));
if (!directoryInfo.Exists)
{
onMissingCallback.Invoke(FileSystem, path);
onMissingCallback.Invoke(_fileSystem, path);
}

return directoryInfo;
}

private static void ThrowMissingFileCreatedTimeException(IFileSystem fileSystem, string path)
private static void ThrowMissingFileCreatedTimeException(MockFileSystem fileSystem, string path)
{
#if NET7_0_OR_GREATER
Execute.OnMac(
fileSystem.Execute.OnMac(
() =>
throw ExceptionFactory.DirectoryNotFound(
fileSystem.Path.GetFullPath(path)),
() =>
throw ExceptionFactory.FileNotFound(
fileSystem.Path.GetFullPath(path)));
#else
Execute.OnWindows(
fileSystem.Execute.OnWindows(
() =>
throw ExceptionFactory.FileNotFound(
fileSystem.Path.GetFullPath(path)),
Expand All @@ -435,14 +435,14 @@ private static void ThrowMissingFileCreatedTimeException(IFileSystem fileSystem,
#endif
}

private static void ThrowMissingFileLastAccessOrLastWriteTimeException(IFileSystem fileSystem,
private static void ThrowMissingFileLastAccessOrLastWriteTimeException(MockFileSystem fileSystem,
string path)
{
#if NET7_0_OR_GREATER
throw ExceptionFactory.FileNotFound(
fileSystem.Path.GetFullPath(path));
#else
Execute.OnWindows(
fileSystem.Execute.OnWindows(
() =>
throw ExceptionFactory.FileNotFound(
fileSystem.Path.GetFullPath(path)),
Expand All @@ -458,8 +458,8 @@ private IEnumerable<string> EnumerateInternal(FileSystemTypes fileSystemTypes,
EnumerationOptions enumerationOptions)
{
StorageExtensions.AdjustedLocation adjustedLocation = _fileSystem.Storage
.AdjustLocationFromSearchPattern(
path.EnsureValidFormat(FileSystem),
.AdjustLocationFromSearchPattern(_fileSystem,
path.EnsureValidFormat(_fileSystem),
searchPattern);
return _fileSystem.Storage.EnumerateLocations(
adjustedLocation.Location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private DriveInfoMock(string driveName, MockFileSystem fileSystem)
{
_fileSystem = fileSystem;

if (driveName.IsUncPath())
if (driveName.IsUncPath(_fileSystem))
{
IsUncPath = true;
driveName = PathHelper.UncPrefix +
Expand Down Expand Up @@ -102,7 +102,7 @@ public string VolumeLabel
set
{
_volumeLabel = value ?? _volumeLabel;
Execute.NotOnWindows(
_fileSystem.Execute.NotOnWindows(
() => throw ExceptionFactory.OperationNotSupportedOnThisPlatform());
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ private string GetTopmostParentDirectory(string path)
}

private static string ValidateDriveLetter(string driveName,
IFileSystem fileSystem)
MockFileSystem fileSystem)
{
if (driveName.Length == 1 &&
char.IsLetter(driveName, 0))
Expand All @@ -186,7 +186,7 @@ private static string ValidateDriveLetter(string driveName,

if (fileSystem.Path.IsPathRooted(driveName))
{
return Execute.OnWindows(() =>
return fileSystem.Execute.OnWindows(() =>
{
string rootedPath = fileSystem.Path.GetPathRoot(driveName)!;
return $"{rootedPath.TrimEnd('\\')}\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public IFileInfo New(string fileName)
throw new ArgumentNullException(nameof(fileName));
}

if (fileName.IsEffectivelyEmpty())
if (fileName.IsEffectivelyEmpty(_fileSystem))
{
throw ExceptionFactory.PathIsEmpty("path");
}
Expand Down
14 changes: 7 additions & 7 deletions Source/Testably.Abstractions.Testing/FileSystem/FileInfoMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public long Length
Container.Type != FileSystemTypes.File)
{
throw ExceptionFactory.FileNotFound(
Execute.OnNetFramework(
_fileSystem.Execute.OnNetFramework(
() => Location.FriendlyName,
() => Location.FullPath));
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public IFileInfo CopyTo(string destFileName, bool overwrite)
/// <inheritdoc cref="IFileInfo.Create()" />
public FileSystemStream Create()
{
Execute.NotOnNetFramework(Refresh);
_fileSystem.Execute.NotOnNetFramework(Refresh);
return _fileSystem.File.Create(FullName);
}

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

return new FileStreamMock(
Expand Down Expand Up @@ -233,7 +233,7 @@ public IFileInfo Replace(string destinationFileName,
() => { },
() =>
{
if (Execute.IsWindows)
if (_fileSystem.Execute.IsWindows)
{
throw ExceptionFactory.FileNotFound(FullName);
}
Expand All @@ -247,7 +247,7 @@ public IFileInfo Replace(string destinationFileName,
() => { },
() =>
{
if (Execute.IsWindows)
if (_fileSystem.Execute.IsWindows)
{
throw ExceptionFactory.DirectoryNotFound(FullName);
}
Expand All @@ -260,14 +260,14 @@ public IFileInfo Replace(string destinationFileName,
() => { },
() =>
{
if (Execute.IsWindows)
if (_fileSystem.Execute.IsWindows)
{
throw ExceptionFactory.FileNotFound(FullName);
}

throw ExceptionFactory.DirectoryNotFound(FullName);
}),
!Execute.IsWindows)
!_fileSystem.Execute.IsWindows)
?? throw ExceptionFactory.FileNotFound(FullName);
return _fileSystem.FileInfo.New(location.FullPath);
}
Expand Down
Loading
Loading