From 0d7f38e10e9c538b0c963ede8769acc18d9a8ec0 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Mon, 23 Jun 2014 11:12:32 -0700 Subject: [PATCH] Commonize code to limit functional tests to a single site each --- Mvc.sln | 15 +++++++- ...Microsoft.AspNet.Mvc.FunctionalTests.kproj | 2 ++ .../TestAssemblyProvider.cs | 25 +++++++++++++ .../TestConfigurationProvider.cs | 18 ++++++++++ .../TestHelper.cs | 34 +++++++++++++++--- .../project.json | 10 +++--- test/WebSites/ActivatorWebSite/Startup.cs | 4 ++- test/WebSites/ActivatorWebSite/project.json | 3 +- test/WebSites/BasicWebSite/Startup.cs | 5 ++- test/WebSites/BasicWebSite/project.json | 4 ++- .../InlineConstraintsWebSite.kproj | 1 - .../InlineConstraintsWebSite/Startup.cs | 31 ++++++++-------- .../TestControllerAssemblyProvider.cs | 17 --------- .../InlineConstraintsWebSite/project.json | 1 + .../BuilderExtensions.cs | 22 ++++++++++++ .../ITestConfigurationProvider.cs | 12 +++++++ ...crosoft.AspNet.Mvc.TestConfiguration.kproj | 35 +++++++++++++++++++ .../project.json | 11 ++++++ 18 files changed, 201 insertions(+), 49 deletions(-) create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/TestAssemblyProvider.cs create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/TestConfigurationProvider.cs delete mode 100644 test/WebSites/InlineConstraintsWebSite/TestControllerAssemblyProvider.cs create mode 100644 test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs create mode 100644 test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/ITestConfigurationProvider.cs create mode 100644 test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/Microsoft.AspNet.Mvc.TestConfiguration.kproj create mode 100644 test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json diff --git a/Mvc.sln b/Mvc.sln index 3620af64f9..e5781ed5c3 100644 --- a/Mvc.sln +++ b/Mvc.sln @@ -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 diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj b/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj index cfa99bcd09..d5a62515f4 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj @@ -33,6 +33,8 @@ + + diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestAssemblyProvider.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestAssemblyProvider.cs new file mode 100644 index 0000000000..8ed5e855c6 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestAssemblyProvider.cs @@ -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 +{ + /// + /// Limits MVC to use a single Assembly for controller discovery. + /// + /// + /// This is a generic type because it needs to instantiated by a service provider to replace + /// a built-in MVC service. + /// + public class TestAssemblyProvider : IControllerAssemblyProvider + { + public TestAssemblyProvider() + { + CandidateAssemblies = new Assembly[] { typeof(T).GetTypeInfo().Assembly }; + } + + public IEnumerable CandidateAssemblies { get; private set; } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestConfigurationProvider.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestConfigurationProvider.cs new file mode 100644 index 0000000000..c5faf9311f --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestConfigurationProvider.cs @@ -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; } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs index 89f1c508ce..caa0dc09c7 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs @@ -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; + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index b4ff151da0..98d04a030f 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -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" diff --git a/test/WebSites/ActivatorWebSite/Startup.cs b/test/WebSites/ActivatorWebSite/Startup.cs index f0a1431a93..61605409af 100644 --- a/test/WebSites/ActivatorWebSite/Startup.cs +++ b/test/WebSites/ActivatorWebSite/Startup.cs @@ -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()); }); diff --git a/test/WebSites/ActivatorWebSite/project.json b/test/WebSites/ActivatorWebSite/project.json index 5935e12903..696e8b9d04 100644 --- a/test/WebSites/ActivatorWebSite/project.json +++ b/test/WebSites/ActivatorWebSite/project.json @@ -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": { }, diff --git a/test/WebSites/BasicWebSite/Startup.cs b/test/WebSites/BasicWebSite/Startup.cs index a411d0998d..5686feef0d 100644 --- a/test/WebSites/BasicWebSite/Startup.cs +++ b/test/WebSites/BasicWebSite/Startup.cs @@ -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, ActionDescriptorCreationCounter>(); }); diff --git a/test/WebSites/BasicWebSite/project.json b/test/WebSites/BasicWebSite/project.json index 5935e12903..8c1ff28de3 100644 --- a/test/WebSites/BasicWebSite/project.json +++ b/test/WebSites/BasicWebSite/project.json @@ -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": { }, diff --git a/test/WebSites/InlineConstraintsWebSite/InlineConstraintsWebSite.kproj b/test/WebSites/InlineConstraintsWebSite/InlineConstraintsWebSite.kproj index e851782a03..7e3c0b295a 100644 --- a/test/WebSites/InlineConstraintsWebSite/InlineConstraintsWebSite.kproj +++ b/test/WebSites/InlineConstraintsWebSite/InlineConstraintsWebSite.kproj @@ -36,7 +36,6 @@ - diff --git a/test/WebSites/InlineConstraintsWebSite/Startup.cs b/test/WebSites/InlineConstraintsWebSite/Startup.cs index ca8accbc27..5becb8178f 100644 --- a/test/WebSites/InlineConstraintsWebSite/Startup.cs +++ b/test/WebSites/InlineConstraintsWebSite/Startup.cs @@ -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(); - }); + configuration.AddEnvironmentVariables(); - var config = new Configuration(); - config.AddEnvironmentVariables(); var commandLineBuilder = app.ApplicationServices.GetService(); 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().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); } diff --git a/test/WebSites/InlineConstraintsWebSite/TestControllerAssemblyProvider.cs b/test/WebSites/InlineConstraintsWebSite/TestControllerAssemblyProvider.cs deleted file mode 100644 index 66b08bec8d..0000000000 --- a/test/WebSites/InlineConstraintsWebSite/TestControllerAssemblyProvider.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; -using System.Reflection; -using Microsoft.AspNet.Mvc; - -namespace InlineConstraints -{ - public class TestControllerAssemblyProvider : IControllerAssemblyProvider - { - public IEnumerable CandidateAssemblies - { - get - { - return new[] { typeof(TestControllerAssemblyProvider).GetTypeInfo().Assembly }; - } - } - } -} diff --git a/test/WebSites/InlineConstraintsWebSite/project.json b/test/WebSites/InlineConstraintsWebSite/project.json index 4ffe0ea5d1..772c2f1bc1 100644 --- a/test/WebSites/InlineConstraintsWebSite/project.json +++ b/test/WebSites/InlineConstraintsWebSite/project.json @@ -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": { diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs new file mode 100644 index 0000000000..2af91cce7b --- /dev/null +++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs @@ -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(); + var configuration = configurationProvider == null + ? new Configuration() + : configurationProvider.Configuration; + + return configuration; + } + } +} \ No newline at end of file diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/ITestConfigurationProvider.cs b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/ITestConfigurationProvider.cs new file mode 100644 index 0000000000..d11f5de0c8 --- /dev/null +++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/ITestConfigurationProvider.cs @@ -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; } + } +} \ No newline at end of file diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/Microsoft.AspNet.Mvc.TestConfiguration.kproj b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/Microsoft.AspNet.Mvc.TestConfiguration.kproj new file mode 100644 index 0000000000..8c99cd5fe9 --- /dev/null +++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/Microsoft.AspNet.Mvc.TestConfiguration.kproj @@ -0,0 +1,35 @@ + + + + 12.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + Debug + AnyCPU + + + + 680d75ed-601f-4d86-b01b-1072d0c31b8c + Library + + + ConsoleDebugger + + + WebDebugger + + + + + 2.0 + + + + + + + + + + \ No newline at end of file diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json new file mode 100644 index 0000000000..f122660330 --- /dev/null +++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/project.json @@ -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" : { } + } +}