Skip to content

Commit

Permalink
refactor: throw exception when encountering unsupported EnumerationOp…
Browse files Browse the repository at this point in the history
…tions

Addresses #653
  • Loading branch information
fgreinacher committed Oct 12, 2020
1 parent c0cd28d commit c3e036c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
41 changes: 39 additions & 2 deletions src/System.IO.Abstractions.TestingHelpers/MockDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public override string[] GetDirectories(string path, string searchPattern, Searc
#if FEATURE_ENUMERATION_OPTIONS
public override string[] GetDirectories(string path, string searchPattern, EnumerationOptions enumerationOptions)
{
return GetDirectories(path, "*", enumerationOptions.RecurseSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
return GetDirectories(path, "*", EnumerationOptionsToSearchOption(enumerationOptions));
}
#endif

Expand Down Expand Up @@ -206,7 +206,7 @@ public override string[] GetFiles(string path, string searchPattern, SearchOptio
#if FEATURE_ENUMERATION_OPTIONS
public override string[] GetFiles(string path, string searchPattern, EnumerationOptions enumerationOptions)
{
return GetFiles(path, "*", enumerationOptions.RecurseSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
return GetFiles(path, "*", EnumerationOptionsToSearchOption(enumerationOptions));
}
#endif

Expand Down Expand Up @@ -638,5 +638,42 @@ private string ReplaceLastOccurrence(string source, string find, string replace)
var result = source.Remove(place, find.Length).Insert(place, replace);
return result;
}

#if FEATURE_ENUMERATION_OPTIONS
private SearchOption EnumerationOptionsToSearchOption(EnumerationOptions enumerationOptions)
{
static Exception CreateExceptionForUnsupportedProperty(string propertyName)
{
return new NotSupportedException(
$"Changing EnumerationOptions.{propertyName} is not yet implemented for the mock file system."
);
}

if (enumerationOptions.AttributesToSkip != (FileAttributes.System | FileAttributes.Hidden))
{
throw CreateExceptionForUnsupportedProperty("AttributesToSkip");
}
if (!enumerationOptions.IgnoreInaccessible)
{
throw CreateExceptionForUnsupportedProperty("IgnoreInaccessible");
}
if (enumerationOptions.MatchCasing != MatchCasing.PlatformDefault)
{
throw CreateExceptionForUnsupportedProperty("MatchCasing");
}
if (enumerationOptions.MatchType != MatchType.Simple)
{
throw CreateExceptionForUnsupportedProperty("MatchType");
}
if (enumerationOptions.ReturnSpecialDirectories)
{
throw CreateExceptionForUnsupportedProperty("ReturnSpecialDirectories");
}

return enumerationOptions.RecurseSubdirectories
? SearchOption.AllDirectories
: SearchOption.TopDirectoryOnly;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<AssemblyName>System.IO.Abstractions.TestingHelpers</AssemblyName>
<RootNamespace>System.IO.Abstractions.TestingHelpers</RootNamespace>
<Description>A set of pre-built mocks to help when testing file system interactions.</Description>
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
<TargetFrameworks>netstandard2.1;netstandard2.0;net461</TargetFrameworks>
<PackageProjectUrl>https://github.com/System-IO-Abstractions/System.IO.Abstractions</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>testing</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.0;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOsUnixLike())">$(TargetFrameworks);net461</TargetFrameworks>
<Description>The unit tests for our pre-built mocks</Description>
<AssemblyName>System.IO.Abstractions.TestingHelpers.Tests</AssemblyName>
Expand Down

0 comments on commit c3e036c

Please sign in to comment.