diff --git a/NzbDrone.Common.Test/DiskProviderFixture.cs b/NzbDrone.Common.Test/DiskProviderFixture.cs index d3de043c6..d0ad18fa9 100644 --- a/NzbDrone.Common.Test/DiskProviderFixture.cs +++ b/NzbDrone.Common.Test/DiskProviderFixture.cs @@ -160,7 +160,7 @@ namespace NzbDrone.Common.Test [Test] public void folder_should_return_correct_value_for_last_write() { - var appPath = new EnvironmentProvider().ApplicationPath; + var appPath = new EnvironmentProvider().WorkingDirectory; Mocker.Resolve().GetLastDirectoryWrite(appPath).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-10)); Mocker.Resolve().GetLastDirectoryWrite(appPath).Should().BeBefore(DateTime.UtcNow); } diff --git a/NzbDrone.Common.Test/EnviromentProviderTest.cs b/NzbDrone.Common.Test/EnviromentProviderTest.cs index 485b36375..66ab9e06d 100644 --- a/NzbDrone.Common.Test/EnviromentProviderTest.cs +++ b/NzbDrone.Common.Test/EnviromentProviderTest.cs @@ -1,6 +1,4 @@ - - -using System; +using System; using System.IO; using FluentAssertions; using NUnit.Framework; @@ -9,38 +7,25 @@ using NzbDrone.Test.Common; namespace NzbDrone.Common.Test { [TestFixture] - public class EnvironmentProviderTest : TestBase + public class EnvironmentProviderTest : TestBase { - readonly EnvironmentProvider environmentProvider = new EnvironmentProvider(); [Test] public void StartupPath_should_not_be_empty() { - environmentProvider.StartUpPath.Should().NotBeBlank(); - Path.IsPathRooted(environmentProvider.StartUpPath).Should().BeTrue("Path is not rooted"); + Subject.StartUpPath.Should().NotBeBlank(); + Path.IsPathRooted(Subject.StartUpPath).Should().BeTrue("Path is not rooted"); } [Test] public void ApplicationPath_should_not_be_empty() { - environmentProvider.ApplicationPath.Should().NotBeBlank(); - Path.IsPathRooted(environmentProvider.ApplicationPath).Should().BeTrue("Path is not rooted"); + Subject.WorkingDirectory.Should().NotBeBlank(); + Path.IsPathRooted(Subject.WorkingDirectory).Should().BeTrue("Path is not rooted"); } - [Test] - public void ApplicationPath_should_find_root_in_current_folder() - { - Directory.CreateDirectory(EnvironmentProvider.ROOT_MARKER); - environmentProvider.ApplicationPath.Should().BeEquivalentTo(Directory.GetCurrentDirectory()); - } - - [Test] - public void crawl_should_return_null_if_cant_find_root() - { - environmentProvider.CrawlToRoot("C:\\").Should().BeNullOrEmpty(); - } [Test] public void IsProduction_should_return_false_when_run_within_nunit() @@ -52,14 +37,8 @@ namespace NzbDrone.Common.Test [TestCase("1.0.0.0")] public void Application_version_should_not_be_default(string version) { - environmentProvider.Version.Should().NotBe(new Version(version)); + Subject.Version.Should().NotBe(new Version(version)); } - [TearDown] - public void TearDown() - { - if (Directory.Exists(EnvironmentProvider.ROOT_MARKER)) - Directory.Delete(EnvironmentProvider.ROOT_MARKER); - } } } diff --git a/NzbDrone.Common.Test/PathExtentionFixture.cs b/NzbDrone.Common.Test/PathExtentionFixture.cs index daf43a56f..2f54d8c07 100644 --- a/NzbDrone.Common.Test/PathExtentionFixture.cs +++ b/NzbDrone.Common.Test/PathExtentionFixture.cs @@ -15,7 +15,7 @@ namespace NzbDrone.Common.Test { var envMoq = new Mock(); - envMoq.SetupGet(c => c.ApplicationPath).Returns(@"C:\NzbDrone\"); + envMoq.SetupGet(c => c.WorkingDirectory).Returns(@"C:\NzbDrone\"); envMoq.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); @@ -62,13 +62,6 @@ namespace NzbDrone.Common.Test GetEnviromentProvider().GetConfigPath().Should().BeEquivalentTo(@"C:\NzbDrone\Config.xml"); } - - [Test] - public void NlogConfig_path_test() - { - GetEnviromentProvider().GetNlogConfigPath().Should().BeEquivalentTo(@"C:\NzbDrone\NzbDrone.Web\log.config"); - } - [Test] public void Sanbox() { diff --git a/NzbDrone.Common/EnvironmentProvider.cs b/NzbDrone.Common/EnvironmentProvider.cs index f8c9da547..e211b34ac 100644 --- a/NzbDrone.Common/EnvironmentProvider.cs +++ b/NzbDrone.Common/EnvironmentProvider.cs @@ -7,30 +7,26 @@ namespace NzbDrone.Common { public class EnvironmentProvider { - public const string NZBDRONE_PATH = "NZBDRONE_PATH"; - public const string NZBDRONE_PID = "NZBDRONE_PID"; - public const string ROOT_MARKER = "IISExpress"; + private static readonly string ProcessName = Process.GetCurrentProcess().ProcessName.ToLower(); - public static readonly char[] NewLineChars = Environment.NewLine.ToCharArray(); + private static readonly EnvironmentProvider Instance = new EnvironmentProvider(); - private static readonly string processName = Process.GetCurrentProcess().ProcessName.ToLower(); - - private static readonly EnvironmentProvider instance = new EnvironmentProvider(); + private const string NZBDRONE_PID = "NZBDRONE_PID"; public static bool IsProduction { get { if (IsDebug || Debugger.IsAttached) return false; - if (instance.Version.Revision > 10000) return false; //Official builds will never have such a high revision + if (Instance.Version.Revision > 10000) return false; //Official builds will never have such a high revision - var lowerProcessName = processName.ToLower(); + var lowerProcessName = ProcessName.ToLower(); if (lowerProcessName.Contains("vshost")) return false; if (lowerProcessName.Contains("nunit")) return false; if (lowerProcessName.Contains("jetbrain")) return false; if (lowerProcessName.Contains("resharper")) return false; - if (instance.StartUpPath.ToLower().Contains("_rawpackage")) return false; + if (Instance.StartUpPath.ToLower().Contains("_rawpackage")) return false; return true; } @@ -57,7 +53,6 @@ namespace NzbDrone.Common { get { - #if DEBUG return true; #else @@ -73,44 +68,16 @@ namespace NzbDrone.Common get { return Environment.UserInteractive; } } - public virtual string ApplicationPath + public virtual string WorkingDirectory { get { return Directory.GetCurrentDirectory(); } } - public string CrawlToRoot(string dir) - { - if (String.IsNullOrWhiteSpace(dir)) - return null; - - var directoryInfo = new DirectoryInfo(dir); - - while (!IsRoot(directoryInfo)) - { - if (directoryInfo.Parent == null) return null; - directoryInfo = directoryInfo.Parent; - } - - return directoryInfo.FullName; - } - - private static bool IsRoot(DirectoryInfo dir) - { - return dir.GetDirectories(ROOT_MARKER).Length != 0; - } - public virtual string StartUpPath { get { var path = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName; - - if (path.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.Windows), - StringComparison.InvariantCultureIgnoreCase)) - { - path = Directory.GetCurrentDirectory(); - } - return path; } } @@ -137,7 +104,7 @@ namespace NzbDrone.Common } } - public virtual int NzbDroneProcessIdFromEnviroment + public virtual int NzbDroneProcessIdFromEnvironment { get { @@ -150,14 +117,6 @@ namespace NzbDrone.Common } } - public virtual string NzbDronePathFromEnvironment - { - get - { - return Environment.GetEnvironmentVariable(NZBDRONE_PATH); - } - } - public virtual Version GetOsVersion() { OperatingSystem os = Environment.OSVersion; diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index cacdf4f96..9c4bb25b6 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -1,22 +1,14 @@ using System; using System.IO; +using NzbDrone.Common.EnsureThat; namespace NzbDrone.Common { public static class PathExtensions { - private const string WEB_FOLDER = "NzbDrone.Web\\"; private const string APP_DATA = "App_Data\\"; - private const string WEB_BIN = "bin\\"; - - private const string LOG_CONFIG_FILE = "log.config"; private const string APP_CONFIG_FILE = "config.xml"; - - public const string NZBDRONE_EXE = "NzbDrone.exe"; - - public const string OBJ_DB_FOLDER = "objDb"; - public const string NZBDRONE_DB = "nzbdrone.db"; - + private const string NZBDRONE_DB = "nzbdrone.db"; private const string BACKUP_ZIP_FILE = "NzbDrone_Backup.zip"; private const string UPDATE_SANDBOX_FOLDER_NAME = "nzbdrone_update\\"; @@ -28,6 +20,8 @@ namespace NzbDrone.Common public static string NormalizePath(this string path) { + Ensure.That(() => path).IsNotNullOrWhiteSpace(); + if (String.IsNullOrWhiteSpace(path)) throw new ArgumentException("Path can not be null or empty"); @@ -41,59 +35,24 @@ namespace NzbDrone.Common return info.FullName.Trim('/', '\\', ' '); } - public static string GetWebRoot(this EnvironmentProvider environmentProvider) - { - return Path.Combine(environmentProvider.ApplicationPath, WEB_FOLDER); - } - public static string GetAppDataPath(this EnvironmentProvider environmentProvider) { - return Path.Combine(environmentProvider.GetWebRoot(), APP_DATA); - } - - public static string GetWebBinPath(this EnvironmentProvider environmentProvider) - { - return Path.Combine(environmentProvider.GetWebRoot(), WEB_BIN); - } - - public static string GetNlogConfigPath(this EnvironmentProvider environmentProvider) - { - return Path.Combine(environmentProvider.GetWebRoot(), LOG_CONFIG_FILE); + return Path.Combine(environmentProvider.WorkingDirectory, APP_DATA); } public static string GetConfigPath(this EnvironmentProvider environmentProvider) { - return Path.Combine(environmentProvider.ApplicationPath, APP_CONFIG_FILE); - } - - public static string GetObjectDbFolder(this EnvironmentProvider environmentProvider) - { - return Path.Combine(environmentProvider.GetAppDataPath(), OBJ_DB_FOLDER); + return Path.Combine(environmentProvider.WorkingDirectory, APP_CONFIG_FILE); } public static string GetMediaCoverPath(this EnvironmentProvider environmentProvider) { - return Path.Combine(environmentProvider.GetWebRoot(), "MediaCover"); - } - - public static string GetBannerPath(this EnvironmentProvider environmentProvider) - { - return Path.Combine(environmentProvider.GetMediaCoverPath(), "Banners"); - } - - public static string GetFanArtPath(this EnvironmentProvider environmentProvider) - { - return Path.Combine(environmentProvider.GetMediaCoverPath(), "Fanarts"); - } - - public static string GetCacheFolder(this EnvironmentProvider environmentProvider) - { - return Path.Combine(environmentProvider.GetWebRoot(), "Cache"); + return Path.Combine(environmentProvider.GetAppDataPath(), "MediaCover"); } public static string GetUpdateLogFolder(this EnvironmentProvider environmentProvider) { - return Path.Combine(environmentProvider.ApplicationPath, UPDATE_LOG_FOLDER_NAME); + return Path.Combine(environmentProvider.WorkingDirectory, UPDATE_LOG_FOLDER_NAME); } public static string GetUpdateSandboxFolder(this EnvironmentProvider environmentProvider) @@ -131,11 +90,6 @@ namespace NzbDrone.Common return Path.Combine(environmentProvider.GetAppDataPath(), BACKUP_ZIP_FILE); } - public static string GetNzbDroneExe(this EnvironmentProvider environmentProvider) - { - return Path.Combine(environmentProvider.ApplicationPath, NZBDRONE_EXE); - } - public static string GetNzbDroneDatabase(this EnvironmentProvider environmentProvider) { return Path.Combine(environmentProvider.GetAppDataPath(), NZBDRONE_DB); diff --git a/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs b/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs index a0b90d7ee..8c96f4602 100644 --- a/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs +++ b/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs @@ -110,7 +110,7 @@ namespace NzbDrone.Core.Test.JobTests var updateClientPath = Mocker.GetMock().Object.GetUpdateClientExePath(); Mocker.GetMock() - .SetupGet(c => c.NzbDroneProcessIdFromEnviroment).Returns(12); + .SetupGet(c => c.NzbDroneProcessIdFromEnvironment).Returns(12); StartUpdate(); diff --git a/NzbDrone.Core.Test/ParserTests/ParserFixture.cs b/NzbDrone.Core.Test/ParserTests/ParserFixture.cs index aaa87703a..7e50f172d 100644 --- a/NzbDrone.Core.Test/ParserTests/ParserFixture.cs +++ b/NzbDrone.Core.Test/ParserTests/ParserFixture.cs @@ -1,6 +1,4 @@ - - -using System; +using System; using System.Linq; using FluentAssertions; using Moq; diff --git a/NzbDrone.Core/Lifecycle/AppShutdownJob.cs b/NzbDrone.Core/Lifecycle/AppShutdownJob.cs index 62ef370cd..f088249bc 100644 --- a/NzbDrone.Core/Lifecycle/AppShutdownJob.cs +++ b/NzbDrone.Core/Lifecycle/AppShutdownJob.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using NLog; using NzbDrone.Common; using NzbDrone.Core.Jobs; @@ -9,17 +8,16 @@ namespace NzbDrone.Core.Lifecycle { public class AppShutdownJob : IJob { - private readonly EnvironmentProvider _environmentProvider; private readonly ProcessProvider _processProvider; private readonly ServiceProvider _serviceProvider; + private readonly Logger _logger; - private static readonly Logger logger = LogManager.GetCurrentClassLogger(); - public AppShutdownJob(EnvironmentProvider environmentProvider, ProcessProvider processProvider, ServiceProvider serviceProvider) + public AppShutdownJob(ProcessProvider processProvider, ServiceProvider serviceProvider, Logger logger) { - _environmentProvider = environmentProvider; _processProvider = processProvider; _serviceProvider = serviceProvider; + _logger = logger; } public string Name @@ -35,21 +33,21 @@ namespace NzbDrone.Core.Lifecycle public virtual void Start(ProgressNotification notification, dynamic options) { notification.CurrentMessage = "Shutting down NzbDrone"; - logger.Info("Shutting down NzbDrone"); + _logger.Info("Shutting down NzbDrone"); if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) && _serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)) { - logger.Debug("Stopping NzbDrone Service"); + _logger.Debug("Stopping NzbDrone Service"); _serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME); } else { - logger.Debug("Stopping NzbDrone console"); + _logger.Debug("Stopping NzbDrone console"); - var pid = _environmentProvider.NzbDroneProcessIdFromEnviroment; - _processProvider.Kill(pid); + var currentProcess = _processProvider.GetCurrentProcess(); + _processProvider.Kill(currentProcess.Id); } } } diff --git a/NzbDrone.Core/Lifecycle/AppUpdateJob.cs b/NzbDrone.Core/Lifecycle/AppUpdateJob.cs index 3b3817650..0406ab6bf 100644 --- a/NzbDrone.Core/Lifecycle/AppUpdateJob.cs +++ b/NzbDrone.Core/Lifecycle/AppUpdateJob.cs @@ -81,7 +81,7 @@ namespace NzbDrone.Core.Lifecycle var startInfo = new ProcessStartInfo { FileName = _environmentProvider.GetUpdateClientExePath(), - Arguments = string.Format("{0} {1}", _environmentProvider.NzbDroneProcessIdFromEnviroment, _configFileProvider.Guid) + Arguments = string.Format("{0} {1}", _environmentProvider.NzbDroneProcessIdFromEnvironment, _configFileProvider.Guid) }; var process = _processProvider.Start(startInfo); diff --git a/NzbDrone.Test.Common/TestBase.cs b/NzbDrone.Test.Common/TestBase.cs index f23c4f54c..583b07d2d 100644 --- a/NzbDrone.Test.Common/TestBase.cs +++ b/NzbDrone.Test.Common/TestBase.cs @@ -118,7 +118,7 @@ namespace NzbDrone.Test.Common protected void WithTempAsAppPath() { Mocker.GetMock() - .SetupGet(c => c.ApplicationPath) + .SetupGet(c => c.WorkingDirectory) .Returns(VirtualPath); } diff --git a/NzbDrone.sln.DotSettings b/NzbDrone.sln.DotSettings index 0cacce13d..134731d7a 100644 --- a/NzbDrone.sln.DotSettings +++ b/NzbDrone.sln.DotSettings @@ -4,6 +4,7 @@ DO_NOT_SHOW DO_NOT_SHOW ERROR + <Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /> <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb"><ExtraRule Prefix="" Suffix="" Style="AaBb" /></Policy> BOTH_SIDES OUTLINE diff --git a/NzbDrone/NzbDroneBootstrapper.cs b/NzbDrone/NzbDroneBootstrapper.cs index 16aed3210..eb7ea249d 100644 --- a/NzbDrone/NzbDroneBootstrapper.cs +++ b/NzbDrone/NzbDroneBootstrapper.cs @@ -43,7 +43,7 @@ namespace NzbDrone ReportingService.RestProvider = container.Resolve(); - logger.Info("Start-up Path:'{0}'", environmentProvider.ApplicationPath); + logger.Info("Start-up Path:'{0}'", environmentProvider.WorkingDirectory); } } }