Files
poli0981andClaude Opus 5.5 eaaf6198a4 feat(core): hashing engine, checksum files, safe folder walking, settings store
HashTool.Core now holds everything that does not need a UI:
- HashEngine reads a file once for any number of algorithms, overlaps disk
  reads with hashing (double buffering), uses BLAKE3's multi-threaded mode
  for large blocks and opens files with FileShare.ReadWrite|Delete so files
  still open in another program can be hashed.
- CRC-32 is now printed in the conventional big-endian order (CBF43926 for
  "123456789"); HashMatcher still accepts the reversed values written by 1.x.
- ChecksumParser reads GNU (sha256sum), BSD/--tag, xxhsum, SFV, bare 1.x
  sidecars and certutil output, including UTF-16 files; ChecksumWriter writes
  GNU, BSD, SFV, JSON and CSV (formula-injection safe). Network (UNC) and
  device paths inside checksum files are never resolved.
- ChecksumFileNames recognises checksum files by extension and *SUMS names
  instead of Enum.TryParse, which accepted numeric extensions like .001.
- FileEnumerator walks folders on a background-friendly lazy enumerator,
  skips unreadable folders and does not follow symbolic links or junctions
  (the old walker looped forever on link cycles).
- BatchProcessor runs small files in parallel and large files in their own
  lane, with per-file cancellation and timeouts, and reports every item once.
- SettingsStore saves atomically, recovers from corrupt files (keeps a .bak),
  clamps values, reads 1.x files and imports font_settings.json, which 1.1.x
  used as the only store for font choices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 13:16:40 +07:00

72 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HashTool.Core.Files;
using Xunit;
namespace HashTool.Core.Tests.Files;
public sealed class FileEnumeratorTests : IDisposable
{
private readonly TempDirectory _temp = new();
public void Dispose() => _temp.Dispose();
[Fact]
public void ExpandsFoldersRecursively_AndKeepsFiles()
{
var a = _temp.File("root/a.txt", "a");
var b = _temp.File("root/sub/deeper/b.txt", "bb");
var loose = _temp.File("loose.bin", "ccc");
var entries = FileEnumerator.Expand([Path.Combine(_temp.Path, "root"), loose], cancellationToken: TestContext.Current.CancellationToken).ToList();
Assert.Equal(new[] { a, b, loose }.Order(), entries.Select(e => e.FullPath).Order());
Assert.Equal(2, entries.Single(e => e.FullPath == b).Length);
}
[Fact]
public void MissingPaths_AreReportedNotThrown()
{
var errors = new List<string>();
var entries = FileEnumerator.Expand([Path.Combine(_temp.Path, "nope.bin")], onError: (p, _) => errors.Add(p), cancellationToken: TestContext.Current.CancellationToken).ToList();
Assert.Empty(entries);
Assert.Single(errors);
}
[Fact]
public void HiddenFiles_AreSkippedUnlessRequested()
{
var visible = _temp.File("dir/visible.txt", "v");
var hidden = _temp.File("dir/hidden.txt", "h");
File.SetAttributes(hidden, File.GetAttributes(hidden) | FileAttributes.Hidden);
var dir = Path.Combine(_temp.Path, "dir");
if (OperatingSystem.IsWindows())
Assert.Equal([visible], FileEnumerator.EnumerateDirectory(dir, cancellationToken: TestContext.Current.CancellationToken).Select(e => e.FullPath));
Assert.Equal(2, FileEnumerator.EnumerateDirectory(dir, new FileEnumerationOptions { IncludeHidden = true }, cancellationToken: TestContext.Current.CancellationToken).Count());
}
[Fact]
public void SymbolicLinkCycles_DoNotLoop()
{
var file = _temp.File("loop/file.txt", "x");
var loopDir = Path.Combine(_temp.Path, "loop");
try
{
Directory.CreateSymbolicLink(Path.Combine(loopDir, "back"), loopDir);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Assert.Skip("Creating symbolic links needs Developer Mode or admin rights on this machine.");
}
var entries = FileEnumerator.EnumerateDirectory(loopDir, cancellationToken: TestContext.Current.CancellationToken).ToList();
Assert.Equal([file], entries.Select(e => e.FullPath));
}
}