From 2e94e322f4ad89c3e82d0176db773b42857116ee Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 12 Nov 2011 21:19:19 -0800 Subject: [PATCH] Path calculations are now done using extension methods. --- NzbDrone.Common/NzbDrone.Common.csproj | 1 + NzbDrone.Common/PathExtentions.cs | 39 +++++++++++++++++++ NzbDrone.Common/PathProvider.cs | 15 ------- .../PreformUpdateFixture.cs | 24 ++++++------ NzbDrone.Core/Providers/UpdateProvider.cs | 4 +- .../UpdateProviderStartTest.cs | 12 +++--- .../UpdateProviderVerifyTest.cs | 3 ++ NzbDrone.Update/Providers/UpdateProvider.cs | 18 ++++----- 8 files changed, 71 insertions(+), 45 deletions(-) create mode 100644 NzbDrone.Common/PathExtentions.cs diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 605dc0094..d4e1e1631 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -45,6 +45,7 @@ + diff --git a/NzbDrone.Common/PathExtentions.cs b/NzbDrone.Common/PathExtentions.cs new file mode 100644 index 000000000..9f3b40074 --- /dev/null +++ b/NzbDrone.Common/PathExtentions.cs @@ -0,0 +1,39 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; + +namespace NzbDrone.Common +{ + public static class PathExtentions + { + private const string WEB_FOLDER = "NzbDrone.Web\\"; + private const string APP_DATA = "App_Data\\"; + + + private const string LOG_CONFIG_FILE = "log.config"; + private const string APP_CONFIG_FILE = "config.xml"; + + private const string NZBDRONE_DB_FILE = "nzbdrone.sdf"; + private const string LOG_DB_FILE = "log.sdf"; + + private const string UPDATE_SANDBOX_FOLDER_NAME = "nzbdrone_update\\"; + private const string UPDATE_PACKAGE_FOLDER_NAME = "nzbdrone\\"; + private const string UPDATE_BACKUP_FOLDER_NAME = "nzbdrone_backup\\"; + + public static string GetUpdateSandboxFolder(this PathProvider pathProvider) + { + return Path.Combine(pathProvider.SystemTemp, UPDATE_SANDBOX_FOLDER_NAME); + } + + public static string GetUpdateBackUpFolder(this PathProvider pathProvider) + { + return Path.Combine(pathProvider.GetUpdateSandboxFolder(), UPDATE_BACKUP_FOLDER_NAME); + } + + public static string GetUpdatePackageFolder(this PathProvider pathProvider) + { + return Path.Combine(pathProvider.GetUpdateSandboxFolder(), UPDATE_PACKAGE_FOLDER_NAME); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Common/PathProvider.cs b/NzbDrone.Common/PathProvider.cs index 1dffafeba..fdae9143e 100644 --- a/NzbDrone.Common/PathProvider.cs +++ b/NzbDrone.Common/PathProvider.cs @@ -103,20 +103,5 @@ namespace NzbDrone.Common { get { return Path.Combine(AppData, "Cache"); } } - - public virtual string UpdateSandboxFolder - { - get { return Path.Combine(SystemTemp, UPDATE_SANDBOX_FOLDER_NAME); } - } - - public virtual string UpdatePackageFolder - { - get { return Path.Combine(UPDATE_SANDBOX_FOLDER_NAME, "NzbDrone"); } - } - - public virtual string UpdateBackUpFolder - { - get { return Path.Combine(UpdateSandboxFolder, UPDATE_BACKUP_FOLDER_NAME); } - } } } \ No newline at end of file diff --git a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/PreformUpdateFixture.cs b/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/PreformUpdateFixture.cs index 1f31f0eb5..bcbf1d92d 100644 --- a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/PreformUpdateFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/PreformUpdateFixture.cs @@ -14,28 +14,23 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests internal class PreformUpdateFixture : TestBase { - private string SandBoxPath; + + private const string SANDBOX_FOLDER = @"C:\Temp\nzbdrone_update\"; [SetUp] public void setup() { WithStrictMocker(); - Mocker.GetMock() - .SetupGet(c => c.UpdateSandboxFolder).Returns(Path.Combine(TempFolder, "NzbDrone_update")); - SandBoxPath = Mocker.GetMock().Object.UpdateSandboxFolder; - - Mocker.GetMock() - .SetupGet(c => c.UpdatePackageFolder).Returns(Path.Combine(SandBoxPath, "NzbDrone")); - } [Test] public void Should_call_download_and_extract_using_correct_arguments() { - //Act + Mocker.GetMock().SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); + var updatePackage = new UpdatePackage { FileName = "NzbDrone.kay.one.0.6.0.2031.zip", @@ -43,12 +38,15 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests Version = new Version("0.6.0.2031") }; + var updateArchive = Path.Combine(SANDBOX_FOLDER, updatePackage.FileName); + Mocker.GetMock().Setup( - c => c.DownloadFile(updatePackage.Url, Path.Combine(SandBoxPath, updatePackage.FileName))); + c => c.DownloadFile(updatePackage.Url, updateArchive)); Mocker.GetMock().Setup( - c => c.ExtractArchive(Path.Combine(SandBoxPath, updatePackage.FileName), SandBoxPath)); + c => c.ExtractArchive(updateArchive, SANDBOX_FOLDER)); + //Act Mocker.Resolve().StartUpgrade(updatePackage); } @@ -56,7 +54,9 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests public void Should_download_and_extract_to_temp_folder() { - var updateSubFolder = new DirectoryInfo(SandBoxPath); + Mocker.GetMock().SetupGet(c => c.SystemTemp).Returns(TempFolder); + + var updateSubFolder = new DirectoryInfo(Mocker.GetMock().Object.GetUpdateSandboxFolder()); var updatePackage = new UpdatePackage { diff --git a/NzbDrone.Core/Providers/UpdateProvider.cs b/NzbDrone.Core/Providers/UpdateProvider.cs index 9bdb51104..720466e28 100644 --- a/NzbDrone.Core/Providers/UpdateProvider.cs +++ b/NzbDrone.Core/Providers/UpdateProvider.cs @@ -77,14 +77,14 @@ namespace NzbDrone.Core.Providers public virtual void StartUpgrade(UpdatePackage updatePackage) { - var packageDestination = Path.Combine(_pathProvider.UpdateSandboxFolder, updatePackage.FileName); + var packageDestination = Path.Combine(_pathProvider.GetUpdateSandboxFolder(), updatePackage.FileName); Logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination); _httpProvider.DownloadFile(updatePackage.Url, packageDestination); Logger.Info("Download completed for update package from [{0}]", updatePackage.FileName); Logger.Info("Extracting Update package"); - _archiveProvider.ExtractArchive(packageDestination, _pathProvider.UpdateSandboxFolder); + _archiveProvider.ExtractArchive(packageDestination, _pathProvider.GetUpdateSandboxFolder()); Logger.Info("Update package extracted successfully"); } diff --git a/NzbDrone.Update.Test/UpdateProviderStartTest.cs b/NzbDrone.Update.Test/UpdateProviderStartTest.cs index 6ac2d71fa..699af817d 100644 --- a/NzbDrone.Update.Test/UpdateProviderStartTest.cs +++ b/NzbDrone.Update.Test/UpdateProviderStartTest.cs @@ -14,23 +14,21 @@ namespace NzbDrone.Update.Test { AutoMoqer mocker = new AutoMoqer(); - private const string UPDATE_FOLDER = @"C:\Temp\NzbDrone_update\NzbDrone"; - private const string BACKUP_FOLDER = @"C:\Temp\NzbDrone_update\NzbDrone_Backup"; + private const string UPDATE_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone\"; + private const string BACKUP_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone_backup\"; private const string TARGET_FOLDER = @"C:\NzbDrone\"; - Mock _pathProvider = null; + Mock _pathProvider; [SetUp] public void Setup() { mocker = new AutoMoqer(); - - + _pathProvider = mocker.GetMock(); - _pathProvider.SetupGet(c => c.UpdateBackUpFolder).Returns(BACKUP_FOLDER); - _pathProvider.SetupGet(c => c.UpdatePackageFolder).Returns(UPDATE_FOLDER); + _pathProvider.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); mocker.GetMock() .Setup(c => c.FolderExists(UPDATE_FOLDER)) diff --git a/NzbDrone.Update.Test/UpdateProviderVerifyTest.cs b/NzbDrone.Update.Test/UpdateProviderVerifyTest.cs index e5b31dd59..7c133869d 100644 --- a/NzbDrone.Update.Test/UpdateProviderVerifyTest.cs +++ b/NzbDrone.Update.Test/UpdateProviderVerifyTest.cs @@ -23,6 +23,9 @@ namespace NzbDrone.Update.Test mocker.GetMock() .Setup(c => c.StartUpPath).Returns(@"C:\Temp\NzbDrone_update\"); + + mocker.GetMock() + .Setup(c => c.SystemTemp).Returns(@"C:\Temp\"); } [TestCase(null)] diff --git a/NzbDrone.Update/Providers/UpdateProvider.cs b/NzbDrone.Update/Providers/UpdateProvider.cs index d9ad53bc6..9dd5279fa 100644 --- a/NzbDrone.Update/Providers/UpdateProvider.cs +++ b/NzbDrone.Update/Providers/UpdateProvider.cs @@ -23,19 +23,19 @@ namespace NzbDrone.Update.Providers _pathProvider = pathProvider; } - private void Verify(string installationFolder) + private void Verify(string targetFolder) { logger.Info("Verifying requirements before update..."); - if (String.IsNullOrWhiteSpace(installationFolder)) + if (String.IsNullOrWhiteSpace(targetFolder)) throw new ArgumentException("Target folder can not be null or empty"); - if (!_diskProvider.FolderExists(installationFolder)) - throw new DirectoryNotFoundException("Target folder doesn't exist" + installationFolder); + if (!_diskProvider.FolderExists(targetFolder)) + throw new DirectoryNotFoundException("Target folder doesn't exist " + targetFolder); logger.Info("Verifying Update Folder"); - if (!_diskProvider.FolderExists(_pathProvider.UpdatePackageFolder)) - throw new DirectoryNotFoundException("Update folder doesn't exist" + _pathProvider.UpdateSandboxFolder); + if (!_diskProvider.FolderExists(_pathProvider.GetUpdatePackageFolder())) + throw new DirectoryNotFoundException("Update folder doesn't exist " + _pathProvider.GetUpdatePackageFolder()); } @@ -62,14 +62,14 @@ namespace NzbDrone.Update.Providers } logger.Info("Creating backup of existing installation"); - _diskProvider.CopyDirectory(targetFolder, _pathProvider.UpdateBackUpFolder); + _diskProvider.CopyDirectory(targetFolder, _pathProvider.GetUpdateBackUpFolder()); logger.Info("Copying update package to target"); try { - _diskProvider.CopyDirectory(_pathProvider.UpdatePackageFolder, targetFolder); + _diskProvider.CopyDirectory(_pathProvider.GetUpdatePackageFolder(), targetFolder); } catch (Exception e) { @@ -85,7 +85,7 @@ namespace NzbDrone.Update.Providers private void RollBack(string targetFolder) { logger.Info("Attempting to rollback upgrade"); - _diskProvider.CopyDirectory(_pathProvider.UpdateBackUpFolder, targetFolder); + _diskProvider.CopyDirectory(_pathProvider.GetUpdateBackUpFolder(), targetFolder); }