From b456c5c4d24067891d196173900f34cc95d5b363 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 7 Oct 2011 21:51:35 -0700 Subject: [PATCH] Cleaned up ConfigProvider, added tests for paths. --- NzbDrone.App.Test/ConfigProviderTest.cs | 58 +++++++++++++++++++++ NzbDrone.App.Test/NzbDrone.App.Test.csproj | 1 + NzbDrone/Application.cs | 2 +- NzbDrone/Providers/ConfigProvider.cs | 59 +++++++++++----------- NzbDrone/Providers/EnviromentProvider.cs | 18 +++++++ NzbDrone/Providers/IISProvider.cs | 10 ++-- 6 files changed, 114 insertions(+), 34 deletions(-) create mode 100644 NzbDrone.App.Test/ConfigProviderTest.cs diff --git a/NzbDrone.App.Test/ConfigProviderTest.cs b/NzbDrone.App.Test/ConfigProviderTest.cs new file mode 100644 index 000000000..9bed9a381 --- /dev/null +++ b/NzbDrone.App.Test/ConfigProviderTest.cs @@ -0,0 +1,58 @@ +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Providers; + +namespace NzbDrone.App.Test +{ + [TestFixture] + public class ConfigProviderTest + { + + private ConfigProvider GetConfigProvider() + { + var envMoq = new Mock(); + envMoq.SetupGet(c => c.ApplicationPath).Returns(@"C:\NzbDrone\"); + + return new ConfigProvider(envMoq.Object); + } + + + [Test] + public void IISExpress_path_test() + { + GetConfigProvider().IISDirectory.Should().BeEquivalentTo(@"C:\NzbDrone\IISExpress"); + } + + [Test] + public void AppDataDirectory_path_test() + { + GetConfigProvider().AppDataDirectory.Should().BeEquivalentTo(@"C:\NzbDrone\NzbDrone.Web\App_Data"); + } + + + [Test] + public void Config_path_test() + { + GetConfigProvider().ConfigFile.Should().BeEquivalentTo(@"C:\NzbDrone\NzbDrone.Web\App_Data\Config.xml"); + } + + [Test] + public void IISConfig_path_test() + { + GetConfigProvider().IISConfigPath.Should().BeEquivalentTo(@"C:\NzbDrone\IISExpress\AppServer\applicationhost.config"); + } + + [Test] + public void IISExe_path_test() + { + GetConfigProvider().IISExePath.Should().BeEquivalentTo(@"C:\NzbDrone\IISExpress\IISExpress.exe"); + } + + [Test] + public void NlogConfig_path_test() + { + GetConfigProvider().NlogConfigPath.Should().BeEquivalentTo(@"C:\NzbDrone\NzbDrone.Web\log.config"); + } + } +} diff --git a/NzbDrone.App.Test/NzbDrone.App.Test.csproj b/NzbDrone.App.Test/NzbDrone.App.Test.csproj index 2c2c2cec6..11b201b71 100644 --- a/NzbDrone.App.Test/NzbDrone.App.Test.csproj +++ b/NzbDrone.App.Test/NzbDrone.App.Test.csproj @@ -73,6 +73,7 @@ + diff --git a/NzbDrone/Application.cs b/NzbDrone/Application.cs index 2e34bae12..8c90cfa42 100644 --- a/NzbDrone/Application.cs +++ b/NzbDrone/Application.cs @@ -33,7 +33,7 @@ namespace NzbDrone _configProvider.ConfigureNlog(); _configProvider.CreateDefaultConfigFile(); - Logger.Info("Starting NZBDrone. Start-up Path:'{0}'", _configProvider.ApplicationRoot); + Logger.Info("Starting NZBDrone. Start-up Path:'{0}'", _enviromentProvider.ApplicationPath); Thread.CurrentThread.Name = "Host"; } diff --git a/NzbDrone/Providers/ConfigProvider.cs b/NzbDrone/Providers/ConfigProvider.cs index fe1524da6..0aaf0076e 100644 --- a/NzbDrone/Providers/ConfigProvider.cs +++ b/NzbDrone/Providers/ConfigProvider.cs @@ -1,35 +1,31 @@ using System; using System.IO; using System.Linq; -using System.Reflection; using System.Xml.Linq; using System.Xml.XPath; using NLog; using NLog.Config; +using Ninject; namespace NzbDrone.Providers { public class ConfigProvider { + private readonly EnviromentProvider _enviromentProvider; private static readonly Logger Logger = LogManager.GetLogger("Host.ConfigProvider"); - public virtual string ApplicationRoot + [Inject] + public ConfigProvider(EnviromentProvider enviromentProvider) { - get - { - var appDir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; + _enviromentProvider = enviromentProvider; + } - while (appDir.GetDirectories("iisexpress").Length == 0) - { - if (appDir.Parent == null) throw new ApplicationException("Can't fine IISExpress folder."); - appDir = appDir.Parent; - } + public ConfigProvider() + { - return appDir.FullName; - } } - public virtual int Port + public virtual int PortNumber { get { return GetValueInt("Port"); } } @@ -39,41 +35,46 @@ namespace NzbDrone.Providers get { return GetValueBoolean("LaunchBrowser"); } } - public virtual string AppDataDirectory + public virtual string IISDirectory { - get { return Path.Combine(ApplicationRoot, "NzbDrone.Web", "App_Data"); } + get { return Path.Combine(_enviromentProvider.ApplicationPath, "IISExpress"); } } - public virtual string ConfigFile + public virtual string IISExePath { - get { return Path.Combine(AppDataDirectory, "Config.xml"); } + get { return Path.Combine(IISDirectory, "iisexpress.exe"); } + } + + public virtual string IISConfigPath + { + get { return Path.Combine(IISDirectory, "AppServer", "applicationhost.config"); } } - public virtual string IISFolder + public virtual string AppDataDirectory { - get { return Path.Combine(ApplicationRoot, @"IISExpress\"); } + get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web", "App_Data"); } } - public virtual string IISExePath + public virtual string ConfigFile { - get { return IISFolder + @"iisexpress.exe"; } + get { return Path.Combine(AppDataDirectory, "Config.xml"); } } - public virtual string IISConfigPath + public virtual string NlogConfigPath { - get { return Path.Combine(IISFolder, "AppServer", "applicationhost.config"); } + get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web\\log.config"); } } + public virtual void ConfigureNlog() { - LogManager.Configuration = new XmlLoggingConfiguration( - Path.Combine(ApplicationRoot, "NzbDrone.Web\\log.config"), false); + LogManager.Configuration = new XmlLoggingConfiguration(NlogConfigPath, false); } public virtual void UpdateIISConfig(string configPath) { Logger.Info(@"Server configuration file: {0}", configPath); - Logger.Info(@"Configuring server to: [http://localhost:{0}]", Port); + Logger.Info(@"Configuring server to: [http://localhost:{0}]", PortNumber); var configXml = XDocument.Load(configPath); @@ -84,13 +85,13 @@ namespace NzbDrone.Providers bindings.Add( new XElement("binding", new XAttribute("protocol", "http"), - new XAttribute("bindingInformation", String.Format("*:{0}:localhost", Port)) + new XAttribute("bindingInformation", String.Format("*:{0}:localhost", PortNumber)) )); bindings.Add( new XElement("binding", new XAttribute("protocol", "http"), - new XAttribute("bindingInformation", String.Format("*:{0}:", Port)) + new XAttribute("bindingInformation", String.Format("*:{0}:", PortNumber)) )); configXml.Save(configPath); @@ -107,7 +108,7 @@ namespace NzbDrone.Providers } } - public virtual void WriteDefaultConfig() + private void WriteDefaultConfig() { var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); diff --git a/NzbDrone/Providers/EnviromentProvider.cs b/NzbDrone/Providers/EnviromentProvider.cs index 45c914342..2659806bc 100644 --- a/NzbDrone/Providers/EnviromentProvider.cs +++ b/NzbDrone/Providers/EnviromentProvider.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using System.Reflection; namespace NzbDrone.Providers { @@ -13,5 +15,21 @@ namespace NzbDrone.Providers { get { return Environment.UserInteractive; } } + + public virtual string ApplicationPath + { + get + { + var dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; + + while (dir.GetDirectories("iisexpress").Length == 0) + { + if (dir.Parent == null) throw new ApplicationException("Can't fine IISExpress folder."); + dir = dir.Parent; + } + + return dir.FullName; + } + } } } \ No newline at end of file diff --git a/NzbDrone/Providers/IISProvider.cs b/NzbDrone/Providers/IISProvider.cs index 14b042c81..ce2478f7d 100644 --- a/NzbDrone/Providers/IISProvider.cs +++ b/NzbDrone/Providers/IISProvider.cs @@ -12,13 +12,15 @@ namespace NzbDrone.Providers private static readonly Logger Logger = LogManager.GetLogger("Host.IISProvider"); private readonly ConfigProvider _configProvider; private readonly ProcessProvider _processProvider; + private readonly EnviromentProvider _enviromentProvider; [Inject] - public IISProvider(ConfigProvider configProvider, ProcessProvider processProvider) + public IISProvider(ConfigProvider configProvider, ProcessProvider processProvider, EnviromentProvider enviromentProvider) { _configProvider = configProvider; _processProvider = processProvider; + _enviromentProvider = enviromentProvider; } public IISProvider() @@ -27,7 +29,7 @@ namespace NzbDrone.Providers public string AppUrl { - get { return string.Format("http://localhost:{0}/", _configProvider.Port); } + get { return string.Format("http://localhost:{0}/", _configProvider.PortNumber); } } public int IISProcessId { get; private set; } @@ -42,7 +44,7 @@ namespace NzbDrone.Providers startInfo.FileName = _configProvider.IISExePath; startInfo.Arguments = String.Format("/config:\"{0}\" /trace:i", _configProvider.IISConfigPath); - startInfo.WorkingDirectory = _configProvider.ApplicationRoot; + startInfo.WorkingDirectory = _enviromentProvider.ApplicationPath; startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; @@ -50,7 +52,7 @@ namespace NzbDrone.Providers startInfo.CreateNoWindow = true; //Set Variables for the config file. - startInfo.EnvironmentVariables.Add("NZBDRONE_PATH", _configProvider.ApplicationRoot); + startInfo.EnvironmentVariables.Add("NZBDRONE_PATH", _enviromentProvider.ApplicationPath); startInfo.EnvironmentVariables.Add("NZBDRONE_PID", Process.GetCurrentProcess().Id.ToString()); try