-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemapFilesystemTests.cs
More file actions
593 lines (525 loc) · 23.2 KB
/
Copy pathRemapFilesystemTests.cs
File metadata and controls
593 lines (525 loc) · 23.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
using System.Text;
using XISOSharp.Models;
namespace XISOSharp.Tests;
/// <summary>
/// Tests for <see cref="RemapRule"/> and <see cref="RemapFilesystem"/>.
/// Covers rule parsing, spec file handling, dry-run and image building.
/// </summary>
[Collection("Sequential")]
public class RemapFilesystemTests : IDisposable
{
private readonly List<string> _tempDirs = [];
private readonly List<string> _tempFiles = [];
public void Dispose()
{
Logger.Quiet = false;
Logger.RealQuiet = false;
foreach (string dir in _tempDirs)
{
try
{
if (Directory.Exists(dir)) Directory.Delete(dir, true);
}
catch
{
// ignored
}
}
foreach (string file in _tempFiles)
{
try
{
if (File.Exists(file)) File.Delete(file);
}
catch
{
// ignored
}
}
}
private string CreateTempDir()
{
string dir = Path.Combine(Path.GetTempPath(), $"xiso_remap_{Guid.NewGuid():N}");
Directory.CreateDirectory(dir);
_tempDirs.Add(dir);
return dir;
}
private static void CreateFile(string root, string relative, string content = "data")
{
string full = Path.Combine(root, relative.Replace('/', Path.DirectorySeparatorChar));
Directory.CreateDirectory(Path.GetDirectoryName(full)!);
File.WriteAllText(full, content);
}
// -----------------------------------------------------------------
// RemapRule.TryParse
// -----------------------------------------------------------------
[Fact]
public void TryParse_ValidSimpleRule_Succeeds()
{
bool ok = RemapRule.TryParse("src/**:dest/{1}", out RemapRule? rule, out string? error);
Assert.True(ok);
Assert.NotNull(rule);
Assert.Null(error);
Assert.Equal("src/**", rule.HostGlob);
Assert.Equal("dest/{1}", rule.ImagePath);
Assert.False(rule.IsExclusion);
}
[Fact]
public void TryParse_ValidSingleFileRule_Succeeds()
{
bool ok = RemapRule.TryParse("*.txt:docs/{1}", out RemapRule? rule, out _);
Assert.True(ok);
Assert.NotNull(rule);
Assert.Equal("*.txt", rule.HostGlob);
Assert.Equal("docs/{1}", rule.ImagePath);
}
[Fact]
public void TryParse_ExclusionRule_SetsIsExclusion()
{
bool ok = RemapRule.TryParse("!skip/**", out RemapRule? rule, out string? error);
Assert.True(ok);
Assert.NotNull(rule);
Assert.True(rule.IsExclusion);
Assert.Equal("skip/**", rule.HostGlob);
Assert.Equal(string.Empty, rule.ImagePath);
Assert.Null(error);
}
[Fact]
public void TryParse_ExclusionWithImagePath_Succeeds()
{
// Exclusion may still have image part but it is ignored; host is !pattern
bool ok = RemapRule.TryParse("!skip/**:ignored", out RemapRule? rule, out _);
Assert.True(ok);
Assert.NotNull(rule);
Assert.True(rule.IsExclusion);
}
[Fact]
public void TryParse_EmptyRaw_Fails()
{
bool ok = RemapRule.TryParse("", out RemapRule? rule, out string? error);
Assert.False(ok);
Assert.Null(rule);
Assert.NotNull(error);
Assert.Contains("empty", error, StringComparison.OrdinalIgnoreCase);
ok = RemapRule.TryParse(" ", out rule, out error);
Assert.False(ok);
Assert.NotNull(error);
}
[Fact]
public void TryParse_MissingImagePath_ForNonExclusion_Fails()
{
bool ok = RemapRule.TryParse("src/**", out RemapRule? rule, out string? error);
Assert.False(ok);
Assert.Null(rule);
Assert.NotNull(error);
Assert.Contains("image path", error, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void TryParse_EmptyHost_Fails()
{
bool ok = RemapRule.TryParse(":dest", out RemapRule? rule, out string? error);
Assert.False(ok);
Assert.Null(rule);
Assert.NotNull(error);
}
[Fact]
public void TryParse_ExclusionEmptyHost_Fails()
{
Assert.False(RemapRule.TryParse("! :dest", out _, out string? error));
Assert.NotNull(error);
// Try another: "!"
bool ok2 = RemapRule.TryParse("!", out _, out string? err2);
Assert.False(ok2);
Assert.NotNull(err2);
}
[Fact]
public void TryParse_InvalidHostGlob_EmbeddedDoubleStar_Fails()
{
bool ok = RemapRule.TryParse("a**/b:dest", out RemapRule? rule, out string? error);
Assert.False(ok);
Assert.Null(rule);
Assert.NotNull(error);
Assert.Contains("Invalid host glob", error, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void TryParse_InvalidRewrite_NonDigitInBraces_Fails()
{
bool ok = RemapRule.TryParse("src/**:dest/{a}", out RemapRule? rule, out string? error);
Assert.False(ok);
Assert.Null(rule);
Assert.NotNull(error);
Assert.Contains("Invalid rewrite", error, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void TryParse_InvalidRewrite_NestedBrace_Fails()
{
bool ok = RemapRule.TryParse("src/**:dest/{{1}", out RemapRule? rule, out string? error);
Assert.False(ok);
Assert.Null(rule);
Assert.NotNull(error);
}
[Fact]
public void TryParse_EscapedColonInHost_ParsesLiteralColon()
{
bool ok = RemapRule.TryParse(@"my\:games/**:dest/{1}", out RemapRule? rule, out string? error);
Assert.True(ok);
Assert.NotNull(rule);
Assert.Null(error);
Assert.Equal("my:games/**", rule.HostGlob);
Assert.Equal("dest/{1}", rule.ImagePath);
}
[Fact]
public void TryParse_EscapedColonInImage_ParsesLiteralColon()
{
bool ok = RemapRule.TryParse(@"src/**:dest\:v2/{1}", out RemapRule? rule, out _);
Assert.True(ok);
Assert.NotNull(rule);
Assert.Equal("src/**", rule.HostGlob);
Assert.Equal("dest:v2/{1}", rule.ImagePath);
}
[Fact]
public void TryParse_EscapedBackslash_StaysSingleBackslash()
{
bool ok = RemapRule.TryParse(@"dir\\*.bin:dest/{1}", out RemapRule? rule, out _);
Assert.True(ok);
Assert.NotNull(rule);
Assert.Equal(@"dir\*.bin", rule.HostGlob);
}
[Fact]
public void TryParse_BackslashBeforeOtherChars_StaysLiteral()
{
// Windows separators must survive untouched: \g and \s are not escapes.
bool ok = RemapRule.TryParse(@"games\saves\*.bin:dest/{1}", out RemapRule? rule, out _);
Assert.True(ok);
Assert.NotNull(rule);
Assert.Equal(@"games\saves\*.bin", rule.HostGlob);
}
[Fact]
public void TryParse_DriveLetterColon_IsNotSeparator()
{
bool ok = RemapRule.TryParse(@"C:\games\*.bin:{0}", out RemapRule? rule, out string? error);
Assert.True(ok);
Assert.NotNull(rule);
Assert.Null(error);
Assert.Equal(@"C:\games\*.bin", rule.HostGlob);
Assert.Equal("{0}", rule.ImagePath);
}
[Fact]
public void TryParse_DriveLetterForwardSlash_IsNotSeparator()
{
bool ok = RemapRule.TryParse("D:/games/**:{0}", out RemapRule? rule, out _);
Assert.True(ok);
Assert.NotNull(rule);
Assert.Equal("D:/games/**", rule.HostGlob);
Assert.Equal("{0}", rule.ImagePath);
}
[Fact]
public void TryParse_ExclusionDriveLetter_IsNotSeparator()
{
bool ok = RemapRule.TryParse(@"!C:\secret\*.bin", out RemapRule? rule, out _);
Assert.True(ok);
Assert.NotNull(rule);
Assert.True(rule.IsExclusion);
Assert.Equal(@"C:\secret\*.bin", rule.HostGlob);
}
[Fact]
public void TryParse_RuleWithoutSeparator_StillFails()
{
// No unescaped ':' outside a drive prefix => missing image path.
bool ok = RemapRule.TryParse(@"my\:games\**", out RemapRule? rule, out string? error);
Assert.False(ok);
Assert.Null(rule);
Assert.NotNull(error);
}
[Fact]
public void TryParse_InvalidRewrite_UnclosedBrace_Fails()
{
bool ok = RemapRule.TryParse("src/**:dest/{1", out RemapRule? rule, out string? error);
Assert.False(ok);
Assert.Null(rule);
Assert.NotNull(error);
}
// -----------------------------------------------------------------
// ParseSpecText / ParseSpecFile / GenerateSpecText
// -----------------------------------------------------------------
[Fact]
public void ParseSpecText_WithMetadataAndRules_ParsesCorrectly()
{
const string toml = """
[metadata]
output = "out.iso"
[map_rules]
"src/**" = "dest/{1}"
"!skip/**" = ""
"*.txt" = "docs/{0}"
""";
(string? output, List<RemapRule> rules) = RemapFilesystem.ParseSpecText(toml);
Assert.Equal("out.iso", output);
Assert.Equal(3, rules.Count);
Assert.Equal("src/**", rules[0].HostGlob);
Assert.Equal("dest/{1}", rules[0].ImagePath);
Assert.False(rules[0].IsExclusion);
Assert.True(rules[1].IsExclusion);
Assert.Equal("skip/**", rules[1].HostGlob);
Assert.Equal("*.txt", rules[2].HostGlob);
}
[Fact]
public void ParseSpecText_IgnoresCommentsAndEmptyLines()
{
const string toml = """
# comment
[metadata]
# another
output = "a.iso"
[map_rules]
; semicolon comment
"a/**" = "b/{1}"
""";
(string? output, List<RemapRule> rules) = RemapFilesystem.ParseSpecText(toml);
Assert.Equal("a.iso", output);
Assert.Single(rules);
}
[Fact]
public void ParseSpecFile_ReadsFromFile()
{
const string toml = """
[metadata]
output = "fromfile.iso"
[map_rules]
"**" = "{0}"
""";
string tmp = Path.Combine(Path.GetTempPath(), $"xiso_spec_{Guid.NewGuid():N}.toml");
File.WriteAllText(tmp, toml, Encoding.UTF8);
_tempFiles.Add(tmp);
(string? output, List<RemapRule> rules) = RemapFilesystem.ParseSpecFile(tmp);
Assert.Equal("fromfile.iso", output);
Assert.Single(rules);
Assert.Equal("**", rules[0].HostGlob);
}
[Fact]
public void ParseSpecFile_MissingFile_Throws()
{
string missing = Path.Combine(Path.GetTempPath(), $"missing_{Guid.NewGuid():N}.toml");
Assert.Throws<FileNotFoundException>(() => RemapFilesystem.ParseSpecFile(missing));
}
[Fact]
public void GenerateSpecText_RoundTrip_PreservesRules()
{
List<RemapRule> rules = new()
{
new RemapRule { HostGlob = "src/**", ImagePath = "dest/{1}", IsExclusion = false },
new RemapRule { HostGlob = "skip/**", ImagePath = "", IsExclusion = true },
};
string toml = RemapFilesystem.GenerateSpecText(rules, "out.iso");
Assert.Contains("output = \"out.iso\"", toml, StringComparison.OrdinalIgnoreCase);
Assert.Contains("src/**", toml, StringComparison.OrdinalIgnoreCase);
Assert.Contains("!skip/**", toml, StringComparison.OrdinalIgnoreCase);
(string? output, List<RemapRule> parsed) = RemapFilesystem.ParseSpecText(toml);
Assert.Equal("out.iso", output);
Assert.Equal(2, parsed.Count);
Assert.Equal(rules[0].HostGlob, parsed[0].HostGlob);
Assert.Equal(rules[0].ImagePath, parsed[0].ImagePath);
Assert.Equal(rules[0].IsExclusion, parsed[0].IsExclusion);
Assert.Equal(rules[1].IsExclusion, parsed[1].IsExclusion);
}
[Fact]
public void GenerateSpecText_WithoutOutput_OmitsMetadata()
{
List<RemapRule> rules = new() { new RemapRule { HostGlob = "**", ImagePath = "{0}" } };
string toml = RemapFilesystem.GenerateSpecText(rules, null);
Assert.DoesNotContain("[metadata]", toml, StringComparison.OrdinalIgnoreCase);
Assert.Contains("[map_rules]", toml, StringComparison.OrdinalIgnoreCase);
}
// -----------------------------------------------------------------
// DryRunRemap
// -----------------------------------------------------------------
[Fact]
public void DryRunRemap_SimpleWildcard_MapsFiles()
{
string src = CreateTempDir();
CreateFile(src, "a.txt", "a");
CreateFile(src, "b.txt", "b");
CreateFile(src, "sub/c.txt", "c");
// Map all txt files to docs/
Assert.True(RemapRule.TryParse("**/*.txt:docs/{0}", out _, out _));
// Actually "**/*.txt" captures via WaxGlob? For remap, {0} is whole match, should produce docs/<path>. DryRun will rewrite via caps[0].
// Simpler: map everything to root via "**"
Assert.True(RemapRule.TryParse("**:{0}", out RemapRule? rAll, out _));
List<RemapRule> rules = new() { rAll! };
IReadOnlyList<(string HostPath, string ImagePath)> mappings = RemapFilesystem.DryRunRemap(src, rules);
// Every file should be mapped to itself (since {0} is whole)
Assert.Contains(mappings,
m => string.Equals(m.HostPath, "/a.txt", StringComparison.OrdinalIgnoreCase) &&
string.Equals(m.ImagePath, "/a.txt", StringComparison.OrdinalIgnoreCase));
Assert.Contains(mappings,
m => string.Equals(m.HostPath, "/sub/c.txt", StringComparison.OrdinalIgnoreCase) &&
string.Equals(m.ImagePath, "/sub/c.txt", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public void DryRunRemap_WithCapture_RemapsToDest()
{
string src = CreateTempDir();
CreateFile(src, "src/file.txt", "hello");
CreateFile(src, "src/sub/nested.txt", "world");
CreateFile(src, "other.txt", "other");
Assert.True(RemapRule.TryParse("src/**:dest/{1}", out RemapRule? rule, out _));
List<RemapRule> rules = new() { rule! };
IReadOnlyList<(string HostPath, string ImagePath)> mappings = RemapFilesystem.DryRunRemap(src, rules);
// src/* should be mapped to dest/*
Assert.Contains(mappings,
m => string.Equals(m.HostPath, "/src/file.txt", StringComparison.OrdinalIgnoreCase) &&
string.Equals(m.ImagePath, "/dest/file.txt", StringComparison.OrdinalIgnoreCase));
Assert.Contains(mappings,
m => string.Equals(m.HostPath, "/src/sub/nested.txt", StringComparison.OrdinalIgnoreCase) &&
string.Equals(m.ImagePath, "/dest/sub/nested.txt", StringComparison.OrdinalIgnoreCase));
// other.txt is child of ? It is not under src, and src/** does not match other.txt, so it should not be mapped.
// But due to parentPrefix logic, only descendants of matched prefix are mapped. other.txt has no prefix, so omitted.
Assert.DoesNotContain(mappings,
m => string.Equals(m.HostPath, "/other.txt", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public void DryRunRemap_Exclusion_RemovesMatchingFiles()
{
string src = CreateTempDir();
CreateFile(src, "keep.txt", "keep");
CreateFile(src, "skip.txt", "skip");
CreateFile(src, "sub/keep2.txt", "keep2");
CreateFile(src, "sub/skip2.txt", "skip2");
Assert.True(RemapRule.TryParse("**:{0}", out RemapRule? all, out _));
Assert.True(RemapRule.TryParse("!**/skip*.txt", out RemapRule? excl, out _));
// Order matters: exclusion after inclusion should exclude.
// But BuildMappings processes rules in order per prefix: first match via any glob, then iterates ordered rules F
// For DryRun, exclusion should null out rewritten if later rule is exclusion but IsExclusion clears.
// Let's test with mapping all then excluding skip
List<RemapRule> rulesInclusionFirst = new() { all!, excl! };
IReadOnlyList<(string HostPath, string ImagePath)> mappings =
RemapFilesystem.DryRunRemap(src, rulesInclusionFirst);
// Depending on implementation, exclusion after inclusion with same prefix should exclude?
// The logic: for each prefix, loop rules idx 0..count-1, if caps matches and IsExclusion -> rewritten=null; continue;
// if already rewritten != null -> continue (skip)
// So exclusion only applies if it appears before the inclusion that would set rewritten, or it clears previous?
// Actually rewritten=null continue after exclusion, but if rewritten was already set, exclusion still clears it (since it doesn't check rewritten != null before clearing).
// For rules [all, excl]: for prefix "skip.txt": idx0 all caps not null, IsExclusion false, rewritten=null so it sets rewritten to "skip.txt"; idx1 excl caps not null, IsExclusion true => rewritten=null (clears). So final null -> omitted.
// For order [excl, all]: for "skip.txt": idx0 excl matches -> rewritten stays null; idx1 all matches but IsExclusion false, but rewritten==null so it would set. So order matters differently.
// We test inclusion then exclusion should exclude.
Assert.DoesNotContain(mappings, m => m.HostPath.Contains("skip", StringComparison.OrdinalIgnoreCase));
Assert.Contains(mappings, m => string.Equals(m.HostPath, "/keep.txt", StringComparison.OrdinalIgnoreCase));
Assert.Contains(mappings, m => string.Equals(m.HostPath, "/sub/keep2.txt", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public void DryRunRemap_DirectoryMapping_MapsChildrenViaSuffix()
{
string src = CreateTempDir();
CreateFile(src, "srcdir/file.txt", "a");
CreateFile(src, "srcdir/sub/b.txt", "b");
// Map directory srcdir to destdir (no wildcard)
Assert.True(RemapRule.TryParse("srcdir:destdir", out RemapRule? rule, out _));
IReadOnlyList<(string HostPath, string ImagePath)> mappings =
RemapFilesystem.DryRunRemap(src, new List<RemapRule> { rule! });
Assert.Contains(mappings,
m => string.Equals(m.HostPath, "/srcdir/file.txt", StringComparison.OrdinalIgnoreCase) &&
string.Equals(m.ImagePath, "/destdir/file.txt", StringComparison.OrdinalIgnoreCase));
Assert.Contains(mappings,
m => string.Equals(m.HostPath, "/srcdir/sub/b.txt", StringComparison.OrdinalIgnoreCase) &&
string.Equals(m.ImagePath, "/destdir/sub/b.txt", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public void DryRunRemap_MissingSourceDir_Throws()
{
string missing = Path.Combine(Path.GetTempPath(), $"missing_{Guid.NewGuid():N}");
Assert.True(RemapRule.TryParse("**:{0}", out RemapRule? r, out _));
Assert.Throws<DirectoryNotFoundException>(() =>
RemapFilesystem.DryRunRemap(missing, new List<RemapRule> { r! }));
}
[Fact]
public void DryRunRemap_EmptyRules_MapsNothing()
{
string src = CreateTempDir();
CreateFile(src, "a.txt", "x");
IReadOnlyList<(string HostPath, string ImagePath)> mappings =
RemapFilesystem.DryRunRemap(src, new List<RemapRule>());
Assert.Empty(mappings);
}
[Fact]
public void DryRunRemap_FirstWins_ForDuplicateGuest()
{
string src = CreateTempDir();
CreateFile(src, "a.txt", "contentA");
CreateFile(src, "b.txt", "contentB");
Assert.True(RemapRule.TryParse("a.txt:dest.txt", out RemapRule? r1, out _));
Assert.True(RemapRule.TryParse("b.txt:dest.txt", out RemapRule? r2, out _));
IReadOnlyList<(string HostPath, string ImagePath)> mappings =
RemapFilesystem.DryRunRemap(src, new List<RemapRule> { r1!, r2! });
// Both host files map to same guest dest.txt, but first wins, second omitted via guestSeen dedup
Assert.Single(mappings);
Assert.Equal("/a.txt", mappings[0].HostPath);
Assert.Equal("/dest.txt", mappings[0].ImagePath);
}
// -----------------------------------------------------------------
// BuildImage
// -----------------------------------------------------------------
[Fact]
public void BuildImage_SimpleRemap_CreatesIso()
{
string src = CreateTempDir();
CreateFile(src, "orig/a.txt", "hello");
CreateFile(src, "orig/sub/b.txt", "world");
CreateFile(src, "ignore.txt", "ignore");
Assert.True(RemapRule.TryParse("orig/**:new/{1}", out RemapRule? rule, out _));
string outDir = CreateTempDir();
string isoPath = Path.Combine(outDir, "remap.iso");
int res = RemapFilesystem.BuildImage(src, isoPath, new List<RemapRule> { rule! });
Assert.Equal(0, res);
Assert.True(File.Exists(isoPath));
// Extract and verify structure
string extract = CreateTempDir();
int ext = XisoReader.Extract(isoPath, extract, false);
Assert.Equal(0, ext);
Assert.True(File.Exists(Path.Combine(extract, "new", "a.txt")));
Assert.True(File.Exists(Path.Combine(extract, "new", "sub", "b.txt")));
Assert.False(File.Exists(Path.Combine(extract, "ignore.txt")));
Assert.False(File.Exists(Path.Combine(extract, "orig", "a.txt")));
Assert.Equal("hello", File.ReadAllText(Path.Combine(extract, "new", "a.txt")));
}
[Fact]
public void BuildImage_Exclusion_CreatesIsoWithoutExcluded()
{
string src = CreateTempDir();
CreateFile(src, "keep.txt", "keep");
CreateFile(src, "skip.tmp", "skip");
CreateFile(src, "sub/keep2.txt", "keep2");
CreateFile(src, "sub/skip2.tmp", "skip2");
Assert.True(RemapRule.TryParse("**:{0}", out RemapRule? all, out _));
Assert.True(RemapRule.TryParse("!**/*.tmp", out RemapRule? excl, out _));
string outDir = CreateTempDir();
string isoPath = Path.Combine(outDir, "remap_excl.iso");
int res = RemapFilesystem.BuildImage(src, isoPath, new List<RemapRule> { all!, excl! });
Assert.Equal(0, res);
string extract = CreateTempDir();
XisoReader.Extract(isoPath, extract, false);
Assert.True(File.Exists(Path.Combine(extract, "keep.txt")));
Assert.True(File.Exists(Path.Combine(extract, "sub", "keep2.txt")));
Assert.False(File.Exists(Path.Combine(extract, "skip.tmp")));
Assert.False(File.Exists(Path.Combine(extract, "sub", "skip2.tmp")));
}
[Fact]
public void BuildImage_NoRules_ReturnsError()
{
string src = CreateTempDir();
CreateFile(src, "a.txt", "a");
string outDir = CreateTempDir();
string isoPath = Path.Combine(outDir, "no_rules.iso");
int res = RemapFilesystem.BuildImage(src, isoPath, new List<RemapRule>());
Assert.Equal(1, res);
}
[Fact]
public void BuildImage_MissingSource_ReturnsError()
{
string missing = Path.Combine(Path.GetTempPath(), $"missing_{Guid.NewGuid():N}");
string outDir = CreateTempDir();
string isoPath = Path.Combine(outDir, "missing.iso");
Assert.True(RemapRule.TryParse("**:{0}", out RemapRule? r, out _));
int res = RemapFilesystem.BuildImage(missing, isoPath, new List<RemapRule> { r! });
Assert.Equal(1, res);
}
}