Commonize code to limit functional tests to a single site each

This commit is contained in:
Ryan Nowak 2014-06-23 11:12:32 -07:00
parent dd2216fa89
commit 0d7f38e10e
18 changed files with 201 additions and 49 deletions

15
Mvc.sln
View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.21722.1
VisualStudioVersion = 14.0.21806.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE4C74-D06F-4874-A166-33305D2643CE}"
EndProject
@ -43,6 +43,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "InlineConstraintsWebSite",
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AutofacWebSite", "test\WebSites\AutofacWebSite\AutofacWebSite.kproj", "{07C0E921-FCBB-458C-AC11-3D01CE68B16B}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Mvc.TestConfiguration", "test\WebSites\Microsoft.AspNet.Mvc.TestConfiguration\Microsoft.AspNet.Mvc.TestConfiguration.kproj", "{680D75ED-601F-4D86-B01B-1072D0C31B8C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -213,6 +215,16 @@ Global
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|x86.ActiveCfg = Release|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Debug|x86.ActiveCfg = Debug|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Release|Any CPU.Build.0 = Release|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{680D75ED-601F-4D86-B01B-1072D0C31B8C}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -235,5 +247,6 @@ Global
{DB79BCBA-9538-4A53-87D9-77728E2BAA39} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{EA34877F-1AC1-42B7-B4E6-15A093F40CAE} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{07C0E921-FCBB-458C-AC11-3D01CE68B16B} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{680D75ED-601F-4D86-B01B-1072D0C31B8C} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
EndGlobalSection
EndGlobal

View File

@ -33,6 +33,8 @@
<Compile Include="ActivatorTests.cs" />
<Compile Include="DependencyResolverTests.cs" />
<Compile Include="InlineConstraintTests.cs" />
<Compile Include="TestAssemblyProvider.cs" />
<Compile Include="TestConfigurationProvider.cs" />
<Compile Include="TestHelper.cs" />
<Compile Include="BasicTests.cs" />
<Compile Include="HttpResponseHelpers.cs" />

View File

@ -0,0 +1,25 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Reflection;
namespace Microsoft.AspNet.Mvc.FunctionalTests
{
/// <summary>
/// Limits MVC to use a single Assembly for controller discovery.
/// </summary>
/// <remarks>
/// This is a generic type because it needs to instantiated by a service provider to replace
/// a built-in MVC service.
/// </remarks>
public class TestAssemblyProvider<T> : IControllerAssemblyProvider
{
public TestAssemblyProvider()
{
CandidateAssemblies = new Assembly[] { typeof(T).GetTypeInfo().Assembly };
}
public IEnumerable<Assembly> CandidateAssemblies { get; private set; }
}
}

View File

@ -0,0 +1,18 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Framework.ConfigurationModel;
namespace Microsoft.AspNet.Mvc.FunctionalTests
{
public class TestConfigurationProvider : ITestConfigurationProvider
{
public TestConfigurationProvider()
{
Configuration = new Configuration();
Configuration.Add(new MemoryConfigurationSource());
}
public Configuration Configuration { get; set; }
}
}

View File

@ -14,6 +14,7 @@ using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.Runtime;
using Microsoft.Framework.Runtime.Infrastructure;
using Xunit;
using Microsoft.Framework.ConfigurationModel;
namespace Microsoft.AspNet.Mvc.FunctionalTests
{
@ -32,12 +33,25 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// environment value so that components like the view engine work properly in the context of the
// test.
var appBasePath = CalculateApplicationBasePath(appEnvironment, applicationWebSiteName);
var provider = new ServiceCollection()
.AddInstance(typeof(IApplicationEnvironment),
new TestApplicationEnvironment(appEnvironment, appBasePath))
.BuildServiceProvider(originalProvider);
var services = new ServiceCollection();
services.AddInstance(
typeof(IApplicationEnvironment),
new TestApplicationEnvironment(appEnvironment, appBasePath));
return provider;
// Injecting a custom assembly provider via configuration setting. It's not good enough to just
// add the service directly since it's registered by MVC.
var providerType = CreateAssemblyProviderType(applicationWebSiteName);
var configuration = new TestConfigurationProvider();
configuration.Configuration.Set(
typeof(IControllerAssemblyProvider).FullName,
providerType.AssemblyQualifiedName);
services.AddInstance(
typeof(ITestConfigurationProvider),
configuration);
return services.BuildServiceProvider(originalProvider);
}
// Calculate the path relative to the application base path.
@ -53,5 +67,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// Mvc/test/WebSites/applicationWebSiteName
return Path.GetFullPath(Path.Combine(appBase, "..", "WebSites", applicationWebSiteName));
}
private static Type CreateAssemblyProviderType(string siteName)
{
// Creates a service type that will limit MVC to only the controllers in the test site.
// We only want this to happen when running in proc.
var assembly = Assembly.Load(new AssemblyName(siteName));
var providerType = typeof(TestAssemblyProvider<>).MakeGenericType(assembly.GetExportedTypes()[0]);
return providerType;
}
}
}

View File

