From 4ca774182a6bdc1144e62d6edc8678060cc6c217 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sat, 12 Dec 2020 21:28:59 -0500 Subject: [PATCH] Improve root folder health check Co-Authored-By: Mark McDowall Signed-off-by: Robin Dadswell --- .../GetBestRootFolderPathFixture.cs | 48 +++++++++++++++++++ .../HealthCheck/Checks/RootFolderCheck.cs | 15 +++--- .../RootFolders/RootFolderService.cs | 2 +- 3 files changed, 58 insertions(+), 7 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..49a013703 --- /dev/null +++ b/src/NzbDrone.Core.Test/RootFolderTests/GetBestRootFolderPathFixture.cs @@ -0,0 +1,48 @@ +using System.Linq; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Common.Disk; +using NzbDrone.Core.RootFolders; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common; + +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\Music".AsOsAgnostic(), @"D:\Test\Music".AsOsAgnostic()); + Subject.GetBestRootFolderPath(@"C:\Test\Music\Series Title".AsOsAgnostic()).Should().Be(@"C:\Test\Music".AsOsAgnostic()); + } + + [Test] + public void should_return_root_folder_that_is_grandparent_path() + { + GivenRootFolders(@"C:\Test\Music".AsOsAgnostic(), @"D:\Test\Music".AsOsAgnostic()); + Subject.GetBestRootFolderPath(@"C:\Test\Music\S\Series Title".AsOsAgnostic()).Should().Be(@"C:\Test\Music".AsOsAgnostic()); + } + + [Test] + public void should_get_parent_path_from_diskProvider_if_matching_root_folder_is_not_found() + { + var seriesPath = @"T:\Test\Music\Series Title".AsOsAgnostic(); + + GivenRootFolders(@"C:\Test\Music".AsOsAgnostic(), @"D:\Test\Music".AsOsAgnostic()); + Subject.GetBestRootFolderPath(seriesPath); + + Mocker.GetMock() + .Verify(v => v.GetParentFolder(seriesPath), Times.Once); + } + } +} diff --git a/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs index c74655174..de020ae21 100644 --- a/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs +++ b/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs @@ -16,21 +16,24 @@ namespace NzbDrone.Core.HealthCheck.Checks private readonly IAuthorService _authorService; private readonly IImportListFactory _importListFactory; private readonly IDiskProvider _diskProvider; + private readonly IRootFolderService _rootFolderService; - public RootFolderCheck(IAuthorService authorService, IImportListFactory importListFactory, IDiskProvider diskProvider) + public RootFolderCheck(IArtistService authorService, IImportListFactory importListFactory, IDiskProvider diskProvider, IRootFolderService rootFolderService) { _authorService = authorService; _importListFactory = importListFactory; _diskProvider = diskProvider; + _rootFolderService = rootFolderService; } public override HealthCheck Check() { - var missingRootFolders = _authorService.GetAllAuthors() - .Select(s => _diskProvider.GetParentFolder(s.Path)) - .Distinct() - .Where(s => !_diskProvider.FolderExists(s)) - .ToList(); + var rootFolders = _artistService.GetAllAuthors() + .Select(s => _rootFolderService.GetBestRootFolderPath(s.Path)) + .Distinct(); + + var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s)) + .ToList(); missingRootFolders.AddRange(_importListFactory.All() .Select(s => s.RootFolderPath) diff --git a/src/NzbDrone.Core/RootFolders/RootFolderService.cs b/src/NzbDrone.Core/RootFolders/RootFolderService.cs index 368d668b9..cb85af184 100644 --- a/src/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/src/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -153,7 +153,7 @@ namespace NzbDrone.Core.RootFolders if (possibleRootFolder == null) { - return Path.GetDirectoryName(path); + return _diskProvider.GetParentFolder(path); } return possibleRootFolder.Path;