mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-17 23:27:40 -05:00
fix(io): close a nest file that fails to read
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>
This commit is contained in:
1 parent
83c14a41e1
commit
a2a6d3abc2
2 files changed
+66
-4
No files matched your search
@@ -0,0 +1,44 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,15 @@ namespace OpenNest.IO
|
||||
public NestReader(string file)
|
||||
{
|
||||
stream = new FileStream(file, FileMode.Open, FileAccess.Read);
|
||||
zipArchive = new ZipArchive(stream, ZipArchiveMode.Read);
|
||||
try
|
||||
{
|
||||
zipArchive = new ZipArchive(stream, ZipArchiveMode.Read);
|
||||
}
|
||||
catch
|
||||
{
|
||||
stream.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public NestReader(Stream stream)
|
||||
@@ -58,6 +66,19 @@ namespace OpenNest.IO
|
||||
}
|
||||
|
||||
public Nest Read()
|
||||
{
|
||||
try
|
||||
{
|
||||
return ReadNest();
|
||||
}
|
||||
finally
|
||||
{
|
||||
zipArchive.Dispose();
|
||||
stream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private Nest ReadNest()
|
||||
{
|
||||
var nestJson = ReadEntry("nest.json");
|
||||
var dto = JsonSerializer.Deserialize<NestDto>(nestJson, JsonOptions);
|
||||
@@ -72,9 +93,6 @@ namespace OpenNest.IO
|
||||
// back to the drawing so removing lead-ins does not remove the marks too.
|
||||
DrawingMarkRecovery.Apply(nest);
|
||||
|
||||
zipArchive.Dispose();
|
||||
stream.Close();
|
||||
|
||||
return nest;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user