Skip to content

Commit

Permalink
refactor: adapt and execute cleanup (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss authored Mar 16, 2024
1 parent 1aede2b commit 9aa3722
Show file tree
Hide file tree
Showing 159 changed files with 3,061 additions and 2,539 deletions.
32 changes: 16 additions & 16 deletions Source/Testably.Abstractions.AccessControl/AccessControlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ internal static class AccessControlHelpers
{
public const string AccessControl = nameof(AccessControl);

public static IFileSystemExtensibility GetExtensibilityOrThrow(
this IDirectoryInfo directoryInfo)
=> directoryInfo as IFileSystemExtensibility
?? throw new NotSupportedException(
$"{directoryInfo.GetType()} does not support IFileSystemExtensibility.");

public static IFileSystemExtensibility GetExtensibilityOrThrow(this IFileInfo fileInfo)
=> fileInfo as IFileSystemExtensibility
?? throw new NotSupportedException(
$"{fileInfo.GetType()} does not support IFileSystemExtensibility.");

public static IFileSystemExtensibility GetExtensibilityOrThrow(this FileSystemStream fileStream)
=> fileStream as IFileSystemExtensibility
?? throw new NotSupportedException(
$"{fileStream.GetType()} does not support IFileSystemExtensibility.");

public static TFileSystemInfo ThrowIfMissing<TFileSystemInfo>(
this TFileSystemInfo fileSystemInfo)
where TFileSystemInfo : IFileSystemInfo
Expand Down Expand Up @@ -49,20 +65,4 @@ public static IDirectoryInfo ThrowIfParentMissing(

return fileSystemInfo;
}

public static IFileSystemExtensibility GetExtensibilityOrThrow(
this IDirectoryInfo directoryInfo)
=> directoryInfo as IFileSystemExtensibility
?? throw new NotSupportedException(
$"{directoryInfo.GetType()} does not support IFileSystemExtensibility.");

public static IFileSystemExtensibility GetExtensibilityOrThrow(this IFileInfo fileInfo)
=> fileInfo as IFileSystemExtensibility
?? throw new NotSupportedException(
$"{fileInfo.GetType()} does not support IFileSystemExtensibility.");

public static IFileSystemExtensibility GetExtensibilityOrThrow(this FileSystemStream fileStream)
=> fileStream as IFileSystemExtensibility
?? throw new NotSupportedException(
$"{fileStream.GetType()} does not support IFileSystemExtensibility.");
}
10 changes: 10 additions & 0 deletions Source/Testably.Abstractions.Compression/IZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ public interface IZipFile : IFileSystemEntity
void CreateFromDirectory(
string sourceDirectoryName,
Stream destination);
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="ZipFile.CreateFromDirectory(string, Stream, CompressionLevel, bool)" />
void CreateFromDirectory(
string sourceDirectoryName,
Stream destination,
CompressionLevel compressionLevel,
bool includeBaseDirectory);
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="ZipFile.CreateFromDirectory(string, Stream, CompressionLevel, bool, Encoding)" />
void CreateFromDirectory(
string sourceDirectoryName,
Expand Down Expand Up @@ -54,19 +58,25 @@ void CreateFromDirectory(
void ExtractToDirectory(
Stream source,
string destinationDirectoryName);
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="ZipFile.ExtractToDirectory(Stream, string, bool)" />
void ExtractToDirectory(
Stream source,
string destinationDirectoryName,
bool overwriteFiles);
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="ZipFile.ExtractToDirectory(Stream, string, Encoding)" />
void ExtractToDirectory(
Stream source,
string destinationDirectoryName,
Encoding entryNameEncoding);
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="ZipFile.ExtractToDirectory(Stream, string, Encoding, bool)" />
void ExtractToDirectory(
Stream source,
Expand Down
6 changes: 3 additions & 3 deletions Source/Testably.Abstractions.Compression/Internal/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace Testably.Abstractions.Internal;

internal static class Execute
{
private static bool IsRealFileSystem(IFileSystem fileSystem)
=> fileSystem.GetType().Name == "RealFileSystem";

/// <summary>
/// Executes <paramref name="onRealFileSystem" /> when
/// the <paramref name="fileSystem" /> is a real file system,
Expand Down Expand Up @@ -35,4 +32,7 @@ public static T WhenRealFileSystem<T>(IFileSystem fileSystem,
=> IsRealFileSystem(fileSystem)
? onRealFileSystem()
: onMockFileSystem();

private static bool IsRealFileSystem(IFileSystem fileSystem)
=> fileSystem.GetType().Name == "RealFileSystem";
}
10 changes: 10 additions & 0 deletions Source/Testably.Abstractions.Compression/ZipFileWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public void CreateFromDirectory(
FileSystem,
sourceDirectoryName,
destination));
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="IZipFile.CreateFromDirectory(string, Stream, CompressionLevel, bool)" />
public void CreateFromDirectory(
string sourceDirectoryName,
Expand All @@ -49,7 +51,9 @@ public void CreateFromDirectory(
destination,
compressionLevel,
includeBaseDirectory));
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="IZipFile.CreateFromDirectory(string, Stream, CompressionLevel, bool, Encoding)" />
public void CreateFromDirectory(
string sourceDirectoryName,
Expand Down Expand Up @@ -140,7 +144,9 @@ public void ExtractToDirectory(
FileSystem,
source,
destinationDirectoryName));
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="ZipFile.ExtractToDirectory(Stream, string, bool)" />
public void ExtractToDirectory(
Stream source,
Expand All @@ -156,7 +162,9 @@ public void ExtractToDirectory(
source,
destinationDirectoryName,
overwriteFiles: overwriteFiles));
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="ZipFile.ExtractToDirectory(Stream, string, Encoding)" />
public void ExtractToDirectory(
Stream source,
Expand All @@ -172,7 +180,9 @@ public void ExtractToDirectory(
source,
destinationDirectoryName,
entryNameEncoding));
#endif

#if FEATURE_COMPRESSION_STREAM
/// <inheritdoc cref="ZipFile.ExtractToDirectory(Stream, string, Encoding, bool)" />
public void ExtractToDirectory(
Stream source,
Expand Down
34 changes: 24 additions & 10 deletions Source/Testably.Abstractions.Interface/Helpers/GuidSystemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ protected GuidSystemBase(IRandomSystem randomSystem)
/// <inheritdoc cref="IGuid.NewGuid()" />
public abstract Guid NewGuid();

#endregion

#if FEATURE_GUID_PARSE
/// <inheritdoc cref="IGuid.Parse(string)" />
public Guid Parse(string input)
=> Guid.Parse(input);
#endif

#if FEATURE_GUID_PARSE
/// <inheritdoc cref="IGuid.Parse(ReadOnlySpan{char})" />
public Guid Parse(ReadOnlySpan<char> input)
=> Guid.Parse(input);
Expand All @@ -48,17 +48,33 @@ public Guid Parse(ReadOnlySpan<char> input)
/// <inheritdoc cref="IGuid.Parse(string, IFormatProvider?)" />
public Guid Parse(string s, IFormatProvider? provider)
=> Guid.Parse(s, provider);
#endif

#if FEATURE_GUID_FORMATPROVIDER
/// <inheritdoc cref="IGuid.Parse(ReadOnlySpan{char}, IFormatProvider?)" />
public Guid Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
=> Guid.Parse(s, provider);
#endif

#if FEATURE_GUID_PARSE
/// <inheritdoc cref="IGuid.ParseExact(string, string)" />
public Guid ParseExact(string input, string format)
=> Guid.ParseExact(input, format);
#endif

#if FEATURE_GUID_PARSE
/// <inheritdoc cref="IGuid.ParseExact(ReadOnlySpan{char}, ReadOnlySpan{char})" />
public Guid ParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format)
=> Guid.ParseExact(input, format);
#endif

#if FEATURE_GUID_PARSE
/// <inheritdoc cref="IGuid.TryParse(string, out Guid)" />
public bool TryParse(string? input, out Guid result)
=> Guid.TryParse(input, out result);
#endif

#if FEATURE_GUID_PARSE
/// <inheritdoc cref="IGuid.TryParse(ReadOnlySpan{char}, out Guid)" />
public bool TryParse(ReadOnlySpan<char> input, out Guid result)
=> Guid.TryParse(input, out result);
Expand All @@ -68,30 +84,28 @@ public bool TryParse(ReadOnlySpan<char> input, out Guid result)
/// <inheritdoc cref="IGuid.TryParse(string, IFormatProvider?, out Guid)" />
public bool TryParse(string? s, IFormatProvider? provider, out Guid result)
=> Guid.TryParse(s, provider, out result);
#endif

#if FEATURE_GUID_FORMATPROVIDER
/// <inheritdoc cref="IGuid.TryParse(ReadOnlySpan{char}, IFormatProvider?, out Guid)" />
public bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out Guid result)
=> Guid.TryParse(s, provider, out result);
#endif

#if FEATURE_GUID_PARSE
/// <inheritdoc cref="IGuid.ParseExact(string, string)" />
public Guid ParseExact(string input, string format)
=> Guid.ParseExact(input, format);

/// <inheritdoc cref="IGuid.ParseExact(ReadOnlySpan{char}, ReadOnlySpan{char})" />
public Guid ParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format)
=> Guid.ParseExact(input, format);

/// <inheritdoc cref="IGuid.TryParseExact(string?, string?, out Guid)" />
public bool TryParseExact([NotNullWhen(true)] string? input,
[NotNullWhen(true)] string? format,
out Guid result)
=> Guid.TryParseExact(input, format, out result);
#endif

#if FEATURE_GUID_PARSE
/// <inheritdoc cref="IGuid.TryParseExact(ReadOnlySpan{char}, ReadOnlySpan{char}, out Guid)" />
public bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format,
out Guid result)
=> Guid.TryParseExact(input, format, out result);
#endif

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ namespace Testably.Abstractions.Helpers;
public interface IFileSystemExtensibility
{
/// <summary>
/// The wrapped instance on a real file system.
/// Retrieves a previously stored metadata on the <see cref="IFileSystemInfo" />.
/// </summary>
/// <returns>
/// <see langword="null" /> when not on a real file system or if the requested type does not match,
/// otherwise the wrapped instance.
/// </returns>
bool TryGetWrappedInstance<T>([NotNullWhen(true)] out T? wrappedInstance);
/// <typeparam name="T">The type of the value to retrieve.</typeparam>
/// <param name="key">The key under which the metadata was stored.</param>
/// <returns>The value of the previously stored metadata, or <see langword="default" />.</returns>
T? RetrieveMetadata<T>(string key);

/// <summary>
/// Stores additional metadata to the <see cref="IFileSystemInfo" />.
Expand All @@ -26,10 +25,11 @@ public interface IFileSystemExtensibility
void StoreMetadata<T>(string key, T? value);

/// <summary>
/// Retrieves a previously stored metadata on the <see cref="IFileSystemInfo" />.
/// The wrapped instance on a real file system.
/// </summary>
/// <typeparam name="T">The type of the value to retrieve.</typeparam>
/// <param name="key">The key under which the metadata was stored.</param>
/// <returns>The value of the previously stored metadata, or <see langword="default" />.</returns>
T? RetrieveMetadata<T>(string key);
/// <returns>
/// <see langword="null" /> when not on a real file system or if the requested type does not match,
/// otherwise the wrapped instance.
/// </returns>
bool TryGetWrappedInstance<T>([NotNullWhen(true)] out T? wrappedInstance);
}
40 changes: 28 additions & 12 deletions Source/Testably.Abstractions.Interface/Helpers/PathSystemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public virtual string Combine(string path1, string path2, string path3, string p
public virtual string Combine(params string[] paths)
=> Path.Combine(paths);

#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="Path.EndsInDirectorySeparator(ReadOnlySpan{char})" />
public virtual bool EndsInDirectorySeparator(ReadOnlySpan<char> path)
=> Path.EndsInDirectorySeparator(path);
#endif

#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="Path.EndsInDirectorySeparator(string)" />
public virtual bool EndsInDirectorySeparator(string path)
=> Path.EndsInDirectorySeparator(path);
#endif

#if FEATURE_FILESYSTEM_NET7
/// <inheritdoc cref="Path.Exists(string)" />
public abstract bool Exists([NotNullWhen(true)] string? path);
Expand Down Expand Up @@ -194,23 +206,13 @@ public virtual bool IsPathRooted(ReadOnlySpan<char> path)
public virtual bool IsPathRooted(string? path)
=> Path.IsPathRooted(path);

#endregion

#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="Path.EndsInDirectorySeparator(ReadOnlySpan{char})" />
public virtual bool EndsInDirectorySeparator(ReadOnlySpan<char> path)
=> Path.EndsInDirectorySeparator(path);

/// <inheritdoc cref="Path.EndsInDirectorySeparator(string)" />
public virtual bool EndsInDirectorySeparator(string path)
=> Path.EndsInDirectorySeparator(path);
#endif

#if FEATURE_PATH_JOIN
/// <inheritdoc cref="Path.Join(ReadOnlySpan{char}, ReadOnlySpan{char})" />
public virtual string Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2)
=> Path.Join(path1, path2);
#endif

#if FEATURE_PATH_JOIN
/// <inheritdoc cref="Path.Join(ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char})" />
public virtual string Join(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
Expand All @@ -225,19 +227,27 @@ public virtual string Join(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path3,
ReadOnlySpan<char> path4)
=> Path.Join(path1, path2, path3, path4);
#endif

#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="Path.Join(string, string)" />
public virtual string Join(string? path1, string? path2)
=> Path.Join(path1, path2);
#endif

#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="Path.Join(string, string, string)" />
public virtual string Join(string? path1, string? path2, string? path3)
=> Path.Join(path1, path2, path3);
#endif

#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="Path.Join(string, string, string, string)" />
public virtual string Join(string? path1, string? path2, string? path3, string? path4)
=> Path.Join(path1, path2, path3, path4);
#endif

#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="Path.Join(string[])" />
public virtual string Join(params string?[] paths)
=> Path.Join(paths);
Expand All @@ -247,7 +257,9 @@ public virtual string Join(params string?[] paths)
/// <inheritdoc cref="Path.TrimEndingDirectorySeparator(ReadOnlySpan{char})" />
public virtual ReadOnlySpan<char> TrimEndingDirectorySeparator(ReadOnlySpan<char> path)
=> Path.TrimEndingDirectorySeparator(path);
#endif

#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="Path.TrimEndingDirectorySeparator(string)" />
public virtual string TrimEndingDirectorySeparator(string path)
=> Path.TrimEndingDirectorySeparator(path);
Expand All @@ -260,7 +272,9 @@ public virtual bool TryJoin(ReadOnlySpan<char> path1,
Span<char> destination,
out int charsWritten)
=> Path.TryJoin(path1, path2, destination, out charsWritten);
#endif

#if FEATURE_PATH_JOIN
/// <inheritdoc cref="Path.TryJoin(ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char}, Span{char}, out int)" />
public virtual bool TryJoin(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
Expand All @@ -269,4 +283,6 @@ public virtual bool TryJoin(ReadOnlySpan<char> path1,
out int charsWritten)
=> Path.TryJoin(path1, path2, path3, destination, out charsWritten);
#endif

#endregion
}
Loading

0 comments on commit 9aa3722

Please sign in to comment.