-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackFromDirectoryTests.cs
More file actions
242 lines (209 loc) · 8.07 KB
/
Copy pathPackFromDirectoryTests.cs
File metadata and controls
242 lines (209 loc) · 8.07 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
using System.Security.Cryptography;
namespace XISOSharp.Tests;
/// <summary>
/// Tests for <see cref="XisoWriter.PackFromDirectory"/> — directory-to-ISO packing
/// with a 1:1 mapping (the library behind the CLI's --pack flag).
/// </summary>
[Collection("Sequential")]
public class PackFromDirectoryTests : 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_pack_{Guid.NewGuid():N}");
Directory.CreateDirectory(dir);
_tempDirs.Add(dir);
return dir;
}
private static string CreateSourceTree()
{
string root = Path.Combine(Path.GetTempPath(), $"xiso_pack_src_{Guid.NewGuid():N}");
Directory.CreateDirectory(Path.Combine(root, "sub"));
Directory.CreateDirectory(Path.Combine(root, "node_modules"));
File.WriteAllText(Path.Combine(root, "a.txt"), "hello");
File.WriteAllText(Path.Combine(root, "sub", "b.bin"), new string('B', 5000));
File.WriteAllText(Path.Combine(root, "skip.tmp"), "temp");
File.WriteAllText(Path.Combine(root, "node_modules", "x.js"), "js");
return root;
}
private static Dictionary<string, string> HashTree(string root)
{
Dictionary<string, string> result = new(StringComparer.OrdinalIgnoreCase);
foreach (string file in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories))
{
string rel = Path.GetRelativePath(root, file).Replace('\\', '/');
using SHA256 sha = SHA256.Create();
using FileStream fs = File.OpenRead(file);
result[rel] = Convert.ToHexString(sha.ComputeHash(fs));
}
return result;
}
private static string ExtractToTemp(string isoPath)
{
string dest = Path.Combine(Path.GetTempPath(), $"xiso_pack_out_{Guid.NewGuid():N}");
Directory.CreateDirectory(dest);
int result = XisoReader.Extract(isoPath, dest, false);
Assert.Equal(0, result);
return dest;
}
[Fact]
public void PackFromDirectory_CreatesIso_With1To1Mapping()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
string isoPath = Path.Combine(outputDir, "packed.iso");
int result = XisoWriter.PackFromDirectory(src, isoPath);
Assert.Equal(0, result);
Assert.True(File.Exists(isoPath));
string extracted = ExtractToTemp(isoPath);
Assert.Equal(HashTree(src), HashTree(extracted));
}
[Fact]
public void PackFromDirectory_OutputPathMayIncludeSubdirectory()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
string nested = Path.Combine(outputDir, "nested");
string isoPath = Path.Combine(nested, "packed.iso");
int result = XisoWriter.PackFromDirectory(src, isoPath);
Assert.Equal(0, result);
Assert.True(File.Exists(isoPath));
string extracted = ExtractToTemp(isoPath);
Assert.Equal(HashTree(src), HashTree(extracted));
}
[Fact]
public void PackFromDirectory_ExcludePatterns_AreHonored()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
string isoPath = Path.Combine(outputDir, "packed.iso");
int result = XisoWriter.PackFromDirectory(src, isoPath,
excludePatterns: ["**/*.tmp", "**/node_modules/**"]);
Assert.Equal(0, result);
string extracted = ExtractToTemp(isoPath);
Dictionary<string, string> files = HashTree(extracted);
Assert.Contains("a.txt", files.Keys, StringComparer.OrdinalIgnoreCase);
Assert.Contains("sub/b.bin", files.Keys, StringComparer.OrdinalIgnoreCase);
Assert.DoesNotContain("skip.tmp", files.Keys, StringComparer.OrdinalIgnoreCase);
Assert.DoesNotContain("node_modules/x.js", files.Keys, StringComparer.OrdinalIgnoreCase);
}
[Fact]
public void PackFromDirectory_MissingSource_Throws()
{
string outputDir = CreateTempDir();
string isoPath = Path.Combine(outputDir, "packed.iso");
Assert.Throws<DirectoryNotFoundException>(() =>
XisoWriter.PackFromDirectory(Path.Combine(outputDir, "nope"), isoPath));
}
[Fact]
public void PackFromDirectory_NullOrEmptyOutputPath_Throws()
{
string src = CreateSourceTree();
Assert.Throws<ArgumentException>(() => XisoWriter.PackFromDirectory(src, ""));
Assert.Throws<ArgumentNullException>(() => XisoWriter.PackFromDirectory(src, null!));
}
[Fact]
public async Task PackFromDirectoryAsync_CreatesIso()
{
string src = CreateSourceTree();
string outputDir = CreateTempDir();
string isoPath = Path.Combine(outputDir, "async.iso");
int result = await XisoWriter.PackFromDirectoryAsync(src, isoPath);
Assert.Equal(0, result);
Assert.True(File.Exists(isoPath));
string extracted = ExtractToTemp(isoPath);
Assert.Equal(HashTree(src), HashTree(extracted));
}
[Fact]
public void PackFromDirectory_RestoresCwd_OnSuccess()
{
// The create path hops CWD internally; it must not leak (#22).
string before = Directory.GetCurrentDirectory();
try
{
string src = CreateSourceTree();
_tempDirs.Add(src);
string isoPath = Path.Combine(CreateTempDir(), "packed.iso");
Assert.Equal(0, XisoWriter.PackFromDirectory(src, isoPath));
Assert.Equal(before, Directory.GetCurrentDirectory());
}
finally
{
try
{
Directory.SetCurrentDirectory(before);
}
catch
{
// ignored: assertion above already reports the leak
}
}
}
[Fact]
public void CreateXiso_RestoresCwd_WhenOutputCollides()
{
// The #55 collision throws *after* chdir into the source; CWD must
// still be restored (#22).
string before = Directory.GetCurrentDirectory();
string src = CreateSourceTree();
_tempDirs.Add(src);
try
{
string leaf = Path.GetFileName(src);
string parent = Path.GetDirectoryName(src)!;
Assert.Throws<ArgumentException>(() =>
XisoWriter.CreateXiso(src, parent, null, null, out _, leaf, null));
Assert.Equal(before, Directory.GetCurrentDirectory());
}
finally
{
try
{
Directory.SetCurrentDirectory(before);
}
catch
{
// ignored: assertion above already reports the leak
}
}
}
[Fact]
public async Task PackFromDirectory_ConcurrentCalls_AllSucceed()
{
// Creates are serialized process-wide (#22) — parallel packs must
// all succeed with intact outputs instead of corrupting shared CWD.
string before = Directory.GetCurrentDirectory();
List<(string src, string iso)> cases = Enumerable.Range(0, 4).Select(_ =>
{
string src = CreateSourceTree();
_tempDirs.Add(src);
string iso = Path.Combine(CreateTempDir(), "packed.iso");
return (src, iso);
}).ToList();
Task<int>[] tasks = cases.Select(c => Task.Run(() => XisoWriter.PackFromDirectory(c.src, c.iso))).ToArray();
int[] results = await Task.WhenAll(tasks);
foreach ((int result, (string src, string iso)) in results.Zip(cases))
{
Assert.Equal(0, result);
string extracted = ExtractToTemp(iso);
Assert.Equal(HashTree(src), HashTree(extracted));
}
Assert.Equal(before, Directory.GetCurrentDirectory());
}
}