2023-09-11 08:43:27 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace FileUtils
|
|
|
|
|
{
|
2023-09-12 11:32:06 +00:00
|
|
|
|
public class TrackedFile
|
2023-09-11 08:43:27 +00:00
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
private readonly ILog log;
|
2023-09-11 08:43:27 +00:00
|
|
|
|
|
2023-09-12 11:32:06 +00:00
|
|
|
|
public TrackedFile(ILog log, string filename, string label)
|
2023-09-11 08:43:27 +00:00
|
|
|
|
{
|
|
|
|
|
this.log = log;
|
|
|
|
|
Filename = filename;
|
|
|
|
|
Label = label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Filename { get; }
|
|
|
|
|
public string Label { get; }
|
|
|
|
|
|
2023-09-12 11:32:06 +00:00
|
|
|
|
public void AssertIsEqual(TrackedFile? actual)
|
2023-09-11 08:43:27 +00:00
|
|
|
|
{
|
|
|
|
|
var sw = Stopwatch.Begin(log);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
AssertEqual(actual);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2023-09-12 11:32:06 +00:00
|
|
|
|
sw.End($"{nameof(TrackedFile)}.{nameof(AssertIsEqual)}");
|
2023-09-11 08:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Describe()
|
|
|
|
|
{
|
|
|
|
|
var sizePostfix = $" ({Formatter.FormatByteSize(GetFileSize())})";
|
|
|
|
|
if (!string.IsNullOrEmpty(Label)) return Label + sizePostfix;
|
|
|
|
|
return $"'{Filename}'{sizePostfix}";
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-06 08:59:45 +00:00
|
|
|
|
public ByteSize GetFilesize()
|
|
|
|
|
{
|
|
|
|
|
return new ByteSize(GetFileSize());
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 11:32:06 +00:00
|
|
|
|
private void AssertEqual(TrackedFile? actual)
|
2023-09-11 08:43:27 +00:00
|
|
|
|
{
|
2023-09-20 08:51:47 +00:00
|
|
|
|
if (actual == null) FrameworkAssert.Fail("TestFile is null.");
|
|
|
|
|
if (actual == this || actual!.Filename == Filename) FrameworkAssert.Fail("TestFile is compared to itself.");
|
2023-09-11 08:43:27 +00:00
|
|
|
|
|
2023-09-20 08:51:47 +00:00
|
|
|
|
FrameworkAssert.That(actual.GetFileSize() == GetFileSize(), "Files are not of equal length.");
|
2023-09-11 08:43:27 +00:00
|
|
|
|
|
|
|
|
|
using var streamExpected = new FileStream(Filename, FileMode.Open, FileAccess.Read);
|
|
|
|
|
using var streamActual = new FileStream(actual.Filename, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
|
|
|
|
var bytesExpected = new byte[FileManager.ChunkSize];
|
|
|
|
|
var bytesActual = new byte[FileManager.ChunkSize];
|
|
|
|
|
|
|
|
|
|
var readExpected = 0;
|
|
|
|
|
var readActual = 0;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
readExpected = streamExpected.Read(bytesExpected, 0, FileManager.ChunkSize);
|
|
|
|
|
readActual = streamActual.Read(bytesActual, 0, FileManager.ChunkSize);
|
|
|
|
|
|
|
|
|
|
if (readExpected == 0 && readActual == 0)
|
|
|
|
|
{
|
|
|
|
|
log.Log($"OK: '{Describe()}' is equal to '{actual.Describe()}'.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 08:51:47 +00:00
|
|
|
|
FrameworkAssert.That(readActual == readExpected, "Unable to read buffers of equal length.");
|
2023-09-11 08:43:27 +00:00
|
|
|
|
|
|
|
|
|
for (var i = 0; i < readActual; i++)
|
|
|
|
|
{
|
2023-09-20 08:51:47 +00:00
|
|
|
|
if (bytesExpected[i] != bytesActual[i]) FrameworkAssert.Fail("File contents not equal.");
|
2023-09-11 08:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long GetFileSize()
|
|
|
|
|
{
|
|
|
|
|
var info = new FileInfo(Filename);
|
|
|
|
|
return info.Length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|