Cleaned up ConfigProvider, added tests for paths.

pull/7/merge
kay.one 13 years ago
parent e15e79a6c1
commit b456c5c4d2

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

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

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

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

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

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

Loading…
Cancel
Save