Skip to content

Commit

Permalink
Refactor test and prefer global.json for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon committed Mar 22, 2024
1 parent adf6301 commit 849e347
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 69 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,40 @@ 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"

- name: publish canary packages github package repository
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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Serialises the <paramref name="data"/> using the sync and async request/response serializer methods, comparing the results.
/// </summary>
/// <returns>
/// The JSON as a string for further comparisons and assertions.
/// </returns>
protected static async Task<string> SerializeAndGetJsonStringAsync<T>(T data)
protected static Stream SerializeToStream<T>(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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<PackageReference Include="Nullean.VsTest.Pretty.TestLogger" Version="0.4.0" />
<PackageReference Include="JunitXml.TestLogger" Version="3.1.12" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Verify.Xunit" Version="23.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 849e347

Please sign in to comment.