From 01d740efd35717e7a063b408bafb7217997682f0 Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Mon, 13 Dec 2010 16:32:00 -0800 Subject: [PATCH] SpecFlow test for dynamic compilation The SpecFlow test for dynamic compilation wasn't actually trigering dynamic compilation for all modules, because some of the were copied to the "~/bin" assembly of the hosted ASP.NET application used for SpecFlow testing. This now works as expected, and we have 3 tests for this: one with dynamic compilation enabled, one with dynamic comilation disabled, and one with forced dynamic compilation (other loaders disabled). --HG-- branch : dev --- src/Orchard.Specs/Bindings/WebAppHosting.cs | 14 +++- .../Hosting/ExtensionDeploymentOptions.cs | 6 ++ ...leDynamicCompilation.HostComponents.config | 10 +++ ...ceDynamicCompilation.HostComponents.config | 20 ++++++ src/Orchard.Specs/Hosting/WebHost.cs | 65 ++++++++++++++----- src/Orchard.Specs/Orchard.Specs.csproj | 10 ++- src/Orchard.Specs/SiteCompilation.feature | 16 ++++- src/Orchard.Specs/SiteCompilation.feature.cs | 58 ++++++++++++++--- 8 files changed, 167 insertions(+), 32 deletions(-) create mode 100644 src/Orchard.Specs/Hosting/TemplateConfigs/DisableDynamicCompilation.HostComponents.config create mode 100644 src/Orchard.Specs/Hosting/TemplateConfigs/ForceDynamicCompilation.HostComponents.config diff --git a/src/Orchard.Specs/Bindings/WebAppHosting.cs b/src/Orchard.Specs/Bindings/WebAppHosting.cs index 7648b2e9d..2fedd28b6 100644 --- a/src/Orchard.Specs/Bindings/WebAppHosting.cs +++ b/src/Orchard.Specs/Bindings/WebAppHosting.cs @@ -21,6 +21,7 @@ namespace Orchard.Specs.Bindings { private MessageSink _messages; private static readonly Path _orchardTemp = Path.Get(System.IO.Path.GetTempPath()).Combine("Orchard.Specs"); private ExtensionDeploymentOptions _moduleDeploymentOptions = ExtensionDeploymentOptions.CompiledAssembly; + private DynamicComilationOption _dynamicCompilationOption = DynamicComilationOption.Enabled; public WebHost Host { get { return _webHost; } @@ -70,6 +71,17 @@ namespace Orchard.Specs.Bindings { _moduleDeploymentOptions = ExtensionDeploymentOptions.SourceCode; } + [Given(@"I have chosen to load modules using dymamic compilation only")] + public void GivenIHaveChosenToLoadModulesUsingDynamicComilationOnly() { + _moduleDeploymentOptions = ExtensionDeploymentOptions.SourceCode; + _dynamicCompilationOption = DynamicComilationOption.Force; + } + + [Given(@"I have chosen to load modules with dynamic compilation disabled")] + public void GivenIHaveChosenToLoadModulesAsSourceFilesOnly() { + _dynamicCompilationOption = DynamicComilationOption.Disabled; + } + [Given(@"I have a clean site based on (.*)")] public void GivenIHaveACleanSiteBasedOn(string siteFolder) { GivenIHaveACleanSiteBasedOn(siteFolder, "/"); @@ -78,7 +90,7 @@ namespace Orchard.Specs.Bindings { [Given(@"I have a clean site based on (.*) at ""(.*)""")] public void GivenIHaveACleanSiteBasedOn(string siteFolder, string virtualDirectory) { _webHost = new WebHost(_orchardTemp); - Host.Initialize(siteFolder, virtualDirectory ?? "/"); + Host.Initialize(siteFolder, virtualDirectory ?? "/", _dynamicCompilationOption); var shuttle = new Shuttle(); Host.Execute(() => { log4net.Config.BasicConfigurator.Configure(new CastleAppender()); diff --git a/src/Orchard.Specs/Hosting/ExtensionDeploymentOptions.cs b/src/Orchard.Specs/Hosting/ExtensionDeploymentOptions.cs index 09ef7bcd3..6f1065ba0 100644 --- a/src/Orchard.Specs/Hosting/ExtensionDeploymentOptions.cs +++ b/src/Orchard.Specs/Hosting/ExtensionDeploymentOptions.cs @@ -6,4 +6,10 @@ namespace Orchard.Specs.Hosting { CompiledAssembly = 0x01, SourceCode = 0x02, } + + public enum DynamicComilationOption { + Enabled, // Allow compiling of csproj files as needed + Disabled, // Never compile csproj files + Force // Force loading modules by compiling csproj files + } } \ No newline at end of file diff --git a/src/Orchard.Specs/Hosting/TemplateConfigs/DisableDynamicCompilation.HostComponents.config b/src/Orchard.Specs/Hosting/TemplateConfigs/DisableDynamicCompilation.HostComponents.config new file mode 100644 index 000000000..1ece073bc --- /dev/null +++ b/src/Orchard.Specs/Hosting/TemplateConfigs/DisableDynamicCompilation.HostComponents.config @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/Orchard.Specs/Hosting/TemplateConfigs/ForceDynamicCompilation.HostComponents.config b/src/Orchard.Specs/Hosting/TemplateConfigs/ForceDynamicCompilation.HostComponents.config new file mode 100644 index 000000000..2c476630d --- /dev/null +++ b/src/Orchard.Specs/Hosting/TemplateConfigs/ForceDynamicCompilation.HostComponents.config @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/Orchard.Specs/Hosting/WebHost.cs b/src/Orchard.Specs/Hosting/WebHost.cs index 08673f9fb..e1d88d885 100644 --- a/src/Orchard.Specs/Hosting/WebHost.cs +++ b/src/Orchard.Specs/Hosting/WebHost.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Reflection; using System.Threading; @@ -24,8 +25,8 @@ namespace Orchard.Specs.Hosting { _orchardTemp = orchardTemp; } - public void Initialize(string templateName, string virtualDirectory) { - Stopwatch stopwatch = new Stopwatch(); + public void Initialize(string templateName, string virtualDirectory, DynamicComilationOption dynamicCompilationOption) { + var stopwatch = new Stopwatch(); stopwatch.Start(); var baseDir = Path.Get(AppDomain.CurrentDomain.BaseDirectory); @@ -59,6 +60,19 @@ namespace Orchard.Specs.Hosting { baseDir.Combine("Hosting").Combine(templateName) .DeepCopy(_tempSite); + if (dynamicCompilationOption != DynamicComilationOption.Enabled) { + var sourceConfig = baseDir.Combine("Hosting").Combine("TemplateConfigs"); + var siteConfig = _tempSite.Combine("Config"); + switch (dynamicCompilationOption) { + case DynamicComilationOption.Disabled: + File.Copy(sourceConfig.Combine("DisableDynamicCompilation.HostComponents.config"), siteConfig.Combine("HostComponents.config")); + break; + case DynamicComilationOption.Force: + File.Copy(sourceConfig.Combine("ForceDynamicCompilation.HostComponents.config"), siteConfig.Combine("HostComponents.config")); + break; + } + } + Log("Copy binaries of the \"Orchard.Web\" project"); _orchardWebPath.Combine("bin") .ShallowCopy("*.dll", _tempSite.Combine("bin")) @@ -81,11 +95,16 @@ namespace Orchard.Specs.Hosting { // can be achieved through serialization to the ASP.NET appdomain // (see Execute(Action) method) Log("Copy Orchard.Specflow test project binaries"); - baseDir - .ShallowCopy("*.dll", _tempSite.Combine("bin")) - .ShallowCopy("*.exe", _tempSite.Combine("bin")) - .ShallowCopy("*.pdb", _tempSite.Combine("bin")); + baseDir.ShallowCopy( + path => IsSpecFlowTestAssembly(path) && !_tempSite.Combine("bin").Combine(path.FileName).Exists, + _tempSite.Combine("bin")); + StartAspNetHost(virtualDirectory); + + Log("ASP.NET host initialization completed in {0} sec", stopwatch.Elapsed.TotalSeconds); + } + + private void StartAspNetHost(string virtualDirectory) { Log("Starting up ASP.NET host"); HostName = "localhost"; PhysicalDirectory = _tempSite; @@ -93,6 +112,7 @@ namespace Orchard.Specs.Hosting { _webHostAgent = (WebHostAgent)ApplicationHost.CreateApplicationHost(typeof(WebHostAgent), VirtualDirectory, PhysicalDirectory); + var shuttle = new Shuttle(); Execute(() => { shuttle.CodeGenDir = HttpRuntime.CodegenDir; }); @@ -100,11 +120,10 @@ namespace Orchard.Specs.Hosting { _codeGenDir = shuttle.CodeGenDir; _codeGenDir = _codeGenDir.Parent; Log("ASP.NET CodeGenDir: \"{0}\"", _codeGenDir); - Log("ASP.NET host initialization completed in {0} sec", stopwatch.Elapsed.TotalSeconds); } [Serializable] - class Shuttle { + public class Shuttle { public string CodeGenDir; } @@ -140,7 +159,7 @@ namespace Orchard.Specs.Hosting { try { _codeGenDir.Delete(true); // <- clean as much as possible } - catch(Exception e) { + catch (Exception e) { if (lastTry) Log("Failure: \"{0}\"", e); result = false; @@ -148,15 +167,15 @@ namespace Orchard.Specs.Hosting { } if (_tempSite != null && _tempSite.Exists) - try { - Log("Trying to delete temporary files at \"{0}\"", _tempSite); - _tempSite.Delete(true); // <- progressively clean as much as possible - } - catch (Exception e) { - if (lastTry) - Log("failure: \"{0}\"", e); - result = false; - } + try { + Log("Trying to delete temporary files at \"{0}\"", _tempSite); + _tempSite.Delete(true); // <- progressively clean as much as possible + } + catch (Exception e) { + if (lastTry) + Log("failure: \"{0}\"", e); + result = false; + } return result; } @@ -199,6 +218,16 @@ namespace Orchard.Specs.Hosting { return true; } + private bool IsSpecFlowTestAssembly(Path path) { + if (!IsAssemblyFile(path)) + return false; + + if (IsOrchardExtensionFile(path)) + return false; + + return true; + } + private bool IsAssemblyFile(Path path) { return StringComparer.OrdinalIgnoreCase.Equals(path.Extension, ".exe") || StringComparer.OrdinalIgnoreCase.Equals(path.Extension, ".dll") || diff --git a/src/Orchard.Specs/Orchard.Specs.csproj b/src/Orchard.Specs/Orchard.Specs.csproj index e5c83ec95..2c1d3c53d 100644 --- a/src/Orchard.Specs/Orchard.Specs.csproj +++ b/src/Orchard.Specs/Orchard.Specs.csproj @@ -107,7 +107,7 @@ 3.5 - True + False 3.5 @@ -249,6 +249,14 @@ SpecFlowSingleFileGenerator ContentTypes.feature.cs + + Designer + Always + + + Designer + Always + SpecFlowSingleFileGenerator SiteCompilation.feature.cs diff --git a/src/Orchard.Specs/SiteCompilation.feature b/src/Orchard.Specs/SiteCompilation.feature index 9e10f6350..fac1e10c7 100644 --- a/src/Orchard.Specs/SiteCompilation.feature +++ b/src/Orchard.Specs/SiteCompilation.feature @@ -3,8 +3,20 @@ As a privileged user I want to have the modules compiled/installed properly -Scenario: Dynamic compilation support: modules can be deployed as source files only +Scenario: Dynamic compilation can be disabled + Given I have chosen to load modules with dynamic compilation disabled + And I have installed Orchard + When I go to "admin" + Then I should see "
Orchard v(?:\.\d+){2,4}
" + +Scenario: Dynamic compilation will kick in if modules are deployed as source files only Given I have chosen to deploy modules as source files only And I have installed Orchard When I go to "admin" - Then I should see "
Orchard v(?:\.\d+){2,4}
" \ No newline at end of file + Then I should see "
Orchard v(?:\.\d+){2,4}
" + +Scenario: Dynamic compilation can be forced by disabling the precompiled module loader + Given I have chosen to load modules using dymamic compilation only + And I have installed Orchard + When I go to "admin" + Then I should see "
Orchard v(?:\.\d+){2,4}
" diff --git a/src/Orchard.Specs/SiteCompilation.feature.cs b/src/Orchard.Specs/SiteCompilation.feature.cs index 3fe20ad33..5d25d049a 100644 --- a/src/Orchard.Specs/SiteCompilation.feature.cs +++ b/src/Orchard.Specs/SiteCompilation.feature.cs @@ -1,7 +1,7 @@ // ------------------------------------------------------------------------------ // // This code was generated by SpecFlow (http://www.specflow.org/). -// SpecFlow Version:1.3.2.0 +// SpecFlow Version:1.4.0.0 // Runtime Version:4.0.30319.1 // // Changes to this file may cause incorrect behavior and will be lost if @@ -14,7 +14,7 @@ namespace Orchard.Specs using TechTalk.SpecFlow; - [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.3.2.0")] + [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.4.0.0")] [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [NUnit.Framework.TestFixtureAttribute()] [NUnit.Framework.DescriptionAttribute("The compilation of modules installed in a site")] @@ -31,7 +31,7 @@ namespace Orchard.Specs { testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner(); TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "The compilation of modules installed in a site", "In order to install on Orchard site\r\nAs a privileged user\r\nI want to have the mod" + - "ules compiled/installed properly", ((string[])(null))); + "ules compiled/installed properly", GenerationTargetLanguage.CSharp, ((string[])(null))); testRunner.OnFeatureStart(featureInfo); } @@ -54,20 +54,58 @@ namespace Orchard.Specs } [NUnit.Framework.TestAttribute()] - [NUnit.Framework.DescriptionAttribute("Dynamic compilation support: modules can be deployed as source files only")] - public virtual void DynamicCompilationSupportModulesCanBeDeployedAsSourceFilesOnly() + [NUnit.Framework.DescriptionAttribute("Dynamic compilation can be disabled")] + public virtual void DynamicCompilationCanBeDisabled() { - TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Dynamic compilation support: modules can be deployed as source files only", ((string[])(null))); + TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Dynamic compilation can be disabled", ((string[])(null))); #line 6 this.ScenarioSetup(scenarioInfo); #line 7 -testRunner.Given("I have chosen to deploy modules as source files only"); + testRunner.Given("I have chosen to load modules with dynamic compilation disabled"); #line 8 -testRunner.And("I have installed Orchard"); + testRunner.And("I have installed Orchard"); #line 9 -testRunner.When("I go to \"admin\""); + testRunner.When("I go to \"admin\""); #line 10 -testRunner.Then("I should see \"
Orchard v(?:\\.\\d+){2,4}
\""); + testRunner.Then("I should see \"
Orchard v(?:\\.\\d+){2,4}
\""); +#line hidden + testRunner.CollectScenarioErrors(); + } + + [NUnit.Framework.TestAttribute()] + [NUnit.Framework.DescriptionAttribute("Dynamic compilation will kick in if modules are deployed as source files only")] + public virtual void DynamicCompilationWillKickInIfModulesAreDeployedAsSourceFilesOnly() + { + TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Dynamic compilation will kick in if modules are deployed as source files only", ((string[])(null))); +#line 12 +this.ScenarioSetup(scenarioInfo); +#line 13 + testRunner.Given("I have chosen to deploy modules as source files only"); +#line 14 + testRunner.And("I have installed Orchard"); +#line 15 + testRunner.When("I go to \"admin\""); +#line 16 + testRunner.Then("I should see \"
Orchard v(?:\\.\\d+){2,4}
\""); +#line hidden + testRunner.CollectScenarioErrors(); + } + + [NUnit.Framework.TestAttribute()] + [NUnit.Framework.DescriptionAttribute("Dynamic compilation can be forced by disabling the precompiled module loader")] + public virtual void DynamicCompilationCanBeForcedByDisablingThePrecompiledModuleLoader() + { + TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Dynamic compilation can be forced by disabling the precompiled module loader", ((string[])(null))); +#line 18 +this.ScenarioSetup(scenarioInfo); +#line 19 + testRunner.Given("I have chosen to load modules using dymamic compilation only"); +#line 20 + testRunner.And("I have installed Orchard"); +#line 21 + testRunner.When("I go to \"admin\""); +#line 22 + testRunner.Then("I should see \"
Orchard v(?:\\.\\d+){2,4}
\""); #line hidden testRunner.CollectScenarioErrors(); }