Skip to content

Commit

Permalink
Test changes to make sure applicationName is set when using new templ…
Browse files Browse the repository at this point in the history
…ate pattern

EF-side tests for dotnet/runtime#59743

These will fail until the new source package for HostFactoryResolver is available. I have tested locally that they work once this is done.
  • Loading branch information
ajcvickers committed Sep 29, 2021
1 parent fe1e482 commit efe0795
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.EntityFrameworkCore
{
public class AppServiceProviderFactoryTest
{
[ConditionalFact]
public void Create_services_from_template_method()
{
TestCreateServices(typeof(ProgramWithBuildWebHost));
TestCreateServices(typeof(ProgramWithCreateWebHostBuilder));
TestCreateServices(typeof(ProgramWithCreateHostBuilder));
}

private static void TestCreateServices(Type programType)
{
var factory = new TestAppServiceProviderFactory(
MockAssembly.Create(programType));

Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null);
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", null);
var services = factory.Create(new[] { "arg1" });

Assert.NotNull(services.GetRequiredService<TestService>());
}

private class ProgramWithBuildWebHost
{
public static TestWebHost BuildWebHost(string[] args)
{
ValidateEnvironmentAndArgs(args);

return new TestWebHost(BuildTestServiceProvider());
}
}

private class ProgramWithCreateWebHostBuilder
{
public static TestWebHostBuilder CreateWebHostBuilder(string[] args)
{
ValidateEnvironmentAndArgs(args);

return new TestWebHostBuilder(BuildTestServiceProvider());
}
}

private class ProgramWithCreateHostBuilder
{
public static TestWebHostBuilder CreateHostBuilder(string[] args)
{
ValidateEnvironmentAndArgs(args);

return new TestWebHostBuilder(BuildTestServiceProvider());
}
}

[ConditionalFact]
public void Create_with_no_builder_method()
{
var factory = new TestAppServiceProviderFactory(
MockAssembly.Create(
new[] { typeof(ProgramWithNoHostBuilder) },
new MockMethodInfo(typeof(ProgramWithNoHostBuilder), InjectHostIntoDiagnostics)));

Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null);
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", null);
var services = factory.Create(new[] { "arg1" });

Assert.NotNull(services.GetRequiredService<TestService>());
}

private static void InjectHostIntoDiagnostics(object[] args)
{
Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"));
Assert.Single(args);
Assert.Equal((string[])args[0], new[] { "arg1", "--applicationName", "MockAssembly" });

using var diagnosticListener = new DiagnosticListener("Microsoft.Extensions.Hosting");

diagnosticListener.Write(
"HostBuilt",
new TestWebHost(BuildTestServiceProvider()));
}

private class ProgramWithNoHostBuilder
{
}

private static void ValidateEnvironmentAndArgs(string[] args)
{
Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"));
Assert.Equal(args, new[] { "arg1" });
}

private static ServiceProvider BuildTestServiceProvider()
=> new ServiceCollection()
.AddScoped<TestService>()
.BuildServiceProvider(validateScopes: true);

private class TestService
{
}

[ConditionalFact]
public void Create_works_when_no_BuildWebHost()
{
var factory = new TestAppServiceProviderFactory(
MockAssembly.Create(typeof(ProgramWithoutBuildWebHost)));

var services = factory.Create(Array.Empty<string>());

Assert.NotNull(services);
}

private class ProgramWithoutBuildWebHost
{
}

[ConditionalFact]
public void Create_works_when_BuildWebHost_throws()
{
var reporter = new TestOperationReporter();
var factory = new TestAppServiceProviderFactory(
MockAssembly.Create(typeof(ProgramWithThrowingBuildWebHost)),
reporter);

var services = factory.Create(Array.Empty<string>());

Assert.NotNull(services);
Assert.Contains(
"warn: " + DesignStrings.InvokeCreateHostBuilderFailed("This is a test."),
reporter.Messages);
}

private static class ProgramWithThrowingBuildWebHost
{
public static TestWebHost BuildWebHost(string[] args)
=> throw new Exception("This is a test.");
}
}

public class TestAppServiceProviderFactory : AppServiceProviderFactory
{
public TestAppServiceProviderFactory(Assembly startupAssembly, IOperationReporter reporter = null)
: base(startupAssembly, reporter ?? new TestOperationReporter())
{
}
}

public class TestWebHost
{
public TestWebHost(IServiceProvider services)
=> Services = services;

public IServiceProvider Services { get; }
}

public class TestWebHostBuilder
{
public TestWebHostBuilder(IServiceProvider services)
=> Services = services;

public IServiceProvider Services { get; }

public TestWebHost Build()
=> new TestWebHost(Services);
}

public class TestOperationReporter : IOperationReporter
{
private readonly List<string> _messages = new();

public IReadOnlyList<string> Messages
=> _messages;

public void Clear()
=> _messages.Clear();

public void WriteInformation(string message)
=> _messages.Add("info: " + message);

public void WriteVerbose(string message)
=> _messages.Add("verbose: " + message);

public void WriteWarning(string message)
=> _messages.Add("warn: " + message);

public void WriteError(string message)
=> _messages.Add("error: " + message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\EFCore.Relational\EFCore.Relational.csproj" />
<ProjectReference Include="..\EFCore.Relational.Tests\EFCore.Relational.Tests.csproj" />
<ProjectReference Include="..\EFCore.Specification.Tests\EFCore.Specification.Tests.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.1" />
<PackageReference Include="IdentityServer4.EntityFramework" Version="4.1.1" />
Expand Down

This file was deleted.

18 changes: 18 additions & 0 deletions test/EFCore.Design.Tests/TestUtilities/TestWebHostBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.EntityFrameworkCore.TestUtilities
{
public class TestWebHostBuilder
{
public TestWebHostBuilder(IServiceProvider services)
=> Services = services;

public IServiceProvider Services { get; }

public TestWebHost Build()
=> new(Services);
}
}
48 changes: 2 additions & 46 deletions test/EFCore.Tests/TestUtilities/MockAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -34,50 +33,7 @@ public MockAssembly(IEnumerable<TypeInfo> definedTypes, MethodInfo entryPoint)
public override AssemblyName GetName()
=> new(nameof(MockAssembly));

private class MockMethodInfo : MethodInfo
{
public MockMethodInfo(Type declaringType)
{
DeclaringType = declaringType;
}

public override Type DeclaringType { get; }

public override ICustomAttributeProvider ReturnTypeCustomAttributes
=> throw new NotImplementedException();

public override RuntimeMethodHandle MethodHandle
=> throw new NotImplementedException();

public override MethodAttributes Attributes
=> throw new NotImplementedException();

public override string Name
=> throw new NotImplementedException();

public override Type ReflectedType
=> throw new NotImplementedException();

public override MethodInfo GetBaseDefinition()
=> throw new NotImplementedException();

public override object[] GetCustomAttributes(bool inherit)
=> throw new NotImplementedException();

public override object[] GetCustomAttributes(Type attributeType, bool inherit)
=> throw new NotImplementedException();

public override MethodImplAttributes GetMethodImplementationFlags()
=> throw new NotImplementedException();

public override ParameterInfo[] GetParameters()
=> throw new NotImplementedException();

public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
=> throw new NotImplementedException();

public override bool IsDefined(Type attributeType, bool inherit)
=> throw new NotImplementedException();
}
public override string FullName
=> nameof(MockAssembly);
}
}
Loading

0 comments on commit efe0795

Please sign in to comment.