From d2dc8d9ae7cf7e566fd0f3e9210e61115081f1c7 Mon Sep 17 00:00:00 2001 From: Valentin Date: Sun, 12 Jan 2025 19:45:24 +0100 Subject: [PATCH 1/4] Fix sonar issues --- .../Helpers/ExceptionFactory.cs | 2 +- .../Testably.Abstractions.Testing/Storage/InMemoryStorage.cs | 5 ++--- Source/Testably.Abstractions.Testing/TimeProvider.cs | 2 ++ Tests/Directory.Build.props | 2 +- .../Testably.Abstractions.TestHelpers/AssertionHelpers.cs | 4 +++- .../ClassGenerators/FileSystemClassGenerator.cs | 2 +- .../FileSystem/DriveInfoMockTests.cs | 2 +- .../FileSystem/FileStreamFactory/Tests.cs | 2 +- .../Testably.Abstractions.Tests/RandomSystem/RandomTests.cs | 4 ++-- .../Testably.Abstractions.Tests/TimeSystem/DateTimeTests.cs | 2 ++ 10 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Source/Testably.Abstractions.Testing/Helpers/ExceptionFactory.cs b/Source/Testably.Abstractions.Testing/Helpers/ExceptionFactory.cs index e33b2eb05..38c238f3f 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/ExceptionFactory.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/ExceptionFactory.cs @@ -21,7 +21,7 @@ internal static IOException AclAccessToPathDenied(string path) internal static ArgumentException AppendAccessOnlyInWriteOnlyMode( string paramName = "access") - => new($"{FileMode.Append} access can be requested only in write-only mode.", + => new($"{nameof(FileMode.Append)} access can be requested only in write-only mode.", paramName) { #if FEATURE_EXCEPTION_HRESULT diff --git a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs index 0545f121d..229e48832 100644 --- a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs +++ b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs @@ -128,8 +128,7 @@ public bool DeleteContainer(IStorageLocation location, bool recursive = false) if (!_containers.TryGetValue(location, out IStorageContainer? container)) { IStorageLocation? parentLocation = location.GetParent(); - if (parentLocation != null && - !_containers.TryGetValue(parentLocation, out _)) + if (parentLocation != null && !_containers.ContainsKey(parentLocation)) { throw ExceptionFactory.DirectoryNotFound(parentLocation.FullPath); } @@ -1044,7 +1043,7 @@ private void ThrowIfParentDoesNotExist(IStorageLocation location, _fileSystem.Execute.Path.GetPathRoot(parentLocation.FullPath), parentLocation.FullPath, _fileSystem.Execute.StringComparisonMode) && - !_containers.TryGetValue(parentLocation, out _)) + !_containers.ContainsKey(parentLocation)) { throw exceptionCallback(parentLocation); } diff --git a/Source/Testably.Abstractions.Testing/TimeProvider.cs b/Source/Testably.Abstractions.Testing/TimeProvider.cs index 6c94e4084..ef4a55457 100644 --- a/Source/Testably.Abstractions.Testing/TimeProvider.cs +++ b/Source/Testably.Abstractions.Testing/TimeProvider.cs @@ -24,8 +24,10 @@ public static ITimeProvider Now() /// public static ITimeProvider Random() { + #pragma warning disable MA0113 // Use DateTime.UnixEpoch DateTime randomTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) .AddSeconds(RandomFactory.Shared.Next()); + #pragma warning restore MA0113 return new TimeProviderMock(randomTime, "Random"); } diff --git a/Tests/Directory.Build.props b/Tests/Directory.Build.props index 900b9c9d6..999f3f2d9 100644 --- a/Tests/Directory.Build.props +++ b/Tests/Directory.Build.props @@ -15,7 +15,7 @@ disable enable false - $(NoWarn);701;1702;CA1845;MA0003;MA0004;MA0042;MA0076;xUnit1044;xUnit1045;NU1603 + $(NoWarn);701;1702;CA1845;MA0003;MA0004;MA0018;MA0020;MA0042;MA0076;xUnit1044;xUnit1045;NU1603 diff --git a/Tests/Helpers/Testably.Abstractions.TestHelpers/AssertionHelpers.cs b/Tests/Helpers/Testably.Abstractions.TestHelpers/AssertionHelpers.cs index 06cdd819e..a74503998 100644 --- a/Tests/Helpers/Testably.Abstractions.TestHelpers/AssertionHelpers.cs +++ b/Tests/Helpers/Testably.Abstractions.TestHelpers/AssertionHelpers.cs @@ -124,7 +124,8 @@ private static void AssertExceptionMessage(TException exception, { if (messageContains != null) { - #pragma warning disable MA0074 + #pragma warning disable MA0074 // Avoid implicit culture-sensitive methods + #pragma warning disable MA0001 // Use an overload of 'Contains' that has a StringComparison parameter Execute.Assertion .ForCondition(exception.Message.Contains(messageContains)) .BecauseOf(because, becauseArgs) @@ -133,6 +134,7 @@ private static void AssertExceptionMessage(TException exception, "Expected {context} to have a message containing {0}{reason}, but found {1}.", messageContains, exception.Message); + #pragma warning restore MA0001 #pragma warning restore MA0074 } } diff --git a/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs b/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs index a8946ba92..75bc2cb62 100644 --- a/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs +++ b/Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs @@ -264,7 +264,7 @@ public override void SkipIfLongRunningTestsShouldBeSkipped() } } - private bool IncludeSimulatedTests(ClassModel @class) + private static bool IncludeSimulatedTests(ClassModel @class) { return !@class.Namespace.Equals( "Testably.Abstractions.AccessControl.Tests", StringComparison.Ordinal); diff --git a/Tests/Testably.Abstractions.Testing.Tests/FileSystem/DriveInfoMockTests.cs b/Tests/Testably.Abstractions.Testing.Tests/FileSystem/DriveInfoMockTests.cs index 8faefea32..0b45e3984 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/FileSystem/DriveInfoMockTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/FileSystem/DriveInfoMockTests.cs @@ -138,7 +138,7 @@ public void New_DriveNameWithUncPath_ShouldUseTopMostDirectory( expectedName = expectedName .Replace('/', FileSystem.Path.DirectorySeparatorChar); - IDriveInfo drive = + DriveInfoMock drive = DriveInfoMock.New(driveName, FileSystem); drive.Name.Should().Be(expectedName); diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/FileStreamFactory/Tests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/FileStreamFactory/Tests.cs index 0233ae24b..0bcb0bfae 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/FileStreamFactory/Tests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/FileStreamFactory/Tests.cs @@ -18,7 +18,7 @@ public void New_AppendAccessWithReadWriteMode_ShouldThrowArgumentException( }); exception.Should().BeException( - messageContains: FileMode.Append.ToString(), + messageContains: nameof(FileMode.Append), hResult: -2147024809, paramName: Test.IsNetFramework ? null : "access"); } diff --git a/Tests/Testably.Abstractions.Tests/RandomSystem/RandomTests.cs b/Tests/Testably.Abstractions.Tests/RandomSystem/RandomTests.cs index c0f37514d..49e7e59cc 100644 --- a/Tests/Testably.Abstractions.Tests/RandomSystem/RandomTests.cs +++ b/Tests/Testably.Abstractions.Tests/RandomSystem/RandomTests.cs @@ -320,7 +320,7 @@ public void Shuffle_Array_ShouldShuffleItemsInPlace() values.Should().OnlyHaveUniqueItems(); values.Should().NotContainInOrder(originalValues); - values.OrderBy(x => x).Should().ContainInOrder(originalValues); + values.Order().Should().ContainInOrder(originalValues); } #endif @@ -337,7 +337,7 @@ public void Shuffle_Span_ShouldShuffleItemsInPlace() int[] result = values.ToArray(); result.Should().OnlyHaveUniqueItems(); result.Should().NotContainInOrder(originalValues); - result.OrderBy(x => x).Should().ContainInOrder(originalValues); + result.Order().Should().ContainInOrder(originalValues); } #endif } diff --git a/Tests/Testably.Abstractions.Tests/TimeSystem/DateTimeTests.cs b/Tests/Testably.Abstractions.Tests/TimeSystem/DateTimeTests.cs index 0193e5547..153d6e307 100644 --- a/Tests/Testably.Abstractions.Tests/TimeSystem/DateTimeTests.cs +++ b/Tests/Testably.Abstractions.Tests/TimeSystem/DateTimeTests.cs @@ -55,7 +55,9 @@ public void Today_ShouldBeSetToToday() [SkippableFact] public void UnixEpoch_ShouldReturnDefaultValue() { + #pragma warning disable MA0113 // Use DateTime.UnixEpoch DateTime expectedResult = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + #pragma warning restore MA0113 DateTime result = TimeSystem.DateTime.UnixEpoch; From 5d24d7370586996bb439e4ae0c315bb2b92f2c5a Mon Sep 17 00:00:00 2001 From: Valentin Date: Sun, 12 Jan 2025 19:50:48 +0100 Subject: [PATCH 2/4] Fix methods in FileSystemRegistration that can be made static --- .../Helpers/FileSystemExtensions.cs | 2 +- .../Helpers/FileSystemRegistration.cs | 4 ++-- Source/Testably.Abstractions.Testing/MockFileSystem.cs | 2 +- .../Storage/InMemoryContainer.cs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs b/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs index 3fc68f8d9..f3f189cfe 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs @@ -129,7 +129,7 @@ internal static IDisposable IgnoreStatistics(this IFileSystem fileSystem) { if (fileSystem is MockFileSystem mockFileSystem) { - return mockFileSystem.Registration.Ignore(); + return FileSystemRegistration.Ignore(); } return new NoOpDisposable(); diff --git a/Source/Testably.Abstractions.Testing/Helpers/FileSystemRegistration.cs b/Source/Testably.Abstractions.Testing/Helpers/FileSystemRegistration.cs index fc6b95611..7a4f8f5a9 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/FileSystemRegistration.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/FileSystemRegistration.cs @@ -43,7 +43,7 @@ public bool TryGetLock(out IDisposable release) /// /// Ignores all registrations until the return value is disposed. /// - internal IDisposable Ignore() + internal static IDisposable Ignore() { if (IsDisabled.Value) { @@ -59,7 +59,7 @@ internal IDisposable Ignore() }); } - internal bool IsInitializing() + internal static bool IsInitializing() => IsInit.Value; private sealed class TemporaryDisable : IDisposable diff --git a/Source/Testably.Abstractions.Testing/MockFileSystem.cs b/Source/Testably.Abstractions.Testing/MockFileSystem.cs index e7dfcb181..20a838fba 100644 --- a/Source/Testably.Abstractions.Testing/MockFileSystem.cs +++ b/Source/Testably.Abstractions.Testing/MockFileSystem.cs @@ -125,7 +125,7 @@ public MockFileSystem(Func options #endif Registration = new FileSystemRegistration(); StatisticsRegistration = new FileSystemStatistics(this); - using IDisposable release = Registration.Ignore(); + using IDisposable release = FileSystemRegistration.Ignore(); RandomSystem = new MockRandomSystem(initialization.RandomProvider ?? RandomProvider.Default()); TimeSystem = new MockTimeSystem(TimeProvider.Now()); diff --git a/Source/Testably.Abstractions.Testing/Storage/InMemoryContainer.cs b/Source/Testably.Abstractions.Testing/Storage/InMemoryContainer.cs index 2bd1bb7f0..cbe4c1f6b 100644 --- a/Source/Testably.Abstractions.Testing/Storage/InMemoryContainer.cs +++ b/Source/Testably.Abstractions.Testing/Storage/InMemoryContainer.cs @@ -155,7 +155,7 @@ public IStorageAccessHandle RequestAccess(FileAccess access, FileShare share, bool ignoreMetadataErrors = true, int? hResult = null) { - if (_fileSystem.Registration.IsInitializing()) + if (FileSystemRegistration.IsInitializing()) { return FileHandle.Ignore; } From 40ddfe6eab8f3d86f6cbac11151af4310198fa21 Mon Sep 17 00:00:00 2001 From: Valentin Date: Sun, 12 Jan 2025 20:28:19 +0100 Subject: [PATCH 3/4] Fix sonar issue --- Pipeline/Build.UnitTest.cs | 4 ++-- .../Helpers/FileSystemExtensions.cs | 9 +-------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/Pipeline/Build.UnitTest.cs b/Pipeline/Build.UnitTest.cs index 50e6e7dce..48e503ebe 100644 --- a/Pipeline/Build.UnitTest.cs +++ b/Pipeline/Build.UnitTest.cs @@ -47,7 +47,7 @@ partial class Build { if (retry == 0) { - Log.Error($"All {MaxRetries} tries failed: {ex}"); + Log.Error($"All {MaxRetries + 1} tries failed: {ex}"); throw; } @@ -91,7 +91,7 @@ partial class Build { if (retry == 0) { - Log.Error($"All {MaxRetries} tries failed: {ex}"); + Log.Error($"All {MaxRetries + 1} tries failed: {ex}"); throw; } diff --git a/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs b/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs index f3f189cfe..bbc1c8b18 100644 --- a/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs +++ b/Source/Testably.Abstractions.Testing/Helpers/FileSystemExtensions.cs @@ -126,14 +126,7 @@ internal static string GetSubdirectoryPath(this MockFileSystem fileSystem, /// Ignores all registrations on the until the return value is disposed. /// internal static IDisposable IgnoreStatistics(this IFileSystem fileSystem) - { - if (fileSystem is MockFileSystem mockFileSystem) - { - return FileSystemRegistration.Ignore(); - } - - return new NoOpDisposable(); - } + => FileSystemRegistration.Ignore(); /// /// Returns the shared instance from the , if it is a From b226720e9c7c418fff2703b32d9b3b68a71b4ee1 Mon Sep 17 00:00:00 2001 From: Valentin Date: Mon, 13 Jan 2025 11:45:30 +0100 Subject: [PATCH 4/4] Make TimerTests less brittle --- Tests/Testably.Abstractions.Tests/TimeSystem/TimerTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Tests/Testably.Abstractions.Tests/TimeSystem/TimerTests.cs b/Tests/Testably.Abstractions.Tests/TimeSystem/TimerTests.cs index e889787d9..ba9cae9de 100644 --- a/Tests/Testably.Abstractions.Tests/TimeSystem/TimerTests.cs +++ b/Tests/Testably.Abstractions.Tests/TimeSystem/TimerTests.cs @@ -229,7 +229,6 @@ public void Change_WithInt_ShouldResetTimer() } triggerTimes[0].Should() - .BeGreaterThan(70 * TimerMultiplier).And .BeLessThan(130 * TimerMultiplier); for (int i = 1; i < triggerTimes.Count; i++) { @@ -304,7 +303,6 @@ public void Change_WithLong_ShouldResetTimer() } triggerTimes[0].Should() - .BeGreaterThan(70 * TimerMultiplier).And .BeLessThan(130 * TimerMultiplier); for (int i = 1; i < triggerTimes.Count; i++) { @@ -381,7 +379,6 @@ public void Change_WithTimeSpan_ShouldResetTimer() } triggerTimes[0].Should() - .BeGreaterThan(70 * TimerMultiplier).And .BeLessThan(130 * TimerMultiplier); for (int i = 1; i < triggerTimes.Count; i++) {