-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressInfoTests.cs
More file actions
251 lines (207 loc) · 10 KB
/
Copy pathProgressInfoTests.cs
File metadata and controls
251 lines (207 loc) · 10 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
using XISOSharp.Models;
namespace XISOSharp.Tests;
/// <summary>
/// Tests for the structured write-progress channel (<see cref="IProgress{T}"/> of
/// <see cref="ProgressInfo"/>) on create and rewrite operations.
/// </summary>
[Collection("Sequential")]
public class ProgressInfoTests : IDisposable
{
private readonly List<string> _tempDirs = [];
public void Dispose()
{
Logger.Quiet = false;
Logger.RealQuiet = false;
foreach (string dir in _tempDirs)
{
try
{
if (Directory.Exists(dir)) Directory.Delete(dir, true);
}
catch
{
/* best effort cleanup */
}
}
}
private string CreateTempDir()
{
string dir = Path.Combine(Path.GetTempPath(), $"xiso_prog_{Guid.NewGuid():N}");
Directory.CreateDirectory(dir);
_tempDirs.Add(dir);
return dir;
}
/// <summary>
/// Synchronous event collector. The built-in <see cref="Progress{T}"/> posts callbacks
/// asynchronously when no synchronization context is present, which would make the
/// tests racy — this implementation records events inline.
/// </summary>
private sealed class CollectingProgress : IProgress<ProgressInfo>
{
public List<ProgressInfo> Events { get; } = [];
public void Report(ProgressInfo value)
{
lock (Events)
{
Events.Add(value);
}
}
}
private static string CreateSourceTree()
{
string root = Path.Combine(Path.GetTempPath(), $"xiso_prog_src_{Guid.NewGuid():N}");
Directory.CreateDirectory(Path.Combine(root, "sub", "deep"));
Directory.CreateDirectory(Path.Combine(root, "empty"));
File.WriteAllText(Path.Combine(root, "root.txt"), "hello");
File.WriteAllBytes(Path.Combine(root, "sub", "data.bin"), new byte[5000]);
File.WriteAllText(Path.Combine(root, "sub", "deep", "nested.txt"), "nested");
return root;
}
[Fact]
public void CreateXiso_ProgressEvents_ReportCountsFirstAndFinishLast()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
CollectingProgress progress = new();
int result = XisoWriter.CreateXiso(src, outputDir, null, null, out _, null, null, progress: progress);
Assert.Equal(0, result);
Assert.True(progress.Events.Count >= 5, $"expected at least 5 events, got {progress.Events.Count}");
Assert.Equal(ProgressInfoType.FileCount, progress.Events[0].Type);
Assert.Equal(ProgressInfoType.DirCount, progress.Events[1].Type);
Assert.Equal(ProgressInfoType.FinishedPacking, progress.Events[^1].Type);
Assert.Equal(3, progress.Events[0].Count); // root.txt, data.bin, nested.txt
Assert.Equal(3, progress.Events[1].Count); // sub, sub/deep, empty
}
[Fact]
public void CreateXiso_ProgressEvents_ReportPathsAndSizes()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
CollectingProgress progress = new();
XisoWriter.CreateXiso(src, outputDir, null, null, out _, null, null, progress: progress);
List<ProgressInfo> dirs = progress.Events.Where(e => e.Type == ProgressInfoType.DirAdded).ToList();
List<ProgressInfo> files = progress.Events.Where(e => e.Type == ProgressInfoType.FileAdded).ToList();
string[] dirPaths = dirs.Select(d => d.Path ?? "").OrderBy(p => p, StringComparer.Ordinal).ToArray();
Assert.Equal(["/", "/empty", "/sub", "/sub/deep"], dirPaths);
string[] filePaths = files.Select(f => f.Path ?? "").OrderBy(p => p, StringComparer.Ordinal).ToArray();
Assert.Equal(["/root.txt", "/sub/data.bin", "/sub/deep/nested.txt"], filePaths);
ProgressInfo dataBin = files.Single(f => string.Equals(f.Path, "/sub/data.bin", StringComparison.Ordinal));
Assert.Equal(5000, dataBin.Size);
Assert.True(dataBin.Sector > 0);
ProgressInfo rootTxt = files.Single(f => string.Equals(f.Path, "/root.txt", StringComparison.Ordinal));
Assert.Equal(5, rootTxt.Size);
}
[Fact]
public void CreateXiso_ProgressEvents_ParentDirectoryPrecedesChildren()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
CollectingProgress progress = new();
XisoWriter.CreateXiso(src, outputDir, null, null, out _, null, null, progress: progress);
Dictionary<(ProgressInfoType Type, string? Path), int> indexOf = progress.Events
.Select((e, i) => (e, i))
.ToDictionary(x => (x.e.Type, x.e.Path), x => x.i);
Assert.True(indexOf[(ProgressInfoType.DirAdded, "/sub")] < indexOf[(ProgressInfoType.DirAdded, "/sub/deep")]);
Assert.True(indexOf[(ProgressInfoType.DirAdded, "/sub")] <
indexOf[(ProgressInfoType.FileAdded, "/sub/data.bin")]);
Assert.True(indexOf[(ProgressInfoType.DirAdded, "/sub/deep")] <
indexOf[(ProgressInfoType.FileAdded, "/sub/deep/nested.txt")]);
}
[Fact]
public void CreateXiso_ProgressEvents_EmptyDirectoryEmitsDirAddedOnly()
{
string src = CreateTempDir();
Directory.CreateDirectory(Path.Combine(src, "empty"));
File.WriteAllText(Path.Combine(src, "keep.txt"), "x");
string outputDir = CreateTempDir();
CollectingProgress progress = new();
XisoWriter.CreateXiso(src, outputDir, null, null, out _, null, null, progress: progress);
Assert.Contains(progress.Events,
e => e.Type == ProgressInfoType.DirAdded && string.Equals(e.Path, "/empty", StringComparison.Ordinal));
Assert.DoesNotContain(progress.Events,
e => e.Type == ProgressInfoType.FileAdded && string.Equals(e.Path, "/empty", StringComparison.Ordinal));
}
[Fact]
public void CreateXiso_ProgressEvents_FileSizesSumToSourceBytes()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
CollectingProgress progress = new();
XisoWriter.CreateXiso(src, outputDir, null, null, out _, null, null, progress: progress);
long expected = Directory.EnumerateFiles(src, "*", SearchOption.AllDirectories)
.Sum(f => new FileInfo(f).Length);
long reported = progress.Events.Where(e => e.Type == ProgressInfoType.FileAdded).Sum(e => e.Size);
Assert.Equal(expected, reported);
}
[Fact]
public void CreateXiso_ProgressEvents_NoFinishedPackingOnFailure()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
CollectingProgress progress = new();
// A throwing byte-progress callback aborts the write phase mid-operation.
// (The first invocation — the pre-write total report — is outside the write
// try-block, so the callback must fail on a later invocation to exercise it.)
int calls = 0;
int result = XisoWriter.CreateXiso(src, outputDir, null, null, out _, null,
(_, _) =>
{
if (++calls > 1) throw new InvalidOperationException("abort");
},
progress: progress);
Assert.Equal(1, result);
Assert.DoesNotContain(progress.Events, e => e.Type == ProgressInfoType.FinishedPacking);
Assert.Contains(progress.Events, e => e.Type == ProgressInfoType.FileCount);
}
[Fact]
public void CreateXiso_EmptySource_ReportsZeroCounts()
{
string src = CreateTempDir(); // empty
string outputDir = CreateTempDir();
CollectingProgress progress = new();
int result = XisoWriter.CreateXiso(src, outputDir, null, null, out _, null, null, progress: progress);
Assert.Equal(0, result);
Assert.Equal(ProgressInfoType.FileCount, progress.Events[0].Type);
Assert.Equal(0, progress.Events[0].Count);
Assert.Equal(ProgressInfoType.DirCount, progress.Events[1].Type);
Assert.Equal(0, progress.Events[1].Count);
Assert.Contains(progress.Events,
e => e.Type == ProgressInfoType.DirAdded && string.Equals(e.Path, "/", StringComparison.Ordinal));
Assert.Equal(ProgressInfoType.FinishedPacking, progress.Events[^1].Type);
}
[Fact]
public void Rewrite_WithProgress_ReportsEvents()
{
string src = CreateSourceTree();
string createDir = CreateTempDir();
XisoWriter.CreateXiso(src, createDir, null, null, out string? isoPath, null, null);
Assert.NotNull(isoPath);
string rewriteDir = CreateTempDir();
CollectingProgress progress = new();
int result = XisoReader.Rewrite(isoPath, rewriteDir, out string? rewrittenPath, progress: progress);
Assert.Equal(0, result);
Assert.NotNull(rewrittenPath);
Assert.True(progress.Events.Count >= 5, $"expected at least 5 events, got {progress.Events.Count}");
Assert.Equal(ProgressInfoType.FileCount, progress.Events[0].Type);
Assert.Equal(ProgressInfoType.DirCount, progress.Events[1].Type);
Assert.Equal(ProgressInfoType.FinishedPacking, progress.Events[^1].Type);
Assert.Contains(progress.Events,
e => e.Type == ProgressInfoType.FileAdded &&
string.Equals(e.Path, "/sub/data.bin", StringComparison.Ordinal));
Assert.Contains(progress.Events,
e => e.Type == ProgressInfoType.DirAdded && string.Equals(e.Path, "/sub/deep", StringComparison.Ordinal));
}
[Fact]
public async Task CreateXisoAsync_WithProgress_ReportsEvents()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
CollectingProgress progress = new();
(int result, _) = await XisoWriter.CreateXisoAsync(
src, outputDir, null, null, null, null, progress: progress);
Assert.Equal(0, result);
Assert.Equal(ProgressInfoType.FileCount, progress.Events[0].Type);
Assert.Equal(ProgressInfoType.FinishedPacking, progress.Events[^1].Type);
Assert.Equal(3, progress.Events.Count(e => e.Type == ProgressInfoType.FileAdded));
}
}