Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
17 changes: 8 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,14 @@ jobs:
timeout-minutes: 15
continue-on-error: false
run: >
dotnet test ${{ matrix.path }}
--collect:"XPlat Code Coverage;Format=opencover"
dotnet test
--project ${{ matrix.path }}
--framework net10.0
--logger trx
--no-build
--
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.ExcludeByFile="**/test/**"
--coverage
--coverage-output-format cobertura
--report-trx
--results-directory ${{ matrix.directoryPath }}/TestResults
env:
CI_BUILD: true

Expand All @@ -289,9 +290,7 @@ jobs:
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: coverage-${{ matrix.name }}
# The * matches a single directory that is named with a GUID.
# Take note of https://github.com/microsoft/vstest/issues/2334.
path: ${{ matrix.directoryPath }}/TestResults/*/coverage.opencover.xml
path: ${{ matrix.directoryPath }}/TestResults/*.cobertura.xml

- name: Upload mismatch files as Artifact
if: steps.run-tests.outcome == 'failure'
Expand All @@ -317,7 +316,7 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
name: graphql-platform
files: "./output/download/coverage-*/*/coverage.opencover.xml"
files: "./output/download/coverage-*/*.cobertura.xml"
disable_search: true
flags: unittests
fail_ci_if_error: true
Expand Down
17 changes: 8 additions & 9 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ jobs:
timeout-minutes: 15
continue-on-error: true
run: >
dotnet test ${{ matrix.path }}
--collect:"XPlat Code Coverage;Format=opencover"
dotnet test
--project ${{ matrix.path }}
--framework net10.0
--logger trx
--no-build
--coverage
--coverage-output-format cobertura
--report-trx
--results-directory ${{ matrix.directoryPath }}/TestResults
--verbosity q
--
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.ExcludeByFile="**/test/**"
env:
CI_BUILD: true

Expand All @@ -95,9 +96,7 @@ jobs:
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: coverage-${{ matrix.name }}
# The * matches a single directory that is named with a GUID.
# Take note of https://github.com/microsoft/vstest/issues/2334.
path: ${{ matrix.directoryPath }}/TestResults/*/coverage.opencover.xml
path: ${{ matrix.directoryPath }}/TestResults/*.cobertura.xml

- name: Upload mismatch files as Artifact
if: failure()
Expand Down Expand Up @@ -129,7 +128,7 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
name: graphql-platform
files: "./output/download/coverage-*/*/coverage.opencover.xml"
files: "./output/download/coverage-*/*.cobertura.xml"
disable_search: true
flags: unittests
fail_ci_if_error: true
3 changes: 3 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"sdk": {
"version": "10.0.201",
"rollForward": "latestMinor"
},
"test": {
"runner": "Microsoft.Testing.Platform"
}
}
3 changes: 1 addition & 2 deletions src/All.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<Project Path="HotChocolate/Core/test/Execution.Tests/HotChocolate.Execution.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Features.Tests/HotChocolate.Features.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Fetching.Tests/HotChocolate.Fetching.Tests.csproj" />
<Project Path="HotChocolate/Core/test/StarWars/HotChocolate.StarWars.Tests.csproj" />
<Project Path="HotChocolate/Core/test/StarWars/HotChocolate.StarWars.csproj" />
<Project Path="HotChocolate/Core/test/Subscriptions.InMemory.Tests/HotChocolate.Subscriptions.InMemory.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Subscriptions.Nats.Tests/HotChocolate.Subscriptions.Nats.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Subscriptions.Postgres.Tests/HotChocolate.Subscriptions.Postgres.Tests.csproj" />
Expand Down Expand Up @@ -233,7 +233,6 @@
<Project Path="HotChocolate/Fusion/test/Fusion.Connectors.InMemory.Tests/HotChocolate.Fusion.Connectors.InMemory.Tests.csproj" />
<Project Path="HotChocolate/Fusion/test/Fusion.Caching.Tests/HotChocolate.Fusion.Caching.Tests.csproj" />
<Project Path="HotChocolate/Fusion/test/Fusion.Diagnostics.Tests/HotChocolate.Fusion.Diagnostics.Tests.csproj" />
<Project Path="HotChocolate/Fusion/test/Fusion.EventSources.Tests/HotChocolate.Fusion.EventSources.Tests.csproj" />
<Project Path="HotChocolate/Fusion/test/Fusion.Execution.Tests/HotChocolate.Fusion.Execution.Tests.csproj" />
<Project Path="HotChocolate/Fusion/test/Fusion.Language.Tests/HotChocolate.Fusion.Language.Tests.csproj" />
<Project Path="HotChocolate/Fusion/test/Fusion.Packaging.Tests/HotChocolate.Fusion.Packaging.Tests.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ public static void MatchSnapshot(
postFix,
formatter: CoreFormatters.PlainText);

