Skip to content

Commit

Permalink
Support xunit v3
Browse files Browse the repository at this point in the history
- Add support for xunit v3 via the new MartinCostello.Logging.XUnit.v3 NuGet package.
- Bump version to 0.5.0.
  • Loading branch information
martincostello committed Dec 19, 2024
1 parent 8eae44c commit bd52954
Show file tree
Hide file tree
Showing 29 changed files with 224 additions and 55 deletions.
9 changes: 2 additions & 7 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<UseArtifactsOutput>true</UseArtifactsOutput>
<AssemblyVersion>0.4.0.0</AssemblyVersion>
<VersionPrefix>0.4.1</VersionPrefix>
<AssemblyVersion>0.5.0.0</AssemblyVersion>
<VersionPrefix>0.5.0</VersionPrefix>
</PropertyGroup>
<PropertyGroup Condition=" '$(GITHUB_ACTIONS)' != '' ">
<VersionSuffix Condition=" '$(VersionSuffix)' == '' AND '$(GITHUB_HEAD_REF)' == '' ">beta.$(GITHUB_RUN_NUMBER)</VersionSuffix>
Expand Down Expand Up @@ -73,9 +73,4 @@
<ItemGroup>
<Using Include="System.Globalization" />
</ItemGroup>
<ItemGroup Condition=" '$(IsTestProject)' == 'true' ">
<Using Include="Shouldly" />
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<PackageVersion Include="xunit.abstractions" Version="2.0.3" />
<PackageVersion Include="xunit.extensibility.execution" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0" />
<PackageVersion Include="xunit.v3" Version="1.0.0" />
<PackageVersion Include="xunit.v3.common" Version="1.0.0" />
<PackageVersion Include="xunit.v3.core" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(IsTestProject)' == 'true' ">
<PackageReference Include="coverlet.msbuild" PrivateAssets="All" />
Expand Down
14 changes: 14 additions & 0 deletions Logging.XUnit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
.github\workflows\release.yml = .github\workflows\release.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MartinCostello.Logging.XUnit.v3", "src\Logging.XUnit.v3\MartinCostello.Logging.XUnit.v3.csproj", "{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MartinCostello.Logging.XUnit.v3.Tests", "tests\Logging.XUnit.v3.Tests\MartinCostello.Logging.XUnit.v3.Tests.csproj", "{828C7199-ADF2-456C-8BBB-4A73A329D017}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -85,6 +89,14 @@ Global
{B690F271-3B5D-4975-A607-AED1768595B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B690F271-3B5D-4975-A607-AED1768595B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B690F271-3B5D-4975-A607-AED1768595B1}.Release|Any CPU.Build.0 = Release|Any CPU
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E}.Release|Any CPU.Build.0 = Release|Any CPU
{828C7199-ADF2-456C-8BBB-4A73A329D017}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{828C7199-ADF2-456C-8BBB-4A73A329D017}.Debug|Any CPU.Build.0 = Debug|Any CPU
{828C7199-ADF2-456C-8BBB-4A73A329D017}.Release|Any CPU.ActiveCfg = Release|Any CPU
{828C7199-ADF2-456C-8BBB-4A73A329D017}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -97,6 +109,8 @@ Global
{BB443063-F523-474D-83E3-D5FF5B075950} = {2684B19D-7D49-4099-8BD2-4D281455EB29}
{B690F271-3B5D-4975-A607-AED1768595B1} = {278BCCB1-39B2-46DB-9395-7F85995A6132}
{7764A046-DEE7-4D88-83E2-537DB7767123} = {D0426D09-1FF8-4E1F-A9AF-38DDEE5D7CCA}
{2C161BB9-E6D0-4561-9D98-EDEAA8A49C5E} = {2684B19D-7D49-4099-8BD2-4D281455EB29}
{828C7199-ADF2-456C-8BBB-4A73A329D017} = {278BCCB1-39B2-46DB-9395-7F85995A6132}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3B9E157C-5E92-4357-B233-281B4530EABD}
Expand Down
12 changes: 9 additions & 3 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ $ProgressPreference = "SilentlyContinue"
$solutionPath = $PSScriptRoot
$sdkFile = Join-Path $solutionPath "global.json"

