mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-18 14:07:39 -05:00
NestReader opened the file and closed it only after a successful read, so a file that is not a nest, or a nest that failed part-way, stayed open until the garbage collector ran. On Windows that keeps the file from being renamed, replaced or deleted; the Desktop template-migration tests caught it in the Windows release build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using System.IO.Compression;
|
|
using OpenNest.IO;
|
|
|
|
namespace OpenNest.IO.Tests;
|
|
|
|
/// <summary>
|
|
/// A file that fails to read must not stay open: on Windows an open handle keeps the user from
|
|
/// renaming, replacing or deleting it until the garbage collector runs.
|
|
/// </summary>
|
|
public sealed class NestReaderFileReleaseTests : IDisposable
|
|
{
|
|
private readonly string directory =
|
|
Directory.CreateTempSubdirectory("opennest-reader-release-").FullName;
|
|
|
|
public void Dispose() => Directory.Delete(directory, recursive: true);
|
|
|
|
private static void AssertNotOpen(string path)
|
|
{
|
|
using var exclusive = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
|
|
}
|
|
|
|
[Fact]
|
|
public void AFileThatIsNotANest_IsClosedWhenTheReaderRefusesIt()
|
|
{
|
|
var path = Path.Combine(directory, "broken.nest");
|
|
File.WriteAllText(path, "not a nest");
|
|
|
|
Assert.ThrowsAny<Exception>(() => new NestReader(path).Read());
|
|
|
|
AssertNotOpen(path);
|
|
}
|
|
|
|
[Fact]
|
|
public void AnArchiveWithoutANest_IsClosedWhenReadFails()
|
|
{
|
|
var path = Path.Combine(directory, "empty.nest");
|
|
using (var zip = ZipFile.Open(path, ZipArchiveMode.Create))
|
|
zip.CreateEntry("readme.txt");
|
|
|
|
Assert.ThrowsAny<Exception>(() => new NestReader(path).Read());
|
|
|
|
AssertNotOpen(path);
|
|
}
|
|
}
|