Skip to content

Commit

Permalink
feat: add Meziantou.Analyzer (#519)
Browse files Browse the repository at this point in the history
Add
[Meziantou.Analyzer](https://github.com/meziantou/Meziantou.Analyzer)
and fix the reported issues.

**Breaking changes:**
- Rename namespace from
`Testably.Abstractions.Testing.FileSystemInitializer` to
`Testably.Abstractions.Testing.Initializer`
- Move static methods from `RandomProvider.Generator<T>` to
`RandomProvider.Generator`

---------

Co-authored-by: Valentin <valentin.breuss@baur.at>
  • Loading branch information
vbreuss and vbreuss authored Mar 22, 2024
1 parent e33d5e5 commit 0ee5868
Show file tree
Hide file tree
Showing 140 changed files with 1,010 additions and 704 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,8 @@ dotnet_diagnostic.CA1512.severity = none
dotnet_diagnostic.CA1513.severity = none
dotnet_diagnostic.CA1860.severity = none
dotnet_diagnostic.CA1861.severity = none

dotnet_diagnostic.MA0005.severity = none
dotnet_diagnostic.MA0016.severity = none
dotnet_diagnostic.MA0048.severity = none
dotnet_diagnostic.MA0051.severity = none
81 changes: 42 additions & 39 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
<PackageVersion Include="TestableIO.System.IO.Abstractions" Version="21.0.2" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="MinVer" Version="5.0.0" />
<PackageVersion Include="Nullable" Version="1.3.1" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Xunit.SkippableFact" Version="1.4.13" />
<PackageVersion Include="xunit" Version="2.7.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="PublicApiGenerator" Version="11.1.0" />
<PackageVersion Include="NUnit" Version="4.1.0" />
<PackageVersion Include="NUnit.Analyzers" Version="4.1.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Testably.Abstractions" Version="2.6.1" />
<PackageVersion Include="Testably.Abstractions.Testing" Version="2.6.1" />
<PackageVersion Include="Testably.Abstractions.FluentAssertions" Version="1.0.1" />
</ItemGroup>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
<PackageVersion Include="TestableIO.System.IO.Abstractions" Version="21.0.2" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="MinVer" Version="5.0.0" />
<PackageVersion Include="Nullable" Version="1.3.1" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Xunit.SkippableFact" Version="1.4.13" />
<PackageVersion Include="xunit" Version="2.7.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="PublicApiGenerator" Version="11.1.0" />
<PackageVersion Include="NUnit" Version="4.1.0" />
<PackageVersion Include="NUnit.Analyzers" Version="4.1.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Testably.Abstractions" Version="2.6.1" />
<PackageVersion Include="Testably.Abstractions.Testing" Version="2.6.1" />
<PackageVersion Include="Testably.Abstractions.FluentAssertions" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Meziantou.Analyzer" Version="1.0.570" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Examples/ZipFile/ZipFile.Tests/ZipFileHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.IO.Abstractions;
using System.IO.Compression;
using Testably.Abstractions.Testing;
using Testably.Abstractions.Testing.FileSystemInitializer;
using Testably.Abstractions.Testing.Initializer;
using Xunit;

namespace Testably.Abstractions.Examples.ZipFile.Tests;
Expand Down
7 changes: 7 additions & 0 deletions Source/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@
Link="\Docs\logo_256x256.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
6 changes: 4 additions & 2 deletions Source/Testably.Abstractions.Compression/IZipFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.IO;
using System.IO.Compression;
using System.IO.Compression;
using System.Text;
#if FEATURE_COMPRESSION_STREAM
using System.IO;
#endif

namespace Testably.Abstractions;

Expand Down
3 changes: 2 additions & 1 deletion Source/Testably.Abstractions.Compression/Internal/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ public static T WhenRealFileSystem<T>(IFileSystem fileSystem,
: onMockFileSystem();

private static bool IsRealFileSystem(IFileSystem fileSystem)
=> fileSystem.GetType().Name == "RealFileSystem";
=> string.Equals(fileSystem.GetType().Name, "RealFileSystem",
StringComparison.Ordinal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ internal static void CreateFromDirectory(IFileSystem fileSystem,

if (file is IFileInfo fileInfo)
{
#pragma warning disable MA0074
string entryName = file.FullName
.Substring(basePath.Length + 1)
.Replace("\\", "/");
#pragma warning restore MA0074
ZipArchiveEntry entry = compressionLevel.HasValue
? archive.CreateEntry(entryName, compressionLevel.Value)
: archive.CreateEntry(entryName);
Expand Down Expand Up @@ -121,6 +123,7 @@ internal static void CreateFromDirectory(IFileSystem fileSystem,
/// <see
/// href="https://github.com/dotnet/runtime/blob/v6.0.10/src/libraries/System.IO.Compression.ZipFile/src/System/IO/Compression/ZipFile.Create.cs#L354" />
/// </remarks>
#pragma warning disable MA0051 // Method is too long
internal static void CreateFromDirectory(
IFileSystem fileSystem,
string sourceDirectoryName,
Expand Down Expand Up @@ -163,9 +166,11 @@ internal static void CreateFromDirectory(

if (file is IFileInfo fileInfo)
{
#pragma warning disable MA0074
string entryName = file.FullName
.Substring(basePath.Length + 1)
.Replace("\\", "/");
#pragma warning restore MA0074
ZipArchiveEntry entry = compressionLevel.HasValue
? archive.CreateEntry(entryName, compressionLevel.Value)
: archive.CreateEntry(entryName);
Expand All @@ -175,9 +180,9 @@ internal static void CreateFromDirectory(
else if (file is IDirectoryInfo directoryInfo &&
directoryInfo.GetFileSystemInfos().Length == 0)
{
#pragma warning disable CA1845
#pragma warning disable CA1845
string entryName = file.FullName.Substring(basePath.Length + 1) + "/";
#pragma warning restore CA1845
#pragma warning restore CA1845
archive.CreateEntry(entryName);
}
}
Expand All @@ -189,6 +194,7 @@ internal static void CreateFromDirectory(
}
}
}
#pragma warning restore MA0051 // Method is too long
#endif

internal static void ExtractRelativeToDirectory(this IZipArchiveEntry source,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NETSTANDARD2_0
using System;
using System.Diagnostics.CodeAnalysis;

namespace Testably.Abstractions.Polyfills;
Expand All @@ -16,7 +17,9 @@ internal static bool EndsWith(
this string @this,
char value)
{
#pragma warning disable MA0074
return @this.EndsWith($"{value}");
#pragma warning restore MA0074
}
}
#endif
6 changes: 4 additions & 2 deletions Source/Testably.Abstractions.Compression/ZipFileWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.IO;
using System.IO.Compression;
using System.IO.Compression;
using System.Text;
using Testably.Abstractions.Internal;
#if FEATURE_COMPRESSION_STREAM
using System.IO;
#endif

namespace Testably.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ protected GuidSystemBase(IRandomSystem randomSystem)
public abstract Guid NewGuid();

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

#if FEATURE_GUID_PARSE
#pragma warning disable MA0011
/// <inheritdoc cref="IGuid.Parse(ReadOnlySpan{char})" />
public Guid Parse(ReadOnlySpan<char> input)
=> Guid.Parse(input);
#pragma warning restore MA0011
#endif

#if FEATURE_GUID_FORMATPROVIDER
Expand Down Expand Up @@ -69,15 +73,19 @@ public Guid ParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format)
#endif

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

#if FEATURE_GUID_PARSE
#pragma warning disable MA0011
/// <inheritdoc cref="IGuid.TryParse(ReadOnlySpan{char}, out Guid)" />
public bool TryParse(ReadOnlySpan<char> input, out Guid result)
=> Guid.TryParse(input, out result);
#pragma warning restore MA0011
#endif

#if FEATURE_GUID_FORMATPROVIDER
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.Win32.SafeHandles;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Statistics;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

namespace Testably.Abstractions.Testing.FileSystem;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ public virtual string Name
{
using IDisposable registration = RegisterProperty(nameof(Name), PropertyAccess.Get);

return _fileSystem.Execute.Path.GetPathRoot(Location.FullPath) == Location.FullPath
return string.Equals(
_fileSystem.Execute.Path.GetPathRoot(Location.FullPath),
Location.FullPath,
_fileSystem.Execute.StringComparisonMode)
? Location.FullPath
: _fileSystem.Execute.Path.GetFileName(Location.FullPath.TrimEnd(
_fileSystem.Execute.Path.DirectorySeparatorChar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private FileSystemWatcherMock(MockFileSystem fileSystem)
_fileSystem = fileSystem;
}

private event EventHandler<ChangeDescription>? InternalEvent;
private event EventHandler<ChangeDescriptionEventArgs>? InternalEvent;

#region IFileSystemWatcher Members

Expand Down Expand Up @@ -321,12 +321,15 @@ private bool MatchesFilter(ChangeDescription changeDescription)
string fullPath = _fileSystem.Execute.Path.GetFullPath(Path);
if (IncludeSubdirectories)
{
if (!changeDescription.Path.StartsWith(fullPath))
if (!changeDescription.Path.StartsWith(fullPath,
_fileSystem.Execute.StringComparisonMode))
{
return false;
}
}
else if (_fileSystem.Execute.Path.GetDirectoryName(changeDescription.Path) != fullPath)
else if (!string.Equals(
_fileSystem.Execute.Path.GetDirectoryName(changeDescription.Path),
fullPath))
{
return false;
}
Expand All @@ -351,7 +354,7 @@ private bool MatchesFilter(ChangeDescription changeDescription)

private void NotifyChange(ChangeDescription item)
{
InternalEvent?.Invoke(this, item);
InternalEvent?.Invoke(this, new ChangeDescriptionEventArgs(item));
if (MatchesFilter(item))
{
if (item.ChangeType.HasFlag(WatcherChangeTypes.Created))
Expand Down Expand Up @@ -507,7 +510,7 @@ private string TransformPathAndName(
transformedName = _fileSystem.Execute.Path.GetFileName(changeDescriptionPath);
path = _fileSystem.Execute.Path.GetDirectoryName(path);
}
else if (path.EndsWith(transformedName))
else if (path.EndsWith(transformedName, _fileSystem.Execute.StringComparisonMode))
{
path = path.Substring(0, path.Length - transformedName.Length);
}
Expand Down Expand Up @@ -579,12 +582,15 @@ private IWaitForChangedResult WaitForChangedInternal(
TaskCompletionSource<IWaitForChangedResult>
tcs = new();

void EventHandler(object? _, ChangeDescription c)
void EventHandler(object? _, ChangeDescriptionEventArgs c)
{
if ((c.ChangeType & changeType) != 0)
if ((c.ChangeDescription.ChangeType & changeType) != 0)
{
tcs.TrySetResult(new WaitForChangedResultMock(c.ChangeType, c.Name,
oldName: c.OldName, timedOut: false));
tcs.TrySetResult(new WaitForChangedResultMock(
c.ChangeDescription.ChangeType,
c.ChangeDescription.Name,
oldName: c.ChangeDescription.OldName,
timedOut: false));
}
}

Expand Down Expand Up @@ -648,4 +654,9 @@ public WaitForChangedResultMock(
/// <inheritdoc cref="IWaitForChangedResult.TimedOut" />
public bool TimedOut { get; }
}

internal class ChangeDescriptionEventArgs(ChangeDescription changeDescription) : EventArgs
{
public ChangeDescription ChangeDescription { get; } = changeDescription;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.IO;
using System.Reflection;
using Testably.Abstractions.Testing.FileSystemInitializer;
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Initializer;

namespace Testably.Abstractions.Testing;

Expand Down Expand Up @@ -37,6 +37,7 @@ public static IFileSystemInitializer<TFileSystem> Initialize<TFileSystem>(
/// <paramref name="relativePath" /> or should include all subdirectories.<br />
/// The default value is <see cref="SearchOption.AllDirectories" />.
/// </param>
#pragma warning disable MA0051 // Method is too long
public static void InitializeEmbeddedResourcesFromAssembly(this IFileSystem fileSystem,
string directoryPath,
Assembly assembly,
Expand All @@ -63,7 +64,7 @@ public static void InitializeEmbeddedResourcesFromAssembly(this IFileSystem file
foreach (string resourcePath in resourcePaths)
{
string fileName = resourcePath;
if (fileName.StartsWith(assemblyNamePrefix))
if (fileName.StartsWith(assemblyNamePrefix, StringComparison.Ordinal))
{
fileName = fileName.Substring(assemblyNamePrefix.Length);
}
Expand All @@ -80,7 +81,7 @@ public static void InitializeEmbeddedResourcesFromAssembly(this IFileSystem file

if (relativePath != null)
{
if (!fileName.StartsWith(relativePath))
if (!fileName.StartsWith(relativePath, StringComparison.Ordinal))
{
continue;
}
Expand All @@ -107,6 +108,7 @@ public static void InitializeEmbeddedResourcesFromAssembly(this IFileSystem file
}
}
}
#pragma warning restore MA0051 // Method is too long

/// <summary>
/// Initializes the <see cref="IFileSystem" /> in the <paramref name="basePath" /> with test data.
Expand Down
Loading

0 comments on commit 0ee5868

Please sign in to comment.