$libraryProject = Join-Path $solutionPath "src" "Logging.XUnit" "MartinCostello.Logging.XUnit.csproj"
$libraryProjects = @(
(Join-Path $solutionPath "src" "Logging.XUnit" "MartinCostello.Logging.XUnit.csproj")
(Join-Path $solutionPath "src" "Logging.XUnit.v3" "MartinCostello.Logging.XUnit.v3.csproj")
)

$testProjects = @(
(Join-Path $solutionPath "tests" "Logging.XUnit.Tests" "MartinCostello.Logging.XUnit.Tests.csproj")
(Join-Path $solutionPath "tests" "Logging.XUnit.v3.Tests" "MartinCostello.Logging.XUnit.v3.Tests.csproj")
)

$dotnetVersion = (Get-Content $sdkFile | Out-String | ConvertFrom-Json).sdk.version
Expand Down Expand Up @@ -102,8 +106,10 @@ function DotNetTest {
}
}

Write-Information "Packaging library..."
DotNetPack $libraryProject
Write-Information "Packaging libraries..."
ForEach ($libraryProject in $libraryProjects) {
DotNetPack $libraryProject
}

if (-Not $SkipTests) {
Write-Information "Running tests..."
Expand Down
68 changes: 68 additions & 0 deletions src/Logging.XUnit.v3/MartinCostello.Logging.XUnit.v3.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyTitle>Logging Extensions for xunit v3</AssemblyTitle>
<DefineConstants>$(DefineConstants);XUNIT_V3</DefineConstants>
<Description>Extensions for Microsoft.Extensions.Logging for xunit v3.</Description>
<EnablePackageValidation>true</EnablePackageValidation>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>true</IsPackable>
<IsTestProject>false</IsTestProject>
<OutputType>Library</OutputType>
<PackageId>MartinCostello.Logging.XUnit.v3</PackageId>
<!--
<PackageValidationBaselineVersion>0.4.0</PackageValidationBaselineVersion>
-->
<RootNamespace>MartinCostello.Logging.XUnit</RootNamespace>
<Summary>$(Description)</Summary>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
<TestProject>false</TestProject>
<Title>xunit v3 Logging Extensions</Title>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
<!-- TODO Work out what properties are needed to correctly build the library without it appearing to be tests itself -->
<PropertyGroup Condition="false">
<GenerateProgramFile>false</GenerateProgramFile>
<GenerateSelfRegisteredExtensions>false</GenerateSelfRegisteredExtensions>
<GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>
<IsTestProject>false</IsTestProject>
<XunitAutoGeneratedEntryPoint>false</XunitAutoGeneratedEntryPoint>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" VersionOverride="8.0.1" />
<PackageReference Include="xunit.v3.common" />
<PackageReference Include="xunit.v3.core" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.InteropServices.GuidAttribute">
<_Parameter1>ed8d1c5e-3ee7-45fe-8d1d-94257a71f02a</_Parameter1>
</AssemblyAttribute>
<InternalsVisibleTo Include="MartinCostello.Logging.XUnit.v3.Tests" PublicKey="$(StrongNamePublicKey)" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
<Using Include="Xunit.Sdk" />
<Using Include="Xunit.v3" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Logging.XUnit\AmbientTestOutputHelperAccessor.cs" Link="AmbientTestOutputHelperAccessor.cs" />
<Compile Include="..\Logging.XUnit\IMessageSinkAccessor.cs" Link="IMessageSinkAccessor.cs" />
<Compile Include="..\Logging.XUnit\IMessageSinkExtensions.cs" Link="IMessageSinkExtensions.cs" />
<Compile Include="..\Logging.XUnit\ITestOutputHelperAccessor.cs" Link="ITestOutputHelperAccessor.cs" />
<Compile Include="..\Logging.XUnit\ITestOutputHelperExtensions.cs" Link="ITestOutputHelperExtensions.cs" />
<Compile Include="..\Logging.XUnit\MessageSinkAccessor.cs" Link="MessageSinkAccessor.cs" />
<Compile Include="..\Logging.XUnit\StringSyntaxAttribute.cs" Link="StringSyntaxAttribute.cs" />
<Compile Include="..\Logging.XUnit\TestOutputHelperAccessor.cs" Link="TestOutputHelperAccessor.cs" />
<Compile Include="..\Logging.XUnit\XUnitLogger.cs" Link="XUnitLogger.cs" />
<Compile Include="..\Logging.XUnit\XUnitLogger.IMessageSink.cs" Link="XUnitLogger.IMessageSink.cs" />
<Compile Include="..\Logging.XUnit\XUnitLogger.ITestOutputHelper.cs" Link="XUnitLogger.ITestOutputHelper.cs" />
<Compile Include="..\Logging.XUnit\XUnitLoggerExtensions.IMessageSink.cs" Link="XUnitLoggerExtensions.IMessageSink.cs" />
<Compile Include="..\Logging.XUnit\XUnitLoggerExtensions.ITestOutputHelper.cs" Link="XUnitLoggerExtensions.ITestOutputHelper.cs" />
<Compile Include="..\Logging.XUnit\XUnitLoggerOptions.cs" Link="XUnitLoggerOptions.cs" />
<Compile Include="..\Logging.XUnit\XUnitLoggerProvider.cs" Link="XUnitLoggerProvider.cs" />
<Compile Include="..\Logging.XUnit\XUnitLoggerProvider.IMessageSink.cs" Link="XUnitLoggerProvider.IMessageSink.cs" />
<Compile Include="..\Logging.XUnit\XUnitLoggerProvider.ITestOutputHelper.cs" Link="XUnitLoggerProvider.ITestOutputHelper.cs" />
<Compile Include="..\Logging.XUnit\XUnitLogScope.cs" Link="XUnitLogScope.cs" />
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions src/Logging.XUnit/AmbientTestOutputHelperAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Xunit.Abstractions;

namespace MartinCostello.Logging.XUnit;

/// <summary>
Expand Down
11 changes: 0 additions & 11 deletions src/Logging.XUnit/CompatibilitySuppressions.xml

This file was deleted.

2 changes: 0 additions & 2 deletions src/Logging.XUnit/IMessageSinkAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Xunit.Abstractions;

namespace MartinCostello.Logging.XUnit;

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Logging.XUnit/IMessageSinkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
using System.ComponentModel;
using Microsoft.Extensions.Logging;

#if XUNIT_V3
namespace Xunit;
#else
#pragma warning disable IDE0130
namespace Xunit.Abstractions;
#endif

/// <summary>
/// A class containing extension methods for the <see cref="IMessageSink"/> interface. This class cannot be inherited.
Expand Down
2 changes: 0 additions & 2 deletions src/Logging.XUnit/ITestOutputHelperAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Xunit.Abstractions;

namespace MartinCostello.Logging.XUnit;

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Logging.XUnit/ITestOutputHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
using System.ComponentModel;
using Microsoft.Extensions.Logging;

#if XUNIT_V3
namespace Xunit;
#else
#pragma warning disable IDE0130
namespace Xunit.Abstractions;
#endif

/// <summary>
/// A class containing extension methods for the <see cref="ITestOutputHelper"/> interface. This class cannot be inherited.
Expand Down
12 changes: 7 additions & 5 deletions src/Logging.XUnit/MartinCostello.Logging.XUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@
<Description>Extensions for Microsoft.Extensions.Logging for xunit.</Description>
<EnablePackageValidation>true</EnablePackageValidation>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>true</IsPackable>
<OutputType>Library</OutputType>
<PackageId>MartinCostello.Logging.XUnit</PackageId>
<PackageValidationBaselineVersion>0.3.0</PackageValidationBaselineVersion>
<PackageValidationBaselineVersion>0.4.0</PackageValidationBaselineVersion>
<RootNamespace>MartinCostello.Logging.XUnit</RootNamespace>
<Summary>$(Description)</Summary>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<Title>xunit Logging Extensions</Title>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<EnableAotAnalyzer>true</EnableAotAnalyzer>
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<IsAotCompatible>true</IsAotCompatible>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" />
Expand All @@ -33,4 +30,9 @@
</AssemblyAttribute>
<InternalsVisibleTo Include="MartinCostello.Logging.XUnit.Tests" PublicKey="$(StrongNamePublicKey)" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
<Using Include="Xunit.Sdk" />
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions src/Logging.XUnit/MessageSinkAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Xunit.Abstractions;

namespace MartinCostello.Logging.XUnit;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Logging.XUnit/StringSyntaxAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//// Based on https://github.com/dotnet/runtime/blob/65067052e433eda400c5e7cc9f7b21c84640f901/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/StringSyntaxAttribute.cs

#if NETSTANDARD
#if NETSTANDARD || !NETCOREAPP

#pragma warning disable IDE0130
#pragma warning disable SA1600
Expand Down
2 changes: 0 additions & 2 deletions src/Logging.XUnit/TestOutputHelperAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Xunit.Abstractions;

namespace MartinCostello.Logging.XUnit;

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/Logging.XUnit/XUnitLogger.IMessageSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

namespace MartinCostello.Logging.XUnit;

Expand Down
1 change: 0 additions & 1 deletion src/Logging.XUnit/XUnitLogger.ITestOutputHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

namespace MartinCostello.Logging.XUnit;

Expand Down
2 changes: 0 additions & 2 deletions src/Logging.XUnit/XUnitLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System.Text;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace MartinCostello.Logging.XUnit;

Expand Down
1 change: 0 additions & 1 deletion src/Logging.XUnit/XUnitLoggerExtensions.IMessageSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using MartinCostello.Logging.XUnit;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Xunit.Abstractions;

namespace Microsoft.Extensions.Logging;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.ComponentModel;
using MartinCostello.Logging.XUnit;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Xunit.Abstractions;

namespace Microsoft.Extensions.Logging;

Expand Down
2 changes: 0 additions & 2 deletions src/Logging.XUnit/XUnitLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace MartinCostello.Logging.XUnit;

Expand Down
1 change: 0 additions & 1 deletion src/Logging.XUnit/XUnitLoggerProvider.IMessageSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

namespace MartinCostello.Logging.XUnit;

Expand Down
1 change: 0 additions & 1 deletion src/Logging.XUnit/XUnitLoggerProvider.ITestOutputHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

namespace MartinCostello.Logging.XUnit;

Expand Down
24 changes: 23 additions & 1 deletion tests/Logging.XUnit.Tests/Integration/DatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,33 @@ public sealed class DatabaseFixture : IAsyncLifetime
public DatabaseFixture(IMessageSink messageSink)
{
using var loggerFactory = new LoggerFactory();
_initializeLogger = loggerFactory.AddXUnit(messageSink, c => c.MessageSinkMessageFactory = m => new PrintableDiagnosticMessage(m)).CreateLogger<DatabaseFixture>();

_initializeLogger = loggerFactory.AddXUnit(messageSink, c => c.MessageSinkMessageFactory = CreateMessage).CreateLogger<DatabaseFixture>();
_disposeLogger = messageSink.ToLogger<DatabaseFixture>();

#if XUNIT_V3
static IMessageSinkMessage CreateMessage(string message) => new DiagnosticMessage() { Message = message };
#else
static IMessageSinkMessage CreateMessage(string message) => new PrintableDiagnosticMessage(message);
#endif
}

public string ConnectionString => _connectionString ?? throw new InvalidOperationException("The connection string is only available after InitializeAsync has completed.");

#if XUNIT_V3
ValueTask IAsyncLifetime.InitializeAsync()
{
_initializeLogger.LogInformation("Initializing database");
_connectionString = "Server=localhost";
return ValueTask.CompletedTask;
}

ValueTask IAsyncDisposable.DisposeAsync()
{
_disposeLogger.LogInformation("Disposing database");
return ValueTask.CompletedTask;
}
#else
Task IAsyncLifetime.InitializeAsync()
{
_initializeLogger.LogInformation("Initializing database");
Expand All @@ -32,4 +53,5 @@ Task IAsyncLifetime.DisposeAsync()
_disposeLogger.LogInformation("Disposing database");
return Task.CompletedTask;
}
#endif
}
Loading

0 comments on commit bd52954

Please sign in to comment.