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
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ private static string GetTypesDictionary(IEnumerable<ExecutableFunction> functio
}

return $$"""
private readonly Dictionary<string, Type> types = new()
private readonly Dictionary<string, Type> types = new Dictionary<string, Type>()
{
{{string.Join($",{Environment.NewLine} ", typesDict.Select(c => $$""" { "{{c.Key}}", Type.GetType("{{c.Key}}, {{c.Value}}")! }"""))}}
{{string.Join($",{Environment.NewLine} ", typesDict.Select(c => $$""" { "{{c.Key}}", Type.GetType("{{c.Key}}, {{c.Value}}") }"""))}}
};

""";
Expand Down Expand Up @@ -144,8 +144,8 @@ private static string GetMethodBody(IEnumerable<ExecutableFunction> functions, b
var sb = new StringBuilder();
sb.Append(
$$"""
var inputBindingFeature = context.Features.Get<IFunctionInputBindingFeature>()!;
var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context)!;
var inputBindingFeature = context.Features.Get<IFunctionInputBindingFeature>();
var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context);
var inputArguments = inputBindingResult.Values;
{{(anyDefaultExecutor ? $" _defaultExecutor = new Lazy<IFunctionExecutor>(() => CreateDefaultExecutorInstance(context));{Environment.NewLine}" : string.Empty)}}
""");
Expand Down
40 changes: 31 additions & 9 deletions test/Sdk.Generator.Tests/ExtensionStartupRunnerGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Tests.WorkerExtensionsSample;
using Microsoft.Azure.Functions.Worker.Sdk.Generators;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using Worker.Extensions.Sample_IncorrectImplementation;
using Xunit;
Expand All @@ -19,8 +20,14 @@ public class Foo
}
""";

[Fact]
public async Task StartupExecutorCodeGetsGenerated()
[Theory]
[InlineData(LanguageVersion.CSharp7_3)]
[InlineData(LanguageVersion.CSharp8)]
[InlineData(LanguageVersion.CSharp9)]
[InlineData(LanguageVersion.CSharp10)]
[InlineData(LanguageVersion.CSharp11)]
[InlineData(LanguageVersion.Latest)]
public async Task StartupExecutorCodeGetsGenerated(LanguageVersion languageVersion)
{
// Source generation is based on referenced assembly.
var referencedExtensionAssemblies = new[]
Expand Down Expand Up @@ -66,11 +73,18 @@ await TestHelpers.RunTestAsync<ExtensionStartupRunnerGenerator>(
referencedExtensionAssemblies,
InputCode,
expectedGeneratedFileName,
expectedOutput);
expectedOutput,
languageVersion: languageVersion);
}

[Fact]
public async Task StartupExecutorCodeDoesNotGetsGeneratedWheNoExtensionAssembliesAreReferenced()
[Theory]
[InlineData(LanguageVersion.CSharp7_3)]
[InlineData(LanguageVersion.CSharp8)]
[InlineData(LanguageVersion.CSharp9)]
[InlineData(LanguageVersion.CSharp10)]
[InlineData(LanguageVersion.CSharp11)]
[InlineData(LanguageVersion.Latest)]
public async Task StartupExecutorCodeDoesNotGetsGeneratedWheNoExtensionAssembliesAreReferenced(LanguageVersion languageVersion)
{
// source gen will happen only when an assembly with worker startup type is defined.
var referencedExtensionAssemblies = Array.Empty<System.Reflection.Assembly>();
Expand All @@ -82,11 +96,18 @@ await TestHelpers.RunTestAsync<ExtensionStartupRunnerGenerator>(
referencedExtensionAssemblies,
InputCode,
expectedGeneratedFileName,
expectedOutput);
expectedOutput,
languageVersion: languageVersion);
}

[Fact]
public async Task DiagnosticErrorsAreReportedWhenStartupTypeIsInvalid()
[Theory]
[InlineData(LanguageVersion.CSharp7_3)]
[InlineData(LanguageVersion.CSharp8)]
[InlineData(LanguageVersion.CSharp9)]
[InlineData(LanguageVersion.CSharp10)]
[InlineData(LanguageVersion.CSharp11)]
[InlineData(LanguageVersion.Latest)]
public async Task DiagnosticErrorsAreReportedWhenStartupTypeIsInvalid(LanguageVersion languageVersion)
{
var referencedExtensionAssemblies = new[]
{
Expand Down Expand Up @@ -154,7 +175,8 @@ await TestHelpers.RunTestAsync<ExtensionStartupRunnerGenerator>(
expectedGeneratedFileName,
expectedOutput,
expectedDiagnosticResults,
buildPropertiesDict);
buildPropertiesDict,
languageVersion: languageVersion);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ internal class DirectFunctionExecutor : IFunctionExecutor
{
private readonly IFunctionActivator _functionActivator;
private Lazy<IFunctionExecutor> _defaultExecutor;
private readonly Dictionary<string, Type> types = new()
private readonly Dictionary<string, Type> types = new Dictionary<string, Type>()
{
{ "MyCompany.MyHttpTriggers", Type.GetType("MyCompany.MyHttpTriggers, TestProject, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")! },
{ "DependentAssemblyWithFunctions.DependencyFunction", Type.GetType("DependentAssemblyWithFunctions.DependencyFunction, DependentAssemblyWithFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")! },
{ "MyCompany.MyProduct.MyApp.HttpFunctions", Type.GetType("MyCompany.MyProduct.MyApp.HttpFunctions, DependentAssemblyWithFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")! },
{ "MyCompany.MyProduct.MyApp.Foo.Bar", Type.GetType("MyCompany.MyProduct.MyApp.Foo.Bar, DependentAssemblyWithFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")! }
{ "MyCompany.MyHttpTriggers", Type.GetType("MyCompany.MyHttpTriggers, TestProject, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null") },
{ "DependentAssemblyWithFunctions.DependencyFunction", Type.GetType("DependentAssemblyWithFunctions.DependencyFunction, DependentAssemblyWithFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") },
{ "MyCompany.MyProduct.MyApp.HttpFunctions", Type.GetType("MyCompany.MyProduct.MyApp.HttpFunctions, DependentAssemblyWithFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") },
{ "MyCompany.MyProduct.MyApp.Foo.Bar", Type.GetType("MyCompany.MyProduct.MyApp.Foo.Bar, DependentAssemblyWithFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") }
};

public DirectFunctionExecutor(IFunctionActivator functionActivator)
Expand All @@ -89,8 +89,8 @@ public DirectFunctionExecutor(IFunctionActivator functionActivator)
/// <inheritdoc/>
public async ValueTask ExecuteAsync(FunctionContext context)
{
var inputBindingFeature = context.Features.Get<IFunctionInputBindingFeature>()!;
var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context)!;
var inputBindingFeature = context.Features.Get<IFunctionInputBindingFeature>();
var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context);
var inputArguments = inputBindingResult.Values;
_defaultExecutor = new Lazy<IFunctionExecutor>(() => CreateDefaultExecutorInstance(context));

Expand Down
Loading