|
| 1 | +using Cocoar.FileSystem.Tests.TestUtilities; |
| 2 | + |
| 3 | +namespace Cocoar.FileSystem.Tests; |
| 4 | + |
| 5 | +public sealed class MultiplePatternTests : IDisposable |
| 6 | +{ |
| 7 | + private readonly string _testRoot; |
| 8 | + |
| 9 | + public MultiplePatternTests() |
| 10 | + { |
| 11 | + _testRoot = Path.Combine(Path.GetTempPath(), $"CocoarFS_MultiPattern_{Guid.NewGuid():N}"); |
| 12 | + Directory.CreateDirectory(_testRoot); |
| 13 | + } |
| 14 | + |
| 15 | + public void Dispose() |
| 16 | + { |
| 17 | + try |
| 18 | + { |
| 19 | + if (Directory.Exists(_testRoot)) |
| 20 | + Directory.Delete(_testRoot, recursive: true); |
| 21 | + } |
| 22 | + catch |
| 23 | + { |
| 24 | + // Best effort cleanup |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task MultiplePatterns_MatchesAllSpecified() |
| 30 | + { |
| 31 | + // Arrange |
| 32 | + var createdFiles = new List<string>(); |
| 33 | + |
| 34 | + using var monitor = ResilientFileSystemMonitor.Watch(_testRoot) |
| 35 | + .WithFilter("*.pfx", "*.p12", "*.cer") |
| 36 | + .Build(); |
| 37 | + |
| 38 | + monitor.Created += (_, e) => createdFiles.Add(Path.GetFileName(e.FullPath)); |
| 39 | + |
| 40 | + await Task.Delay(200); |
| 41 | + |
| 42 | + // Act |
| 43 | + File.WriteAllText(Path.Combine(_testRoot, "cert.pfx"), "pfx"); |
| 44 | + File.WriteAllText(Path.Combine(_testRoot, "cert.p12"), "p12"); |
| 45 | + File.WriteAllText(Path.Combine(_testRoot, "cert.cer"), "cer"); |
| 46 | + File.WriteAllText(Path.Combine(_testRoot, "readme.txt"), "txt"); |
| 47 | + |
| 48 | + await ActiveWaitHelpers.WaitUntilAsync( |
| 49 | + () => createdFiles.Count >= 3, |
| 50 | + timeout: TimeSpan.FromSeconds(3)); |
| 51 | + |
| 52 | + // Assert - Only certificate files detected |
| 53 | + Assert.Contains("cert.pfx", createdFiles); |
| 54 | + Assert.Contains("cert.p12", createdFiles); |
| 55 | + Assert.Contains("cert.cer", createdFiles); |
| 56 | + Assert.DoesNotContain("readme.txt", createdFiles); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public async Task MultiplePatterns_WorksWithWildcards() |
| 61 | + { |
| 62 | + // Arrange |
| 63 | + var createdFiles = new List<string>(); |
| 64 | + |
| 65 | + using var monitor = ResilientFileSystemMonitor.Watch(_testRoot) |
| 66 | + .WithFilter("test-*.txt", "backup-*.log") |
| 67 | + .Build(); |
| 68 | + |
| 69 | + monitor.Created += (_, e) => createdFiles.Add(Path.GetFileName(e.FullPath)); |
| 70 | + |
| 71 | + await Task.Delay(200); |
| 72 | + |
| 73 | + // Act |
| 74 | + File.WriteAllText(Path.Combine(_testRoot, "test-1.txt"), "1"); |
| 75 | + File.WriteAllText(Path.Combine(_testRoot, "test-2.txt"), "2"); |
| 76 | + File.WriteAllText(Path.Combine(_testRoot, "backup-old.log"), "old"); |
| 77 | + File.WriteAllText(Path.Combine(_testRoot, "data.csv"), "csv"); |
| 78 | + File.WriteAllText(Path.Combine(_testRoot, "other.txt"), "other"); |
| 79 | + |
| 80 | + await ActiveWaitHelpers.WaitUntilAsync( |
| 81 | + () => createdFiles.Count >= 3, |
| 82 | + timeout: TimeSpan.FromSeconds(3)); |
| 83 | + |
| 84 | + // Assert |
| 85 | + Assert.Contains("test-1.txt", createdFiles); |
| 86 | + Assert.Contains("test-2.txt", createdFiles); |
| 87 | + Assert.Contains("backup-old.log", createdFiles); |
| 88 | + Assert.DoesNotContain("data.csv", createdFiles); |
| 89 | + Assert.DoesNotContain("other.txt", createdFiles); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public async Task MultiplePatterns_WorksWithSubdirectories() |
| 94 | + { |
| 95 | + // Arrange |
| 96 | + var subDir = Path.Combine(_testRoot, "sub"); |
| 97 | + Directory.CreateDirectory(subDir); |
| 98 | + |
| 99 | + var createdFiles = new List<string>(); |
| 100 | + |
| 101 | + using var monitor = ResilientFileSystemMonitor.Watch(_testRoot) |
| 102 | + .WithFilter("*.json", "*.xml") |
| 103 | + .IncludeSubdirectories() |
| 104 | + .Build(); |
| 105 | + |
| 106 | + monitor.Created += (_, e) => createdFiles.Add(Path.GetFileName(e.FullPath)); |
| 107 | + |
| 108 | + await Task.Delay(200); |
| 109 | + |
| 110 | + // Act |
| 111 | + File.WriteAllText(Path.Combine(_testRoot, "root.json"), "{}"); |
| 112 | + File.WriteAllText(Path.Combine(subDir, "sub.xml"), "<xml/>"); |
| 113 | + File.WriteAllText(Path.Combine(subDir, "sub.txt"), "txt"); |
| 114 | + |
| 115 | + await ActiveWaitHelpers.WaitUntilAsync( |
| 116 | + () => createdFiles.Count >= 2, |
| 117 | + timeout: TimeSpan.FromSeconds(3)); |
| 118 | + |
| 119 | + // Assert |
| 120 | + Assert.Contains("root.json", createdFiles); |
| 121 | + Assert.Contains("sub.xml", createdFiles); |
| 122 | + Assert.DoesNotContain("sub.txt", createdFiles); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public async Task MultiplePatterns_SnapshotFindsExistingFiles() |
| 127 | + { |
| 128 | + // Arrange - Create files BEFORE starting monitor |
| 129 | + File.WriteAllText(Path.Combine(_testRoot, "existing.pfx"), "pfx"); |
| 130 | + File.WriteAllText(Path.Combine(_testRoot, "existing.p12"), "p12"); |
| 131 | + File.WriteAllText(Path.Combine(_testRoot, "existing.txt"), "txt"); |
| 132 | + |
| 133 | + var changedFiles = new List<string>(); |
| 134 | + |
| 135 | + // Act - Start monitor (should snapshot existing files) |
| 136 | + using var monitor = ResilientFileSystemMonitor.Watch(_testRoot) |
| 137 | + .WithFilter("*.pfx", "*.p12") |
| 138 | + .Build(); |
| 139 | + |
| 140 | + monitor.Changed += (_, e) => changedFiles.Add(Path.GetFileName(e.FullPath)); |
| 141 | + |
| 142 | + await Task.Delay(500); // Give time for initial snapshot |
| 143 | + |
| 144 | + // Modify files to trigger change events |
| 145 | + File.WriteAllText(Path.Combine(_testRoot, "existing.pfx"), "modified-pfx"); |
| 146 | + File.WriteAllText(Path.Combine(_testRoot, "existing.p12"), "modified-p12"); |
| 147 | + File.WriteAllText(Path.Combine(_testRoot, "existing.txt"), "modified-txt"); |
| 148 | + |
| 149 | + await ActiveWaitHelpers.WaitUntilAsync( |
| 150 | + () => changedFiles.Count >= 2, |
| 151 | + timeout: TimeSpan.FromSeconds(3)); |
| 152 | + |
| 153 | + // Assert - Should detect changes to pfx/p12 but not txt |
| 154 | + Assert.Contains("existing.pfx", changedFiles); |
| 155 | + Assert.Contains("existing.p12", changedFiles); |
| 156 | + Assert.DoesNotContain("existing.txt", changedFiles); |
| 157 | + } |
| 158 | + |
| 159 | + [Fact] |
| 160 | + public void WithFilter_EmptyArray_ThrowsArgumentException() |
| 161 | + { |
| 162 | + // Act & Assert |
| 163 | + Assert.Throws<ArgumentException>(() => |
| 164 | + ResilientFileSystemMonitor.Watch(_testRoot).WithFilter([])); |
| 165 | + } |
| 166 | + |
| 167 | + [Fact] |
| 168 | + public void WithFilter_NullPattern_ThrowsArgumentNullException() |
| 169 | + { |
| 170 | + // Act & Assert |
| 171 | + Assert.Throws<ArgumentNullException>(() => |
| 172 | + ResilientFileSystemMonitor.Watch(_testRoot).WithFilter((string[])null!)); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public void WithFilter_ContainsNullOrWhitespace_ThrowsArgumentException() |
| 177 | + { |
| 178 | + // Act & Assert |
| 179 | + Assert.Throws<ArgumentException>(() => |
| 180 | + ResilientFileSystemMonitor.Watch(_testRoot).WithFilter("*.txt", null!, "*.log")); |
| 181 | + } |
| 182 | + |
| 183 | + [Fact] |
| 184 | + public async Task WithFilter_MultipleCalls_AddsPatterns() |
| 185 | + { |
| 186 | + // Arrange |
| 187 | + var createdFiles = new List<string>(); |
| 188 | + |
| 189 | + using var monitor = ResilientFileSystemMonitor.Watch(_testRoot) |
| 190 | + .WithFilter("*.txt") // First call |
| 191 | + .WithFilter("*.pfx", "*.p12") // Second call - should ADD, not replace |
| 192 | + .Build(); |
| 193 | + |
| 194 | + monitor.Created += (_, e) => createdFiles.Add(Path.GetFileName(e.FullPath)); |
| 195 | + |
| 196 | + await Task.Delay(200); |
| 197 | + |
| 198 | + // Act |
| 199 | + File.WriteAllText(Path.Combine(_testRoot, "test.txt"), "txt"); |
| 200 | + File.WriteAllText(Path.Combine(_testRoot, "test.pfx"), "pfx"); |
| 201 | + File.WriteAllText(Path.Combine(_testRoot, "test.p12"), "p12"); |
| 202 | + |
| 203 | + await ActiveWaitHelpers.WaitUntilAsync( |
| 204 | + () => createdFiles.Count >= 3, |
| 205 | + timeout: TimeSpan.FromSeconds(3)); |
| 206 | + |
| 207 | + // Assert - Should match ALL patterns (additive behavior) |
| 208 | + Assert.Contains("test.txt", createdFiles); |
| 209 | + Assert.Contains("test.pfx", createdFiles); |
| 210 | + Assert.Contains("test.p12", createdFiles); |
| 211 | + } |
| 212 | + |
| 213 | + [Fact] |
| 214 | + public async Task ClearFilters_RemovesAllPatterns() |
| 215 | + { |
| 216 | + // Arrange |
| 217 | + var createdFiles = new List<string>(); |
| 218 | + |
| 219 | + using var monitor = ResilientFileSystemMonitor.Watch(_testRoot) |
| 220 | + .WithFilter("*.txt") |
| 221 | + .WithFilter("*.pfx") |
| 222 | + .ClearFilters() // Should clear all patterns |
| 223 | + .Build(); |
| 224 | + |
| 225 | + monitor.Created += (_, e) => createdFiles.Add(Path.GetFileName(e.FullPath)); |
| 226 | + |
| 227 | + await Task.Delay(200); |
| 228 | + |
| 229 | + // Act |
| 230 | + File.WriteAllText(Path.Combine(_testRoot, "test.txt"), "txt"); |
| 231 | + File.WriteAllText(Path.Combine(_testRoot, "test.pfx"), "pfx"); |
| 232 | + |
| 233 | + await ActiveWaitHelpers.WaitUntilAsync( |
| 234 | + () => createdFiles.Count >= 2, |
| 235 | + timeout: TimeSpan.FromSeconds(3)); |
| 236 | + |
| 237 | + // Assert - Should match ALL files (no filters) |
| 238 | + Assert.Contains("test.txt", createdFiles); |
| 239 | + Assert.Contains("test.pfx", createdFiles); |
| 240 | + } |
| 241 | +} |
0 commit comments