Repository navigation
Expand file tree
/
Copy pathFolderDiffServiceTests.cs
More file actions
407 lines (350 loc) · 20.1 KB
/
Copy pathFolderDiffServiceTests.cs
File metadata and controls
407 lines (350 loc) · 20.1 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using FolderDiffIL4DotNet.Services.Caching;
using FolderDiffIL4DotNet.Models;
using FolderDiffIL4DotNet.Services;
using FolderDiffIL4DotNet.Services.ILOutput;
using Xunit;
namespace FolderDiffIL4DotNet.Tests.Services
{
[Trait("Category", "Integration")]
public sealed class FolderDiffServiceTests : IDisposable
{
private readonly string _rootDir;
private readonly FileDiffResultLists _resultLists = new();
private readonly ILoggerService _logger = new LoggerService();
public FolderDiffServiceTests()
{
_rootDir = Path.Combine(Path.GetTempPath(), "fd-folderdiff-tests-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_rootDir);
TimestampCache.Clear();
_resultLists.ResetAll();
}
public void Dispose()
{
TimestampCache.Clear();
_resultLists.ResetAll();
try
{
if (Directory.Exists(_rootDir))
{
Directory.Delete(_rootDir, recursive: true);
}
}
catch
{
// ignore cleanup errors / クリーンアップエラーを無視
}
}
[Fact]
public async Task ExecuteFolderDiffAsync_SequentialMode_ClassifiesFilesAndRecordsIgnored()
{
var oldDir = Path.Combine(_rootDir, "old-sequential");
var newDir = Path.Combine(_rootDir, "new-sequential");
var reportDir = Path.Combine(_rootDir, "report-sequential");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
Directory.CreateDirectory(reportDir);
WriteFile(oldDir, "same.txt", "same");
WriteFile(newDir, "same.txt", "same");
WriteFile(oldDir, "modified.txt", "before");
WriteFile(newDir, "modified.txt", "after");
WriteFile(oldDir, "removed.txt", "removed");
WriteFile(newDir, "added.txt", "added");
WriteFile(oldDir, "ignored.pdb", "ignore-old");
WriteFile(newDir, "ignored.pdb", "ignore-new");
var config = CreateConfig(maxParallelism: 1);
using var progressReporter = new ProgressReportService(new ConfigSettingsBuilder().Build());
var service = CreateService(config, progressReporter, oldDir, newDir, reportDir);
await service.ExecuteFolderDiffAsync();
Assert.Contains("same.txt", _resultLists.UnchangedFilesRelativePath);
Assert.Contains("modified.txt", _resultLists.ModifiedFilesRelativePath);
Assert.Contains(Path.Combine(oldDir, "removed.txt"), _resultLists.RemovedFilesAbsolutePath);
Assert.Contains(Path.Combine(newDir, "added.txt"), _resultLists.AddedFilesAbsolutePath);
Assert.Equal(
FileDiffResultLists.IgnoredFileLocation.Old | FileDiffResultLists.IgnoredFileLocation.New,
_resultLists.IgnoredFilesRelativePathToLocation["ignored.pdb"]);
Assert.Equal(FileDiffResultLists.DiffDetailResult.SHA256Match, _resultLists.FileRelativePathToDiffDetailDictionary["same.txt"]);
Assert.Equal(FileDiffResultLists.DiffDetailResult.TextMismatch, _resultLists.FileRelativePathToDiffDetailDictionary["modified.txt"]);
}
[Fact]
public async Task ExecuteFolderDiffAsync_ParallelMode_ClassifiesWithoutRegression()
{
var oldDir = Path.Combine(_rootDir, "old-parallel");
var newDir = Path.Combine(_rootDir, "new-parallel");
var reportDir = Path.Combine(_rootDir, "report-parallel");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
Directory.CreateDirectory(reportDir);
WriteFile(oldDir, Path.Combine("nested", "same.txt"), "same");
WriteFile(newDir, Path.Combine("nested", "same.txt"), "same");
WriteFile(oldDir, Path.Combine("nested", "modified.txt"), "before");
WriteFile(newDir, Path.Combine("nested", "modified.txt"), "after");
WriteFile(newDir, Path.Combine("nested", "added.txt"), "added");
var config = CreateConfig(maxParallelism: 2);
using var progressReporter = new ProgressReportService(new ConfigSettingsBuilder().Build());
var service = CreateService(config, progressReporter, oldDir, newDir, reportDir);
await service.ExecuteFolderDiffAsync();
Assert.Contains(Path.Combine("nested", "same.txt"), _resultLists.UnchangedFilesRelativePath);
Assert.Contains(Path.Combine("nested", "modified.txt"), _resultLists.ModifiedFilesRelativePath);
Assert.Contains(Path.Combine(newDir, "nested", "added.txt"), _resultLists.AddedFilesAbsolutePath);
Assert.Equal(FileDiffResultLists.DiffDetailResult.SHA256Match, _resultLists.FileRelativePathToDiffDetailDictionary[Path.Combine("nested", "same.txt")]);
Assert.Equal(FileDiffResultLists.DiffDetailResult.TextMismatch, _resultLists.FileRelativePathToDiffDetailDictionary[Path.Combine("nested", "modified.txt")]);
}
[Fact]
public async Task ExecuteFolderDiffAsync_ClearsPreviousRunStateAtStart()
{
_resultLists.AddModifiedFileRelativePath("stale.txt");
_resultLists.RecordDiffDetail("stale.txt", FileDiffResultLists.DiffDetailResult.SHA256Mismatch);
var oldDir = Path.Combine(_rootDir, "old-empty");
var newDir = Path.Combine(_rootDir, "new-empty");
var reportDir = Path.Combine(_rootDir, "report-empty");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
Directory.CreateDirectory(reportDir);
var config = CreateConfig(maxParallelism: 1);
using var progressReporter = new ProgressReportService(new ConfigSettingsBuilder().Build());
var service = CreateService(config, progressReporter, oldDir, newDir, reportDir);
await service.ExecuteFolderDiffAsync();
Assert.Empty(_resultLists.ModifiedFilesRelativePath);
Assert.Empty(_resultLists.FileRelativePathToDiffDetailDictionary);
Assert.Empty(_resultLists.UnchangedFilesRelativePath);
Assert.Empty(_resultLists.AddedFilesAbsolutePath);
Assert.Empty(_resultLists.RemovedFilesAbsolutePath);
Assert.Empty(_resultLists.IgnoredFilesRelativePathToLocation);
Assert.Empty(_resultLists.OldFilesAbsolutePath);
Assert.Empty(_resultLists.NewFilesAbsolutePath);
}
[Fact]
public async Task ExecuteFolderDiffAsync_TextExtensionComparison_IsCaseInsensitive()
{
var oldDir = Path.Combine(_rootDir, "old-case-insensitive");
var newDir = Path.Combine(_rootDir, "new-case-insensitive");
var reportDir = Path.Combine(_rootDir, "report-case-insensitive");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
Directory.CreateDirectory(reportDir);
const string fileRelativePath = "sample.TxT";
WriteFile(oldDir, fileRelativePath, "before");
WriteFile(newDir, fileRelativePath, "after");
var config = CreateConfig(maxParallelism: 1, textFileExtensions: new List<string> { ".TXT" });
using var progressReporter = new ProgressReportService(new ConfigSettingsBuilder().Build());
var service = CreateService(config, progressReporter, oldDir, newDir, reportDir);
await service.ExecuteFolderDiffAsync();
Assert.Equal(
FileDiffResultLists.DiffDetailResult.TextMismatch,
_resultLists.FileRelativePathToDiffDetailDictionary[fileRelativePath]);
}
[Fact]
public async Task ExecuteFolderDiffAsync_CasingOnlyDifference_FollowsCurrentFileSystemSemantics()
{
var oldDir = Path.Combine(_rootDir, "old-case-path");
var newDir = Path.Combine(_rootDir, "new-case-path");
var reportDir = Path.Combine(_rootDir, "report-case-path");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
Directory.CreateDirectory(reportDir);
WriteFile(oldDir, "Case.txt", "same");
WriteFile(newDir, "case.txt", "same");
var config = CreateConfig(maxParallelism: 1);
using var progressReporter = new ProgressReportService(new ConfigSettingsBuilder().Build());
var service = CreateService(config, progressReporter, oldDir, newDir, reportDir);
await service.ExecuteFolderDiffAsync();
if (IsCaseInsensitiveDirectory(oldDir) && IsCaseInsensitiveDirectory(newDir))
{
Assert.Contains("Case.txt", _resultLists.UnchangedFilesRelativePath);
Assert.Empty(_resultLists.AddedFilesAbsolutePath);
Assert.Empty(_resultLists.RemovedFilesAbsolutePath);
return;
}
Assert.Contains(Path.Combine(oldDir, "Case.txt"), _resultLists.RemovedFilesAbsolutePath);
Assert.Contains(Path.Combine(newDir, "case.txt"), _resultLists.AddedFilesAbsolutePath);
Assert.Empty(_resultLists.UnchangedFilesRelativePath);
}
[Fact]
public async Task ExecuteFolderDiffAsync_WhenModifiedFileTimestampIsOlder_RecordsWarning()
{
var oldDir = Path.Combine(_rootDir, "old-timestamp-warning");
var newDir = Path.Combine(_rootDir, "new-timestamp-warning");
var reportDir = Path.Combine(_rootDir, "report-timestamp-warning");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
Directory.CreateDirectory(reportDir);
const string fileRelativePath = "timestamp.txt";
WriteFile(oldDir, fileRelativePath, "old content");
WriteFile(newDir, fileRelativePath, "new content");
var oldFile = Path.Combine(oldDir, fileRelativePath);
var newFile = Path.Combine(newDir, fileRelativePath);
File.SetLastWriteTimeUtc(oldFile, new DateTime(2026, 3, 14, 1, 0, 0, DateTimeKind.Utc));
File.SetLastWriteTimeUtc(newFile, new DateTime(2026, 3, 14, 0, 0, 0, DateTimeKind.Utc));
var config = CreateConfig(maxParallelism: 1);
using var progressReporter = new ProgressReportService(new ConfigSettingsBuilder().Build());
var service = CreateService(config, progressReporter, oldDir, newDir, reportDir);
await service.ExecuteFolderDiffAsync();
var warning = Assert.Single(_resultLists.NewFileTimestampOlderThanOldWarnings.Values);
Assert.Equal(fileRelativePath, warning.FileRelativePath);
Assert.Equal(TimestampCache.GetOrAdd(oldFile), warning.OldTimestamp);
Assert.Equal(TimestampCache.GetOrAdd(newFile), warning.NewTimestamp);
}
[Fact]
public async Task ExecuteFolderDiffAsync_WhenUnchangedFileTimestampIsOlder_DoesNotRecordWarning()
{
var oldDir = Path.Combine(_rootDir, "old-timestamp-warning-unchanged");
var newDir = Path.Combine(_rootDir, "new-timestamp-warning-unchanged");
var reportDir = Path.Combine(_rootDir, "report-timestamp-warning-unchanged");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
Directory.CreateDirectory(reportDir);
const string fileRelativePath = "timestamp.txt";
WriteFile(oldDir, fileRelativePath, "same");
WriteFile(newDir, fileRelativePath, "same");
File.SetLastWriteTimeUtc(Path.Combine(oldDir, fileRelativePath), new DateTime(2026, 3, 14, 1, 0, 0, DateTimeKind.Utc));
File.SetLastWriteTimeUtc(Path.Combine(newDir, fileRelativePath), new DateTime(2026, 3, 14, 0, 0, 0, DateTimeKind.Utc));
var config = CreateConfig(maxParallelism: 1);
using var progressReporter = new ProgressReportService(new ConfigSettingsBuilder().Build());
var service = CreateService(config, progressReporter, oldDir, newDir, reportDir);
await service.ExecuteFolderDiffAsync();
Assert.Empty(_resultLists.NewFileTimestampOlderThanOldWarnings);
Assert.Contains(fileRelativePath, _resultLists.UnchangedFilesRelativePath);
}
[Fact]
public async Task ExecuteFolderDiffAsync_WhenTimestampWarningDisabled_DoesNotRecordWarning()
{
var oldDir = Path.Combine(_rootDir, "old-timestamp-warning-off");
var newDir = Path.Combine(_rootDir, "new-timestamp-warning-off");
var reportDir = Path.Combine(_rootDir, "report-timestamp-warning-off");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
Directory.CreateDirectory(reportDir);
const string fileRelativePath = "timestamp.txt";
WriteFile(oldDir, fileRelativePath, "old content");
WriteFile(newDir, fileRelativePath, "new content");
File.SetLastWriteTimeUtc(Path.Combine(oldDir, fileRelativePath), new DateTime(2026, 3, 14, 1, 0, 0, DateTimeKind.Utc));
File.SetLastWriteTimeUtc(Path.Combine(newDir, fileRelativePath), new DateTime(2026, 3, 14, 0, 0, 0, DateTimeKind.Utc));
var config = CreateConfig(maxParallelism: 1, shouldWarnWhenNewFileTimestampIsOlderThanOldFileTimestamp: false);
using var progressReporter = new ProgressReportService(new ConfigSettingsBuilder().Build());
var service = CreateService(config, progressReporter, oldDir, newDir, reportDir);
await service.ExecuteFolderDiffAsync();
Assert.Empty(_resultLists.NewFileTimestampOlderThanOldWarnings);
}
// Verify that files accessed via symbolic links are enumerated, compared, and classified by relative path
// シンボリックリンク経由のファイルも列挙・比較して相対パス分類できることを確認する
[Fact]
public async Task ExecuteFolderDiffAsync_WhenComparingFileSymlinks_ClassifiesUsingLinkedContents()
{
var oldDir = Path.Combine(_rootDir, "old-symlink");
var newDir = Path.Combine(_rootDir, "new-symlink");
var reportDir = Path.Combine(_rootDir, "report-symlink");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
Directory.CreateDirectory(reportDir);
if (!TryCreateFileSymbolicLink(
sourceFileAbsolutePath: Path.Combine(oldDir, "target.txt"),
linkFileAbsolutePath: Path.Combine(oldDir, "linked.txt"),
content: "same")
|| !TryCreateFileSymbolicLink(
sourceFileAbsolutePath: Path.Combine(newDir, "target.txt"),
linkFileAbsolutePath: Path.Combine(newDir, "linked.txt"),
content: "same"))
{
return;
}
var config = CreateConfig(maxParallelism: 1);
using var progressReporter = new ProgressReportService(new ConfigSettingsBuilder().Build());
var service = CreateService(config, progressReporter, oldDir, newDir, reportDir);
await service.ExecuteFolderDiffAsync();
Assert.Contains("linked.txt", _resultLists.UnchangedFilesRelativePath);
Assert.Equal(FileDiffResultLists.DiffDetailResult.SHA256Match, _resultLists.FileRelativePathToDiffDetailDictionary["linked.txt"]);
}
private static ConfigSettings CreateConfig(
int maxParallelism,
List<string> textFileExtensions = null,
bool shouldWarnWhenNewFileTimestampIsOlderThanOldFileTimestamp = true) => new ConfigSettingsBuilder()
{
IgnoredExtensions = new List<string> { ".pdb" },
TextFileExtensions = textFileExtensions ?? new List<string> { ".txt" },
ShouldIncludeUnchangedFiles = true,
ShouldIncludeIgnoredFiles = true,
ShouldOutputILText = false,
ShouldIgnoreILLinesContainingConfiguredStrings = false,
ILIgnoreLineContainingStrings = new List<string>(),
ShouldOutputFileTimestamps = false,
ShouldWarnWhenNewFileTimestampIsOlderThanOldFileTimestamp = shouldWarnWhenNewFileTimestampIsOlderThanOldFileTimestamp,
MaxParallelism = maxParallelism,
OptimizeForNetworkShares = false,
AutoDetectNetworkShares = false
}.Build();
private FolderDiffService CreateService(ConfigSettings config, ProgressReportService progressReporter, string oldDir, string newDir, string reportDir)
{
var executionContext = new DiffExecutionContext(
oldDir,
newDir,
reportDir,
optimizeForNetworkShares: false,
detectedNetworkOld: false,
detectedNetworkNew: false);
var ilTextOutputService = new ILTextOutputService(executionContext, _logger);
var dotNetDisassembleService = new DotNetDisassembleService(config, ilCache: null, _resultLists, _logger, new DotNetDisassemblerCache(_logger));
var ilOutputService = new ILOutputService(config, executionContext, ilTextOutputService, dotNetDisassembleService, ilCache: null, _logger);
var fileDiffService = new FileDiffService(config, ilOutputService, executionContext, _resultLists, _logger);
return new FolderDiffService(config, progressReporter, executionContext, fileDiffService, _resultLists, _logger);
}
private static void WriteFile(string rootDir, string relativePath, string content)
{
var absolutePath = Path.Combine(rootDir, relativePath);
var parentDir = Path.GetDirectoryName(absolutePath);
if (!string.IsNullOrEmpty(parentDir))
{
Directory.CreateDirectory(parentDir);
}
File.WriteAllText(absolutePath, content);
}
// Creates a real file and a symbolic link pointing to it; returns false if symlink creation fails
// 実ファイルとそれを指すシンボリックリンクを生成し、リンク作成に失敗した場合は false を返す
private static bool TryCreateFileSymbolicLink(string sourceFileAbsolutePath, string linkFileAbsolutePath, string content)
{
try
{
File.WriteAllText(sourceFileAbsolutePath, content);
File.CreateSymbolicLink(linkFileAbsolutePath, sourceFileAbsolutePath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
catch (PlatformNotSupportedException)
{
return false;
}
catch (IOException)
{
return false;
}
}
private static bool IsCaseInsensitiveDirectory(string directoryPath)
{
if (OperatingSystem.IsWindows())
{
return true;
}
string fullPath = Path.GetFullPath(directoryPath);
string trimmedPath = fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
string? parentPath = Path.GetDirectoryName(trimmedPath);
string directoryName = Path.GetFileName(trimmedPath);
if (string.IsNullOrEmpty(parentPath) || string.IsNullOrEmpty(directoryName))
{
return OperatingSystem.IsMacOS();
}
string alternateName = directoryName[0] switch
{
>= 'a' and <= 'z' => char.ToUpperInvariant(directoryName[0]) + directoryName[1..],
>= 'A' and <= 'Z' => char.ToLowerInvariant(directoryName[0]) + directoryName[1..],
_ => directoryName,
};
return alternateName != directoryName && Directory.Exists(Path.Combine(parentPath, alternateName));
}
}
}