|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using FolderDiffIL4DotNet.Models; |
| 6 | +using FolderDiffIL4DotNet.Services; |
| 7 | +using FolderDiffIL4DotNet.Services.Caching; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace FolderDiffIL4DotNet.Tests.Services |
| 12 | +{ |
| 13 | + public sealed class FileDiffServiceTests : IDisposable |
| 14 | + { |
| 15 | + private readonly string _rootDir; |
| 16 | + |
| 17 | + public FileDiffServiceTests() |
| 18 | + { |
| 19 | + _rootDir = Path.Combine(Path.GetTempPath(), "fd-filediff-tests-" + Guid.NewGuid().ToString("N")); |
| 20 | + Directory.CreateDirectory(_rootDir); |
| 21 | + } |
| 22 | + |
| 23 | + public void Dispose() |
| 24 | + { |
| 25 | + try |
| 26 | + { |
| 27 | + if (Directory.Exists(_rootDir)) |
| 28 | + { |
| 29 | + Directory.Delete(_rootDir, recursive: true); |
| 30 | + } |
| 31 | + } |
| 32 | + catch |
| 33 | + { |
| 34 | + // ignore cleanup errors in tests |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task FilesAreEqualAsync_WhenPrimaryTextDiffThrows_LogsWarningAndFallsBackToSequentialDiff() |
| 40 | + { |
| 41 | + var oldDir = Path.Combine(_rootDir, "old"); |
| 42 | + var newDir = Path.Combine(_rootDir, "new"); |
| 43 | + Directory.CreateDirectory(oldDir); |
| 44 | + Directory.CreateDirectory(newDir); |
| 45 | + |
| 46 | + const string fileRelativePath = "sample.txt"; |
| 47 | + var oldFileAbsolutePath = Path.Combine(oldDir, fileRelativePath); |
| 48 | + var newFileAbsolutePath = Path.Combine(newDir, fileRelativePath); |
| 49 | + File.WriteAllText(oldFileAbsolutePath, "old-content"); |
| 50 | + File.WriteAllText(newFileAbsolutePath, "new"); |
| 51 | + |
| 52 | + var config = new ConfigSettings |
| 53 | + { |
| 54 | + TextFileExtensions = new List<string> { ".txt" }, |
| 55 | + IgnoredExtensions = new List<string>(), |
| 56 | + ShouldOutputILText = false, |
| 57 | + EnableILCache = false, |
| 58 | + OptimizeForNetworkShares = true |
| 59 | + }; |
| 60 | + |
| 61 | + FileStream exclusiveLockStream = new FileStream(oldFileAbsolutePath, FileMode.Open, FileAccess.Read, FileShare.None); |
| 62 | + var logger = new TestLogger(entry => |
| 63 | + { |
| 64 | + if (entry.LogLevel == AppLogLevel.Warning && entry.Message.Contains("Falling back to sequential text diff", StringComparison.Ordinal)) |
| 65 | + { |
| 66 | + exclusiveLockStream?.Dispose(); |
| 67 | + exclusiveLockStream = null; |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + var resultLists = new FileDiffResultLists(); |
| 72 | + var provider = new ServiceCollection() |
| 73 | + .AddSingleton<ILoggerService>(logger) |
| 74 | + .AddSingleton(resultLists) |
| 75 | + .AddSingleton(new DotNetDisassemblerCache(logger)) |
| 76 | + .BuildServiceProvider(); |
| 77 | + |
| 78 | + try |
| 79 | + { |
| 80 | + var ilOutputService = new ILOutputService(config, oldDir, newDir, provider, logger); |
| 81 | + var service = new FileDiffService(config, ilOutputService, oldDir, newDir, optimizeForNetworkShares: true, resultLists, logger); |
| 82 | + |
| 83 | + var areEqual = await service.FilesAreEqualAsync(fileRelativePath, maxParallel: 1); |
| 84 | + |
| 85 | + Assert.False(areEqual); |
| 86 | + Assert.Equal(FileDiffResultLists.DiffDetailResult.TextMismatch, resultLists.FileRelativePathToDiffDetailDictionary[fileRelativePath]); |
| 87 | + var warningLog = Assert.Single(logger.Entries, entry => entry.LogLevel == AppLogLevel.Warning); |
| 88 | + Assert.Contains("Falling back to sequential text diff", warningLog.Message); |
| 89 | + Assert.IsType<IOException>(warningLog.Exception); |
| 90 | + } |
| 91 | + finally |
| 92 | + { |
| 93 | + exclusiveLockStream?.Dispose(); |
| 94 | + provider.Dispose(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private sealed class TestLogger : ILoggerService |
| 99 | + { |
| 100 | + private readonly Action<LogEntry> _onEntry; |
| 101 | + |
| 102 | + public TestLogger(Action<LogEntry> onEntry) |
| 103 | + { |
| 104 | + _onEntry = onEntry; |
| 105 | + } |
| 106 | + |
| 107 | + public string LogFileAbsolutePath => null; |
| 108 | + |
| 109 | + public List<LogEntry> Entries { get; } = new(); |
| 110 | + |
| 111 | + public void Initialize() { } |
| 112 | + |
| 113 | + public void CleanupOldLogFiles(int maxLogGenerations) { } |
| 114 | + |
| 115 | + public void LogMessage(AppLogLevel logLevel, string message, bool shouldOutputMessageToConsole, Exception exception = null) |
| 116 | + => LogMessage(logLevel, message, shouldOutputMessageToConsole, consoleForegroundColor: null, exception); |
| 117 | + |
| 118 | + public void LogMessage(AppLogLevel logLevel, string message, bool shouldOutputMessageToConsole, ConsoleColor? consoleForegroundColor, Exception exception = null) |
| 119 | + { |
| 120 | + var entry = new LogEntry(logLevel, message, exception); |
| 121 | + Entries.Add(entry); |
| 122 | + _onEntry?.Invoke(entry); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private sealed record LogEntry(AppLogLevel LogLevel, string Message, Exception Exception); |
| 127 | + } |
| 128 | +} |
0 commit comments