-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[6.0] Test changes to make sure applicationName is set when using new…
… template pattern (#26204)
- Loading branch information
1 parent
9dd8f67
commit 2e0c4a4
Showing
6 changed files
with
286 additions
and
130 deletions.
There are no files selected for viewing
204 changes: 204 additions & 0 deletions
204
test/EFCore.AspNet.InMemory.FunctionalTests/AppServiceProviderFactoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 0 additions & 84 deletions
84
test/EFCore.Design.Tests/Design/Internal/AppServiceProviderFactoryTest.cs
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
test/EFCore.Design.Tests/TestUtilities/TestWebHostBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.