-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
AppServiceProviderFactoryTest.cs
200 lines (157 loc) · 5.93 KB
/
AppServiceProviderFactoryTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Internal;
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(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);
}