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

Enable Microsoft.CodeAnalysis.NetAnalyzers #201

Merged
merged 8 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
10 changes: 10 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@
<LangVersion>latest</LangVersion>
<Nullable>Enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<AnalysisLevel>8</AnalysisLevel>
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
<RunAnalyzersDuringLiveAnalysis>true</RunAnalyzersDuringLiveAnalysis>
<_SkipUpgradeNetAnalyzersNuGetWarning>true</_SkipUpgradeNetAnalyzersNuGetWarning>
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
</PropertyGroup>
<PropertyGroup Label="Package Versions">
<SystemConfigurationConfigurationManagerPackageVersion>4.5.0</SystemConfigurationConfigurationManagerPackageVersion>
<MicrosoftSourceLinkGitHubPackageVersion>8.0.0</MicrosoftSourceLinkGitHubPackageVersion>
<!-- Analyzer packages -->
<MicrosoftNetAnalyzersPackageVersion>8.0.0</MicrosoftNetAnalyzersPackageVersion>
<!-- Test Packages -->
<MicrosoftNetTestSdkPackageVersion>17.11.1</MicrosoftNetTestSdkPackageVersion>
<NUnitAnalyzersPackageVersion>4.3.0</NUnitAnalyzersPackageVersion>
<NUnitPackageVersion>4.2.2</NUnitPackageVersion>
<NUnit3TestAdapterPackageVersion>4.6.0</NUnit3TestAdapterPackageVersion>
<QuackersTestLoggerPackageVersion>1.0.25</QuackersTestLoggerPackageVersion>
</PropertyGroup>
<ItemGroup>
<GlobalAnalyzerConfigFiles Include="$(MSBuildThisFileDirectory)\log4net.globalconfig" />
</ItemGroup>
</Project>
27 changes: 6 additions & 21 deletions src/log4net.Tests/Appender/AdoNet/Log4NetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace log4net.Tests.Appender.AdoNet;

