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 nullable reference types #222

Merged
merged 1 commit into from
Apr 28, 2021
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
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<NeutralLanguage>en-US</NeutralLanguage>
<NoWarn>$(NoWarn)</NoWarn>
<NoWarn Condition=" '$(GenerateDocumentationFile)' != 'true' ">$(NoWarn);SA0001</NoWarn>
<Nullable>enable</Nullable>
<PackageIcon></PackageIcon>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/martincostello/xunit-logging</PackageProjectUrl>
Expand Down
4 changes: 2 additions & 2 deletions src/Logging.XUnit/AmbientTestOutputHelperAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ internal sealed class AmbientTestOutputHelperAccessor : ITestOutputHelperAccesso
/// <summary>
/// A backing field for the <see cref="ITestOutputHelper"/> for the current thread.
/// </summary>
private static readonly AsyncLocal<ITestOutputHelper> _current = new AsyncLocal<ITestOutputHelper>();
private static readonly AsyncLocal<ITestOutputHelper?> _current = new AsyncLocal<ITestOutputHelper?>();

#pragma warning disable CA1822
/// <summary>
/// Gets or sets the current <see cref="ITestOutputHelper"/>.
/// </summary>
public ITestOutputHelper OutputHelper
public ITestOutputHelper? OutputHelper
{
get { return _current.Value; }
set { _current.Value = value; }
Expand Down
2 changes: 1 addition & 1 deletion src/Logging.XUnit/ITestOutputHelperAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface ITestOutputHelperAccessor
/// <summary>
/// Gets or sets the <see cref="ITestOutputHelper"/> to use.
/// </summary>
ITestOutputHelper OutputHelper { get; set; }
ITestOutputHelper? OutputHelper { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Logging.XUnit/TestOutputHelperAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ internal TestOutputHelperAccessor(ITestOutputHelper outputHelper)
/// <summary>
/// Gets or sets the current <see cref="ITestOutputHelper"/>.
/// </summary>
public ITestOutputHelper OutputHelper { get; set; }
public ITestOutputHelper? OutputHelper { get; set; }
}
}
8 changes: 4 additions & 4 deletions src/Logging.XUnit/XUnitLogScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class XUnitLogScope
/// <summary>
/// The scope for the current thread.
/// </summary>
private static readonly AsyncLocal<XUnitLogScope> _value = new AsyncLocal<XUnitLogScope>();
private static readonly AsyncLocal<XUnitLogScope?> _value = new AsyncLocal<XUnitLogScope?>();

