|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System.IO; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.Testing; |
| 8 | +using Xunit; |
| 9 | +using VerifyCS = Amazon.Lambda.Annotations.SourceGenerators.Tests.CSharpSourceGeneratorVerifier<Amazon.Lambda.Annotations.SourceGenerator.Generator>; |
| 10 | + |
| 11 | +namespace Amazon.Lambda.Annotations.SourceGenerators.Tests |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Tests for AWSLambda0145: when a [DurableExecution] function registers the source-generator |
| 15 | + /// serializer (SourceGeneratorLambdaJsonSerializer<TContext>), the durable invocation envelope |
| 16 | + /// types must be registered on TContext with [JsonSerializable], otherwise serialization fails at |
| 17 | + /// invocation time. |
| 18 | + /// </summary> |
| 19 | + public class DurableExecutionSerializerContextDiagnosticsTests |
| 20 | + { |
| 21 | + // Minimal durable SDK stubs. The real Amazon.Lambda.DurableExecution package cannot be referenced |
| 22 | + // by this test project (its AWSSDK.Core 4.x conflicts with the 3.7.x pin required by the generator |
| 23 | + // test framework), so the types the generator resolves by metadata name are supplied as source. |
| 24 | + private const string DurableStubs = @" |
| 25 | +namespace Amazon.Lambda.DurableExecution |
| 26 | +{ |
| 27 | + public interface IDurableContext { } |
| 28 | + public sealed class DurableExecutionInvocationInput { } |
| 29 | + public sealed class DurableExecutionInvocationOutput { } |
| 30 | +} |
| 31 | +"; |
| 32 | + |
| 33 | + private static async Task<string> AnnotationsSource(string fileName) => |
| 34 | + await File.ReadAllTextAsync(Path.Combine("Amazon.Lambda.Annotations", fileName)); |
| 35 | + |
| 36 | + // The user source registers the serializer itself (via [assembly: LambdaSerializer]) so the default |
| 37 | + // DefaultLambdaJsonSerializer registration is intentionally not added here. |
| 38 | + private static async Task<VerifyCS.Test> NewTestAsync(string userSource) |
| 39 | + { |
| 40 | + var test = new VerifyCS.Test |
| 41 | + { |
| 42 | + TestState = |
| 43 | + { |
| 44 | + OutputKind = OutputKind.ConsoleApplication, |
| 45 | + Sources = |
| 46 | + { |
| 47 | + ("Workflow.cs", userSource), |
| 48 | + ("DurableStubs.cs", DurableStubs), |
| 49 | + }, |
| 50 | + } |
| 51 | + }; |
| 52 | + test.TestState.Sources.Add((Path.Combine("Amazon.Lambda.Annotations", "LambdaFunctionAttribute.cs"), await AnnotationsSource("LambdaFunctionAttribute.cs"))); |
| 53 | + test.TestState.Sources.Add((Path.Combine("Amazon.Lambda.Annotations", "DurableExecutionAttribute.cs"), await AnnotationsSource("DurableExecutionAttribute.cs"))); |
| 54 | + |
| 55 | + // Generation proceeds (Warning severity does not halt it); ignore the per-file codegen Info |
| 56 | + // (AWSLambda0103), the generated-source list, and compiler errors from the deliberately |
| 57 | + // unimplemented JsonSerializerContext (only the generator's symbol resolution matters here). |
| 58 | + test.DisabledDiagnostics.Add("AWSLambda0103"); |
| 59 | + test.TestBehaviors |= TestBehaviors.SkipGeneratedSourcesCheck; |
| 60 | + test.CompilerDiagnostics = CompilerDiagnostics.None; |
| 61 | + return test; |
| 62 | + } |
| 63 | + |
| 64 | + // Builds a workflow that registers SourceGeneratorLambdaJsonSerializer<MyContext>, where MyContext |
| 65 | + // registers whichever envelope types are passed in via extraJsonSerializable. |
| 66 | + private static string WorkflowSource(string extraJsonSerializable) => $@" |
| 67 | +using System.Threading.Tasks; |
| 68 | +using System.Text.Json.Serialization; |
| 69 | +using Amazon.Lambda.Annotations; |
| 70 | +using Amazon.Lambda.Core; |
| 71 | +using Amazon.Lambda.DurableExecution; |
| 72 | +using Amazon.Lambda.Serialization.SystemTextJson; |
| 73 | +
|
| 74 | +[assembly: LambdaSerializer(typeof(SourceGeneratorLambdaJsonSerializer<MyApp.MyContext>))] |
| 75 | +
|
| 76 | +namespace MyApp |
| 77 | +{{ |
| 78 | +{extraJsonSerializable} |
| 79 | + public partial class MyContext : JsonSerializerContext {{ }} |
| 80 | +
|
| 81 | + public class Workflows |
| 82 | + {{ |
| 83 | + [LambdaFunction] |
| 84 | + [DurableExecution(executionTimeout: 300)] |
| 85 | + public Task<string> Run(string input, IDurableContext ctx) => Task.FromResult(input); |
| 86 | + }} |
| 87 | +}}"; |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public async Task BothEnvelopeTypesRegistered_NoDiagnostic() |
| 91 | + { |
| 92 | + var source = WorkflowSource( |
| 93 | + " [JsonSerializable(typeof(DurableExecutionInvocationInput))]\n" + |
| 94 | + " [JsonSerializable(typeof(DurableExecutionInvocationOutput))]"); |
| 95 | + var test = await NewTestAsync(source); |
| 96 | + // No AWSLambda0145 expected. |
| 97 | + await test.RunAsync(); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public async Task OutputEnvelopeTypeMissing_ReportsWarning() |
| 102 | + { |
| 103 | + var source = WorkflowSource( |
| 104 | + " [JsonSerializable(typeof(DurableExecutionInvocationInput))]"); |
| 105 | + var test = await NewTestAsync(source); |
| 106 | + test.TestState.ExpectedDiagnostics.Add( |
| 107 | + new DiagnosticResult("AWSLambda0145", DiagnosticSeverity.Warning) |
| 108 | + .WithSpan("Workflow.cs", 18, 9, 20, 94) |
| 109 | + .WithArguments("MyApp.MyContext", "Amazon.Lambda.DurableExecution.DurableExecutionInvocationOutput")); |
| 110 | + await test.RunAsync(); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public async Task BothEnvelopeTypesMissing_ReportsTwoWarnings() |
| 115 | + { |
| 116 | + var source = WorkflowSource(string.Empty); |
| 117 | + var test = await NewTestAsync(source); |
| 118 | + test.TestState.ExpectedDiagnostics.Add( |
| 119 | + new DiagnosticResult("AWSLambda0145", DiagnosticSeverity.Warning) |
| 120 | + .WithSpan("Workflow.cs", 18, 9, 20, 94) |
| 121 | + .WithArguments("MyApp.MyContext", "Amazon.Lambda.DurableExecution.DurableExecutionInvocationInput")); |
| 122 | + test.TestState.ExpectedDiagnostics.Add( |
| 123 | + new DiagnosticResult("AWSLambda0145", DiagnosticSeverity.Warning) |
| 124 | + .WithSpan("Workflow.cs", 18, 9, 20, 94) |
| 125 | + .WithArguments("MyApp.MyContext", "Amazon.Lambda.DurableExecution.DurableExecutionInvocationOutput")); |
| 126 | + await test.RunAsync(); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public async Task DefaultSerializer_NoDiagnostic() |
| 131 | + { |
| 132 | + // The reflection-based DefaultLambdaJsonSerializer needs no [JsonSerializable] registration, |
| 133 | + // so the durable envelope check does not apply and AWSLambda0145 is never emitted. |
| 134 | + var source = @" |
| 135 | +using System.Threading.Tasks; |
| 136 | +using Amazon.Lambda.Annotations; |
| 137 | +using Amazon.Lambda.Core; |
| 138 | +using Amazon.Lambda.DurableExecution; |
| 139 | +
|
| 140 | +[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] |
| 141 | +
|
| 142 | +namespace MyApp |
| 143 | +{ |
| 144 | + public class Workflows |
| 145 | + { |
| 146 | + [LambdaFunction] |
| 147 | + [DurableExecution(executionTimeout: 300)] |
| 148 | + public Task<string> Run(string input, IDurableContext ctx) => Task.FromResult(input); |
| 149 | + } |
| 150 | +}"; |
| 151 | + var test = await NewTestAsync(source); |
| 152 | + await test.RunAsync(); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments