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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<PackageVersion Include="BenchmarkDotNet.Diagnostics.dotMemory" Version="0.15.8" />
<PackageVersion Include="ChilliCream.ModelContextProtocol.AspNetCore" Version="1.3.0" />
<PackageVersion Include="ChilliCream.Nitro.App" Version="$(NitroVersion)" />
<PackageVersion Include="ChilliCream.Testing.Utilities" Version="0.2.0" />
<PackageVersion Include="coverlet.collector" Version="10.0.0" />
<PackageVersion Include="DiffPlex" Version="1.9.0" />
<PackageVersion Include="Duende.IdentityModel" Version="7.1.0" />
Expand Down Expand Up @@ -54,7 +53,6 @@
<PackageVersion Include="ProjNET" Version="2.0.0" />
<PackageVersion Include="RabbitMQ.Client" Version="7.0.0" />
<PackageVersion Include="RavenDB.Client" Version="7.0.0" />
<PackageVersion Include="Snapshooter.Xunit" Version="0.5.4" />
<PackageVersion Include="Spectre.Console.Json" Version="0.55.2" />
<PackageVersion Include="Spectre.Console" Version="0.55.2" />
<PackageVersion Include="Spectre.Console.Testing" Version="0.55.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using ChilliCream.Testing;
using CookieCrumble;
using StrawberryShake.Integration.Mappers;
using StrawberryShake.Serialization;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using ChilliCream.Testing;

namespace StrawberryShake.Integration.Mappers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

namespace StrawberryShake.CodeGeneration.CSharp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

namespace StrawberryShake.CodeGeneration.CSharp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.CodeAnalysis;
using ChilliCream.Testing;
using HotChocolate;
using HotChocolate.Language;
using Snapshooter;
using Snapshooter.Xunit;
using StrawberryShake.CodeGeneration.Analyzers;
using StrawberryShake.CodeGeneration.Analyzers.Models;
using StrawberryShake.CodeGeneration.Utilities;
using Snapshot = Snapshooter.Xunit.Snapshot;
using RequestStrategyGen = StrawberryShake.Tools.Configuration.RequestStrategy;
using static StrawberryShake.CodeGeneration.CSharp.CSharpGenerator;

