mirror of https://github.com/Sonarr/Sonarr
Path calculations are now done using extension methods.
This commit is contained in:
parent
18b10d45c6
commit
2e94e322f4
|
@ -45,6 +45,7 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ConsoleProvider.cs" />
|
||||
<Compile Include="PathExtentions.cs" />
|
||||
<Compile Include="PathProvider.cs" />
|
||||
<Compile Include="DiskProvider.cs" />
|
||||
<Compile Include="EnviromentProvider.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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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); }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<PathProvider>()
|
||||
.SetupGet(c => c.UpdateSandboxFolder).Returns(Path.Combine(TempFolder, "NzbDrone_update"));
|
||||
|
||||
|
||||
SandBoxPath = Mocker.GetMock<PathProvider>().Object.UpdateSandboxFolder;
|
||||
|
||||
Mocker.GetMock<PathProvider>()
|
||||
.SetupGet(c => c.UpdatePackageFolder).Returns(Path.Combine(SandBoxPath, "NzbDrone"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Should_call_download_and_extract_using_correct_arguments()
|
||||
{
|
||||
//Act
|
||||
Mocker.GetMock<PathProvider>().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<HttpProvider>().Setup(
|
||||
c => c.DownloadFile(updatePackage.Url, Path.Combine(SandBoxPath, updatePackage.FileName)));
|
||||
c => c.DownloadFile(updatePackage.Url, updateArchive));
|
||||
|
||||
Mocker.GetMock<ArchiveProvider>().Setup(
|
||||
c => c.ExtractArchive(Path.Combine(SandBoxPath, updatePackage.FileName), SandBoxPath));
|
||||
c => c.ExtractArchive(updateArchive, SANDBOX_FOLDER));
|
||||
|
||||
//Act
|
||||
Mocker.Resolve<UpdateProvider>().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<PathProvider>().SetupGet(c => c.SystemTemp).Returns(TempFolder);
|
||||
|
||||
var updateSubFolder = new DirectoryInfo(Mocker.GetMock<PathProvider>().Object.GetUpdateSandboxFolder());
|
||||
|
||||
var updatePackage = new UpdatePackage
|
||||
{
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
|
|
@ -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> _pathProvider = null;
|
||||
Mock<PathProvider> _pathProvider;
|
||||
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
mocker = new AutoMoqer();
|
||||
|
||||
|
||||
|
||||
_pathProvider = mocker.GetMock<PathProvider>();
|
||||
|
||||
_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<DiskProvider>()
|
||||
.Setup(c => c.FolderExists(UPDATE_FOLDER))
|
||||
|
|
|
@ -23,6 +23,9 @@ namespace NzbDrone.Update.Test
|
|||
|
||||
mocker.GetMock<EnviromentProvider>()
|
||||
.Setup(c => c.StartUpPath).Returns(@"C:\Temp\NzbDrone_update\");
|
||||
|
||||
mocker.GetMock<PathProvider>()
|
||||
.Setup(c => c.SystemTemp).Returns(@"C:\Temp\");
|
||||
}
|
||||
|
||||
[TestCase(null)]
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue