diff --git a/src/Orchard.Specs/Bindings/WebAppHosting.cs b/src/Orchard.Specs/Bindings/WebAppHosting.cs index d101d76c8..095a78adc 100644 --- a/src/Orchard.Specs/Bindings/WebAppHosting.cs +++ b/src/Orchard.Specs/Bindings/WebAppHosting.cs @@ -9,6 +9,7 @@ using log4net.Core; using NUnit.Framework; using Orchard.Specs.Hosting; using TechTalk.SpecFlow; +using Path = Bleroy.FluentPath.Path; namespace Orchard.Specs.Bindings { [Binding] @@ -17,9 +18,7 @@ namespace Orchard.Specs.Bindings { private RequestDetails _details; private HtmlDocument _doc; private MessageSink _messages; - - public WebAppHosting() { - } + private static readonly Path _orchardTemp = Path.Get(System.IO.Path.GetTempPath()).Combine("Orchard.Specs"); public WebHost Host { get { return _webHost; } @@ -30,6 +29,16 @@ namespace Orchard.Specs.Bindings { set { _details = value; } } + [BeforeTestRun] + public static void BeforeTestRun() { + _orchardTemp.Delete(true).CreateDirectory(); + } + + [AfterTestRun] + public static void AfterTestRun() { + _orchardTemp.Delete(true); // <- try to clear any stragglers on the way out + } + [AfterScenario] public void AfterScenario() { if (_webHost != null) { @@ -46,7 +55,7 @@ namespace Orchard.Specs.Bindings { [Given(@"I have a clean site based on (.*)")] public void GivenIHaveACleanSiteBasedOn(string siteFolder) { - _webHost = new WebHost(); + _webHost = new WebHost(_orchardTemp); Host.Initialize(siteFolder, "/"); var shuttle = new Shuttle(); Host.Execute(() => { diff --git a/src/Orchard.Specs/Hosting/WebHost.cs b/src/Orchard.Specs/Hosting/WebHost.cs index 3a46e6d69..4cb5bef90 100644 --- a/src/Orchard.Specs/Hosting/WebHost.cs +++ b/src/Orchard.Specs/Hosting/WebHost.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Reflection; using System.Web.Hosting; using Orchard.Specs.Util; @@ -7,15 +6,25 @@ using Path = Bleroy.FluentPath.Path; namespace Orchard.Specs.Hosting { public class WebHost { + private readonly Path _orchardTemp; private WebHostAgent _webHostAgent; private Path _tempSite; private Path _orchardWebPath; + public WebHost(Path orchardTemp) { + _orchardTemp = orchardTemp; + AppDomain.CurrentDomain.DomainUnload += WebHostCleanup; + AppDomain.CurrentDomain.ProcessExit += WebHostCleanup; + } + + void WebHostCleanup(object sender, EventArgs e) { + _tempSite.Delete(true); // <- try to clean up after the appdomain unloads (still not guaranteed to get everything - probably overkill - might go away) + } public void Initialize(string templateName, string virtualDirectory) { var baseDir = Path.Get(AppDomain.CurrentDomain.BaseDirectory); - _tempSite = Path.Get(System.IO.Path.GetTempFileName()).Delete().CreateDirectory(); + _tempSite = Path.Get(_orchardTemp).Combine(System.IO.Path.GetRandomFileName()).Delete().CreateDirectory(); // Trying the two known relative paths to the Orchard.Web directory. // The second one is for the target "spec" in orchard.proj. @@ -62,6 +71,10 @@ namespace Orchard.Specs.Hosting { _webHostAgent.Shutdown(); _webHostAgent = null; } + try { + _tempSite.Delete(true); // <- progressively clean as much as possible + } + catch {} } public void CopyExtension(string extensionFolder, string extensionName) { diff --git a/src/Orchard.Specs/Hosting/WebHostAgent.cs b/src/Orchard.Specs/Hosting/WebHostAgent.cs index eb2a41726..229eeb90a 100644 --- a/src/Orchard.Specs/Hosting/WebHostAgent.cs +++ b/src/Orchard.Specs/Hosting/WebHostAgent.cs @@ -1,14 +1,9 @@ using System; -using System.Threading; -using System.Web; using System.Web.Hosting; -using Orchard.Specs.Hosting.Orchard.Web; namespace Orchard.Specs.Hosting { - public class WebHostAgent : MarshalByRefObject - { - public SerializableDelegate Execute(SerializableDelegate shuttle) - { + public class WebHostAgent : MarshalByRefObject { + public SerializableDelegate Execute(SerializableDelegate shuttle) { shuttle.Delegate(); return shuttle; } @@ -17,4 +12,4 @@ namespace Orchard.Specs.Hosting { HostingEnvironment.InitiateShutdown(); } } -} +} \ No newline at end of file