From ed28f94f025a2a7efde796c643d0bf195952d0d1 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 27 Mar 2020 15:24:20 -0700 Subject: [PATCH] Improve root folder health check --- .../GetBestRootFolderPathFixture.cs | 47 +++++++++++++++++++ .../RootFolderServiceFixture.cs | 1 - .../HealthCheck/Checks/RootFolderCheck.cs | 16 ++++--- .../RootFolders/RootFolderService.cs | 2 +- 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 src/NzbDrone.Core.Test/RootFolderTests/GetBestRootFolderPathFixture.cs diff --git a/src/NzbDrone.Core.Test/RootFolderTests/GetBestRootFolderPathFixture.cs b/src/NzbDrone.Core.Test/RootFolderTests/GetBestRootFolderPathFixture.cs new file mode 100644 index 000000000..6ddc36b09 --- /dev/null +++ b/src/NzbDrone.Core.Test/RootFolderTests/GetBestRootFolderPathFixture.cs @@ -0,0 +1,47 @@ +using System.Linq; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Common.Disk; +using NzbDrone.Core.RootFolders; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.RootFolderTests +{ + [TestFixture] + public class GetBestRootFolderPathFixture : CoreTest + { + private void GivenRootFolders(params string[] paths) + { + Mocker.GetMock() + .Setup(s => s.All()) + .Returns(paths.Select(p => new RootFolder { Path = p })); + } + + [Test] + public void should_return_root_folder_that_is_parent_path() + { + GivenRootFolders(@"C:\Test\TV", @"D:\Test\TV"); + Subject.GetBestRootFolderPath(@"C:\Test\TV\Series Title").Should().Be(@"C:\Test\TV"); + } + + [Test] + public void should_return_root_folder_that_is_grandparent_path() + { + GivenRootFolders(@"C:\Test\TV", @"D:\Test\TV"); + Subject.GetBestRootFolderPath(@"C:\Test\TV\S\Series Title").Should().Be(@"C:\Test\TV"); + } + + [Test] + public void should_get_parent_path_from_diskProvider_if_matching_root_folder_is_not_found() + { + var seriesPath = @"T:\Test\TV\Series Title"; + + GivenRootFolders(@"C:\Test\TV", @"D:\Test\TV"); + Subject.GetBestRootFolderPath(seriesPath); + + Mocker.GetMock() + .Verify(v => v.GetParentFolder(seriesPath), Times.Once); + } + } +} diff --git a/src/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs b/src/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs index 972535a46..bf7c1e1c8 100644 --- a/src/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs +++ b/src/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs @@ -15,7 +15,6 @@ using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.RootFolderTests { [TestFixture] - public class RootFolderServiceFixture : CoreTest { [SetUp] diff --git a/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs index f5605f661..cbb507095 100644 --- a/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs +++ b/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs @@ -1,6 +1,7 @@ using System.Linq; using NzbDrone.Common.Disk; using NzbDrone.Core.MediaFiles.Events; +using NzbDrone.Core.RootFolders; using NzbDrone.Core.Tv; using NzbDrone.Core.Tv.Events; @@ -14,20 +15,23 @@ namespace NzbDrone.Core.HealthCheck.Checks { private readonly ISeriesService _seriesService; private readonly IDiskProvider _diskProvider; + private readonly IRootFolderService _rootFolderService; - public RootFolderCheck(ISeriesService seriesService, IDiskProvider diskProvider) + public RootFolderCheck(ISeriesService seriesService, IDiskProvider diskProvider, IRootFolderService rootFolderService) { _seriesService = seriesService; _diskProvider = diskProvider; + _rootFolderService = rootFolderService; } public override HealthCheck Check() { - var missingRootFolders = _seriesService.GetAllSeries() - .Select(s => _diskProvider.GetParentFolder(s.Path)) - .Distinct() - .Where(s => !_diskProvider.FolderExists(s)) - .ToList(); + var rootFolders = _seriesService.GetAllSeries() + .Select(s => _rootFolderService.GetBestRootFolderPath(s.Path)) + .Distinct(); + + var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s)) + .ToList(); if (missingRootFolders.Any()) { diff --git a/src/NzbDrone.Core/RootFolders/RootFolderService.cs b/src/NzbDrone.Core/RootFolders/RootFolderService.cs index e24ee2e84..f33e401f9 100644 --- a/src/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/src/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -171,7 +171,7 @@ namespace NzbDrone.Core.RootFolders if (possibleRootFolder == null) { - return Path.GetDirectoryName(path); + return _diskProvider.GetParentFolder(path); } return possibleRootFolder.Path;