Expand Down Expand Up @@ -140,11 +136,7 @@ public static void AssertResult(

if (settings.SnapshotFile is not null)
{
documents.ToString()
.MatchSnapshot(
new SnapshotFullName(
settings.SnapshotFile,
Snapshot.FullName().FolderPath));
MatchSnapshotAtPath(documents.ToString(), settings.SnapshotFile);
}
else
{
Expand Down Expand Up @@ -200,12 +192,17 @@ public static AssertSettings CreateIntegrationTest(
TransportProfile[]? profiles = null,
AccessModifier accessModifier = AccessModifier.Public,
bool noStore = false,
[CallerMemberName] string? testName = null)
[CallerMemberName] string? testName = null,
[CallerFilePath] string? callerFilePath = null)
{
var snapshotFullName = Snapshot.FullName();
var testFile = System.IO.Path.Combine(
snapshotFullName.FolderPath,
testName + "Test.cs");
ArgumentException.ThrowIfNullOrEmpty(testName);
ArgumentException.ThrowIfNullOrEmpty(callerFilePath);

var folder = System.IO.Path.GetDirectoryName(callerFilePath)
?? throw new ArgumentException(
$"Could not determine directory from caller file path '{callerFilePath}'.",
nameof(callerFilePath));
var testFile = System.IO.Path.Combine(folder, testName + "Test.cs");
var ns = "StrawberryShake.CodeGeneration.CSharp.Integration." + testName;
Comment thread
glen-84 marked this conversation as resolved.

if (!File.Exists(testFile))
Expand All @@ -219,13 +216,11 @@ public static AssertSettings CreateIntegrationTest(

return new AssertSettings
{
ClientName = testName! + "Client",
ClientName = testName + "Client",
Namespace = ns,
AccessModifier = accessModifier,
StrictValidation = true,
SnapshotFile = System.IO.Path.Combine(
snapshotFullName.FolderPath,
testName + "Test.Client.cs"),
SnapshotFile = System.IO.Path.Combine(folder, testName + "Test.Client.cs"),
RequestStrategy = requestStrategy,
NoStore = noStore,
Profiles = (profiles ??
Expand All @@ -235,6 +230,46 @@ public static AssertSettings CreateIntegrationTest(
};
}

private static void MatchSnapshotAtPath(string content, string snapshotFile)
{
content = content.Replace("\r\n", "\n");

if (!File.Exists(snapshotFile))
{
Comment thread
glen-84 marked this conversation as resolved.
CheckStrictMode();
File.WriteAllText(snapshotFile, content);
return;
}

var existing = File.ReadAllText(snapshotFile).Replace("\r\n", "\n");
if (string.Equals(existing, content, StringComparison.Ordinal))
{
return;
}

var folder = System.IO.Path.GetDirectoryName(snapshotFile)!;
var mismatchDir = System.IO.Path.Combine(folder, "__snapshots__", "__mismatch__");
Directory.CreateDirectory(mismatchDir);
var mismatchFile = System.IO.Path.Combine(mismatchDir, System.IO.Path.GetFileName(snapshotFile));
File.WriteAllText(mismatchFile, content);

Assert.Fail($"Snapshot mismatch. Mismatch file written to {mismatchFile}");
}

private static void CheckStrictMode()
{
var value = Environment.GetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE");

if (string.Equals(value, "on", StringComparison.Ordinal)
|| (bool.TryParse(value, out var b) && b))
{
Assert.Fail(
"Strict mode is enabled and no snapshot has been found "
+ "for the current test. Create a new snapshot locally and "
+ "rerun your tests.");
}
}
Comment thread
glen-84 marked this conversation as resolved.

private static ClientModel CreateClientModel(
string[] sourceText,
bool strictValidation,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

namespace StrawberryShake.CodeGeneration.CSharp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using HotChocolate.AspNetCore.Tests.Utilities;
using HotChocolate.Types;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter.Xunit;
using StrawberryShake.Transport.WebSockets;

namespace StrawberryShake.CodeGeneration.CSharp.Integration.AnyScalarDefaultSerialization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using HotChocolate.AspNetCore.Tests.Utilities;
using Microsoft.Extensions.DependencyInjection;
using HotChocolate.Types;
using Snapshooter.Xunit;

namespace StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HotChocolate.AspNetCore.Tests.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter.Xunit;
using StrawberryShake.Transport.WebSockets;

namespace StrawberryShake.CodeGeneration.CSharp.Integration.StarWarsIntrospection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HotChocolate.AspNetCore.Tests.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter.Xunit;
using StrawberryShake.Transport.WebSockets;

namespace StrawberryShake.CodeGeneration.CSharp.Integration.StarWarsTypeNameOnInterfaces;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HotChocolate.AspNetCore.Tests.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter.Xunit;
using StrawberryShake.Transport.WebSockets;

namespace StrawberryShake.CodeGeneration.CSharp.Integration.StarWarsTypeNameOnUnions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HotChocolate.AspNetCore.Tests.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter.Xunit;
using StrawberryShake.Transport.WebSockets;

namespace StrawberryShake.CodeGeneration.CSharp.Integration.StarWarsUnionList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using StrawberryShake.Tools.Configuration;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"Data": {
"__schema": {
"QueryType": {
Expand Down Expand Up @@ -7471,4 +7471,4 @@
"Errors": [],
"Extensions": {},
"ContextData": {}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

namespace StrawberryShake.CodeGeneration.CSharp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

namespace StrawberryShake.CodeGeneration.CSharp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using Xunit.Sdk;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

namespace StrawberryShake.CodeGeneration.CSharp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

namespace StrawberryShake.CodeGeneration.CSharp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.CodeAnalysis;
using ChilliCream.Testing;
using HotChocolate;
using HotChocolate.Language;
using Snapshooter;
using Snapshooter.Xunit;
using StrawberryShake.CodeGeneration.Analyzers;
using StrawberryShake.CodeGeneration.Analyzers.Models;
using StrawberryShake.CodeGeneration.Utilities;
using Snapshot = Snapshooter.Xunit.Snapshot;
using RequestStrategyGen = StrawberryShake.Tools.Configuration.RequestStrategy;
using static StrawberryShake.CodeGeneration.CSharp.CSharpGenerator;

Expand Down Expand Up @@ -138,11 +134,7 @@ public static void AssertResult(

if (settings.SnapshotFile is not null)
{
documents.ToString()
.MatchSnapshot(
new SnapshotFullName(
settings.SnapshotFile,
Snapshot.FullName().FolderPath));
MatchSnapshotAtPath(documents.ToString(), settings.SnapshotFile);
}
else
{
Expand Down Expand Up @@ -197,12 +189,17 @@ public static AssertSettings CreateIntegrationTest(
RequestStrategyGen requestStrategy = RequestStrategyGen.Default,
TransportProfile[]? profiles = null,
bool noStore = false,
[CallerMemberName] string? testName = null)
[CallerMemberName] string? testName = null,
[CallerFilePath] string? callerFilePath = null)
{
var snapshotFullName = Snapshot.FullName();
var testFile = System.IO.Path.Combine(
snapshotFullName.FolderPath,
testName + "Test.cs");
ArgumentException.ThrowIfNullOrEmpty(testName);
ArgumentException.ThrowIfNullOrEmpty(callerFilePath);

var folder = System.IO.Path.GetDirectoryName(callerFilePath)
?? throw new ArgumentException(
$"Could not determine directory from caller file path '{callerFilePath}'.",
nameof(callerFilePath));
var testFile = System.IO.Path.Combine(folder, testName + "Test.cs");
var ns = "StrawberryShake.CodeGeneration.CSharp.Integration." + testName;
Comment thread
glen-84 marked this conversation as resolved.

if (!File.Exists(testFile))
Expand All @@ -216,12 +213,10 @@ public static AssertSettings CreateIntegrationTest(

return new AssertSettings
{
ClientName = testName! + "Client",
ClientName = testName + "Client",
Namespace = ns,
StrictValidation = true,
SnapshotFile = System.IO.Path.Combine(
snapshotFullName.FolderPath,
testName + "Test.Client.cs"),
SnapshotFile = System.IO.Path.Combine(folder, testName + "Test.Client.cs"),
RequestStrategy = requestStrategy,
NoStore = noStore,
Profiles = (profiles ??
Expand All @@ -231,6 +226,46 @@ public static AssertSettings CreateIntegrationTest(
};
}

private static void MatchSnapshotAtPath(string content, string snapshotFile)
{
content = content.Replace("\r\n", "\n");

if (!File.Exists(snapshotFile))
{
CheckStrictMode();
File.WriteAllText(snapshotFile, content);
return;
}
Comment thread
glen-84 marked this conversation as resolved.

var existing = File.ReadAllText(snapshotFile).Replace("\r\n", "\n");
if (string.Equals(existing, content, StringComparison.Ordinal))
{
return;
}

var folder = System.IO.Path.GetDirectoryName(snapshotFile)!;
var mismatchDir = System.IO.Path.Combine(folder, "__snapshots__", "__mismatch__");
Directory.CreateDirectory(mismatchDir);
var mismatchFile = System.IO.Path.Combine(mismatchDir, System.IO.Path.GetFileName(snapshotFile));
File.WriteAllText(mismatchFile, content);

Assert.Fail($"Snapshot mismatch. Mismatch file written to {mismatchFile}");
}

private static void CheckStrictMode()
{
var value = Environment.GetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE");

if (string.Equals(value, "on", StringComparison.Ordinal)
|| (bool.TryParse(value, out var b) && b))
{
Assert.Fail(
"Strict mode is enabled and no snapshot has been found "
+ "for the current test. Create a new snapshot locally and "
+ "rerun your tests.");
}
}

private static ClientModel CreateClientModel(
string[] sourceText,
bool strictValidation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using StrawberryShake.CodeGeneration.Analyzers;
using StrawberryShake.CodeGeneration.Analyzers.Models;
using StrawberryShake.CodeGeneration.Utilities;
using static ChilliCream.Testing.FileResource;
using static CookieCrumble.FileResource;

namespace StrawberryShake.CodeGeneration.Mappers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HotChocolate.Language;
using Snapshooter.Xunit;
using static ChilliCream.Testing.FileResource;
using static CookieCrumble.FileResource;
using static HotChocolate.Language.Utf8GraphQLParser;
using static StrawberryShake.CodeGeneration.Utilities.OperationDocumentHelper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using HotChocolate.Language.Utilities;
using HotChocolate.StarWars;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter.Xunit;

namespace StrawberryShake.CodeGeneration.Utilities;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ChilliCream.Testing;
using HotChocolate.Language;
using HotChocolate.Types;
using StrawberryShake.CodeGeneration.Extensions;
Expand Down
Loading
Loading