From 849e347b32d24d3ecece384ed32afff259c82d45 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Fri, 22 Mar 2024 08:15:48 +0000 Subject: [PATCH] Refactor test and prefer global.json for actions --- .github/workflows/ci.yml | 12 +++- .github/workflows/test-windows.yml | 2 +- ...ts.SerializesException_net481.verified.txt | 11 ---- ...sts.SerializesException_net80.verified.txt | 11 ---- .../LowLevelRequestResponseSerializerTests.cs | 57 ++++++++++++++----- .../Serialization/SerializationTestBase.cs | 32 ++--------- .../Elastic.Transport.Tests.csproj | 1 - 7 files changed, 57 insertions(+), 69 deletions(-) delete mode 100644 tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.SerializesException_net481.verified.txt delete mode 100644 tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.SerializesException_net80.verified.txt diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 174cbf4..36a597b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,25 +18,32 @@ jobs: build: runs-on: ubuntu-latest steps: + - uses: actions/checkout@v4 with: fetch-depth: 1 + - run: | git fetch --prune --unshallow --tags echo exit code $? git tag --list + - uses: actions/setup-dotnet@v4 with: - dotnet-version: 8.0.x + global-json-file: ./global.json - run: ./build.sh build -s true name: Build + - run: ./build.sh test -s true name: Test + - run: ./build.sh generatepackages -s true name: Generate local nuget packages + - run: ./build.sh validatepackages -s true name: "validate *.npkg files that were created" + - run: ./build.sh generateapichanges -s true name: "Inspect public API changes" @@ -44,7 +51,7 @@ jobs: if: github.event_name == 'push' && startswith(github.ref, 'refs/heads') shell: bash run: | - dotnet nuget add source --username USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/elastic/index.json" + dotnet nuget add source --username USERNAME --password ${{secrets.GITHUB_TOKEN}} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/elastic/index.json" until dotnet nuget push 'build/output/*.nupkg' -k ${{secrets.GITHUB_TOKEN}} --skip-duplicate --no-symbols --source "github"; do echo "Retrying"; sleep 1; done; # Github packages requires authentication, this is likely going away in the future so for now we publish to feedz.io @@ -55,6 +62,7 @@ jobs: - run: ./build.sh generatereleasenotes -s true name: Generate release notes for tag if: github.event_name == 'push' && startswith(github.ref, 'refs/tags') + - run: ./build.sh createreleaseongithub -s true --token ${{secrets.GITHUB_TOKEN}} if: github.event_name == 'push' && startswith(github.ref, 'refs/tags') name: Create or update release for tag on github diff --git a/.github/workflows/test-windows.yml b/.github/workflows/test-windows.yml index 0194c62..1330bdc 100644 --- a/.github/workflows/test-windows.yml +++ b/.github/workflows/test-windows.yml @@ -57,7 +57,7 @@ jobs: - name: Setup dotnet uses: actions/setup-dotnet@v4 with: - dotnet-version: 8.0.x + global-json-file: ./global.json - run: ./build.bat build -s true name: Build diff --git a/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.SerializesException_net481.verified.txt b/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.SerializesException_net481.verified.txt deleted file mode 100644 index 439fc9c..0000000 --- a/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.SerializesException_net481.verified.txt +++ /dev/null @@ -1,11 +0,0 @@ -[ - { - Depth: 0, - ClassName: Elastic.Transport.Tests.Components.Serialization.CustomException, - Message: Testing, - Source: Elastic.Transport.Tests, - StackTraceString: at Elastic.Transport.Tests.Components.Serialization.LowLevelRequestResponseSerializerTests.d__0.MoveNext() in {ProjectDirectory}Components\Serialization\LowLevelRequestResponseSerializerTests.cs:line 26, - HResult: -2146233088, - HelpURL: https://www.elastic.co - } -] \ No newline at end of file diff --git a/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.SerializesException_net80.verified.txt b/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.SerializesException_net80.verified.txt deleted file mode 100644 index 1e9e040..0000000 --- a/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.SerializesException_net80.verified.txt +++ /dev/null @@ -1,11 +0,0 @@ -[ - { - Depth: 0, - ClassName: Elastic.Transport.Tests.Components.Serialization.CustomException, - Message: Testing, - Source: Elastic.Transport.Tests, - StackTraceString: at Elastic.Transport.Tests.Components.Serialization.LowLevelRequestResponseSerializerTests.SerializesException() in {UserProfile}\Code\Elastic\elastic-transport-net\tests\Elastic.Transport.Tests\Components\Serialization\LowLevelRequestResponseSerializerTests.cs:line 26, - HResult: -2146233088, - HelpURL: https://www.elastic.co - } -] \ No newline at end of file diff --git a/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.cs b/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.cs index a022bec..b7807d5 100644 --- a/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.cs +++ b/tests/Elastic.Transport.Tests/Components/Serialization/LowLevelRequestResponseSerializerTests.cs @@ -3,44 +3,71 @@ // See the LICENSE file in the project root for more information using System; -using System.Threading.Tasks; -using VerifyXunit; +using System.Linq; +using System.Text.Json; +using FluentAssertions; using Xunit; namespace Elastic.Transport.Tests.Components.Serialization; -public class LowLevelRequestResponseSerializerTests : VerifySerializerTestBase +public class LowLevelRequestResponseSerializerTests : SerializerTestBase { [Fact] - public async Task SerializesException() + public void SerializesException() { // NOTE: Any changes to this file, may change the assertion since we validate the full JSON which // includes the stack trace line numbers. As we don't foresee this changing, this should be okay. - const string url = "https://www.elastic.co"; + const string urlValue = "https://www.elastic.co"; + const string messageValue = "Testing"; Exception e; try { - throw new CustomException("Testing") { HelpLink = url }; + throw new CustomException(messageValue) { HelpLink = urlValue }; } catch (Exception ex) { e = ex; } - var json = await SerializeAndGetJsonStringAsync(e); + using var stream = SerializeToStream(e); -#if NET481 - var version = "net481"; -#elif NET8_0 - var version = "net80"; -#else - var version = "unspecified_version"; -#endif + var jsonDocument = JsonDocument.Parse(stream); - await Verifier.VerifyJson(json).UseFileName($"LowLevelRequestResponseSerializerTests.SerializesException_{version}"); + jsonDocument.RootElement.EnumerateArray().Should().HaveCount(1); + var exception = jsonDocument.RootElement.EnumerateArray().First(); + + exception.TryGetProperty("Depth", out var depth).Should().BeTrue(); + depth.ValueKind.Should().Be(JsonValueKind.Number); + depth.GetInt32().Should().Be(0); + + exception.TryGetProperty("ClassName", out var className).Should().BeTrue(); + className.ValueKind.Should().Be(JsonValueKind.String); + className.GetString().Should().Be("Elastic.Transport.Tests.Components.Serialization.CustomException"); + + exception.TryGetProperty("Message", out var message).Should().BeTrue(); + message.ValueKind.Should().Be(JsonValueKind.String); + message.GetString().Should().Be(messageValue); + + exception.TryGetProperty("Source", out var source).Should().BeTrue(); + source.ValueKind.Should().Be(JsonValueKind.String); + source.GetString().Should().Be("Elastic.Transport.Tests"); + + exception.TryGetProperty("StackTraceString", out var stackTrace).Should().BeTrue(); + stackTrace.ValueKind.Should().Be(JsonValueKind.String); + stackTrace.GetString().Should() + .Contain("at Elastic.Transport.Tests.Components.Serialization.LowLevelRequestResponseSerializerTests") + .And.Contain("Components\\Serialization\\LowLevelRequestResponseSerializerTests.cs:line"); + + exception.TryGetProperty("HResult", out var hResult).Should().BeTrue(); + hResult.ValueKind.Should().Be(JsonValueKind.Number); + hResult.GetInt32().Should().Be(-2146233088); + + exception.TryGetProperty("HelpURL", out var helpUrl).Should().BeTrue(); + helpUrl.ValueKind.Should().Be(JsonValueKind.String); + helpUrl.GetString().Should().Be(urlValue); } } diff --git a/tests/Elastic.Transport.Tests/Components/Serialization/SerializationTestBase.cs b/tests/Elastic.Transport.Tests/Components/Serialization/SerializationTestBase.cs index 03cc385..cbc85b0 100644 --- a/tests/Elastic.Transport.Tests/Components/Serialization/SerializationTestBase.cs +++ b/tests/Elastic.Transport.Tests/Components/Serialization/SerializationTestBase.cs @@ -3,40 +3,16 @@ // See the LICENSE file in the project root for more information using System.IO; -using System.Threading.Tasks; -using FluentAssertions; -using VerifyXunit; namespace Elastic.Transport.Tests.Components.Serialization; -public abstract class VerifySerializerTestBase : VerifyBase +public abstract class SerializerTestBase { - public VerifySerializerTestBase() : base() { } - - private static readonly Serializer RequestResponseSerializer = LowLevelRequestResponseSerializer.Instance; - - /// - /// Serialises the using the sync and async request/response serializer methods, comparing the results. - /// - /// - /// The JSON as a string for further comparisons and assertions. - /// - protected static async Task SerializeAndGetJsonStringAsync(T data) + protected static Stream SerializeToStream(T data) { var stream = new MemoryStream(); - await RequestResponseSerializer.SerializeAsync(data, stream); - stream.Position = 0; - var reader = new StreamReader(stream); - var asyncJsonString = await reader.ReadToEndAsync(); - - stream.SetLength(0); - RequestResponseSerializer.Serialize(data, stream); + LowLevelRequestResponseSerializer.Instance.Serialize(data, stream); stream.Position = 0; - reader = new StreamReader(stream); - var syncJsonString = await reader.ReadToEndAsync(); - - syncJsonString.Should().Be(asyncJsonString); - - return asyncJsonString; + return stream; } } diff --git a/tests/Elastic.Transport.Tests/Elastic.Transport.Tests.csproj b/tests/Elastic.Transport.Tests/Elastic.Transport.Tests.csproj index c65b9a2..7976082 100644 --- a/tests/Elastic.Transport.Tests/Elastic.Transport.Tests.csproj +++ b/tests/Elastic.Transport.Tests/Elastic.Transport.Tests.csproj @@ -20,7 +20,6 @@ -