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

feat: avoid arbitrary statistic calls #502

Merged
merged 5 commits into from
Mar 17, 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
11 changes: 7 additions & 4 deletions Source/Testably.Abstractions.Interface/Helpers/PathSystemBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Abstractions;
#if !NETSTANDARD2_0
using System;
#endif

namespace Testably.Abstractions.Helpers;

Expand All @@ -12,6 +10,11 @@ namespace Testably.Abstractions.Helpers;
/// <para />
/// Implements <seealso cref="IPath" />
/// </summary>
#if NETSTANDARD2_0
[Obsolete]
#else
[Obsolete("Will be removed in a future version!")]
#endif
public abstract class PathSystemBase : IPath
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public IDirectoryInfo CreateSubdirectory(string path)

DirectoryInfoMock directory = New(
_fileSystem.Storage.GetLocation(
_fileSystem.Path.Combine(FullName, path
_fileSystem.Execute.Path.Combine(FullName, path
.EnsureValidFormat(_fileSystem, nameof(path),
_fileSystem.Execute.IsWindows && !_fileSystem.Execute.IsNetFramework))),
_fileSystem);
Expand Down
28 changes: 14 additions & 14 deletions Source/Testably.Abstractions.Testing/FileSystem/DirectoryMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public IDirectoryInfo CreateTempSubdirectory(string? prefix = null)

do
{
string localBasePath = _fileSystem.Path.Combine(
_fileSystem.Path.GetTempPath(),
(prefix ?? "") + _fileSystem.Path.GetFileNameWithoutExtension(
_fileSystem.Path.GetRandomFileName()));
string localBasePath = _fileSystem.Execute.Path.Combine(
_fileSystem.Execute.Path.GetTempPath(),
(prefix ?? "") + _fileSystem.Execute.Path.GetFileNameWithoutExtension(
_fileSystem.Execute.Path.GetRandomFileName()));
_fileSystem.Execute.OnMac(() => localBasePath = "/private" + localBasePath);
basePath = localBasePath;
} while (_fileSystem.Directory.Exists(basePath));
Expand Down Expand Up @@ -361,8 +361,8 @@ public string GetDirectoryRoot(string path)
using IDisposable registration = RegisterMethod(nameof(GetDirectoryRoot),
path);

return _fileSystem.Path.GetPathRoot(
_fileSystem.Path.GetFullPath(path)) ??
return _fileSystem.Execute.Path.GetPathRoot(
_fileSystem.Execute.Path.GetFullPath(path)) ??
throw ExceptionFactory.PathIsEmpty(nameof(path));
}

Expand Down Expand Up @@ -584,7 +584,7 @@ public void SetCurrentDirectory(string path)
if (!directoryInfo.Exists)
{
throw ExceptionFactory.DirectoryNotFound(
FileSystem.Path.GetFullPath(path));
_fileSystem.Execute.Path.GetFullPath(path));
}

_fileSystem.Storage.CurrentDirectory = directoryInfo.FullName;
Expand Down Expand Up @@ -692,18 +692,18 @@ private static void ThrowMissingFileCreatedTimeException(MockFileSystem fileSyst
fileSystem.Execute.OnMac(
() =>
throw ExceptionFactory.DirectoryNotFound(
fileSystem.Path.GetFullPath(path)),
fileSystem.Execute.Path.GetFullPath(path)),
() =>
throw ExceptionFactory.FileNotFound(
fileSystem.Path.GetFullPath(path)));
fileSystem.Execute.Path.GetFullPath(path)));
#else
fileSystem.Execute.OnWindows(
() =>
throw ExceptionFactory.FileNotFound(
fileSystem.Path.GetFullPath(path)),
fileSystem.Execute.Path.GetFullPath(path)),
() =>
throw ExceptionFactory.DirectoryNotFound(
fileSystem.Path.GetFullPath(path)));
fileSystem.Execute.Path.GetFullPath(path)));
#endif
}