/// <summary>
/// Initializes a new instance of the <see cref="XUnitLogScope"/> class.
Expand All @@ -33,7 +33,7 @@ internal XUnitLogScope(object state)
/// <summary>
/// Gets or sets the current scope.
/// </summary>
internal static XUnitLogScope Current
internal static XUnitLogScope? Current
{
get { return _value.Value; }
set { _value.Value = value; }
Expand All @@ -42,7 +42,7 @@ internal static XUnitLogScope Current
/// <summary>
/// Gets the parent scope.
/// </summary>
internal XUnitLogScope Parent { get; private set; }
internal XUnitLogScope? Parent { get; private set; }

/// <inheritdoc />
public override string ToString()
Expand Down Expand Up @@ -75,7 +75,7 @@ private sealed class DisposableScope : IDisposable
/// <inheritdoc />
public void Dispose()
{
Current = Current.Parent;
Current = Current?.Parent;
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/Logging.XUnit/XUnitLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class XUnitLogger : ILogger
/// The current builder to use to generate log messages.
/// </summary>
[ThreadStatic]
private static StringBuilder _logBuilder;
private static StringBuilder? _logBuilder;

/// <summary>
/// The <see cref="ITestOutputHelperAccessor"/> to use. This field is read-only.
Expand All @@ -47,7 +47,7 @@ public class XUnitLogger : ILogger
/// <summary>
/// Gets or sets the filter to use.
/// </summary>
private Func<string, LogLevel, bool> _filter;
private Func<string?, LogLevel, bool> _filter;

/// <summary>
/// Initializes a new instance of the <see cref="XUnitLogger"/> class.
Expand All @@ -58,7 +58,7 @@ public class XUnitLogger : ILogger
/// <exception cref="ArgumentNullException">
/// <paramref name="name"/> or <paramref name="outputHelper"/> is <see langword="null"/>.
/// </exception>
public XUnitLogger(string name, ITestOutputHelper outputHelper, XUnitLoggerOptions options)
public XUnitLogger(string name, ITestOutputHelper outputHelper, XUnitLoggerOptions? options)
: this(name, new TestOutputHelperAccessor(outputHelper), options)
{
}
Expand All @@ -72,12 +72,12 @@ public XUnitLogger(string name, ITestOutputHelper outputHelper, XUnitLoggerOptio
/// <exception cref="ArgumentNullException">
/// <paramref name="name"/> or <paramref name="accessor"/> is <see langword="null"/>.
/// </exception>
public XUnitLogger(string name, ITestOutputHelperAccessor accessor, XUnitLoggerOptions options)
public XUnitLogger(string name, ITestOutputHelperAccessor accessor, XUnitLoggerOptions? options)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
_accessor = accessor ?? throw new ArgumentNullException(nameof(accessor));

Filter = options?.Filter ?? ((category, logLevel) => true);
_filter = options?.Filter ?? ((category, logLevel) => true);
IncludeScopes = options?.IncludeScopes ?? false;
}

Expand All @@ -87,7 +87,7 @@ public XUnitLogger(string name, ITestOutputHelperAccessor accessor, XUnitLoggerO
/// <exception cref="ArgumentNullException">
/// <paramref name="value"/> is <see langword="null"/>.
/// </exception>
public Func<string, LogLevel, bool> Filter
public Func<string?, LogLevel, bool> Filter
{
get { return _filter; }
set { _filter = value ?? throw new ArgumentNullException(nameof(value)); }
Expand Down Expand Up @@ -131,7 +131,7 @@ public bool IsEnabled(LogLevel logLevel)
}

/// <inheritdoc />
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
public void Log<TState>(LogLevel logLevel, EventId eventId, TState? state, Exception? exception, Func<TState?, Exception?, string?> formatter)
{
if (!IsEnabled(logLevel))
{
Expand All @@ -143,7 +143,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
throw new ArgumentNullException(nameof(formatter));
}

string message = formatter(state, exception);
string? message = formatter(state, exception);

if (!string.IsNullOrEmpty(message) || exception != null)
{
Expand All @@ -158,16 +158,16 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
/// <param name="eventId">The Id of the event.</param>
/// <param name="message">The message to write.</param>
/// <param name="exception">The exception related to this message.</param>
public virtual void WriteMessage(LogLevel logLevel, int eventId, string message, Exception exception)
public virtual void WriteMessage(LogLevel logLevel, int eventId, string? message, Exception? exception)
{
ITestOutputHelper outputHelper = _accessor.OutputHelper;
ITestOutputHelper? outputHelper = _accessor.OutputHelper;

if (outputHelper == null)
{
return;
}

StringBuilder logBuilder = _logBuilder;
StringBuilder? logBuilder = _logBuilder;
_logBuilder = null;

if (logBuilder == null)
Expand Down Expand Up @@ -196,7 +196,7 @@ public virtual void WriteMessage(LogLevel logLevel, int eventId, string message,

int length = logBuilder.Length;
logBuilder.Append(message);
logBuilder.Replace(Environment.NewLine, NewLineWithMessagePadding, length, message.Length);
logBuilder.Replace(Environment.NewLine, NewLineWithMessagePadding, length, message!.Length);
}

if (exception != null)
Expand Down
2 changes: 1 addition & 1 deletion src/Logging.XUnit/XUnitLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHe
/// <exception cref="ArgumentNullException">
/// <paramref name="factory"/>, <paramref name="outputHelper"/> or <paramref name="filter"/> is <see langword="null"/>.
/// </exception>
public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHelper outputHelper, Func<string, LogLevel, bool> filter)
public static ILoggerFactory AddXUnit(this ILoggerFactory factory, ITestOutputHelper outputHelper, Func<string?, LogLevel, bool> filter)
{
if (factory == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Logging.XUnit/XUnitLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public XUnitLoggerOptions()
/// <summary>
/// Gets or sets the category filter to apply to logs.
/// </summary>
public Func<string, LogLevel, bool> Filter { get; set; } = (c, l) => true; // By default log everything
public Func<string?, LogLevel, bool> Filter { get; set; } = (c, l) => true; // By default log everything

/// <summary>
/// Gets or sets a value indicating whether to include scopes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public HttpServerFixture()
}

/// <inheritdoc />
public ITestOutputHelper OutputHelper { get; set; }
public ITestOutputHelper? OutputHelper { get; set; }

/// <inheritdoc />
protected override void ConfigureWebHost(IWebHostBuilder builder)
Expand Down
60 changes: 30 additions & 30 deletions tests/Logging.XUnit.Tests/XUnitLoggerExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public static void AddXUnit_For_ILoggerBuilder_Validates_Parameters()
var accessor = Mock.Of<ITestOutputHelperAccessor>();

// Act and Assert
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder).AddXUnit());
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder).AddXUnit(outputHelper));
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder).AddXUnit(outputHelper, ConfigureAction));
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder).AddXUnit(accessor));
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder).AddXUnit(accessor, ConfigureAction));
Assert.Throws<ArgumentNullException>("accessor", () => builder.AddXUnit(null as ITestOutputHelperAccessor));
Assert.Throws<ArgumentNullException>("accessor", () => builder.AddXUnit(null as ITestOutputHelperAccessor, ConfigureAction));
Assert.Throws<ArgumentNullException>("outputHelper", () => builder.AddXUnit(null as ITestOutputHelper));
Assert.Throws<ArgumentNullException>("outputHelper", () => builder.AddXUnit(null as ITestOutputHelper, ConfigureAction));
Assert.Throws<ArgumentNullException>("configure", () => builder.AddXUnit(outputHelper, null as Action<XUnitLoggerOptions>));
Assert.Throws<ArgumentNullException>("configure", () => builder.AddXUnit(accessor, null as Action<XUnitLoggerOptions>));
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder) !.AddXUnit());
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder) !.AddXUnit(outputHelper));
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder) !.AddXUnit(outputHelper, ConfigureAction));
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder) !.AddXUnit(accessor));
Assert.Throws<ArgumentNullException>("builder", () => (null as ILoggingBuilder) !.AddXUnit(accessor, ConfigureAction));
Assert.Throws<ArgumentNullException>("accessor", () => builder.AddXUnit((null as ITestOutputHelperAccessor) !));
Assert.Throws<ArgumentNullException>("accessor", () => builder.AddXUnit((null as ITestOutputHelperAccessor) !, ConfigureAction));
Assert.Throws<ArgumentNullException>("outputHelper", () => builder.AddXUnit((null as ITestOutputHelper) !));
Assert.Throws<ArgumentNullException>("outputHelper", () => builder.AddXUnit((null as ITestOutputHelper) !, ConfigureAction));
Assert.Throws<ArgumentNullException>("configure", () => builder.AddXUnit(outputHelper, (null as Action<XUnitLoggerOptions>) !));
Assert.Throws<ArgumentNullException>("configure", () => builder.AddXUnit(accessor, (null as Action<XUnitLoggerOptions>) !));
}

