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

fix: sonar issues after activating automatic analysis #495

Merged
merged 4 commits into from
Mar 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal IDisposable RegisterProperty(string path, string name, PropertyAccess a
return callStatistics.RegisterProperty(name, access);
}

private string CreateKey(string currentDirectory, string path)
private static string CreateKey(string currentDirectory, string path)
{
if (string.IsNullOrEmpty(path))
{
Expand All @@ -61,10 +61,10 @@ private string CreateKey(string currentDirectory, string path)

if (Path.IsPathRooted(path))
{
return path.TrimEnd([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]);
return path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}

return Path.GetFullPath(Path.Combine(currentDirectory, path))
.TrimEnd([Path.DirectorySeparatorChar , Path.AltDirectorySeparatorChar]);
.TrimEnd(Path.DirectorySeparatorChar , Path.AltDirectorySeparatorChar);
}
}
58 changes: 29 additions & 29 deletions Source/Testably.Abstractions.Testing/TimeSystem/TimerMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,6 @@ internal TimerMock(MockTimeSystem timeSystem,
public ITimeSystem TimeSystem
=> _mockTimeSystem;

#if FEATURE_ASYNC_DISPOSABLE
/// <inheritdoc cref="IAsyncDisposable.DisposeAsync()" />
public ValueTask DisposeAsync()
{
Dispose();
#if NETSTANDARD2_1
return new ValueTask();
#else
return ValueTask.CompletedTask;
#endif
}
#endif

/// <inheritdoc cref="IDisposable.Dispose()" />
public void Dispose()
{
if (!_isDisposed)
{
_isDisposed = true;
Stop();
_onDispose?.Invoke();
lock (_lock)
{
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
}
}
}

/// <inheritdoc cref="ITimer.Change(int, int)" />
public bool Change(int dueTime, int period)
=> Change(TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period));
Expand Down Expand Up @@ -131,6 +102,22 @@ public bool Change(TimeSpan dueTime, TimeSpan period)
}
}

/// <inheritdoc cref="IDisposable.Dispose()" />
public void Dispose()
{
if (!_isDisposed)
{
_isDisposed = true;
Stop();
_onDispose?.Invoke();
lock (_lock)
{
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
}
}
}

/// <inheritdoc cref="ITimer.Dispose(WaitHandle)" />
public bool Dispose(WaitHandle notifyObject)
{
Expand Down Expand Up @@ -162,6 +149,19 @@ public bool Dispose(WaitHandle notifyObject)
return true;
}

#if FEATURE_ASYNC_DISPOSABLE
/// <inheritdoc cref="IAsyncDisposable.DisposeAsync()" />
public ValueTask DisposeAsync()
{
Dispose();
#if NETSTANDARD2_1
return new ValueTask();
#else
return ValueTask.CompletedTask;
#endif
}
#endif

/// <inheritdoc cref="ITimerMock.Wait(int, int, Action{ITimerMock})" />
public ITimerMock Wait(
int executionCount = 1,
Expand Down
7 changes: 5 additions & 2 deletions Tests/Api/Testably.Abstractions.Api.Tests/ApiAcceptance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ public sealed class ApiAcceptance
public void AcceptApiChanges()
{
string[] assemblyNames =
{
[
"Testably.Abstractions",
"Testably.Abstractions.AccessControl",
"Testably.Abstractions.Compression",
"Testably.Abstractions.Interface",
"Testably.Abstractions.Testing"
};
];

foreach (string assemblyName in assemblyNames)
{
foreach (string framework in Helper.GetTargetFrameworks())
Expand All @@ -29,5 +30,7 @@ public void AcceptApiChanges()
Helper.SetExpectedApi(framework, assemblyName, publicApi);
}
}

Assert.That(assemblyNames, Is.Not.Empty);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void VerifyPublicApiForTestablyAbstractionsTesting(string framework)
Assert.That(publicApi, Is.EqualTo(expectedApi));
}

private class TargetFrameworksTheoryData : IEnumerable
private sealed class TargetFrameworksTheoryData : IEnumerable
{
#region IEnumerable Members

Expand Down
114 changes: 67 additions & 47 deletions Tests/Helpers/Testably.Abstractions.TestHelpers/AssertionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,54 +81,10 @@ public static AndWhichConstraint<ObjectAssertions, TException>
if (objectAssertions.Subject is TException exception)
{
typedSubject = exception;
if (messageContains != null)
{
Execute.Assertion
.ForCondition(exception.Message.Contains(messageContains))
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith(
"Expected {context} to have a message containing {0}{reason}, but found {1}.",
messageContains,
exception.Message);
}

if (hResult != null)
{
Execute.Assertion
.ForCondition(exception.HResult == hResult)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith(
"Expected {context} to have HResult set to {0}{reason}, but found {1}.",
hResult,
exception.HResult);
}

if (paramName != null)
{
if (exception is ArgumentException argumentException)
{
Execute.Assertion
.ForCondition(argumentException.ParamName == paramName)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith(
"Expected {context} to have ParamName set to {0}{reason}, but found {1}.",
paramName,
argumentException.ParamName);
}
else
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith(
"Expected {context} to be {0} with ParamName set to {0}{reason}, but it is no ArgumentException.",
typeof(TException),
paramName);
}
}
AssertExceptionMessage(exception, messageContains, because, becauseArgs);
AssertExceptionHResult(exception, hResult, because, becauseArgs);
AssertExceptionParamName(exception, paramName, because, becauseArgs);
}
else
{
Expand All @@ -144,4 +100,68 @@ public static AndWhichConstraint<ObjectAssertions, TException>
return new AndWhichConstraint<ObjectAssertions, TException>(objectAssertions,
typedSubject!);
}