/// <summary>
/// Matches a snapshot of an execution result, merging an incrementally delivered
/// (<c>@defer</c>/<c>@stream</c>) response into its final aggregated form. The snapshot
/// is identical whether the transport delivered the response across several payloads or
/// as a single bundled payload, so it does not depend on delivery timing.
/// </summary>
public static void MatchAggregatedSnapshot(
this IExecutionResult? value,
string? postFix = null)
=> Snapshot.Match(
value,
postFix,
formatter: SnapshotValueFormatters.ExecutionResultAggregated);

/// <summary>
/// Markdown counterpart of <see cref="MatchAggregatedSnapshot"/>: matches a markdown
/// snapshot of an execution result, merging an incrementally delivered
/// (<c>@defer</c>/<c>@stream</c>) response into its final aggregated form so the snapshot
/// does not depend on whether the transport delivered it incrementally or bundled.
/// </summary>
public static void MatchAggregatedMarkdownSnapshot(
this IExecutionResult? value,
object? postFix = null,
string? extension = null)
=> Snapshot.Create(postFix?.ToString(), extension)
.Add(value, formatter: SnapshotValueFormatters.ExecutionResultAggregated)
.MatchMarkdown();

public static Snapshot AddResult(
this Snapshot snapshot,
IExecutionResult result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace CookieCrumble.HotChocolate.Formatters;

internal sealed class ExecutionResultSnapshotValueFormatter
internal sealed class ExecutionResultSnapshotValueFormatter(bool alwaysAggregate = false)
: SnapshotValueFormatter<IExecutionResult>
{
protected override void Format(IBufferWriter<byte> snapshot, IExecutionResult value)
Expand All @@ -18,7 +18,7 @@ protected override void Format(IBufferWriter<byte> snapshot, IExecutionResult va
}
else
{
FormatStreamAsync(snapshot, (IResponseStream)value).Wait();
FormatStreamAsync(snapshot, (IResponseStream)value, alwaysAggregate).Wait();
}
}

Expand All @@ -34,7 +34,7 @@ protected override void FormatMarkdown(IBufferWriter<byte> snapshot, IExecutionR
{
snapshot.Append("```text");
snapshot.AppendLine();
FormatStreamAsync(snapshot, (IResponseStream)value).Wait();
FormatStreamAsync(snapshot, (IResponseStream)value, alwaysAggregate).Wait();
}

snapshot.AppendLine();
Expand All @@ -44,7 +44,8 @@ protected override void FormatMarkdown(IBufferWriter<byte> snapshot, IExecutionR

private static async Task FormatStreamAsync(
IBufferWriter<byte> snapshot,
IResponseStream stream)
IResponseStream stream,
bool alwaysAggregate)
{
var docs = new List<JsonDocument>();
JsonResultPatcher? patcher = null;
Expand All @@ -56,17 +57,21 @@ private static async Task FormatStreamAsync(
{
if (first)
{
if (queryResult.HasNext ?? false)
first = false;

// When aggregating, the initial payload seeds the patcher regardless of
// whether more payloads follow. This normalizes a response delivered as a
// single bundled payload to the same merged form as an incrementally
// delivered one, so the snapshot is independent of delivery batching.
if (alwaysAggregate || (queryResult.HasNext ?? false))
{
var doc = JsonDocument.Parse(queryResult.ToJson());
docs.Add(doc);

patcher = new JsonResultPatcher();
patcher.SetResponse(doc);
first = false;
continue;
}
first = false;
}

if (patcher is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public static class SnapshotValueFormatters
public static ISnapshotValueFormatter ExecutionResult { get; } =
new ExecutionResultSnapshotValueFormatter();

public static ISnapshotValueFormatter ExecutionResultAggregated { get; } =
new ExecutionResultSnapshotValueFormatter(alwaysAggregate: true);

public static ISnapshotValueFormatter ExecutionResultStable { get; } =
new StableExecutionResultSnapshotValueFormatter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,29 @@
namespace CookieCrumble.Xunit.Attributes;

/// <summary>
/// Apply this attribute to your test method to replace the
/// <see cref="Thread.CurrentThread" /> <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" /> with another culture.
/// Apply this attribute to a test class or method to replace the current thread's
/// <see cref="CultureInfo.CurrentCulture" /> and <see cref="CultureInfo.CurrentUICulture" />
/// with a specific culture while the test runs, restoring the originals afterward.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class UseCultureAttribute : BeforeAfterTestAttribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class UseCultureAttribute : BeforeAfterTestAttribute
{
private readonly Lazy<CultureInfo> _culture;
private readonly Lazy<CultureInfo> _uiCulture;
private CultureInfo _originalCulture = null!;
private CultureInfo _originalUiCulture = null!;

/// <summary>
/// Replaces the culture and UI culture of the current thread with
/// <paramref name="culture" />
/// Initializes the attribute to use <paramref name="culture" /> as both the
/// <see cref="Culture" /> and <see cref="UICulture" />.
/// </summary>
/// <param name="culture">The name of the culture.</param>
/// <remarks>
/// <para>
/// This constructor overload uses <paramref name="culture" /> for both
/// <see cref="Culture" /> and <see cref="UICulture" />.
/// </para>
/// </remarks>
public UseCultureAttribute(string culture)
: this(culture, culture) { }

/// <summary>
/// Replaces the culture and UI culture of the current thread with
/// <paramref name="culture" /> and <paramref name="uiCulture" />
/// Initializes the attribute to use <paramref name="culture" /> as the <see cref="Culture" />
/// and <paramref name="uiCulture" /> as the <see cref="UICulture" />.
/// </summary>
/// <param name="culture">The name of the culture.</param>
/// <param name="uiCulture">The name of the UI culture.</param>
Expand All @@ -58,11 +52,11 @@ public UseCultureAttribute(string culture, string uiCulture)
public CultureInfo UICulture { get { return _uiCulture.Value; } }

/// <summary>
/// Stores the current <see cref="Thread.CurrentPrincipal" />
/// <see cref="CultureInfo.CurrentCulture" /> and <see cref="CultureInfo.CurrentUICulture" />
/// and replaces them with the new cultures defined in the constructor.
/// Captures the current thread's <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" />, then replaces them with
/// <see cref="Culture" /> and <see cref="UICulture" />.
/// </summary>
/// <param name="methodUnderTest">The method under test</param>
/// <param name="methodUnderTest">The method under test.</param>
public override void Before(MethodInfo methodUnderTest)
{
_originalCulture = Thread.CurrentThread.CurrentCulture;
Expand All @@ -76,10 +70,10 @@ public override void Before(MethodInfo methodUnderTest)
}

/// <summary>
/// Restores the original <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" /> to <see cref="Thread.CurrentPrincipal" />
/// Restores the current thread's <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" /> to the values captured before the test ran.
/// </summary>
/// <param name="methodUnderTest">The method under test</param>
/// <param name="methodUnderTest">The method under test.</param>
public override void After(MethodInfo methodUnderTest)
{
Thread.CurrentThread.CurrentCulture = _originalCulture;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* This code was copied from xunit samples https://github.com/xunit/samples.xunit/blob/main/v3/UseCultureExample/UseCultureAttribute.cs
*/

using System.Globalization;
using System.Reflection;
using Xunit.v3;

namespace CookieCrumble.Xunit3.Attributes;

/// <summary>
/// Apply this attribute to a test class or method to replace the current thread's
/// <see cref="CultureInfo.CurrentCulture" /> and <see cref="CultureInfo.CurrentUICulture" />
/// with a specific culture while the test runs, restoring the originals afterward.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class UseCultureAttribute : BeforeAfterTestAttribute
{
private readonly Lazy<CultureInfo> _culture;
private readonly Lazy<CultureInfo> _uiCulture;
private CultureInfo _originalCulture = null!;
private CultureInfo _originalUiCulture = null!;

/// <summary>
/// Initializes the attribute to use <paramref name="culture" /> as both the
/// <see cref="Culture" /> and <see cref="UICulture" />.
/// </summary>
/// <param name="culture">The name of the culture.</param>
public UseCultureAttribute(string culture)
: this(culture, culture) { }

/// <summary>
/// Initializes the attribute to use <paramref name="culture" /> as the <see cref="Culture" />
/// and <paramref name="uiCulture" /> as the <see cref="UICulture" />.
/// </summary>
/// <param name="culture">The name of the culture.</param>
/// <param name="uiCulture">The name of the UI culture.</param>
public UseCultureAttribute(string culture, string uiCulture)
{
_culture = new Lazy<CultureInfo>(() => new CultureInfo(culture, false));
_uiCulture = new Lazy<CultureInfo>(() => new CultureInfo(uiCulture, false));
}

/// <summary>
/// Gets the culture.
/// </summary>
public CultureInfo Culture { get { return _culture.Value; } }

/// <summary>
/// Gets the UI culture.
/// </summary>
public CultureInfo UICulture { get { return _uiCulture.Value; } }

/// <summary>
/// Captures the current thread's <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" />, then replaces them with
/// <see cref="Culture" /> and <see cref="UICulture" />.
/// </summary>
/// <param name="methodUnderTest">The method under test.</param>
/// <param name="test">The current test.</param>
public override void Before(MethodInfo methodUnderTest, IXunitTest test)
{
_originalCulture = Thread.CurrentThread.CurrentCulture;
_originalUiCulture = Thread.CurrentThread.CurrentUICulture;

Thread.CurrentThread.CurrentCulture = Culture;
Thread.CurrentThread.CurrentUICulture = UICulture;

CultureInfo.CurrentCulture.ClearCachedData();
CultureInfo.CurrentUICulture.ClearCachedData();
}

/// <summary>
/// Restores the current thread's <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" /> to the values captured before the test ran.
/// </summary>
/// <param name="methodUnderTest">The method under test.</param>
/// <param name="test">The current test.</param>
public override void After(MethodInfo methodUnderTest, IXunitTest test)
{
Thread.CurrentThread.CurrentCulture = _originalCulture;
Thread.CurrentThread.CurrentUICulture = _originalUiCulture;

CultureInfo.CurrentCulture.ClearCachedData();
CultureInfo.CurrentUICulture.ClearCachedData();
}
}
Loading
Loading