-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathFileTreeTest.cs
More file actions
236 lines (192 loc) · 8.34 KB
/
Copy pathFileTreeTest.cs
File metadata and controls
236 lines (192 loc) · 8.34 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
using NeeLaboratory.IO.Nodes;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using Xunit.Abstractions;
namespace NeeLaboratory.IO.NodesTest
{
public class FileTreeTest
{
#pragma warning disable CS0414 // フィールドが割り当てられていますが、値は使用されていません
private static string RootPath { get; } = @"E:\Work\Labo\サンプル\フォルダー、サブフォルダーx3";
private static readonly string _folderSource = @"TestFolders";
private static readonly string _folderRoot = @"_Work";
private static readonly string _folderRoot2 = @"_Work2";
private static readonly string _folderSub1 = @"_Work\SubFolder1";
private static readonly string _folderSub2 = @"_Work\SubFolder2";
private static readonly string _folderSub3 = @"_Work\SubFolder3";
private static readonly string _folderSub3Ex = @"_Work\SubFolder2\SubFolder3";
private static readonly string _fileAppend1 = @"_Work\SubFolder1\append1.txt";
private static readonly string _fileAppend2 = @"_Work\SubFolder1\append2.bin";
private static readonly string _fileAppend2Ex = @"_Work\SubFolder1\append2.txt";
#pragma warning restore CS0414 // フィールドが割り当てられていますが、値は使用されていません
private readonly ITestOutputHelper output;
public FileTreeTest(ITestOutputHelper output)
{
this.output = output;
}
/// <summary>
/// テスト環境初期化
/// </summary>
public static async Task<FileTree> CreateTestEnvironmentAsync(CancellationToken token)
{
if (Directory.Exists(_folderRoot)) Directory.Delete(_folderRoot, true);
if (Directory.Exists(_folderRoot2)) Directory.Delete(_folderRoot2, true);
CopyDirectory(_folderSource, _folderRoot, true);
var tree = new FileTree(Path.GetFullPath(_folderRoot), IOExtensions.CreateEnumerationOptions(true, FileAttributes.None));
await tree.InitializeAsync(token);
return tree;
}
static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDir);
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
}
// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}
private async Task DumpTree(FileTree tree)
{
using (await tree.LockAsync(CancellationToken.None))
{
foreach (var (value, index) in tree.WalkAll().Select((value, index) => (value, index)))
{
output.WriteLine($"{index}: {value.FullName})");
}
}
}
[Fact]
public async Task FileTreeBasicTest()
{
var tree = new FileTree(RootPath, IOExtensions.CreateEnumerationOptions(true, FileAttributes.None));
await DumpTree(tree);
}
/// <summary>
/// ファイルシステム監視テスト
/// </summary>
[Fact]
public async Task FileTreeWatchFilesystemTest()
{
var tree = await CreateTestEnvironmentAsync(CancellationToken.None);
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(12, tree.WalkAll().Count());
}
// ファイル追加 ...
using (FileStream stream = File.Create(_fileAppend1)) { }
using (FileStream stream = File.Create(_fileAppend2)) { }
await Task.Delay(100);
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(14, tree.WalkAll().Count());
}
// 名前変更
var fileAppend2Ex = Path.ChangeExtension(_fileAppend2, ".txt");
File.Move(_fileAppend2, fileAppend2Ex);
await Task.Delay(100);
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(14, tree.WalkAll().Count());
}
// 内容変更
using (FileStream stream = File.Open(fileAppend2Ex, FileMode.Append))
{
stream.WriteByte(0x00);
}
await Task.Delay(100);
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(14, tree.WalkAll().Count());
}
// ファイル削除...
File.Delete(_fileAppend1);
File.Delete(fileAppend2Ex);
await Task.Delay(100);
await DumpTree(tree);
// 戻ったカウント確認
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(12, tree.WalkAll().Count());
}
}
// 移動テスト
[Fact]
public async Task FileTreeMoveDirectoryTest()
{
var tree = await CreateTestEnvironmentAsync(CancellationToken.None);
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(12, tree.WalkAll().Count());
}
Directory.Move(_folderSub1, _folderSub3);
await Task.Delay(100);
Assert.False(Directory.Exists(_folderSub1));
Assert.True(Directory.Exists(_folderSub3));
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(12, tree.WalkAll().Count());
var node = tree.Find(Path.GetFullPath(_folderSub3));
Assert.NotNull(node);
output.WriteLine($"_folderSub3: {node.FullName}");
}
var src = Path.GetFullPath(_folderSub3);
var dst = Path.GetFullPath(_folderSub3Ex);
output.WriteLine($"move {src} -> {dst}");
Directory.Move(src, dst);
await Task.Delay(100);
Assert.False(Directory.Exists(_folderSub3));
Assert.True(Directory.Exists(_folderSub3Ex));
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(12, tree.WalkAll().Count());
var node = tree.Find(Path.GetFullPath(_folderSub3Ex));
Assert.NotNull(node);
output.WriteLine($"_folderSub3Ex: {node.FullName}");
}
await DumpTree(tree);
}
/// <summary>
/// ディレクトリ削除監視
/// </summary>
/// <returns></returns>
[Fact]
public async Task FileTreeDeleteDirectoryTest()
{
var tree = await CreateTestEnvironmentAsync(CancellationToken.None);
await DumpTree(tree);
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(12, tree.WalkAll().Count());
}
// Sub1フォルダ削除
Directory.Delete(_folderSub1, true);
await Task.Delay(100);
Assert.False(Directory.Exists(_folderSub1));
using (await tree.LockAsync(CancellationToken.None))
{
Assert.Equal(8, tree.WalkAll().Count());
}
// 対象そのものが変化 ... は監視対象外なので変化なくテスト対象外
await DumpTree(tree);
}
}
}