Skip to content

Commit 9a42746

Browse files
committed
Added Startup files where needed (#135 #168)
1 parent 8f99635 commit 9a42746

File tree

65 files changed

+403
-87
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+403
-87
lines changed

samples/ApplicationParts/ApplicationParts.Test/project.lock.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4179,7 +4179,8 @@
41794179
"type": "project",
41804180
"framework": ".NETStandard,Version=v1.6",
41814181
"dependencies": {
4182-
"Microsoft.Extensions.Configuration.Binder": "1.0.0"
4182+
"Microsoft.Extensions.Configuration.Binder": "1.0.0",
4183+
"System.Collections.Concurrent": "4.0.12"
41834184
},
41844185
"compile": {
41854186
"netstandard1.6/MyTested.AspNetCore.Mvc.Configuration.dll": {}

samples/MusicStore/MusicStore.Test/project.lock.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4383,7 +4383,8 @@
43834383
"type": "project",
43844384
"framework": ".NETStandard,Version=v1.6",
43854385
"dependencies": {
4386-
"Microsoft.Extensions.Configuration.Binder": "1.0.0"
4386+
"Microsoft.Extensions.Configuration.Binder": "1.0.0",
4387+
"System.Collections.Concurrent": "4.0.12"
43874388
},
43884389
"compile": {
43894390
"netstandard1.6/MyTested.AspNetCore.Mvc.Configuration.dll": {}

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplication.cs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ internal static Type StartupType
115115

116116
set
117117
{
118+
if (value != null && Configuration().General().NoStartup())
119+
{
120+
throw new InvalidOperationException($"The test configuration ('testconfig.json' file by default) contained 'true' value for the 'General.NoStartup' option but {value.GetName()} class was set through the 'StartsFrom<TStartup>()' method. Either do not set the class or change the option to 'false'.");
121+
}
122+
118123
if (startupType != null && Configuration().General().AsynchronousTests())
119124
{
120-
throw new InvalidOperationException("Multiple Startup types per test project while running asynchronous tests is not supported. Either set 'General.AsynchronousTests' in the 'testconfig.json' file to 'false' or separate your tests into different test projects. The latter is recommended. If you choose the first option, you may need to disable asynchronous testing in your preferred test runner too.");
125+
throw new InvalidOperationException("Multiple Startup types per test project while running asynchronous tests is not supported. Either set 'General.AsynchronousTests' in the test configuration ('testconfig.json' file by default) to 'false' or separate your tests into different test projects. The latter is recommended. If you choose the first option, you may need to disable asynchronous testing in your preferred test runner too.");
121126
}
122127

123128
if (initialiazed)
@@ -163,8 +168,8 @@ internal static IHostingEnvironment Environment
163168
}
164169
}
165170

166-
internal static string ApplicationName =>
167-
Configuration().General().ApplicationName()
171+
internal static string ApplicationName
172+
=> Configuration().General().ApplicationName()
168173
?? TestAssembly?.GetName().Name
169174
?? StartupAssemblyName
170175
?? PlatformServices.Default.Application.ApplicationName;
@@ -192,6 +197,11 @@ public static TestConfiguration Configuration()
192197

193198
public static void TryInitialize()
194199
{
200+
if (Configuration().General().NoStartup())
201+
{
202+
return;
203+
}
204+
195205
lock (Sync)
196206
{
197207
if (!initialiazed && Configuration().General().AutomaticStartup())
@@ -200,7 +210,11 @@ public static void TryInitialize()
200210

201211
if (defaultStartupType == null)
202212
{
203-
throw new InvalidOperationException($"{Environment.EnvironmentName}Startup class could not be found at the root of the test project. Either add it or set 'General.AutomaticStartup' in the 'testconfig.json' file to 'false'.");
213+
throw new InvalidOperationException($"{Environment.EnvironmentName}Startup class could not be found at the root of the test project. Either add it or set 'General.AutomaticStartup' in the test configuration ('testconfig.json' file by default) to 'false'.");
214+
}
215+
else if (Configuration().General().NoStartup())
216+
{
217+
throw new InvalidOperationException($"The test configuration ('testconfig.json' file by default) contained 'true' value for the 'General.NoStartup' option but {Environment.EnvironmentName}Startup class was located at the root of the project. Either remove the class or change the option to 'false'.");
204218
}
205219

206220
startupType = defaultStartupType;
@@ -210,10 +224,20 @@ public static void TryInitialize()
210224
}
211225

212226
internal static DependencyContext LoadDependencyContext()
213-
=> TestAssembly != null
214-
? DependencyContext.Load(TestAssembly)
215-
?? DependencyContext.Default
216-
: DependencyContext.Default;
227+
{
228+
DependencyContext dependencyContext = null;
229+
if (TestAssembly != null)
230+
{
231+
dependencyContext = DependencyContext.Load(TestAssembly);
232+
}
233+
234+
if (dependencyContext == null)
235+
{
236+
dependencyContext = DependencyContext.Default;
237+
}
238+
239+
return dependencyContext;
240+
}
217241

218242
internal static void LoadPlugins(DependencyContext dependencyContext)
219243
{
@@ -286,6 +310,11 @@ internal static Type TryFindDefaultStartupType()
286310

287311
private static void Initialize()
288312
{
313+
if (StartupType == null && !Configuration().General().NoStartup())
314+
{
315+
throw new InvalidOperationException($"The test configuration ('testconfig.json' file by default) contained 'false' value for the 'General.NoStartup' option but a Startup class was not provided. Either add {Environment.EnvironmentName}Startup class to the root of the test project or set it by calling 'StartsFrom<TStartup>(). Additionally, if you do not want to use a global test application for all test cases in this project, you may change the test configuration option to 'true'.");
316+
}
317+
289318
PrepareLicensing();
290319

291320
var dependencyContext = LoadDependencyContext();
@@ -316,18 +345,20 @@ private static void PrepareLicensing()
316345
TestCounter.SetLicenseData(
317346
Configuration().Licenses(),
318347
DateTime.ParseExact(ReleaseDate, "yyyy-MM-dd", CultureInfo.InvariantCulture),
319-
TestAssembly?.GetName().Name ?? StartupAssemblyName);
348+
TestAssembly?.GetName().Name ?? StartupAssemblyName ?? DependencyContext
349+
.Default
350+
.GetDefaultAssemblyNames()
351+
.First()
352+
.Name);
320353
}
321354

322355
private static IHostingEnvironment PrepareEnvironment()
323-
{
324-
return new HostingEnvironment
356+
=> new HostingEnvironment
325357
{
326358
ApplicationName = ApplicationName,
327359
EnvironmentName = Configuration().General().EnvironmentName(),
328360
ContentRootPath = PlatformServices.Default.Application.ApplicationBasePath
329361
};
330-
}
331362

332363
private static IServiceCollection GetInitialServiceCollection()
333364
{
@@ -530,7 +561,7 @@ private static void Reset()
530561
InitializationPlugins.Clear();
531562
LicenseValidator.ClearLicenseDetails();
532563
}
533-
564+
534565
#if NET451
535566
private static void FindTestAssembly()
536567
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class GeneralTestConfiguration : BaseConfiguration
77
private const string AsynchronousTestsConfigKey = "AsynchronousTests";
88
private const string WebAssemblyNameConfigKey = "WebAssemblyName";
99
private const string TestAssemblyNameConfigKey = "TestAssemblyName";
10+
private const string NoStartupConfigKey = "NoStartup";
1011
private const string AutomaticStartupConfigKey = "AutomaticStartup";
1112
private const string StartupTypeConfigKey = "StartupType";
1213
private const string ApplicationNameConfigKey = "ApplicationName";
@@ -26,6 +27,8 @@ public GeneralTestConfiguration(IConfiguration configuration)
2627

2728
public bool AutomaticStartup() => this.GetValue(AutomaticStartupConfigKey, true);
2829

30+
public bool NoStartup() => this.GetValue(NoStartupConfigKey, false);
31+
2932
public string StartupType() => this.GetValue(StartupTypeConfigKey);
3033

3134
public string ApplicationName() => this.GetValue(ApplicationNameConfigKey);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.Configuration
2+
{
3+
public static class TestConfigurationAbstractionsExtensions
4+
{
5+
public static GeneralTestConfiguration General(this TestConfiguration testConfiguration)
6+
=> testConfiguration.GetConfiguration(() => new GeneralTestConfiguration(testConfiguration.Configuration));
7+
}
8+
}

src/MyTested.AspNetCore.Mvc.Abstractions/project.lock.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2865,7 +2865,8 @@
28652865
"type": "project",
28662866
"framework": ".NETStandard,Version=v1.6",
28672867
"dependencies": {
2868-
"Microsoft.Extensions.Configuration.Binder": "1.0.0"
2868+
"Microsoft.Extensions.Configuration.Binder": "1.0.0",
2869+
"System.Collections.Concurrent": "4.0.12"
28692870
},
28702871
"compile": {
28712872
"netstandard1.6/MyTested.AspNetCore.Mvc.Configuration.dll": {}

src/MyTested.AspNetCore.Mvc.Authentication/project.lock.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3027,7 +3027,8 @@
30273027
"type": "project",
30283028
"framework": ".NETStandard,Version=v1.6",
30293029
"dependencies": {
3030-
"Microsoft.Extensions.Configuration.Binder": "1.0.0"
3030+
"Microsoft.Extensions.Configuration.Binder": "1.0.0",
3031+
"System.Collections.Concurrent": "4.0.12"
30313032
},
30323033
"compile": {
30333034
"netstandard1.6/MyTested.AspNetCore.Mvc.Configuration.dll": {}

src/MyTested.AspNetCore.Mvc.Caching/project.lock.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2959,7 +2959,8 @@
29592959
"type": "project",
29602960
"framework": ".NETStandard,Version=v1.6",
29612961
"dependencies": {
2962-
"Microsoft.Extensions.Configuration.Binder": "1.0.0"
2962+
"Microsoft.Extensions.Configuration.Binder": "1.0.0",
2963+
"System.Collections.Concurrent": "4.0.12"
29632964
},
29642965
"compile": {
29652966
"netstandard1.6/MyTested.AspNetCore.Mvc.Configuration.dll": {}

src/MyTested.AspNetCore.Mvc.Configuration/Internal/Configuration/TestConfiguration.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Collections.Concurrent;
56
using Microsoft.Extensions.Configuration;
67

78
public class TestConfiguration : BaseConfiguration
89
{
910
private const string LicenseConfigKey = "License";
1011
private const string LicensesConfigKey = "Licenses";
1112

12-
private readonly IDictionary<Type, BaseConfiguration> configurationTypes;
13+
private readonly ConcurrentDictionary<Type, BaseConfiguration> configurationTypes;
1314

1415
private TestConfiguration(IConfiguration configuration)
1516
: base(configuration)
1617
{
17-
this.configurationTypes = new Dictionary<Type, BaseConfiguration>();
18+
this.configurationTypes = new ConcurrentDictionary<Type, BaseConfiguration>();
1819
}
1920

20-
public GeneralTestConfiguration General()
21-
=> this.GetConfiguration(() => new GeneralTestConfiguration(this.Configuration));
22-
2321
public IEnumerable<string> Licenses()
2422
{
2523
var license = this.Configuration[LicenseConfigKey];
@@ -35,15 +33,8 @@ public IEnumerable<string> Licenses()
3533

3634
public TConfiguration GetConfiguration<TConfiguration>(Func<TConfiguration> configurationFactory)
3735
where TConfiguration : BaseConfiguration
38-
{
39-
var typeOfConfiguration = typeof(TConfiguration);
40-
if (!this.configurationTypes.ContainsKey(typeOfConfiguration))
41-
{
42-
this.configurationTypes[typeOfConfiguration] = configurationFactory();
43-
}
44-
45-
return (TConfiguration)this.configurationTypes[typeOfConfiguration];
46-
}
36+
=> (TConfiguration)this.configurationTypes
37+
.GetOrAdd(typeof(TConfiguration), _ => configurationFactory());
4738

4839
public static TestConfiguration With(IConfiguration configuration)
4940
{

src/MyTested.AspNetCore.Mvc.Configuration/project.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
"frameworks": {
3434
"net451": {},
35-
"netstandard1.6": {}
35+
"netstandard1.6": {
36+
"dependencies": {
37+
"System.Collections.Concurrent": "4.0.12"
38+
}
39+
}
3640
}
3741
}

0 commit comments

Comments
 (0)