public class Log4NetCommand : IDbCommand
internal sealed class Log4NetCommand : IDbCommand
FreeAndNil marked this conversation as resolved.
Show resolved Hide resolved
{
public Log4NetCommand()
{
Expand All @@ -48,10 +48,7 @@ public int ExecuteNonQuery()

public int ExecuteNonQueryCount { get; private set; }

public IDbDataParameter CreateParameter()
{
return new Log4NetParameter();
}
public IDbDataParameter CreateParameter() => new Log4NetParameter();

#pragma warning disable CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes).
public string? CommandText { get; set; }
Expand All @@ -68,25 +65,13 @@ public void Prepare()

public static Log4NetCommand? MostRecentInstance { get; private set; }

public void Cancel()
{
throw new NotImplementedException();
}
public void Cancel() => throw new NotImplementedException();

public IDataReader ExecuteReader()
{
throw new NotImplementedException();
}
public IDataReader ExecuteReader() => throw new NotImplementedException();

public IDataReader ExecuteReader(CommandBehavior behavior)
{
throw new NotImplementedException();
}
public IDataReader ExecuteReader(CommandBehavior behavior) => throw new NotImplementedException();

public object ExecuteScalar()
{
throw new NotImplementedException();
}
public object ExecuteScalar() => throw new NotImplementedException();

public IDbConnection? Connection
{
Expand Down
44 changes: 11 additions & 33 deletions src/log4net.Tests/Appender/AdoNet/Log4NetConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,40 @@

using System;
using System.Data;
using System.Diagnostics.CodeAnalysis;

namespace log4net.Tests.Appender.AdoNet;

public class Log4NetConnection : IDbConnection
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Reflection")]
internal sealed class Log4NetConnection : IDbConnection
{
private bool _open;

public Log4NetConnection()
{
MostRecentInstance = this;
}
public Log4NetConnection() => MostRecentInstance = this;

public void Close()
{
_open = false;
}
public void Close() => _open = false;

public ConnectionState State => _open ? ConnectionState.Open : ConnectionState.Closed;

#pragma warning disable CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes).
public string? ConnectionString { get; set; }
#pragma warning restore CS8766

public IDbTransaction BeginTransaction()
{
return new Log4NetTransaction();
}
public IDbTransaction BeginTransaction() => new Log4NetTransaction();

public IDbCommand CreateCommand()
{
return new Log4NetCommand();
}
public IDbCommand CreateCommand() => new Log4NetCommand();

public void Open()
{
_open = true;
}
public void Open() => _open = true;

public static Log4NetConnection? MostRecentInstance { get; private set; }

public IDbTransaction BeginTransaction(IsolationLevel il)
{
throw new NotImplementedException();
}
public IDbTransaction BeginTransaction(IsolationLevel il) => throw new NotImplementedException();

public void ChangeDatabase(string databaseName)
{
throw new NotImplementedException();
}
public void ChangeDatabase(string databaseName) => throw new NotImplementedException();

public int ConnectionTimeout => throw new NotImplementedException();

public string Database => throw new NotImplementedException();

public void Dispose()
{
throw new NotImplementedException();
}
public void Dispose() => throw new NotImplementedException();
}
12 changes: 3 additions & 9 deletions src/log4net.Tests/Appender/AdoNet/Log4NetParameterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace log4net.Tests.Appender.AdoNet;

public class Log4NetParameterCollection : CollectionBase, IDataParameterCollection
internal sealed class Log4NetParameterCollection : CollectionBase, IDataParameterCollection
{
private readonly Dictionary<string, int> _parameterNameToIndex = new(StringComparer.Ordinal);

Expand Down Expand Up @@ -55,13 +55,7 @@ public object this[string parameterName]
set => InnerList[IndexOf(parameterName)] = value;
}

public void RemoveAt(string parameterName)
{
throw new NotImplementedException();
}
public void RemoveAt(string parameterName) => throw new NotImplementedException();

public bool Contains(string parameterName)
{
throw new NotImplementedException();
}
public bool Contains(string parameterName) => throw new NotImplementedException();
}
7 changes: 2 additions & 5 deletions src/log4net.Tests/Appender/AdoNet/Log4NetTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace log4net.Tests.Appender.AdoNet;

public class Log4NetTransaction : IDbTransaction
internal sealed class Log4NetTransaction : IDbTransaction
{
public void Commit()
{
Expand All @@ -40,8 +40,5 @@ public void Rollback()

public IsolationLevel IsolationLevel => throw new NotImplementedException();

public void Dispose()
{
throw new NotImplementedException();
}
public void Dispose() => throw new NotImplementedException();
}
14 changes: 4 additions & 10 deletions src/log4net.Tests/Appender/CountingAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,11 @@ public class CountingAppender : AppenderSkeleton
/// <summary>
/// Reset the counter to zero
/// </summary>
public void ResetCounter()
{
Counter = 0;
}
public void ResetCounter() => Counter = 0;

/// <summary>
/// Registers how many times the method has been called.
/// </summary>
/// <param name="logEvent">The logging event.</param>
protected override void Append(LoggingEvent logEvent)
{
Counter++;
}
}
/// <param name="loggingEvent">The logging event.</param>
protected override void Append(LoggingEvent loggingEvent) => Counter++;
}
28 changes: 6 additions & 22 deletions src/log4net.Tests/Appender/EventRaisingAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,9 @@ namespace log4net.Tests.Appender;
/// <summary>
/// Provides data for the <see cref="EventRaisingAppender.LoggingEventAppended"/> event.
/// </summary>
/// <seealso cref="System.EventArgs" />
public class LoggingEventEventArgs : EventArgs
public class LoggingEventEventArgs(LoggingEvent loggingEvent) : EventArgs
{
public LoggingEvent LoggingEvent { get; private set; }

public LoggingEventEventArgs(LoggingEvent loggingEvent)
{
if (loggingEvent is null)
{
throw new ArgumentNullException(nameof(loggingEvent));
}

LoggingEvent = loggingEvent;
}
public LoggingEvent LoggingEvent { get; } = loggingEvent ?? throw new ArgumentNullException(nameof(loggingEvent));
}

/// <summary>
Expand All @@ -55,18 +44,13 @@ public class EventRaisingAppender : log4net.Appender.IAppender
public event EventHandler<LoggingEventEventArgs>? LoggingEventAppended;

protected void OnLoggingEventAppended(LoggingEventEventArgs e)
{
LoggingEventAppended?.Invoke(this, e);
}
=> LoggingEventAppended?.Invoke(this, e);

public void Close()
{
}
{ }

public void DoAppend(LoggingEvent loggingEvent)
{
OnLoggingEventAppended(new LoggingEventEventArgs(loggingEvent));
}
=> OnLoggingEventAppended(new LoggingEventEventArgs(loggingEvent));

public string Name { get; set; } = string.Empty;
}
}
36 changes: 17 additions & 19 deletions src/log4net.Tests/Appender/RollingFileAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private static void CreateFile(int iFileNumber)
{
fileStream.Close();
}
catch
catch (Exception e) when (e is not null)
{
// Ignore
}
Expand Down Expand Up @@ -250,9 +250,9 @@ private static void DeleteTestFiles()
Debug.WriteLine("Deleting test file " + sFile);
File.Delete(sFile);
}
catch (Exception ex)
catch (Exception e) when (e is not null)
{
Debug.WriteLine("Exception while deleting test file " + ex);
Debug.WriteLine("Exception while deleting test file " + e);
}
}
}
Expand Down Expand Up @@ -359,19 +359,19 @@ public RollFileEntry(string fileName, long fileLength)
/// A table of entries showing files that should exist and their expected sizes
/// after a message is logged
/// </param>
public sealed class RollConditions(List<RollFileEntry> preLogFileEntries, List<RollFileEntry> postLogFileEntries)
public sealed class RollConditions(IList<RollFileEntry> preLogFileEntries, IList<RollFileEntry> postLogFileEntries)
{
/// <summary>
/// A table of entries showing files that should exist and their expected sizes
/// before logging is called
/// </summary>
public List<RollFileEntry> GetPreLogFileEntries() => preLogFileEntries;
public IList<RollFileEntry> PreLogFileEntries => preLogFileEntries;

/// <summary>
/// A table of entries showing files that should exist and their expected sizes
/// after a message is logged
/// </summary>
public List<RollFileEntry> GetPostLogFileEntries() => postLogFileEntries;
public IList<RollFileEntry> PostLogFileEntries => postLogFileEntries;
}

