aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/LibraryRootIgnoreTests.cs
blob: f37aadbe8d85caeae7dab7b5cb6a523c7d2c79c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.IO;
using AutoFixture;
using AutoFixture.AutoMoq;
using Emby.Naming.Common;
using Emby.Server.Implementations.IO;
using Emby.Server.Implementations.Library;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Sorting;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Jellyfin.Server.Implementations.Tests.Library.LibraryManager;

public sealed class LibraryRootIgnoreTests : IDisposable
{
    private readonly string _tempDir;
    private readonly string _userViewsPath;
    private readonly string _libraryRoot;
    private readonly FileSystemMetadata _movieFile;

    public LibraryRootIgnoreTests()
    {
        _tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        _userViewsPath = Path.Combine(_tempDir, "root", "default");
        _libraryRoot = Path.Combine(_tempDir, "media", "Movies");
        var movieDir = Path.Combine(_libraryRoot, "Movie (2020)");
        Directory.CreateDirectory(movieDir);

        // Ignores everything below it, but sits above the library root
        File.WriteAllText(Path.Combine(_tempDir, ".ignore"), string.Empty);

        _movieFile = new FileSystemMetadata
        {
            FullName = Path.Combine(movieDir, "Movie (2020).mkv"),
            IsDirectory = false
        };
    }

    public void Dispose()
    {
        Directory.Delete(_tempDir, true);
    }

    [Fact]
    public void IgnoreFile_IgnoreFileAboveMblinkLibraryRoot_IsNotConsulted()
    {
        var libraryDir = Path.Combine(_userViewsPath, "Movies");
        Directory.CreateDirectory(libraryDir);
        File.WriteAllText(Path.Combine(libraryDir, "Movies.mblink"), _libraryRoot);

        Assert.False(CreateLibraryManager().IgnoreFile(_movieFile, null));
    }

    [Fact]
    public void IgnoreFile_UserViewsPathMissing_WalksToFilesystemRoot()
    {
        // Before root/default exists there are no library roots, so the .ignore above still applies
        Assert.False(Directory.Exists(_userViewsPath));
        Assert.True(CreateLibraryManager().IgnoreFile(_movieFile, null));
    }

    private Emby.Server.Implementations.Library.LibraryManager CreateLibraryManager()
    {
        var fixture = new Fixture().Customize(new AutoMoqCustomization());
        fixture.Register(() => new NamingOptions());
        var configMock = fixture.Freeze<Mock<IServerConfigurationManager>>();
        configMock.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns(_tempDir);
        configMock.Setup(c => c.ApplicationPaths.DefaultUserViewsPath).Returns(_userViewsPath);
        fixture.Freeze<Mock<IServerApplicationHost>>()
            .Setup(h => h.ExpandVirtualPath(It.IsAny<string>()))
            .Returns<string>(path => path);
        fixture.Inject<IFileSystem>(new ManagedFileSystem(
            NullLogger<ManagedFileSystem>.Instance,
            configMock.Object.ApplicationPaths,
            [new MbLinkShortcutHandler()]));

        // The same instance LibraryManager hands the roots to must be the one evaluating the rules
        var dotIgnoreRule = new DotIgnoreIgnoreRule();
        fixture.Inject(dotIgnoreRule);

        return fixture.Build<Emby.Server.Implementations.Library.LibraryManager>().Do(s => s.AddParts(
                [dotIgnoreRule],
                [],
                [],
                Array.Empty<IBaseItemComparer>(),
                []))
            .Create();
    }
}