private static void AssertExceptionHResult<TException>(TException exception,
int? hResult,
string because, object[] becauseArgs) where TException : Exception
{
if (hResult != null)
{
Execute.Assertion
.ForCondition(exception.HResult == hResult)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith(
"Expected {context} to have HResult set to {0}{reason}, but found {1}.",
hResult,
exception.HResult);
}
}

private static void AssertExceptionMessage<TException>(TException exception,
string? messageContains,
string because, object[] becauseArgs) where TException : Exception
{
if (messageContains != null)
{
Execute.Assertion
.ForCondition(exception.Message.Contains(messageContains))
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith(
"Expected {context} to have a message containing {0}{reason}, but found {1}.",
messageContains,
exception.Message);
}
}

private static void AssertExceptionParamName<TException>(TException exception,
string? paramName,
string because, object[] becauseArgs) where TException : Exception
{
if (paramName != null)
{
if (exception is ArgumentException argumentException)
{
Execute.Assertion
.ForCondition(argumentException.ParamName == paramName)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith(
"Expected {context} to have ParamName set to {0}{reason}, but found {1}.",
paramName,
argumentException.ParamName);
}
else
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith(
"Expected {context} to be {0} with ParamName set to {0}{reason}, but it is no ArgumentException.",
typeof(TException),
paramName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Testably.Abstractions.Tests.SourceGenerator.Model;

Expand Down Expand Up @@ -33,19 +34,19 @@ protected abstract void GenerateSource(
private string CreateFileName(ClassModel classToGenerate)
{
string? fileNamePrefix = ExtractFileNamePrefixFromMarker(Marker);
List<string> namespacePrefixes = new()
{
List<string> namespacePrefixes =
[
"Testably.Abstractions.Tests.FileSystem.",
"Testably.Abstractions."
};
foreach (string? namespacePrefix in namespacePrefixes)
];

string? namespacePrefix = namespacePrefixes
.FirstOrDefault(classToGenerate.Namespace.StartsWith);
if (namespacePrefix != null)
{
if (classToGenerate.Namespace.StartsWith(namespacePrefix))
{
string? @namespace = classToGenerate.Namespace
.Substring(namespacePrefix.Length);
return $"{fileNamePrefix}{@namespace}.{classToGenerate.Name}.cs";
}
string? @namespace = classToGenerate.Namespace
.Substring(namespacePrefix.Length);
return $"{fileNamePrefix}{@namespace}.{classToGenerate.Name}.cs";
}

return $"{fileNamePrefix}{classToGenerate.Name}.cs";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;

Expand All @@ -16,7 +17,7 @@ public abstract partial class ExtensionTests<TFileSystem>
public void CreateEntryFromFile_LastWriteTime_ShouldBeCopiedFromFile(
string lastWriteTimeString)
{
DateTime lastWriteTime = DateTime.Parse(lastWriteTimeString);
DateTime lastWriteTime = DateTime.Parse(lastWriteTimeString, CultureInfo.InvariantCulture);
FileSystem.Initialize()
.WithSubdirectory("foo")
.WithSubdirectory("bar");
Expand Down Expand Up @@ -46,7 +47,8 @@ public void CreateEntryFromFile_LastWriteTimeOutOfRange_ShouldBeFirstJanuary1980
string lastWriteTimeString)
{
DateTime expectedTime = new(1980, 1, 1, 0, 0, 0);
DateTime lastWriteTime = DateTime.Parse(lastWriteTimeString).ToUniversalTime();
DateTime lastWriteTime = DateTime.Parse(lastWriteTimeString, CultureInfo.InvariantCulture)
.ToUniversalTime();
FileSystem.Initialize()
.WithSubdirectory("foo")
.WithSubdirectory("bar");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;

Expand Down Expand Up @@ -112,7 +113,7 @@ public void ExtractToFile_WithOverwrite_ShouldOverwriteExistingFile()
[InlineData("2107-12-31T23:59:59")]
public void ExtractToFile_LastWriteTime_ShouldBeCopiedFromFile(string lastWriteTimeString)
{
DateTime lastWriteTime = DateTime.Parse(lastWriteTimeString);
DateTime lastWriteTime = DateTime.Parse(lastWriteTimeString, CultureInfo.InvariantCulture);
FileSystem.Initialize()
.WithSubdirectory("foo")
.WithSubdirectory("bar").Initialized(s => s
Expand Down
Loading
Loading