@ -3,14 +3,16 @@
"warningsAsErrors": true
},
"dependencies": {
"BasicWebSite": "",
"ActivatorWebSite": "",
"BasicWebSite": "",
"InlineConstraintsWebSite": "",
"Microsoft.AspNet.TestHost": "1.0.0-*",
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*",
"Xunit.KRunner": "1.0.0-*",
"Microsoft.AspNet.Mvc.TestConfiguration": "",
"Microsoft.Framework.ConfigurationModel": "1.0.0-*",
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*",
"Microsoft.Framework.DependencyInjection": "1.0.0-*"
"Microsoft.Framework.DependencyInjection": "1.0.0-*",
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*",
"Xunit.KRunner": "1.0.0-*"
},
"commands": {
"test": "Xunit.KRunner"

View File

@ -11,11 +11,13 @@ namespace ActivatorWebSite
{
public void Configure(IBuilder app)
{
var configuration = app.GetTestConfiguration();
// Set up application services
app.UseServices(services =>
{
// Add MVC services to the services container
services.AddMvc();
services.AddMvc(configuration);
services.AddInstance(new MyService());
});

View File

@ -1,7 +1,8 @@
{
"dependencies": {
"Microsoft.AspNet.Mvc": "",
"Microsoft.AspNet.Server.IIS": "1.0.0-*"
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Mvc.TestConfiguration": ""
},
"configurations": {
"net45": { },

View File

@ -1,6 +1,7 @@
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
namespace BasicWebSite
@ -9,11 +10,13 @@ namespace BasicWebSite
{
public void Configure(IBuilder app)
{
var configuration = app.GetTestConfiguration();
// Set up application services
app.UseServices(services =>
{
// Add MVC services to the services container
services.AddMvc();
services.AddMvc(configuration);
services.AddSingleton<INestedProvider<ActionDescriptorProviderContext>, ActionDescriptorCreationCounter>();
});

View File

@ -1,7 +1,9 @@
{
"dependencies": {
"Microsoft.AspNet.Mvc": "",
"Microsoft.AspNet.Server.IIS": "1.0.0-*"
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Mvc.TestConfiguration": "",
"Microsoft.Framework.ConfigurationModel": ""
},
"configurations": {
"net45": { },

View File

@ -36,7 +36,6 @@
<Compile Include="DefaultCommandLineArgumentBuilder.cs" />
<Compile Include="ICommandLineArgumentProvider.cs" />
<Compile Include="Startup.cs" />
<Compile Include="TestControllerAssemblyProvider.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="App_Data\config.json" />

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
@ -17,40 +16,38 @@ namespace InlineConstraints
public void Configure(IBuilder app)
{
// Set up application services
app.UseServices(services =>
{
// Add MVC services to the services container
services.AddMvc();
var configuration = app.GetTestConfiguration();
// Add a custom assembly provider so that we add only controllers present in
// this assembly.
services.AddTransient<IControllerAssemblyProvider, TestControllerAssemblyProvider>();
});
configuration.AddEnvironmentVariables();
var config = new Configuration();
config.AddEnvironmentVariables();
var commandLineBuilder = app.ApplicationServices.GetService<ICommandLineArgumentBuilder>();
string appConfigPath;
if (config.TryGet("AppConfigPath", out appConfigPath))
if (configuration.TryGet("AppConfigPath", out appConfigPath))
{
config.AddJsonFile(appConfigPath);
configuration.AddJsonFile(appConfigPath);
}
else if (commandLineBuilder != null)
{
var args = commandLineBuilder.Build();
config.AddCommandLine(args.ToArray());
configuration.AddCommandLine(args.ToArray());
}
else
{
var basePath = app.ApplicationServices.GetService<IApplicationEnvironment>().ApplicationBasePath;
config.AddJsonFile(Path.Combine(basePath, @"App_Data\config.json"));
configuration.AddJsonFile(Path.Combine(basePath, @"App_Data\config.json"));
}
// Set up application services
app.UseServices(services =>
{
// Add MVC services to the services container
services.AddMvc(configuration);
});
// Add MVC to the request pipeline
app.UseMvc(routes =>
{
foreach (var item in GetDataFromConfig(config))
foreach (var item in GetDataFromConfig(configuration))
{
routes.MapRoute(item.RouteName, item.RouteTemplateValue);
}

View File

@ -1,17 +0,0 @@
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNet.Mvc;
namespace InlineConstraints
{
public class TestControllerAssemblyProvider : IControllerAssemblyProvider
{
public IEnumerable<Assembly> CandidateAssemblies
{
get
{
return new[] { typeof(TestControllerAssemblyProvider).GetTypeInfo().Assembly };
}
}
}
}

View File

@ -2,6 +2,7 @@
"dependencies": {
"Microsoft.AspNet.Mvc": "",
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Mvc.TestConfiguration": "",
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*"
},
"configurations": {

View File

@ -0,0 +1,22 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Builder
{
public static class BuilderExtensions
{
public static Configuration GetTestConfiguration(this IBuilder app)
{
var configurationProvider = app.ApplicationServices.GetServiceOrDefault<ITestConfigurationProvider>();
var configuration = configurationProvider == null
? new Configuration()
: configurationProvider.Configuration;
return configuration;
}
}
}

View File

@ -0,0 +1,12 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Framework.ConfigurationModel;
namespace Microsoft.AspNet.Mvc
{
public interface ITestConfigurationProvider
{
Configuration Configuration { get; }
}
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>680d75ed-601f-4d86-b01b-1072d0c31b8c</ProjectGuid>
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup Condition="$(OutputType) == 'Console'">
<DebuggerFlavor>ConsoleDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="$(OutputType) == 'Web'">
<DebuggerFlavor>WebDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'" Label="Configuration">
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="BuilderExtensions.cs" />
<Compile Include="ITestConfigurationProvider.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="project.json" />
</ItemGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -0,0 +1,11 @@
{
"dependencies": {
"Microsoft.AspNet.Http": "1.0.0-*",
"Microsoft.Framework.ConfigurationModel": "1.0.0-*",
"Microsoft.Framework.DependencyInjection": "1.0.0-*"
},
"configurations" : {
"net45" : { },
"k10" : { }
}
}