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: throw exception when encountering unsupported EnumerationOptions #655

Merged
merged 1 commit into from
Oct 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Expand Up @@ -188,12 +188,7 @@ public void MockDirectoryInfo_EnumerateFileSystemInfos_ShouldReturnDirectoriesAn

var enumerationOptions = new EnumerationOptions()
{
MatchType = MatchType.Win32,
RecurseSubdirectories = true,
IgnoreInaccessible = true,
ReturnSpecialDirectories = false,
AttributesToSkip = FileAttributes.Hidden,
MatchCasing = MatchCasing.PlatformDefault,
};

var result = directoryInfo.EnumerateFileSystemInfos("*", enumerationOptions).ToArray();
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.1;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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOsUnixLike())">$(TargetFrameworks);net461</TargetFrameworks>
<Description>The unit tests for our the core abstractions</Description>
<AssemblyName>System.IO.Abstractions.Tests</AssemblyName>
Expand Down