Repository navigation
Expand file tree
/
Copy pathFileSystemUtilityTests.cs
More file actions
435 lines (382 loc) · 19.2 KB
/
Copy pathFileSystemUtilityTests.cs
File metadata and controls
435 lines (382 loc) · 19.2 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
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
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using FolderDiffIL4DotNet.Common;
using FolderDiffIL4DotNet.Core.IO;
using Xunit;
namespace FolderDiffIL4DotNet.Tests.Core.IO
{
[Trait("Category", "Unit")]
public class FileSystemUtilityTests : IDisposable
{
private readonly string _tempDir;
public FileSystemUtilityTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), $"FolderDiffTests_{Guid.NewGuid():N}");
Directory.CreateDirectory(_tempDir);
}
public void Dispose()
{
try { Directory.Delete(_tempDir, recursive: true); } catch { }
}
private string CreateTempFile(string name, string content)
{
var path = Path.Combine(_tempDir, name);
File.WriteAllText(path, content);
return path;
}
[Fact]
public void DeleteFileSilent_ExistingFile_Deleted()
{
var file = CreateTempFile("delete_me.txt", "content");
Assert.True(File.Exists(file));
FileSystemUtility.DeleteFileSilent(file);
Assert.False(File.Exists(file));
}
[Fact]
public void DeleteFileSilent_NonexistentFile_DoesNotThrow()
{
FileSystemUtility.DeleteFileSilent(Path.Combine(_tempDir, "no_such_file.txt"));
}
[Fact]
public void DeleteFileSilent_NullOrEmpty_DoesNotThrow()
{
FileSystemUtility.DeleteFileSilent(null);
FileSystemUtility.DeleteFileSilent(string.Empty);
}
[Fact]
public void GetTimestamp_ExistingFile_ReturnsTimestampString()
{
var file = CreateTempFile("timestamp.txt", "content");
File.SetLastWriteTime(file, new DateTime(2026, 3, 14, 10, 0, 0, 123, DateTimeKind.Local));
var timestamp = FileSystemUtility.GetTimestamp(file);
Assert.Equal("2026-03-14 10:00:00.123", timestamp);
Assert.False(string.IsNullOrWhiteSpace(timestamp));
Assert.True(
DateTime.TryParseExact(timestamp, Constants.TIMESTAMP_WITH_TIME_ZONE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out _),
$"Unexpected timestamp format: {timestamp}");
}
[Fact]
public void GetTimestamp_NonexistentFile_ReturnsFormattedString()
{
var missing = Path.Combine(_tempDir, "missing.txt");
var timestamp = FileSystemUtility.GetTimestamp(missing);
Assert.False(string.IsNullOrWhiteSpace(timestamp));
}
[Fact]
public void GetTimestamp_Null_ThrowsOriginalArgumentException()
{
Assert.ThrowsAny<ArgumentException>(() => FileSystemUtility.GetTimestamp(null));
}
[Fact]
public void TrySetReadOnly_ExistingFile_SetsReadOnlyAttribute()
{
var file = CreateTempFile("readonly.txt", "content");
FileSystemUtility.TrySetReadOnly(file);
var attrs = File.GetAttributes(file);
Assert.True((attrs & FileAttributes.ReadOnly) != 0);
}
[Fact]
public void TrySetReadOnly_NullOrWhitespace_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => FileSystemUtility.TrySetReadOnly(null));
Assert.Throws<ArgumentException>(() => FileSystemUtility.TrySetReadOnly(" "));
}
[Fact]
public void TrySetReadOnly_NonexistentFile_ThrowsFileNotFoundException()
{
var missing = Path.Combine(_tempDir, "missing-readonly.txt");
Assert.Throws<FileNotFoundException>(() => FileSystemUtility.TrySetReadOnly(missing));
}
[Fact]
public void IsLikelyNetworkPath_NullOrWhitespace_ReturnsFalse()
{
Assert.False(FileSystemUtility.IsLikelyNetworkPath(null));
Assert.False(FileSystemUtility.IsLikelyNetworkPath(string.Empty));
Assert.False(FileSystemUtility.IsLikelyNetworkPath(" "));
}
[Fact]
public void IsLikelyNetworkPath_LocalTempPath_ReturnsFalse()
{
Assert.False(FileSystemUtility.IsLikelyNetworkPath(_tempDir));
}
[Fact]
public void IsLikelyNetworkPath_InvalidPathCharacters_ReturnsFalse()
{
var invalidPath = $"invalid{'\0'}path";
Assert.False(FileSystemUtility.IsLikelyNetworkPath(invalidPath));
}
/// <summary>
/// Verifies that forward-slash IP-based UNC paths (e.g. //192.168.1.1/share) are detected as Windows network paths.
/// Regression test: IsLikelyWindowsNetworkPath originally only handled <c>\\</c> prefixes;
/// this confirms <c>//</c>-style UNC paths are now also recognised.
/// スラッシュ形式の IP ベース UNC パス(例: //192.168.1.1/share)が Windows ネットワークパスとして検出されることを確認します。
/// 回帰テスト: IsLikelyWindowsNetworkPath は元々 <c>\\</c> プレフィックスのみ対応していましたが、
/// <c>//</c> 形式の UNC パスも検出できるよう修正されたことを確認します。
/// </summary>
[Fact]
public void IsLikelyWindowsNetworkPath_ForwardSlashIpUncPath_ReturnsTrue()
{
var method = typeof(NetworkPathDetector).GetMethod(
"IsLikelyWindowsNetworkPath",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
// //192.168.1.1/share is a valid UNC path on Windows (forward-slash form)
// //192.168.1.1/share は Windows で有効な UNC パス(スラッシュ形式)
var result = method.Invoke(null, ["//192.168.1.1/share/folder"]);
Assert.True(Assert.IsType<bool>(result));
}
/// <summary>
/// Verifies that backslash-style UNC paths (\\server\share) continue to be detected as network paths.
/// バックスラッシュ形式の UNC パス(\\server\share)が引き続きネットワークパスとして検出されることを確認します。
/// </summary>
[Fact]
public void IsLikelyWindowsNetworkPath_BackslashUncPath_ReturnsTrue()
{
var method = typeof(NetworkPathDetector).GetMethod(
"IsLikelyWindowsNetworkPath",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
var result = method.Invoke(null, [@"\\server\share\folder"]);
Assert.True(Assert.IsType<bool>(result));
}
[Fact]
public void GetBestMatchingMountFileSystemType_PicksMostSpecificMountPoint()
{
var method = typeof(NetworkPathDetector).GetMethod(
"GetBestMatchingMountFileSystemType",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
var fullPath = "/mnt/share/deep/file.txt";
var lines = new[]
{
"server:/export /mnt nfs rw 0 0",
"server:/share /mnt/share nfs4 rw 0 0",
"/dev/disk1s1 / apfs rw 0 0",
};
var result = method.Invoke(null, new object[] { fullPath, lines });
Assert.Equal("nfs4", Assert.IsType<string>(result));
}
[Fact]
public void GetBestMatchingMountFileSystemType_IgnoresInvalidLines()
{
var method = typeof(NetworkPathDetector).GetMethod(
"GetBestMatchingMountFileSystemType",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
var fullPath = "/tmp/file.txt";
var lines = new[]
{
"# comment",
"invalid",
"tmpfs /tmp tmpfs rw 0 0"
};
var result = method.Invoke(null, new object[] { fullPath, lines });
Assert.Equal("tmpfs", Assert.IsType<string>(result));
}
[Fact]
public void GetBestMatchingMountFileSystemType_NullInputs_ReturnsNull()
{
var method = typeof(NetworkPathDetector).GetMethod(
"GetBestMatchingMountFileSystemType",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
Assert.Null(method.Invoke(null, new object[] { null, new[] { "tmpfs /tmp tmpfs rw 0 0" } }));
Assert.Null(method.Invoke(null, new object[] { "/tmp/file.txt", null }));
}
/// <summary>
/// Verifies no exception when the file has already been deleted (deletion race).
/// 削除競合(ファイルが既に削除済みの場合)でも例外が出ないことを確認します。
/// </summary>
[Fact]
public void DeleteFileSilent_AlreadyDeletedFile_DoesNotThrow()
{
var path = Path.Combine(_tempDir, "already_gone.txt");
// File does not exist but should not throw
// ファイルは存在しないが例外を投げない
FileSystemUtility.DeleteFileSilent(path);
}
/// <summary>
/// Verifies that attempting to delete a read-only file (UnauthorizedAccessException) is silently ignored.
/// Skipped on non-Unix platforms.
/// 読み取り専用ファイルの削除試行(UnauthorizedAccessException)を silent に無視することを確認します。
/// Unix 以外ではスキップ。
/// </summary>
[Fact]
public void DeleteFileSilent_ReadOnlyFile_OnUnix_DoesNotThrow()
{
if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux)
&& !System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
{
return; // Skip on Windows / Windows ではスキップ
}
if (string.Equals(Environment.UserName, "root", StringComparison.OrdinalIgnoreCase))
{
return; // root bypasses ReadOnly -- skip / root は ReadOnly を無視するためスキップ
}
var file = CreateTempFile("readonly_del.txt", "content");
// Place file in a read-only directory to simulate access denial
// 読み取り専用ディレクトリに配置してアクセス拒否を模擬
var roDir = Path.Combine(_tempDir, "ro_dir");
Directory.CreateDirectory(roDir);
var roFile = Path.Combine(roDir, "locked.txt");
File.WriteAllText(roFile, "locked");
try
{
#pragma warning disable CA1416 // Unix-only API; test is skipped on Windows / Unix 専用 API; Windows ではテストがスキップされます
File.SetUnixFileMode(roDir, UnixFileMode.UserRead | UnixFileMode.UserExecute);
#pragma warning restore CA1416
// Directory is not writable, so deletion raises IOException or UnauthorizedAccessException
// ディレクトリが書き込み不可なので削除は IOException または UnauthorizedAccessException
FileSystemUtility.DeleteFileSilent(roFile); // should not throw / 例外なし
}
finally
{
#pragma warning disable CA1416 // Unix-only API; test is skipped on Windows / Unix 専用 API; Windows ではテストがスキップされます
try { File.SetUnixFileMode(roDir, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); } catch { }
#pragma warning restore CA1416
}
}
/// <summary>
/// Verifies that TrySetReadOnly is idempotent on an already read-only file.
/// TrySetReadOnly が既に ReadOnly なファイルに対しても例外なく動作することを確認します。
/// </summary>
[Fact]
public void TrySetReadOnly_AlreadyReadOnly_DoesNotThrow()
{
var file = CreateTempFile("already_readonly.txt", "content");
FileSystemUtility.TrySetReadOnly(file);
// Calling again should not throw
// 再度呼び出しても例外が出ない
FileSystemUtility.TrySetReadOnly(file);
var attrs = File.GetAttributes(file);
Assert.True((attrs & FileAttributes.ReadOnly) != 0);
}
/// <summary>
/// Verifies that IsLikelyNetworkPath returns false for local paths on Unix.
/// Unix 環境で IsLikelyNetworkPath がローカルパスに対して false を返すことを確認します。
/// </summary>
[Fact]
public void IsLikelyNetworkPath_UnixLocalPath_ReturnsFalse()
{
if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
{
return;
}
// /tmp is normally a local FS (tmpfs etc.)
// /tmp は通常ローカル FS(tmpfs 等)
Assert.False(FileSystemUtility.IsLikelyNetworkPath("/tmp"));
}
/// <summary>
/// Verifies that GetUnixMountsFilePath returns the discovered mount file on Linux.
/// GetUnixMountsFilePath がファイルシステムで見つかったマウントファイルを返すことを確認します(Linux 環境)。
/// </summary>
[Fact]
public void GetUnixMountsFilePath_OnLinux_ReturnsProcMountsOrMtab()
{
if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
{
return;
}
var method = typeof(NetworkPathDetector).GetMethod(
"GetUnixMountsFilePath",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
var result = method.Invoke(null, null) as string;
// Either /proc/mounts or /etc/mtab should exist
// /proc/mounts または /etc/mtab が存在する
Assert.NotNull(result);
}
/// <summary>
/// Verifies via reflection that IsLikelyUnixNetworkPath returns false when no mount file exists.
/// IsLikelyUnixNetworkPath でマウントファイルが存在しない場合は false を返すことをリフレクションで確認します。
/// </summary>
[Fact]
public void IsLikelyUnixNetworkPath_WhenNoMountsFile_ReturnsFalse()
{
var method = typeof(NetworkPathDetector).GetMethod(
"IsLikelyUnixNetworkPath",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
// /nonexistent/path passes TryGetFullPath; if a mounts file is found, it branches there.
// This test only verifies the fall-through path.
// /nonexistent/path は TryGetFullPath を通過し、mounts ファイルがあればそちらへ分岐する。
// このテストはフォールスルーのみ検証。
var result = method.Invoke(null, ["/nonexistent/path/that/does/not/exist"]);
Assert.IsType<bool>(result); // false or true, just don't throw
}
/// <summary>
/// Verifies that TryReadMountLines returns null for a nonexistent file.
/// TryReadMountLines で存在しないファイルを渡した際に null が返ることを確認します。
/// </summary>
[Fact]
public void TryReadMountLines_NonexistentFile_ReturnsNull()
{
var method = typeof(NetworkPathDetector).GetMethod(
"TryReadMountLines",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
// Nonexistent file triggers IOException and returns null
// 存在しないファイルは IOException が発生し null が返る
var result = method.Invoke(null, ["/nonexistent/path/to/mounts"]);
Assert.Null(result);
}
/// <summary>
/// Verifies that //-prefixed UNC paths are correctly detected by the internal Windows network path check.
/// // プレフィックスの UNC パスが内部メソッド IsLikelyWindowsNetworkPath で正しく検出されることを確認します。
/// </summary>
[Fact]
public void IsLikelyWindowsNetworkPath_ForwardSlashUncPrefixVariants_ReturnTrue()
{
var method = typeof(NetworkPathDetector).GetMethod(
"IsLikelyWindowsNetworkPath",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
// //server/share format UNC path / //server/share 形式の UNC パス
Assert.True((bool)method.Invoke(null, ["//server/share"]));
// \\server\share format UNC path / \\server\share 形式の UNC パス
Assert.True((bool)method.Invoke(null, [@"\\server\share"]));
}
/// <summary>
/// Verifies that GetBestMatchingMountFileSystemType matches a mount point without a trailing slash.
/// GetBestMatchingMountFileSystemType でパスが末尾スラッシュなしのマウントポイントと一致することを確認します。
/// </summary>
[Fact]
public void GetBestMatchingMountFileSystemType_ExactPathMatch_ReturnsFsType()
{
var method = typeof(NetworkPathDetector).GetMethod(
"GetBestMatchingMountFileSystemType",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
var fullPath = "/mnt/share";
var lines = new[]
{
"server:/share /mnt/share nfs rw 0 0",
};
var result = method.Invoke(null, new object[] { fullPath, lines });
Assert.Equal("nfs", Assert.IsType<string>(result));
}
/// <summary>
/// Verifies that GetBestMatchingMountFileSystemType correctly interprets mount points with escaped spaces.
/// GetBestMatchingMountFileSystemType でエスケープスペースを含むマウントポイントが正しく解釈されることを確認します。
/// </summary>
[Fact]
public void GetBestMatchingMountFileSystemType_EscapedSpaceInMountPoint_Matches()
{
var method = typeof(NetworkPathDetector).GetMethod(
"GetBestMatchingMountFileSystemType",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
Assert.NotNull(method);
// Lines containing \040 (space) in the mount point
// マウントポイントに \040(スペース)が含まれる行
var lines = new[]
{
@"server:/my\040share /mnt/my share nfs rw 0 0",
"tmpfs /tmp tmpfs rw 0 0",
};
var result = method.Invoke(null, new object[] { "/tmp/file.txt", lines });
Assert.Equal("tmpfs", Assert.IsType<string>(result));
}
}
}