Skip to content

Commit

Permalink
Refactor InMemoryStorage to reduce method complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed May 5, 2024
1 parent bd02b8b commit a06f28a
Showing 1 changed file with 84 additions and 52 deletions.
136 changes: 84 additions & 52 deletions Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,58 +226,8 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
_fileSystem.Execute.StringComparisonMode) &&
!x.Key.Equals(location)))
{
string? parentPath =
_fileSystem.Execute.Path.GetDirectoryName(
item.Key.FullPath.TrimEnd(_fileSystem.Execute.Path
.DirectorySeparatorChar));
if (parentPath == null)
{
continue;
}

if (!parentPath.Equals(fullPathWithoutTrailingSlash,
_fileSystem.Execute.StringComparisonMode))
{
#if NETSTANDARD2_1
if (!enumerationOptions.RecurseSubdirectories)
{
continue;
}
#else
int recursionDepth = parentPath
.Substring(fullPathWithoutTrailingSlash.Length)
.Count(x => x == _fileSystem.Execute.Path.DirectorySeparatorChar);
if (!enumerationOptions.RecurseSubdirectories ||
recursionDepth > enumerationOptions.MaxRecursionDepth)
{
continue;
}
#endif
}

#if NET8_0_OR_GREATER
FileAttributes defaultAttributeToSkip = FileAttributes.None;
#else
FileAttributes defaultAttributeToSkip = 0;
#endif
if (enumerationOptions.AttributesToSkip != defaultAttributeToSkip &&
item.Value.Attributes.HasFlag(enumerationOptions.AttributesToSkip))
{
continue;
}

if (!_fileSystem.AccessControlStrategy
.IsAccessGranted(item.Key.FullPath, item.Value.Extensibility))
{
if (!enumerationOptions.IgnoreInaccessible)
{
throw ExceptionFactory.AccessToPathDenied(item.Key.FullPath);
}

continue;
}

if (type.HasFlag(item.Value.Type))
if (type.HasFlag(item.Value.Type) &&
IncludeItemInEnumeration(item, fullPathWithoutTrailingSlash, enumerationOptions))
{
string name = _fileSystem.Execute.Path.GetFileName(item.Key.FullPath);
if (EnumerationOptionsHelper.MatchesPattern(
Expand Down Expand Up @@ -702,6 +652,88 @@ private void CreateParents(MockFileSystem fileSystem, IStorageLocation location)
}
}

/// <summary>
/// Checks, if the <paramref name="item" /> should be included during enumeration, depending on the
/// <paramref name="directoryPath" /> to enumerate and the <paramref name="enumerationOptions" />.
/// </summary>
/// <remarks>
/// <see cref="EnumerationOptions.RecurseSubdirectories" />:<br />
/// If not set, only items directly in <paramref name="directoryPath" /> are included
/// <para />
/// <see cref="EnumerationOptions.MaxRecursionDepth" />:<br />

Check failure on line 663 in Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

View workflow job for this annotation

GitHub Actions / Test (Examples)

XML comment has cref attribute 'MaxRecursionDepth' that could not be resolved

Check failure on line 663 in Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

View workflow job for this annotation

GitHub Actions / Test (Examples)

XML comment has cref attribute 'MaxRecursionDepth' that could not be resolved

Check failure on line 663 in Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

View workflow job for this annotation

GitHub Actions / Test (MacOS)

XML comment has cref attribute 'MaxRecursionDepth' that could not be resolved

Check failure on line 663 in Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

View workflow job for this annotation

GitHub Actions / Test (MacOS)

XML comment has cref attribute 'MaxRecursionDepth' that could not be resolved

Check failure on line 663 in Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

View workflow job for this annotation

GitHub Actions / Test (Ubuntu)

XML comment has cref attribute 'MaxRecursionDepth' that could not be resolved

Check failure on line 663 in Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

View workflow job for this annotation

GitHub Actions / Test (Ubuntu)

XML comment has cref attribute 'MaxRecursionDepth' that could not be resolved

Check failure on line 663 in Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

View workflow job for this annotation

GitHub Actions / Test (Windows)

XML comment has cref attribute 'MaxRecursionDepth' that could not be resolved

Check failure on line 663 in Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

View workflow job for this annotation

GitHub Actions / Test (Windows)

XML comment has cref attribute 'MaxRecursionDepth' that could not be resolved
/// If set, only items within the given value of recursion are included
/// <para />
/// <see cref="EnumerationOptions.AttributesToSkip" />:<br />
/// If set, only items with the given attributes are included
/// <para />
/// If the item is not granted access by the <see cref="IAccessControlStrategy" />:<br />
/// When <see cref="EnumerationOptions.IgnoreInaccessible" /> is set, the item is ignored, otherwise this method throws
/// an <see cref="UnauthorizedAccessException" />.
/// </remarks>
/// <exception cref="UnauthorizedAccessException">
/// When an item is not granted access by the
/// <see cref="IAccessControlStrategy" /> and <see cref="EnumerationOptions.IgnoreInaccessible" /> is not set to
/// <see langword="true" />.
/// </exception>
private bool IncludeItemInEnumeration(
KeyValuePair<IStorageLocation, IStorageContainer> item,
string directoryPath,
EnumerationOptions enumerationOptions)
{
string? parentPath =
_fileSystem.Execute.Path.GetDirectoryName(
item.Key.FullPath.TrimEnd(_fileSystem.Execute.Path
.DirectorySeparatorChar));
if (parentPath == null)
{
return false;
}

if (!parentPath.Equals(directoryPath,
_fileSystem.Execute.StringComparisonMode))
{
#if NETSTANDARD2_1
if (!enumerationOptions.RecurseSubdirectories)
{
return false;
}
#else
int recursionDepth = parentPath
.Substring(directoryPath.Length)
.Count(x => x == _fileSystem.Execute.Path.DirectorySeparatorChar);
if (!enumerationOptions.RecurseSubdirectories ||
recursionDepth > enumerationOptions.MaxRecursionDepth)
{
return false;
}
#endif
}

#if NET8_0_OR_GREATER
FileAttributes defaultAttributeToSkip = FileAttributes.None;
#else
FileAttributes defaultAttributeToSkip = 0;
#endif
if (enumerationOptions.AttributesToSkip != defaultAttributeToSkip &&
item.Value.Attributes.HasFlag(enumerationOptions.AttributesToSkip))
{
return false;
}

if (!_fileSystem.AccessControlStrategy
.IsAccessGranted(item.Key.FullPath, item.Value.Extensibility))
{
if (!enumerationOptions.IgnoreInaccessible)
{
throw ExceptionFactory.AccessToPathDenied(item.Key.FullPath);
}

return false;
}

return true;
}

#pragma warning disable MA0051 // Method is too long
private IStorageLocation? MoveInternal(IStorageLocation source,
IStorageLocation destination,
Expand Down

0 comments on commit a06f28a

Please sign in to comment.