[Fact]
Expand All @@ -44,32 +44,32 @@ public static void AddXUnit_For_ILoggerFactory_Validates_Parameters()
var options = new XUnitLoggerOptions();

// Act and Assert
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory).AddXUnit(outputHelper));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory).AddXUnit(outputHelper, options));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory).AddXUnit(outputHelper, ConfigureAction));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory).AddXUnit(outputHelper, ConfigureFunction));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory).AddXUnit(outputHelper, Filter));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory).AddXUnit(outputHelper, logLevel));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null as ITestOutputHelper));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null, ConfigureAction));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null, ConfigureFunction));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null, Filter));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null, logLevel));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null, options));
Assert.Throws<ArgumentNullException>("options", () => factory.AddXUnit(outputHelper, null as XUnitLoggerOptions));
Assert.Throws<ArgumentNullException>("configure", () => factory.AddXUnit(outputHelper, null as Action<XUnitLoggerOptions>));
Assert.Throws<ArgumentNullException>("configure", () => factory.AddXUnit(outputHelper, null as Func<XUnitLoggerOptions>));
Assert.Throws<ArgumentNullException>("filter", () => factory.AddXUnit(outputHelper, null as Func<string, LogLevel, bool>));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory) !.AddXUnit(outputHelper));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory) !.AddXUnit(outputHelper, options));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory) !.AddXUnit(outputHelper, ConfigureAction));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory) !.AddXUnit(outputHelper, ConfigureFunction));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory) !.AddXUnit(outputHelper, Filter));
Assert.Throws<ArgumentNullException>("factory", () => (null as ILoggerFactory) !.AddXUnit(outputHelper, logLevel));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit((null as ITestOutputHelper) !));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null!, ConfigureAction));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null!, ConfigureFunction));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null!, Filter));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null!, logLevel));
Assert.Throws<ArgumentNullException>("outputHelper", () => factory.AddXUnit(null!, options));
Assert.Throws<ArgumentNullException>("options", () => factory.AddXUnit(outputHelper, (null as XUnitLoggerOptions) !));
Assert.Throws<ArgumentNullException>("configure", () => factory.AddXUnit(outputHelper, (null as Action<XUnitLoggerOptions>) !));
Assert.Throws<ArgumentNullException>("configure", () => factory.AddXUnit(outputHelper, (null as Func<XUnitLoggerOptions>) !));
Assert.Throws<ArgumentNullException>("filter", () => factory.AddXUnit(outputHelper, (null as Func<string, LogLevel, bool>) !));
}

