Cleaned up ConfigProvider, added tests for paths.

This commit is contained in:
kay.one 2011-10-07 21:51:35 -07:00
parent e15e79a6c1
commit b456c5c4d2
6 changed files with 118 additions and 38 deletions

View File

@ -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<EnviromentProvider>();
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");
}
}
}

View File

@ -73,6 +73,7 @@
<Compile Include="AutoMoq\Unity\AutoMockingBuilderStrategy.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingContainerExtension.cs" />
<Compile Include="ApplicationTest.cs" />
<Compile Include="ConfigProviderTest.cs" />
<Compile Include="IISProviderTest.cs" />
<Compile Include="ProcessProviderTests.cs" />
<Compile Include="EnviromentControllerTest.cs" />

View File

@ -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";
}

View File

@ -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;
while (appDir.GetDirectories("iisexpress").Length == 0)
{
if (appDir.Parent == null) throw new ApplicationException("Can't fine IISExpress folder.");
appDir = appDir.Parent;
_enviromentProvider = enviromentProvider;
}
return appDir.FullName;
}
public ConfigProvider()
{
}
public virtual int Port
public virtual int PortNumber
{
get { return GetValueInt("Port"); }
}
@ -39,9 +35,24 @@ namespace NzbDrone.Providers
get { return GetValueBoolean("LaunchBrowser"); }
}
public virtual string IISDirectory
{
get { return Path.Combine(_enviromentProvider.ApplicationPath, "IISExpress"); }
}
public virtual string IISExePath
{
get { return Path.Combine(IISDirectory, "iisexpress.exe"); }
}
public virtual string IISConfigPath
{
get { return Path.Combine(IISDirectory, "AppServer", "applicationhost.config"); }
}
public virtual string AppDataDirectory
{
get { return Path.Combine(ApplicationRoot, "NzbDrone.Web", "App_Data"); }
get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web", "App_Data"); }
}
public virtual string ConfigFile
@ -49,31 +60,21 @@ namespace NzbDrone.Providers
get { return Path.Combine(AppDataDirectory, "Config.xml"); }
}
public virtual string IISFolder
public virtual string NlogConfigPath
{
get { return Path.Combine(ApplicationRoot, @"IISExpress\"); }
get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web\\log.config"); }
}
public virtual string IISExePath
{
get { return IISFolder + @"iisexpress.exe"; }
}
public virtual string IISConfigPath
{
get { return Path.Combine(IISFolder, "AppServer", "applicationhost.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"));

View File

@ -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;
}
}
}
}

View File

@ -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