Skip to content

Commit 9ad5e3d

Browse files
authored
refactor: fix sonar issues due to updated analyzers (#443)
- Make `TestingException` only serializable when using an older Framework than .NET 8 - Fix [CA1859](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1859) Use concrete types when possible for improved performance - Fix [CA1866](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1865-ca1867) The char overload is a better performing overload than a string with a single char. - Fix [xUnit1042](https://xunit.net/xunit.analyzers/rules/xUnit1042) The member referenced by the MemberData attribute returns untyped data rows - Disable the following rules: - [CA1510](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1510) Use ArgumentNullException throw helper - [CA1512](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1512) Use ArgumentOutOfRangeException throw helper - [CA1513](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1513) Use ObjectDisposedException throw helper - [CA1860](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1860) Avoid using 'Enumerable.Any()' extension method - [CA1861](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1861) Avoid constant arrays as arguments
1 parent c665b33 commit 9ad5e3d

File tree

39 files changed

+314
-249
lines changed

39 files changed

+314
-249
lines changed

.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,10 @@ dotnet_naming_style.begins_with_i.capitalization = pascal_case
229229

230230
# ReSharper inspection severities
231231
resharper_arrange_method_or_operator_body_highlighting = hint
232+
233+
# Diagnostics
234+
dotnet_diagnostic.CA1510.severity = none
235+
dotnet_diagnostic.CA1512.severity = none
236+
dotnet_diagnostic.CA1513.severity = none
237+
dotnet_diagnostic.CA1860.severity = none
238+
dotnet_diagnostic.CA1861.severity = none

Source/Testably.Abstractions.Compression/Internal/ZipUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ internal static void ExtractRelativeToDirectory(this IZipArchiveEntry source,
208208
source.FileSystem.Directory.CreateDirectory(directoryPath);
209209
}
210210

211-
if (source.FullName.EndsWith("/"))
211+
if (source.FullName.EndsWith('/'))
212212
{
213213
if (source.Length != 0)
214214
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#if NETSTANDARD2_0
2+
using System.Diagnostics.CodeAnalysis;
3+
4+
namespace Testably.Abstractions.Polyfills;
5+
6+
/// <summary>
7+
/// Provides extension methods to simplify writing platform independent tests.
8+
/// </summary>
9+
[ExcludeFromCodeCoverage]
10+
internal static class StringExtensionMethods
11+
{
12+
/// <summary>
13+
/// Determines whether the end of this string instance matches the specified character.
14+
/// </summary>
15+
internal static bool EndsWith(
16+
this string @this,
17+
char value)
18+
{
19+
return @this.EndsWith($"{value}");
20+
}
21+
}
22+
#endif
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
global using System.IO.Abstractions;
1+
#if NETSTANDARD2_0 || NETSTANDARD2_1
2+
global using Testably.Abstractions.Polyfills;
3+
# endif
4+
global using System.IO.Abstractions;

Source/Testably.Abstractions.Testing/FileSystem/FileSystemInfoMock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public string Extension
114114
{
115115
get
116116
{
117-
if (Location.FullPath.EndsWith(".") &&
117+
if (Location.FullPath.EndsWith('.') &&
118118
!Execute.IsWindows)
119119
{
120120
return ".";

Source/Testably.Abstractions.Testing/FileSystemInitializer/TestingException.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ namespace Testably.Abstractions.Testing.FileSystemInitializer;
66
/// <summary>
77
/// Custom <see cref="TestingException" /> when using the test system incorrectly.
88
/// </summary>
9+
#if !NET8_0_OR_GREATER
910
[Serializable]
11+
#endif
1012
public class TestingException : Exception
1113
{
1214
/// <summary>

Source/Testably.Abstractions.Testing/Helpers/RandomFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static IRandom Shared
2020
/// <summary>
2121
/// <see href="https://andrewlock.net/building-a-thread-safe-random-implementation-for-dotnet-framework/" />
2222
/// </summary>
23-
private static IRandom CreateThreadSafeRandomWrapper()
23+
private static RandomWrapper CreateThreadSafeRandomWrapper()
2424
{
2525
int seed;
2626
lock (Global)

Source/Testably.Abstractions.Testing/Polyfills/StringExtensionMethods.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,15 @@ internal static bool EndsWith(
1818
{
1919
return @this.EndsWith($"{value}");
2020
}
21+
22+
/// <summary>
23+
/// Determines whether this string instance starts with the specified character.
24+
/// </summary>
25+
internal static bool StartsWith(
26+
this string @this,
27+
char value)
28+
{
29+
return @this.StartsWith($"{value}");
30+
}
2131
}
2232
#endif

Source/Testably.Abstractions.Testing/RandomSystem/RandomProviderMock.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Testably.Abstractions.Testing.RandomSystem;
66

77
internal sealed class RandomProviderMock : IRandomProvider
88
{
9-
[ThreadStatic] private static IRandom? _shared;
9+
[ThreadStatic] private static RandomMock? _shared;
1010

1111
private static Generator<Guid> DefaultGuidGenerator
1212
=> Generator<Guid>.FromCallback(Guid.NewGuid);
@@ -34,7 +34,7 @@ public IRandom GetRandom(int seed = SharedSeed)
3434

3535
#endregion
3636

37-
private static IRandom DefaultRandomGenerator(int seed)
37+
private static RandomMock DefaultRandomGenerator(int seed)
3838
{
3939
if (seed == SharedSeed)
4040
{

Source/Testably.Abstractions.Testing/Storage/InMemoryContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public static IStorageContainer NewFile(IStorageLocation location,
250250

251251
internal FileAttributes AdjustAttributes(FileAttributes attributes)
252252
{
253-
if (Path.GetFileName(_location.FullPath).StartsWith("."))
253+
if (Path.GetFileName(_location.FullPath).StartsWith('.'))
254254
{
255255
FileAttributes attr = attributes;
256256
attributes = Execute.OnLinux(

0 commit comments

Comments
 (0)