[Fact]
public static void ToLoggerFactory_Validates_Parameters()
{
// Arrange
ITestOutputHelper outputHelper = null;
ITestOutputHelper? outputHelper = null;

// Act and Assert
Assert.Throws<ArgumentNullException>("outputHelper", () => outputHelper.ToLoggerFactory());
Assert.Throws<ArgumentNullException>("outputHelper", () => outputHelper!.ToLoggerFactory());
}

private static void ConfigureAction(XUnitLoggerOptions options)
Expand All @@ -78,6 +78,6 @@ private static void ConfigureAction(XUnitLoggerOptions options)

private static XUnitLoggerOptions ConfigureFunction() => new XUnitLoggerOptions();

private static bool Filter(string categoryName, LogLevel level) => true;
private static bool Filter(string? categoryName, LogLevel level) => true;
}
}
8 changes: 4 additions & 4 deletions tests/Logging.XUnit.Tests/XUnitLoggerProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public static void XUnitLoggerProvider_Constructor_Validates_Parameters()
var options = new XUnitLoggerOptions();

// Act and Assert
Assert.Throws<ArgumentNullException>("outputHelper", () => new XUnitLoggerProvider(null as ITestOutputHelper, options));
Assert.Throws<ArgumentNullException>("accessor", () => new XUnitLoggerProvider(null as ITestOutputHelperAccessor, options));
Assert.Throws<ArgumentNullException>("options", () => new XUnitLoggerProvider(outputHelper, null));
Assert.Throws<ArgumentNullException>("options", () => new XUnitLoggerProvider(accessor, null));
Assert.Throws<ArgumentNullException>("outputHelper", () => new XUnitLoggerProvider((null as ITestOutputHelper) !, options));
Assert.Throws<ArgumentNullException>("accessor", () => new XUnitLoggerProvider((null as ITestOutputHelperAccessor) !, options));
Assert.Throws<ArgumentNullException>("options", () => new XUnitLoggerProvider(outputHelper, null!));
Assert.Throws<ArgumentNullException>("options", () => new XUnitLoggerProvider(accessor, null!));
}

[Fact]
Expand Down
Loading