From 85fa517d2b703c1119aa08d4f462da3000b37605 Mon Sep 17 00:00:00 2001 From: martincostello Date: Thu, 18 May 2023 11:29:23 +0100 Subject: [PATCH] .NET 8 preparation Cherry-pick various changes from #475. --- .github/workflows/approve-and-merge.yml | 2 +- .github/workflows/build.yml | 5 +++-- .github/workflows/code-ql.yml | 2 +- .github/workflows/dependency-review.yml | 2 +- Directory.Build.props | 1 - Directory.Build.targets | 1 + JustEat.StatsD.ruleset | 2 +- build.ps1 | 11 +++++------ .../TagsFormatters/StatsDTagsFormatter.cs | 8 ++------ tests/Benchmark/UdpTransportBenchmark.cs | 2 +- .../BufferBasedStatsDPublisherTests.cs | 6 +++--- tests/JustEat.StatsD.Tests/SocketTransportTests.cs | 6 ++---- tests/JustEat.StatsD.Tests/Utf8TagsFormatterTests.cs | 4 ++-- tests/JustEat.StatsD.Tests/WhenRegisteringStatsD.cs | 2 +- .../WhenUsingBufferBasedPublisher.cs | 2 +- tests/JustEat.StatsD.Tests/WhenUsingUdpTransport.cs | 2 +- 16 files changed, 26 insertions(+), 32 deletions(-) diff --git a/.github/workflows/approve-and-merge.yml b/.github/workflows/approve-and-merge.yml index 8ade3ee8..0a154cae 100644 --- a/.github/workflows/approve-and-merge.yml +++ b/.github/workflows/approve-and-merge.yml @@ -2,7 +2,7 @@ name: approve-and-merge on: pull_request: - branches: [ main ] + branches: [ main, dotnet-vnext ] env: REVIEWER_LOGIN: ${{ vars.REVIEWER_USER_NAME }} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f18ddb5f..0ba585a1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,7 +5,7 @@ on: branches: [ main ] tags: [ v* ] pull_request: - branches: [ main ] + branches: [ main, dotnet-vnext ] workflow_dispatch: env: @@ -64,7 +64,7 @@ jobs: - name: Upload coverage to Codecov uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4 with: - file: ./artifacts/coverage.net6.0.cobertura.xml + file: ./artifacts/coverage/coverage.net6.0.cobertura.xml flags: ${{ matrix.os_name }} - name: Publish artifacts @@ -78,6 +78,7 @@ jobs: with: name: packages-${{ matrix.os_name }} path: ./artifacts/packages + if-no-files-found: error validate-packages: needs: build diff --git a/.github/workflows/code-ql.yml b/.github/workflows/code-ql.yml index 8a70c78f..d7ace04b 100644 --- a/.github/workflows/code-ql.yml +++ b/.github/workflows/code-ql.yml @@ -4,7 +4,7 @@ on: push: branches: [ main ] pull_request: - branches: [ main ] + branches: [ main, dotnet-vnext ] schedule: - cron: '0 6 * * MON' workflow_dispatch: diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml index 879a0b35..d601471c 100644 --- a/.github/workflows/dependency-review.yml +++ b/.github/workflows/dependency-review.yml @@ -2,7 +2,7 @@ name: dependency-review on: pull_request: - branches: [ main ] + branches: [ main, dotnet-vnext ] permissions: contents: read diff --git a/Directory.Build.props b/Directory.Build.props index 74d888be..5eb01e00 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -41,7 +41,6 @@ true - $(OutputPath)\ cobertura,json [Benchmark]*,[*.Tests]*,[xunit.*]* System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute diff --git a/Directory.Build.targets b/Directory.Build.targets index 0bdc5742..9d58b9bc 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -1,5 +1,6 @@ + $([System.IO.Path]::Combine($(OutputPath), 'coverage', 'coverage')) true HTML $(ReportGeneratorReportTypes);MarkdownSummaryGitHub diff --git a/JustEat.StatsD.ruleset b/JustEat.StatsD.ruleset index 89ac087f..c0d53ba2 100644 --- a/JustEat.StatsD.ruleset +++ b/JustEat.StatsD.ruleset @@ -1,5 +1,5 @@ - + diff --git a/build.ps1 b/build.ps1 index f37bfcee..f976c5fc 100755 --- a/build.ps1 +++ b/build.ps1 @@ -5,7 +5,6 @@ param( [Parameter(Mandatory = $false)][string] $Configuration = "Release", - [Parameter(Mandatory = $false)][string] $VersionSuffix = "", [Parameter(Mandatory = $false)][string] $OutputPath = "", [Parameter(Mandatory = $false)][switch] $SkipTests ) @@ -13,13 +12,13 @@ param( $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" -$solutionPath = Split-Path $MyInvocation.MyCommand.Definition +$solutionPath = $PSScriptRoot $sdkFile = Join-Path $solutionPath "global.json" -$libraryProject = Join-Path $solutionPath "src\JustEat.StatsD\JustEat.StatsD.csproj" +$libraryProject = Join-Path $solutionPath "src" "JustEat.StatsD" "JustEat.StatsD.csproj" $testProjects = @( - (Join-Path $solutionPath "tests\JustEat.StatsD.Tests\JustEat.StatsD.Tests.csproj") + (Join-Path $solutionPath "tests" "JustEat.StatsD.Tests" "JustEat.StatsD.Tests.csproj") ) $dotnetVersion = (Get-Content $sdkFile | Out-String | ConvertFrom-Json).sdk.version @@ -51,7 +50,7 @@ else { if ($installDotNetSdk -eq $true) { $env:DOTNET_INSTALL_DIR = Join-Path "$(Convert-Path "$PSScriptRoot")" ".dotnetcli" - $sdkPath = Join-Path $env:DOTNET_INSTALL_DIR "sdk\$dotnetVersion" + $sdkPath = Join-Path $env:DOTNET_INSTALL_DIR "sdk" $dotnetVersion if (!(Test-Path $sdkPath)) { if (!(Test-Path $env:DOTNET_INSTALL_DIR)) { @@ -127,7 +126,7 @@ Write-Host "Packaging library..." -ForegroundColor Green DotNetPack $libraryProject Write-Host "Running tests..." -ForegroundColor Green -Remove-Item -Path (Join-Path $OutputPath "coverage.*.json") -Force -ErrorAction SilentlyContinue | Out-Null +Remove-Item -Path (Join-Path $OutputPath "coverage" "coverage.*.json") -Force -ErrorAction SilentlyContinue | Out-Null ForEach ($testProject in $testProjects) { DotNetTest $testProject } diff --git a/src/JustEat.StatsD/TagsFormatters/StatsDTagsFormatter.cs b/src/JustEat.StatsD/TagsFormatters/StatsDTagsFormatter.cs index 722bde2b..beb316e1 100644 --- a/src/JustEat.StatsD/TagsFormatters/StatsDTagsFormatter.cs +++ b/src/JustEat.StatsD/TagsFormatters/StatsDTagsFormatter.cs @@ -120,9 +120,7 @@ private bool TryWriteTags(ref Buffer buffer, in Dictionary buffer, int index, in Dictionary tags) => - !IsLastTag(index, tags) - ? buffer.TryWrite(_tagsSeparator) - : true; + IsLastTag(index, tags) || buffer.TryWrite(_tagsSeparator); [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool IsLastTag(int index, in Dictionary tags) => @@ -135,9 +133,7 @@ private bool TryWriteTags(ref Buffer buffer, in Dictionary buffer, KeyValuePair tag) => - tag.Value != null - ? buffer.TryWrite(_keyValueSeparator) && buffer.TryWriteString(tag.Value!) - : true; + tag.Value == null || buffer.TryWrite(_keyValueSeparator) && buffer.TryWriteString(tag.Value!); [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool AreTagsPresent(in Dictionary? tags) => diff --git a/tests/Benchmark/UdpTransportBenchmark.cs b/tests/Benchmark/UdpTransportBenchmark.cs index ba8356e2..42ded2ba 100644 --- a/tests/Benchmark/UdpTransportBenchmark.cs +++ b/tests/Benchmark/UdpTransportBenchmark.cs @@ -17,7 +17,7 @@ public class UdpTransportBenchmark private SocketTransport? _transport; private SocketTransport? _transportSwitched; - private class MillisecondSwitcher : IEndPointSource + private sealed class MillisecondSwitcher : IEndPointSource { private readonly IEndPointSource _endpointSource1; private readonly IEndPointSource _endpointSource2; diff --git a/tests/JustEat.StatsD.Tests/BufferBasedStatsDPublisherTests.cs b/tests/JustEat.StatsD.Tests/BufferBasedStatsDPublisherTests.cs index a35d4c22..f2d3125a 100644 --- a/tests/JustEat.StatsD.Tests/BufferBasedStatsDPublisherTests.cs +++ b/tests/JustEat.StatsD.Tests/BufferBasedStatsDPublisherTests.cs @@ -5,8 +5,8 @@ namespace JustEat.StatsD; public class BufferBasedStatsDPublisherTests { - private readonly FakeTransport _transport = new FakeTransport(); - private readonly StatsDConfiguration _configuration = new StatsDConfiguration { Prefix = "test" }; + private readonly FakeTransport _transport = new(); + private readonly StatsDConfiguration _configuration = new() { Prefix = "test" }; private readonly BufferBasedStatsDPublisher _sut; public BufferBasedStatsDPublisherTests() @@ -48,7 +48,7 @@ public void TestTimingSampled_Long() _transport.Messages.ShouldHaveSingleItem("test.timing:1234|ms|@0.99"); } - private class FakeTransport : IStatsDTransport + private sealed class FakeTransport : IStatsDTransport { public List Messages { get; } = new List(); diff --git a/tests/JustEat.StatsD.Tests/SocketTransportTests.cs b/tests/JustEat.StatsD.Tests/SocketTransportTests.cs index 55d70bd6..d7be3e64 100644 --- a/tests/JustEat.StatsD.Tests/SocketTransportTests.cs +++ b/tests/JustEat.StatsD.Tests/SocketTransportTests.cs @@ -74,8 +74,6 @@ public static void SocketTransportIsNoopForNullEndpoint() transport.Send("teststat:1|c"); } - private static IEndPointSource LocalStatsEndpoint() - { - return new SimpleEndpointSource(new IPEndPoint(IPAddress.Loopback, StatsDConfiguration.DefaultPort)); - } + private static SimpleEndpointSource LocalStatsEndpoint() + => new(new IPEndPoint(IPAddress.Loopback, StatsDConfiguration.DefaultPort)); } diff --git a/tests/JustEat.StatsD.Tests/Utf8TagsFormatterTests.cs b/tests/JustEat.StatsD.Tests/Utf8TagsFormatterTests.cs index 48109afc..a2248b24 100644 --- a/tests/JustEat.StatsD.Tests/Utf8TagsFormatterTests.cs +++ b/tests/JustEat.StatsD.Tests/Utf8TagsFormatterTests.cs @@ -7,7 +7,7 @@ namespace JustEat.StatsD; public static class Utf8TagsFormatterTests { private static readonly byte[] Buffer = new byte[512]; - private static readonly Dictionary AnyValidTags = new Dictionary + private static readonly Dictionary AnyValidTags = new() { ["foo"] = "bar", ["empty"] = null, @@ -292,7 +292,7 @@ public enum TagsFormatter SignalFx, } - private class AlwaysNullStatsDTagsFormatter : IStatsDTagsFormatter + private sealed class AlwaysNullStatsDTagsFormatter : IStatsDTagsFormatter { public bool AreTrailing { get; } diff --git a/tests/JustEat.StatsD.Tests/WhenRegisteringStatsD.cs b/tests/JustEat.StatsD.Tests/WhenRegisteringStatsD.cs index 5b660c7d..44a61f59 100644 --- a/tests/JustEat.StatsD.Tests/WhenRegisteringStatsD.cs +++ b/tests/JustEat.StatsD.Tests/WhenRegisteringStatsD.cs @@ -319,7 +319,7 @@ public static void AddStatsDThrowsIfServicesIsNull() Assert.Throws("services", () => services!.AddStatsD()); } - private static IServiceProvider Configure(Action registration) + private static ServiceProvider Configure(Action registration) { var services = new ServiceCollection(); diff --git a/tests/JustEat.StatsD.Tests/WhenUsingBufferBasedPublisher.cs b/tests/JustEat.StatsD.Tests/WhenUsingBufferBasedPublisher.cs index 322d0bb8..ec80d885 100644 --- a/tests/JustEat.StatsD.Tests/WhenUsingBufferBasedPublisher.cs +++ b/tests/JustEat.StatsD.Tests/WhenUsingBufferBasedPublisher.cs @@ -30,7 +30,7 @@ public void ItShouldBeAbleToSendMessagesOfArbitraryLength(char ch, int count) fakeTransport.TimesCalled.ShouldBe(1); } - private class FakeTransport : IStatsDTransport + private sealed class FakeTransport : IStatsDTransport { public int TimesCalled { get; private set; } diff --git a/tests/JustEat.StatsD.Tests/WhenUsingUdpTransport.cs b/tests/JustEat.StatsD.Tests/WhenUsingUdpTransport.cs index 69cd4f78..92d3a8c7 100644 --- a/tests/JustEat.StatsD.Tests/WhenUsingUdpTransport.cs +++ b/tests/JustEat.StatsD.Tests/WhenUsingUdpTransport.cs @@ -103,7 +103,7 @@ public static void EndpointSwitchShouldNotCauseExceptionsParallel() }); } - private class MillisecondSwitcher : IEndPointSource + private sealed class MillisecondSwitcher : IEndPointSource { private readonly IEndPointSource _endpointSource1; private readonly IEndPointSource _endpointSource2;