Expand All @@ -713,15 +713,15 @@ private static void ThrowMissingFileLastAccessOrLastWriteTimeException(
{
#if NET7_0_OR_GREATER
throw ExceptionFactory.FileNotFound(
fileSystem.Path.GetFullPath(path));
fileSystem.Execute.Path.GetFullPath(path));
#else
fileSystem.Execute.OnWindows(
() =>
throw ExceptionFactory.FileNotFound(
fileSystem.Path.GetFullPath(path)),
fileSystem.Execute.Path.GetFullPath(path)),
() =>
throw ExceptionFactory.DirectoryNotFound(
fileSystem.Path.GetFullPath(path)));
fileSystem.Execute.Path.GetFullPath(path)));
#endif
}
}
10 changes: 5 additions & 5 deletions Source/Testably.Abstractions.Testing/FileSystem/DriveInfoMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private DriveInfoMock(string driveName, MockFileSystem fileSystem)
if (driveName.IsUncPath(_fileSystem))
{
IsUncPath = true;
driveName = new string(fileSystem.Path.DirectorySeparatorChar, 2) +
driveName = new string(fileSystem.Execute.Path.DirectorySeparatorChar, 2) +
GetTopmostParentDirectory(driveName.Substring(2));
}
else
Expand Down Expand Up @@ -259,7 +259,7 @@ private string GetTopmostParentDirectory(string path)
{
while (true)
{
string? child = FileSystem.Path.GetDirectoryName(path);
string? child = _fileSystem.Execute.Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(child))
{
break;
Expand All @@ -283,14 +283,14 @@ private static string ValidateDriveLetter(string driveName,
return $"{driveName.ToUpperInvariant()}:\\";
}

if (fileSystem.Path.IsPathRooted(driveName))
if (fileSystem.Execute.Path.IsPathRooted(driveName))
{
return fileSystem.Execute.OnWindows(() =>
{
string rootedPath = fileSystem.Path.GetPathRoot(driveName)!;
string rootedPath = fileSystem.Execute.Path.GetPathRoot(driveName)!;
return $"{rootedPath.TrimEnd('\\')}\\";
},
() => fileSystem.Path.GetPathRoot(driveName)!);
() => fileSystem.Execute.Path.GetPathRoot(driveName)!);
}

throw ExceptionFactory.InvalidDriveName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public override string Name
{
using IDisposable registration = RegisterProperty(nameof(Name), PropertyAccess.Get);

if (Location.FullPath.EndsWith(FileSystem.Path.DirectorySeparatorChar))
if (Location.FullPath.EndsWith(_fileSystem.Execute.Path.DirectorySeparatorChar))
{
return string.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private FileStreamMock(MemoryStream stream,
FileOptions options)
: base(
stream,
path == null ? "" : fileSystem.Path.GetFullPath(path),
path == null ? "" : fileSystem.Execute.Path.GetFullPath(path),
(options & FileOptions.Asynchronous) != 0)
{
ThrowIfInvalidModeAccess(mode, access);
Expand All @@ -210,7 +210,7 @@ private FileStreamMock(MemoryStream stream,
_mode.Equals(FileMode.Truncate))
{
throw ExceptionFactory.FileNotFound(
_fileSystem.Path.GetFullPath(base.Name));
_fileSystem.Execute.Path.GetFullPath(base.Name));
}

file = _fileSystem.Storage.GetOrCreateContainer(_location,
Expand All @@ -222,15 +222,15 @@ private FileStreamMock(MemoryStream stream,
_fileSystem.Execute.OnWindows(
() =>
throw ExceptionFactory.AccessToPathDenied(
_fileSystem.Path.GetFullPath(base.Name)),
_fileSystem.Execute.Path.GetFullPath(base.Name)),
() =>
throw ExceptionFactory.FileAlreadyExists(
_fileSystem.Path.GetFullPath(base.Name), 17));
_fileSystem.Execute.Path.GetFullPath(base.Name), 17));
}
else if (_mode.Equals(FileMode.CreateNew))
{
throw ExceptionFactory.FileAlreadyExists(
_fileSystem.Path.GetFullPath(Name),
_fileSystem.Execute.Path.GetFullPath(Name),
_fileSystem.Execute.IsWindows ? -2147024816 : 17);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public string Extension
return ".";
}

return _fileSystem.Path.GetExtension(Location.FullPath);
return _fileSystem.Execute.Path.GetExtension(Location.FullPath);
}
}

Expand Down Expand Up @@ -282,11 +282,11 @@ public virtual string Name
{
using IDisposable registration = RegisterProperty(nameof(Name), PropertyAccess.Get);

return _fileSystem.Path.GetPathRoot(Location.FullPath) == Location.FullPath
return _fileSystem.Execute.Path.GetPathRoot(Location.FullPath) == Location.FullPath
? Location.FullPath
: _fileSystem.Path.GetFileName(Location.FullPath.TrimEnd(
_fileSystem.Path.DirectorySeparatorChar,
_fileSystem.Path.AltDirectorySeparatorChar));
: _fileSystem.Execute.Path.GetFileName(Location.FullPath.TrimEnd(
_fileSystem.Execute.Path.DirectorySeparatorChar,
_fileSystem.Execute.Path.AltDirectorySeparatorChar));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ protected override void Dispose(bool disposing)

private bool MatchesFilter(ChangeDescription changeDescription)
{
string fullPath = _fileSystem.Path.GetFullPath(Path);
string fullPath = _fileSystem.Execute.Path.GetFullPath(Path);
if (IncludeSubdirectories)
{
if (!changeDescription.Path.StartsWith(fullPath))
{
return false;
}
}
else if (FileSystem.Path.GetDirectoryName(changeDescription.Path) != fullPath)
else if (_fileSystem.Execute.Path.GetDirectoryName(changeDescription.Path) != fullPath)
{
return false;
}
Expand All @@ -344,7 +344,7 @@ private bool MatchesFilter(ChangeDescription changeDescription)
EnumerationOptionsHelper.MatchesPattern(
_fileSystem.Execute,
EnumerationOptionsHelper.Compatible,
_fileSystem.Path.GetFileName(changeDescription.Path),
_fileSystem.Execute.Path.GetFileName(changeDescription.Path),
filter));
}

Expand Down Expand Up @@ -501,10 +501,10 @@ private string TransformPathAndName(
string? transformedName = changeDescriptionName;
string? path = changeDescriptionPath;
if (transformedName == null ||
_fileSystem.Path.IsPathRooted(changeDescriptionName))
_fileSystem.Execute.Path.IsPathRooted(changeDescriptionName))
{
transformedName = _fileSystem.Path.GetFileName(changeDescriptionPath);
path = _fileSystem.Path.GetDirectoryName(path);
transformedName = _fileSystem.Execute.Path.GetFileName(changeDescriptionPath);
path = _fileSystem.Execute.Path.GetDirectoryName(path);
}
else if (path.EndsWith(transformedName))
{
Expand Down
Loading
Loading