Repository navigation
Expand file tree
/
Copy pathProgramTests.cs
More file actions
318 lines (289 loc) · 13.9 KB
/
Copy pathProgramTests.cs
File metadata and controls
318 lines (289 loc) · 13.9 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
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using FolderDiffIL4DotNet.Common;
using FolderDiffIL4DotNet.Services;
using FolderDiffIL4DotNet.Services.Caching;
using FolderDiffIL4DotNet.Tests.Helpers;
using Xunit;
namespace FolderDiffIL4DotNet.Tests
{
[Collection("ConsoleOutput NonParallel")]
[Trait("Category", "Unit")]
public sealed class ProgramTests
{
[Fact]
public async Task Main_WithInsufficientArguments_ReturnsErrorCode()
{
var exitCode = await InvokeProgramMainAsync(new[] { "--no-pause" });
Assert.Equal(2, exitCode);
}
[Fact]
public async Task Main_WithMissingExplicitConfigFile_ReturnsConfigurationErrorCode()
{
var tempRoot = Path.Combine(Path.GetTempPath(), "fd-program-tests-" + Guid.NewGuid().ToString("N"));
var oldDir = Path.Combine(tempRoot, "old-config-missing");
var newDir = Path.Combine(tempRoot, "new-config-missing");
var missingConfigPath = Path.Combine(tempRoot, "missing-config.json");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
using var appDataScope = CreateAppDataOverrideScope();
try
{
var exitCode = await InvokeProgramMainAsync(new[] { oldDir, newDir, "report_" + Guid.NewGuid().ToString("N"), "--config", missingConfigPath, "--no-pause" });
Assert.Equal(3, exitCode);
}
finally
{
TryDeleteDirectory(tempRoot);
TryDeleteDirectory(appDataScope.RootAbsolutePath);
}
}
[Fact]
public async Task Main_WithTooLongExplicitConfigPath_ReturnsConfigurationErrorCode()
{
var tempRoot = Path.Combine(Path.GetTempPath(), "fd-program-tests-" + Guid.NewGuid().ToString("N"));
var oldDir = Path.Combine(tempRoot, "old-config-long");
var newDir = Path.Combine(tempRoot, "new-config-long");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
string tooLongConfigPath = CreateTooLongConfigPath();
using var appDataScope = CreateAppDataOverrideScope();
try
{
var exitCode = await InvokeProgramMainAsync(new[] { oldDir, newDir, "report_" + Guid.NewGuid().ToString("N"), "--config", tooLongConfigPath, "--no-pause" });
Assert.Equal(3, exitCode);
}
finally
{
TryDeleteDirectory(tempRoot);
TryDeleteDirectory(appDataScope.RootAbsolutePath);
}
}
[Fact]
public async Task Main_WhenLocalApplicationDataIsUnresolved_ReturnsInvalidArgumentsCode()
{
var tempRoot = Path.Combine(Path.GetTempPath(), "fd-program-tests-" + Guid.NewGuid().ToString("N"));
var oldDir = Path.Combine(tempRoot, "old-appdata-unresolved");
var newDir = Path.Combine(tempRoot, "new-appdata-unresolved");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
using var appDataScope = CreateAppDataOverrideScope();
object? originalOverride = AppContext.GetData(AppDataPaths.LOCAL_APP_DATA_OVERRIDE_KEY);
var originalOut = Console.Out;
using var outputWriter = new StringWriter();
try
{
Console.SetOut(outputWriter);
AppContext.SetData(AppDataPaths.LOCAL_APP_DATA_OVERRIDE_KEY, string.Empty);
var exitCode = await InvokeProgramMainAsync(new[] { oldDir, newDir, "report_" + Guid.NewGuid().ToString("N"), "--no-pause" });
Assert.Equal(2, exitCode);
string output = outputWriter.ToString();
Assert.Contains("File logging was unavailable.", output, StringComparison.Ordinal);
Assert.DoesNotContain("Error details logged to:", output, StringComparison.Ordinal);
}
finally
{
Console.SetOut(originalOut);
AppContext.SetData(AppDataPaths.LOCAL_APP_DATA_OVERRIDE_KEY, originalOverride);
TryDeleteDirectory(tempRoot);
TryDeleteDirectory(appDataScope.RootAbsolutePath);
}
}
[Fact]
public async Task Main_WithValidArguments_ReturnsSuccessAndGeneratesReport()
{
var tempRoot = Path.Combine(Path.GetTempPath(), "fd-program-tests-" + Guid.NewGuid().ToString("N"));
var oldDir = Path.Combine(tempRoot, "old");
var newDir = Path.Combine(tempRoot, "new");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
await File.WriteAllTextAsync(Path.Combine(oldDir, "sample.txt"), "same");
await File.WriteAllTextAsync(Path.Combine(newDir, "sample.txt"), "same");
using var appDataScope = CreateAppDataOverrideScope();
var reportLabel = "report_" + Guid.NewGuid().ToString("N");
var reportRoot = Path.Combine(tempRoot, "reports");
var reportDir = Path.Combine(reportRoot, reportLabel);
var configPath = Path.Combine(tempRoot, "config.json");
var configJson = """
{
"IgnoredExtensions": [],
"TextFileExtensions": [".txt"],
"MaxLogGenerations": 3,
"ShouldIncludeUnchangedFiles": true,
"ShouldIncludeIgnoredFiles": false,
"ShouldOutputILText": false,
"ShouldIgnoreILLinesContainingConfiguredStrings": false,
"ILIgnoreLineContainingStrings": [],
"ShouldOutputFileTimestamps": false,
"ShouldWarnWhenNewFileTimestampIsOlderThanOldFileTimestamp": true,
"MaxParallelism": 1,
"EnableILCache": false,
"OptimizeForNetworkShares": false,
"AutoDetectNetworkShares": false
}
""";
try
{
await File.WriteAllTextAsync(configPath, configJson);
var exitCode = await InvokeProgramMainAsync(new[] { oldDir, newDir, reportLabel, "--config", configPath, "--output", reportRoot, "--no-pause" });
Assert.Equal(0, exitCode);
Assert.True(File.Exists(Path.Combine(reportDir, "diff_report.md")));
}
finally
{
TryDeleteDirectory(tempRoot);
TryDeleteDirectory(appDataScope.RootAbsolutePath);
}
}
[Fact]
public async Task Main_WhenNewFileTimestampIsOlder_WritesWarningAndAddsReportSection()
{
TimestampCache.Clear();
var tempRoot = Path.Combine(Path.GetTempPath(), "fd-program-tests-" + Guid.NewGuid().ToString("N"));
var oldDir = Path.Combine(tempRoot, "old-warning");
var newDir = Path.Combine(tempRoot, "new-warning");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
var fileRelativePath = "sample.txt";
var oldFile = Path.Combine(oldDir, fileRelativePath);
var newFile = Path.Combine(newDir, fileRelativePath);
await File.WriteAllTextAsync(oldFile, "old content");
await File.WriteAllTextAsync(newFile, "new content");
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));
using var appDataScope = CreateAppDataOverrideScope();
var reportLabel = "report_" + Guid.NewGuid().ToString("N");
var reportRoot = Path.Combine(tempRoot, "reports");
var reportDir = Path.Combine(reportRoot, reportLabel);
var configPath = Path.Combine(tempRoot, "config.json");
var configJson = """
{
"IgnoredExtensions": [],
"TextFileExtensions": [".txt"],
"MaxLogGenerations": 3,
"ShouldIncludeUnchangedFiles": true,
"ShouldIncludeIgnoredFiles": false,
"ShouldOutputILText": false,
"ShouldIgnoreILLinesContainingConfiguredStrings": false,
"ILIgnoreLineContainingStrings": [],
"ShouldOutputFileTimestamps": false,
"ShouldWarnWhenNewFileTimestampIsOlderThanOldFileTimestamp": true,
"MaxParallelism": 1,
"EnableILCache": false,
"OptimizeForNetworkShares": false,
"AutoDetectNetworkShares": false
}
""";
var originalOut = Console.Out;
var writer = new StringWriter();
try
{
Console.SetOut(writer);
await File.WriteAllTextAsync(configPath, configJson);
var exitCode = await InvokeProgramMainAsync(new[] { oldDir, newDir, reportLabel, "--config", configPath, "--output", reportRoot, "--no-pause" });
Assert.Equal(0, exitCode);
var consoleText = writer.ToString();
Assert.Contains("older timestamps", consoleText);
Assert.Contains("See diff_report for details.", consoleText);
Assert.DoesNotContain("sample.txt", consoleText);
var reportText = await File.ReadAllTextAsync(Path.Combine(reportDir, "diff_report.md"));
Assert.Contains("## Warnings", reportText);
Assert.Contains("sample.txt", reportText);
Assert.Contains(" → ", reportText);
}
finally
{
Console.SetOut(originalOut);
TimestampCache.Clear();
TryDeleteDirectory(tempRoot);
TryDeleteDirectory(appDataScope.RootAbsolutePath);
}
}
[Fact]
public async Task Main_WhenSha256MismatchExists_WritesWarningAtEndAndAddsReportWarningsSection()
{
TimestampCache.Clear();
var tempRoot = Path.Combine(Path.GetTempPath(), "fd-program-tests-" + Guid.NewGuid().ToString("N"));
var oldDir = Path.Combine(tempRoot, "old-sha256-warning");
var newDir = Path.Combine(tempRoot, "new-sha256-warning");
Directory.CreateDirectory(oldDir);
Directory.CreateDirectory(newDir);
await File.WriteAllTextAsync(Path.Combine(oldDir, "payload.bin"), "old");
await File.WriteAllTextAsync(Path.Combine(newDir, "payload.bin"), "new");
using var appDataScope = CreateAppDataOverrideScope();
var reportLabel = "report_" + Guid.NewGuid().ToString("N");
var reportRoot = Path.Combine(tempRoot, "reports");
var reportDir = Path.Combine(reportRoot, reportLabel);
var configPath = Path.Combine(tempRoot, "config.json");
var configJson = """
{
"IgnoredExtensions": [],
"TextFileExtensions": [],
"MaxLogGenerations": 3,
"ShouldIncludeUnchangedFiles": true,
"ShouldIncludeIgnoredFiles": false,
"ShouldOutputILText": false,
"ShouldIgnoreILLinesContainingConfiguredStrings": false,
"ILIgnoreLineContainingStrings": [],
"ShouldOutputFileTimestamps": false,
"ShouldWarnWhenNewFileTimestampIsOlderThanOldFileTimestamp": true,
"MaxParallelism": 1,
"EnableILCache": false,
"OptimizeForNetworkShares": false,
"AutoDetectNetworkShares": false
}
""";
var originalOut = Console.Out;
var writer = new StringWriter();
try
{
Console.SetOut(writer);
await File.WriteAllTextAsync(configPath, configJson);
var exitCode = await InvokeProgramMainAsync(new[] { oldDir, newDir, reportLabel, "--config", configPath, "--output", reportRoot, "--no-pause" });
Assert.Equal(0, exitCode);
var consoleText = writer.ToString();
Assert.Contains(Constants.WARNING_SHA256_MISMATCH, consoleText);
var reportText = await File.ReadAllTextAsync(Path.Combine(reportDir, "diff_report.md"));
Assert.Contains("## Warnings", reportText);
Assert.Contains("SHA256Mismatch: binary diff only", reportText);
}
finally
{
Console.SetOut(originalOut);
TimestampCache.Clear();
TryDeleteDirectory(tempRoot);
TryDeleteDirectory(appDataScope.RootAbsolutePath);
}
}
private static async Task<int> InvokeProgramMainAsync(string[] args)
{
var programType = typeof(ConfigService).Assembly.GetType("FolderDiffIL4DotNet.Program");
Assert.NotNull(programType);
var mainMethod = programType.GetMethod("Main", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(mainMethod);
var taskObject = mainMethod.Invoke(null, new object[] { args });
var task = Assert.IsAssignableFrom<Task<int>>(taskObject);
return await task;
}
private static AppDataOverrideScope CreateAppDataOverrideScope()
=> new(Path.Combine(Path.GetTempPath(), "fd-program-appdata-" + Guid.NewGuid().ToString("N")));
private static string CreateTooLongConfigPath()
=> Path.Combine(Path.GetTempPath(), new string('a', 5000) + ".json");
private static void TryDeleteDirectory(string path)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
}
catch
{
// ignore cleanup errors in tests / テストのクリーンアップエラーを無視
}
}
}
}