private static void VerifyExistenceAndRemoveFromList(List<string> alExisting,
Expand All @@ -389,7 +389,7 @@ private static void VerifyExistenceAndRemoveFromList(List<string> alExisting,
/// </summary>
/// <param name="sBaseFileName"></param>
/// <param name="fileEntries"></param>
private static void VerifyFileConditions(string sBaseFileName, List<RollFileEntry> fileEntries)
private static void VerifyFileConditions(string sBaseFileName, IList<RollFileEntry> fileEntries)
{
List<string> alExisting = GetExistingFiles(sBaseFileName);

Expand Down Expand Up @@ -429,7 +429,7 @@ private static void VerifyFileConditions(string sBaseFileName, List<RollFileEntr
/// <param name="sBaseFileName"></param>
/// <param name="entry"></param>
private static void VerifyPreConditions(string sBaseFileName, RollConditions entry)
=> VerifyFileConditions(sBaseFileName, entry.GetPreLogFileEntries());
=> VerifyFileConditions(sBaseFileName, entry.PreLogFileEntries);

/// <summary>
/// Called after logging a message to check that all the expected files exist,
Expand All @@ -439,7 +439,7 @@ private static void VerifyPreConditions(string sBaseFileName, RollConditions ent
/// <param name="sBaseFileName"></param>
/// <param name="entry"></param>
private static void VerifyPostConditions(string sBaseFileName, RollConditions entry)
=> VerifyFileConditions(sBaseFileName, entry.GetPostLogFileEntries());
=> VerifyFileConditions(sBaseFileName, entry.PostLogFileEntries);

/// <summary>
/// Logs a message, verifying the expected message counts against the
Expand Down Expand Up @@ -672,7 +672,7 @@ private static RollConditions BuildTableEntry(string sBackupFiles,
return new RollConditions(AddFinalElement(null, current), post);
}

return new RollConditions(preCondition.GetPostLogFileEntries(), post);
return new RollConditions(preCondition.PostLogFileEntries, post);
}

/// <summary>
Expand Down Expand Up @@ -1808,7 +1808,7 @@ private static List<string> GetExistingFiles(string baseFilePath, bool preserveL
{
2 => TestMessage98Chars,
1 => TestMessage99Chars,
_ => throw new Exception("Unexpected Environment.NewLine.Length"),
_ => throw new InvalidOperationException("Unexpected Environment.NewLine.Length"),
};
}

Expand All @@ -1818,15 +1818,13 @@ public sealed class RollingFileAppenderSubClassTest : RollingFileAppender
[Test]
public void TestComputeCheckPeriod()
{
RollingFileAppender rfa = new();

Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM-dd HH:mm"), Is.EqualTo(RollPoint.TopOfMinute), "TopOfMinute pattern");
Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM-dd HH"), Is.EqualTo(RollPoint.TopOfHour), "TopOfHour pattern");
Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM-dd tt"), Is.EqualTo(RollPoint.HalfDay), "HalfDay pattern");
Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM-dd"), Is.EqualTo(RollPoint.TopOfDay), "TopOfDay pattern");
Assert.That(rfa.ComputeCheckPeriod(".yyyy-MM"), Is.EqualTo(RollPoint.TopOfMonth), "TopOfMonth pattern");
Assert.That(ComputeCheckPeriod(".yyyy-MM-dd HH:mm"), Is.EqualTo(RollPoint.TopOfMinute), "TopOfMinute pattern");
Assert.That(ComputeCheckPeriod(".yyyy-MM-dd HH"), Is.EqualTo(RollPoint.TopOfHour), "TopOfHour pattern");
Assert.That(ComputeCheckPeriod(".yyyy-MM-dd tt"), Is.EqualTo(RollPoint.HalfDay), "HalfDay pattern");
Assert.That(ComputeCheckPeriod(".yyyy-MM-dd"), Is.EqualTo(RollPoint.TopOfDay), "TopOfDay pattern");
Assert.That(ComputeCheckPeriod(".yyyy-MM"), Is.EqualTo(RollPoint.TopOfMonth), "TopOfMonth pattern");

// Test invalid roll point
Assert.That(rfa.ComputeCheckPeriod("..."), Is.EqualTo(RollPoint.InvalidRollPoint), "TopOfMonth pattern");
Assert.That(ComputeCheckPeriod("..."), Is.EqualTo(RollPoint.InvalidRollPoint), "TopOfMonth pattern");
}
}
Loading