aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Library/DotIgnoreIgnoreRuleTest.cs
blob: 03c0b4af39ac7398a303322acdde16f24efc11ee (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Emby.Server.Implementations.Library;
using MediaBrowser.Model.IO;
using Xunit;

namespace Jellyfin.Server.Implementations.Tests.Library;

public class DotIgnoreIgnoreRuleTest
{
    private static readonly string[] _rule1 = ["SPs"];
    private static readonly string[] _rule2 = ["SPs", "!thebestshot.mkv"];
    private static readonly string[] _rule3 = ["*.txt", @"{\colortbl;\red255\green255\blue255;}", "videos/", @"\invalid\escape\sequence", "*.mkv"];
    private static readonly string[] _rule4 = [@"{\colortbl;\red255\green255\blue255;}", @"\invalid\escape\sequence"];

    public static TheoryData<string[], string, bool, bool> CheckIgnoreRulesTestData =>
        new()
        {
            // Basic pattern matching
            { _rule1, "f:/cd/sps/ffffff.mkv", false, true },
            { _rule1, "cd/sps/ffffff.mkv", false, true },
            { _rule1, "/cd/sps/ffffff.mkv", false, true },

            // Negate pattern
            { _rule2, "f:/cd/sps/ffffff.mkv", false, true },
            { _rule2, "cd/sps/ffffff.mkv", false, true },
            { _rule2, "/cd/sps/ffffff.mkv", false, true },
            { _rule2, "f:/cd/sps/thebestshot.mkv", false, false },
            { _rule2, "cd/sps/thebestshot.mkv", false, false },
            { _rule2, "/cd/sps/thebestshot.mkv", false, false },

            // Mixed valid and invalid patterns - skips invalid, applies valid
            { _rule3, "test.txt", false, true },
            { _rule3, "videos/movie.mp4", false, true },
            { _rule3, "movie.mkv", false, true },
            { _rule3, "test.mp3", false, false },

            // Only invalid patterns - falls back to ignore all
            { _rule4, "any-file.txt", false, true },
            { _rule4, "any/path/to/file.mkv", false, true },
        };

    public static TheoryData<string[], string, bool, bool> WindowsPathNormalizationTestData =>
        new()
        {
            // Windows paths with backslashes - should match when normalizePath is true
            { _rule1, @"C:\cd\sps\ffffff.mkv", false, true },
            { _rule1, @"D:\media\sps\movie.mkv", false, true },
            { _rule1, @"\\server\share\sps\file.mkv", false, true },

            // Negate pattern with Windows paths
            { _rule2, @"C:\cd\sps\ffffff.mkv", false, true },
            { _rule2, @"C:\cd\sps\thebestshot.mkv", false, false },

            // Directory matching with Windows paths
            { _rule3, @"C:\videos\movie.mp4", false, true },
            { _rule3, @"D:\documents\test.txt", false, true },
            { _rule3, @"E:\music\song.mp3", false, false },
        };

    [Theory]
    [MemberData(nameof(CheckIgnoreRulesTestData))]
    public void CheckIgnoreRules_ReturnsExpectedResult(string[] rules, string path, bool isDirectory, bool expectedIgnored)
    {
        Assert.Equal(expectedIgnored, DotIgnoreIgnoreRule.CheckIgnoreRules(path, rules, isDirectory));
    }

    [Theory]
    [MemberData(nameof(WindowsPathNormalizationTestData))]
    public void CheckIgnoreRules_WithWindowsPaths_NormalizesBackslashes(string[] rules, string path, bool isDirectory, bool expectedIgnored)
    {
        // With normalizePath=true, backslashes should be converted to forward slashes
        Assert.Equal(expectedIgnored, DotIgnoreIgnoreRule.CheckIgnoreRules(path, rules, isDirectory, normalizePath: true));
    }

    [Theory]
    [InlineData(@"C:\cd\sps\ffffff.mkv")]
    [InlineData(@"D:\media\sps\movie.mkv")]
    public void CheckIgnoreRules_WithWindowsPaths_WithoutNormalization_DoesNotMatch(string path)
    {
        // Without normalization, Windows paths with backslashes won't match patterns expecting forward slashes
        Assert.False(DotIgnoreIgnoreRule.CheckIgnoreRules(path, _rule1, isDirectory: false, normalizePath: false));
    }

    [Fact]
    public void CacheHit_RepeatedCallsDoNotRereadFiles()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);
        var subDir = Path.Combine(tempDir, "subdir");
        Directory.CreateDirectory(subDir);

        try
        {
            var ignoreFilePath = Path.Combine(tempDir, ".ignore");
            File.WriteAllText(ignoreFilePath, "*.tmp");

            var rule = new DotIgnoreIgnoreRule();
            var fileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(subDir, "test.tmp"),
                IsDirectory = false
            };

            // First call - should cache
            var result1 = rule.ShouldIgnore(fileInfo, null);
            Assert.True(result1);

            // Second call - should use cache
            var result2 = rule.ShouldIgnore(fileInfo, null);
            Assert.True(result2);

            // Third call with different file in same directory - should use cache
            var fileInfo2 = new FileSystemMetadata
            {
                FullName = Path.Combine(subDir, "other.tmp"),
                IsDirectory = false
            };
            var result3 = rule.ShouldIgnore(fileInfo2, null);
            Assert.True(result3);

            // Call with file that doesn't match pattern
            var fileInfo3 = new FileSystemMetadata
            {
                FullName = Path.Combine(subDir, "other.txt"),
                IsDirectory = false
            };
            var result4 = rule.ShouldIgnore(fileInfo3, null);
            Assert.False(result4);
        }
        finally
        {
            Directory.Delete(tempDir, true);
        }
    }

    [Fact]
    public void CacheInvalidation_ModifyIgnoreFile_Reparses()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);

        try
        {
            var ignoreFilePath = Path.Combine(tempDir, ".ignore");
            File.WriteAllText(ignoreFilePath, "*.tmp");

            var rule = new DotIgnoreIgnoreRule();
            var fileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(tempDir, "test.tmp"),
                IsDirectory = false
            };

            // First call - should ignore .tmp files
            var result1 = rule.ShouldIgnore(fileInfo, null);
            Assert.True(result1);

            // Modify the .ignore file to ignore .txt instead
            // Wait a bit to ensure the file modification time changes
            Thread.Sleep(50);
            File.WriteAllText(ignoreFilePath, "*.txt");

            // Now .tmp files should NOT be ignored
            var result2 = rule.ShouldIgnore(fileInfo, null);
            Assert.False(result2);

            // And .txt files SHOULD be ignored
            var txtFileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(tempDir, "test.txt"),
                IsDirectory = false
            };
            var result3 = rule.ShouldIgnore(txtFileInfo, null);
            Assert.True(result3);
        }
        finally
        {
            Directory.Delete(tempDir, true);
        }
    }

    [Fact]
    public void EmptyIgnoreFile_IgnoresEverything()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);

        try
        {
            var ignoreFilePath = Path.Combine(tempDir, ".ignore");
            File.WriteAllText(ignoreFilePath, string.Empty);

            var rule = new DotIgnoreIgnoreRule();

            var fileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(tempDir, "anyfile.mkv"),
                IsDirectory = false
            };

            // Empty .ignore file should ignore everything
            var result = rule.ShouldIgnore(fileInfo, null);
            Assert.True(result);
        }
        finally
        {
            Directory.Delete(tempDir, true);
        }
    }

    [Fact]
    public void WhitespaceOnlyIgnoreFile_IgnoresEverything()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);

        try
        {
            var ignoreFilePath = Path.Combine(tempDir, ".ignore");
            File.WriteAllText(ignoreFilePath, "   \n\t\n   ");

            var rule = new DotIgnoreIgnoreRule();

            var fileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(tempDir, "anyfile.mkv"),
                IsDirectory = false
            };

            // Whitespace-only .ignore file should ignore everything
            var result = rule.ShouldIgnore(fileInfo, null);
            Assert.True(result);
        }
        finally
        {
            Directory.Delete(tempDir, true);
        }
    }

    [Fact]
    public void NoIgnoreFile_DoesNotIgnore()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);

        try
        {
            var rule = new DotIgnoreIgnoreRule();

            var fileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(tempDir, "anyfile.mkv"),
                IsDirectory = false
            };

            // No .ignore file means don't ignore
            var result = rule.ShouldIgnore(fileInfo, null);
            Assert.False(result);
        }
        finally
        {
            Directory.Delete(tempDir, true);
        }
    }

    [Fact]
    public void ConcurrentAccess_ThreadSafe()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);

        try
        {
            var ignoreFilePath = Path.Combine(tempDir, ".ignore");
            File.WriteAllText(ignoreFilePath, "*.tmp");

            var rule = new DotIgnoreIgnoreRule();

            // Run multiple parallel checks
            Parallel.For(0, 100, i =>
            {
                var fileInfo = new FileSystemMetadata
                {
                    FullName = Path.Combine(tempDir, $"test{i}.tmp"),
                    IsDirectory = false
                };

                var result = rule.ShouldIgnore(fileInfo, null);
                Assert.True(result);
            });

            // Also test with non-matching files
            Parallel.For(0, 100, i =>
            {
                var fileInfo = new FileSystemMetadata
                {
                    FullName = Path.Combine(tempDir, $"test{i}.txt"),
                    IsDirectory = false
                };

                var result = rule.ShouldIgnore(fileInfo, null);
                Assert.False(result);
            });
        }
        finally
        {
            Directory.Delete(tempDir, true);
        }
    }

    [Fact]
    public void ClearCache_ClearsAllCachedData()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);

        try
        {
            var ignoreFilePath = Path.Combine(tempDir, ".ignore");
            File.WriteAllText(ignoreFilePath, "*.tmp");

            var rule = new DotIgnoreIgnoreRule();
            var fileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(tempDir, "test.tmp"),
                IsDirectory = false
            };

            // First call to populate cache
            var result1 = rule.ShouldIgnore(fileInfo, null);
            Assert.True(result1);

            // Clear cache
            rule.ClearDirectoryCache();

            // Should still work (will re-populate cache)
            var result2 = rule.ShouldIgnore(fileInfo, null);
            Assert.True(result2);
        }
        finally
        {
            Directory.Delete(tempDir, true);
        }
    }

    [Fact]
    public void IgnoreFileDeleted_HandlesGracefully()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);

        try
        {
            var ignoreFilePath = Path.Combine(tempDir, ".ignore");
            File.WriteAllText(ignoreFilePath, "*.tmp");

            var rule = new DotIgnoreIgnoreRule();
            var fileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(tempDir, "test.tmp"),
                IsDirectory = false
            };

            // First call - should ignore
            var result1 = rule.ShouldIgnore(fileInfo, null);
            Assert.True(result1);

            // Delete the .ignore file
            File.Delete(ignoreFilePath);

            // Should not ignore anymore (file deleted)
            var result2 = rule.ShouldIgnore(fileInfo, null);
            Assert.False(result2);
        }
        finally
        {
            if (Directory.Exists(tempDir))
            {
                Directory.Delete(tempDir, true);
            }
        }
    }

    [Fact]
    public void ParentDirectoryIgnoreFile_AppliesToSubdirectories()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);
        var subDir1 = Path.Combine(tempDir, "sub1");
        var subDir2 = Path.Combine(tempDir, "sub1", "sub2");
        Directory.CreateDirectory(subDir1);
        Directory.CreateDirectory(subDir2);

        try
        {
            // Put .ignore in root
            var ignoreFilePath = Path.Combine(tempDir, ".ignore");
            File.WriteAllText(ignoreFilePath, "*.tmp");

            var rule = new DotIgnoreIgnoreRule();

            // Check file in sub2 - should find .ignore in parent
            var fileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(subDir2, "test.tmp"),
                IsDirectory = false
            };

            var result = rule.ShouldIgnore(fileInfo, null);
            Assert.True(result);

            // Check file in sub1
            var fileInfo2 = new FileSystemMetadata
            {
                FullName = Path.Combine(subDir1, "test.tmp"),
                IsDirectory = false
            };

            var result2 = rule.ShouldIgnore(fileInfo2, null);
            Assert.True(result2);
        }
        finally
        {
            Directory.Delete(tempDir, true);
        }
    }

    [Fact]
    public void DirectoryMatching_TrailingSlashPattern()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);
        var subDir = Path.Combine(tempDir, "videos");
        Directory.CreateDirectory(subDir);

        try
        {
            var ignoreFilePath = Path.Combine(tempDir, ".ignore");
            File.WriteAllText(ignoreFilePath, "videos/");

            var rule = new DotIgnoreIgnoreRule();

            // Directory should be ignored
            var dirInfo = new FileSystemMetadata
            {
                FullName = subDir,
                IsDirectory = true
            };

            var result = rule.ShouldIgnore(dirInfo, null);
            Assert.True(result);

            // File named "videos" should NOT be ignored (pattern has trailing slash)
            var fileInfo = new FileSystemMetadata
            {
                FullName = Path.Combine(tempDir, "videos"),
                IsDirectory = false
            };

            // Note: The Ignore library behavior may vary here, this tests the actual behavior
            var resultFile = rule.ShouldIgnore(fileInfo, null);
            // The file named "videos" without trailing slash might or might not match depending on the library
            // This test documents the actual behavior
        }
        finally
        {
            Directory.Delete(tempDir, true);
        }
    }
}