Skip to content

Commit dbbeaa8

Browse files
committed
Separated configuration into sections (#165)
1 parent 67014df commit dbbeaa8

File tree

22 files changed

+183
-79
lines changed

22 files changed

+183
-79
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Globalization;
77
using System.Linq;
88
using System.Reflection;
9+
using Configuration;
910
using Licensing;
1011
using Logging;
1112
using Microsoft.AspNetCore.Builder;
@@ -163,21 +164,21 @@ public static TestConfiguration TestConfiguration
163164
}
164165

165166
internal static string ApplicationName =>
166-
TestConfiguration.ApplicationName
167+
TestConfiguration.General.ApplicationName
167168
?? TestAssemblyName
168169
?? PlatformServices.Default.Application.ApplicationName;
169170

170171
public static void TryInitialize()
171172
{
172173
lock (Sync)
173174
{
174-
if (!initialiazed && TestConfiguration.AutomaticStartup)
175+
if (!initialiazed && TestConfiguration.General.AutomaticStartup)
175176
{
176177
var defaultStartupType = TryFindDefaultStartupType();
177178

178179
if (defaultStartupType == null)
179180
{
180-
throw new InvalidOperationException($"{Environment.EnvironmentName}Startup class could not be found at the root of the test project. Either add it or set 'AutomaticStartup' in the 'testconfig.json' file to 'false'.");
181+
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'.");
181182
}
182183

183184
startupType = defaultStartupType;
@@ -240,7 +241,7 @@ internal static Type TryFindDefaultStartupType()
240241
{
241242
var applicationAssembly = Assembly.Load(new AssemblyName(testAssemblyName));
242243

243-
var startupName = TestConfiguration.FullStartupName ?? $"{Environment.EnvironmentName}Startup";
244+
var startupName = TestConfiguration.General.FullStartupName ?? $"{Environment.EnvironmentName}Startup";
244245

245246
// check root of the test project
246247
var startup =
@@ -274,7 +275,7 @@ private static IConfiguration PrepareTestConfiguration()
274275

275276
private static void FindTestAssemblyName()
276277
{
277-
testAssemblyName = TestConfiguration.TestAssemblyName ?? DependencyContext
278+
testAssemblyName = TestConfiguration.General.TestAssemblyName ?? DependencyContext
278279
.Default
279280
.GetDefaultAssemblyNames()
280281
.First()
@@ -294,7 +295,7 @@ private static IHostingEnvironment PrepareEnvironment()
294295
return new HostingEnvironment
295296
{
296297
ApplicationName = ApplicationName,
297-
EnvironmentName = TestConfiguration.EnvironmentName,
298+
EnvironmentName = TestConfiguration.General.EnvironmentName,
298299
ContentRootPath = PlatformServices.Default.Application.ApplicationBasePath
299300
};
300301
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.Configuration
2+
{
3+
using System;
4+
using Microsoft.Extensions.Configuration;
5+
6+
public abstract class BaseConfiguration
7+
{
8+
private IConfiguration configuration;
9+
10+
public BaseConfiguration(IConfiguration configuration)
11+
{
12+
this.Configuration = configuration;
13+
}
14+
15+
protected IConfiguration Configuration
16+
{
17+
get
18+
{
19+
return this.configuration;
20+
}
21+
22+
private set
23+
{
24+
if (value == null)
25+
{
26+
throw new ArgumentNullException("Test configuration cannot be null.");
27+
}
28+
29+
this.configuration = value;
30+
}
31+
}
32+
33+
protected string Prefix { get; set; }
34+
35+
protected string GetValue(string key)
36+
=> this.GetValue<string>(key, null);
37+
38+
protected T GetValue<T>(string key, T defaultValue)
39+
=> this.Configuration.GetSection(this.Prefix).GetValue(key, defaultValue);
40+
}
41+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.Configuration
2+
{
3+
using Microsoft.Extensions.Configuration;
4+
5+
public class ControllersTestConfiguration : BaseConfiguration
6+
{
7+
private const string ModelStateValidationConfigKey = "ModelStateValidation";
8+
9+
public ControllersTestConfiguration(IConfiguration configuration)
10+
: base(configuration)
11+
{
12+
this.Prefix = "Controllers";
13+
}
14+
15+
public bool ModelStateValidation => this.GetValue(ModelStateValidationConfigKey, true);
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.Configuration
2+
{
3+
using Microsoft.Extensions.Configuration;
4+
5+
public class GeneralTestConfiguration : BaseConfiguration
6+
{
7+
private const string TestAssemblyNameConfigKey = "TestAssemblyName";
8+
private const string AutomaticStartupConfigKey = "AutomaticStartup";
9+
private const string FullStartupNameConfigKey = "FullStartupName";
10+
private const string ApplicationNameConfigKey = "ApplicationName";
11+
private const string EnvironmentNameConfigKey = "Environment";
12+
13+
public GeneralTestConfiguration(IConfiguration configuration)
14+
: base(configuration)
15+
{
16+
this.Prefix = "General";
17+
}
18+
19+
public string TestAssemblyName => this.GetValue(TestAssemblyNameConfigKey);
20+
21+
public string ApplicationName => this.GetValue(ApplicationNameConfigKey);
22+
23+
public string EnvironmentName => this.GetValue(EnvironmentNameConfigKey, "Test");
24+
25+
public bool AutomaticStartup => this.GetValue(AutomaticStartupConfigKey, true);
26+
27+
public string FullStartupName => this.GetValue(FullStartupNameConfigKey);
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.Configuration
2+
{
3+
using System.Collections.Generic;
4+
using Microsoft.Extensions.Configuration;
5+
6+
public class TestConfiguration : BaseConfiguration
7+
{
8+
private const string LicenseConfigKey = "License";
9+
private const string LicensesConfigKey = "Licenses";
10+
11+
private TestConfiguration(IConfiguration configuration)
12+
: base(configuration)
13+
{
14+
this.General = new GeneralTestConfiguration(this.Configuration);
15+
this.Controllers = new ControllersTestConfiguration(this.Configuration);
16+
}
17+
18+
public GeneralTestConfiguration General { get; private set; }
19+
20+
public ControllersTestConfiguration Controllers { get; private set; }
21+
22+
public IEnumerable<string> Licenses
23+
{
24+
get
25+
{
26+
var license = this.Configuration[LicenseConfigKey];
27+
if (license != null)
28+
{
29+
return new[] { license };
30+
}
31+
32+
var licenses = new List<string>();
33+
this.Configuration.GetSection(LicensesConfigKey).Bind(licenses);
34+
return licenses;
35+
}
36+
}
37+
38+
public static TestConfiguration With(IConfiguration configuration)
39+
{
40+
return new TestConfiguration(configuration);
41+
}
42+
}
43+
}

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

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Controllers/ControllerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public ControllerBuilder(ControllerTestContext testContext)
3535
{
3636
this.TestContext = testContext;
3737

38-
this.EnabledValidation = TestApplication.TestConfiguration.ModelStateValidation;
38+
this.EnabledValidation = TestApplication.TestConfiguration.Controllers.ModelStateValidation;
3939

4040
#if NETSTANDARD1_6
4141
this.ValidateControllerType();
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"AutomaticStartup": false
2+
"General": {
3+
"AutomaticStartup": false
4+
}
35
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"AutomaticStartup": false
2+
"General": {
3+
"AutomaticStartup": false
4+
}
35
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2-
"AutomaticStartup": false,
2+
"General": {
3+
"AutomaticStartup": false
4+
},
35
"License": "1-3i7E5P3qX5IUWHIAfcXG6DSbOwUBidygp8bnYY/2Rd9zA15SwRWP6QDDp+m/dDTZNBFX2eIHcU/gdcdm83SL695kf3VyvMPw+iyPN6QBh/WnfQwGLqBecrQw+WNPJMz6UgXi2q4e4s/D8/iSjMlwCnzJvC2Yv3zSuADdWObQsygxOjk5OTktMTItMzE6YWRtaW5AbXl0ZXN0ZWRhc3AubmV0Ok15VGVzdGVkLk12YyBUZXN0czpGdWxsOk15VGVzdGVkLkFzcE5ldENvcmUuTXZjLg=="
46
}

0 commit comments

Comments
 (0)