write testing

This commit is contained in:
Adam Hathcock
2026-01-07 15:01:04 +00:00
parent b9792ca491
commit ac0716ddeb
3 changed files with 46 additions and 4 deletions

View File

@@ -41,7 +41,7 @@ public static class ArchiveFactory
{
readerOptions ??= new ReaderOptions();
stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize);
var factory = FindFactory<IArchiveFactory>(stream);
var factory = await FindFactoryAsync<IArchiveFactory>(stream, cancellationToken).ConfigureAwait(false);
return await factory
.OpenAsync(stream, readerOptions, cancellationToken)
.ConfigureAwait(false);
@@ -114,7 +114,7 @@ public static class ArchiveFactory
{
options ??= new ReaderOptions { LeaveStreamOpen = false };
var factory = FindFactory<IArchiveFactory>(fileInfo);
var factory = await FindFactoryAsync<IArchiveFactory>(fileInfo, cancellationToken).ConfigureAwait(false);
return await factory.OpenAsync(fileInfo, options, cancellationToken).ConfigureAwait(false);
}
@@ -292,6 +292,46 @@ public static class ArchiveFactory
);
}
private static ValueTask<T> FindFactoryAsync<T>(FileInfo finfo, CancellationToken cancellationToken )
where T : IFactory
{
finfo.NotNull(nameof(finfo));
using Stream stream = finfo.OpenRead();
return FindFactoryAsync<T>(stream, cancellationToken);
}
private static async ValueTask<T> FindFactoryAsync<T>(Stream stream, CancellationToken cancellationToken )
where T : IFactory
{
stream.NotNull(nameof(stream));
if (!stream.CanRead || !stream.CanSeek)
{
throw new ArgumentException("Stream should be readable and seekable");
}
var factories = Factory.Factories.OfType<T>();
var startPosition = stream.Position;
foreach (var factory in factories)
{
stream.Seek(startPosition, SeekOrigin.Begin);
if (await factory.IsArchiveAsync(stream, cancellationToken: cancellationToken))
{
stream.Seek(startPosition, SeekOrigin.Begin);
return factory;
}
}
var extensions = string.Join(", ", factories.Select(item => item.Name));
throw new InvalidOperationException(
$"Cannot determine compressed stream type. Supported Archive Formats: {extensions}"
);
}
public static bool IsArchive(
string filePath,
out ArchiveType? type,

View File

@@ -9,6 +9,7 @@ using SharpCompress.Compressors.Xz;
using SharpCompress.Crypto;
using SharpCompress.IO;
using SharpCompress.Readers;
using SharpCompress.Test.Mocks;
using SharpCompress.Writers;
using SharpCompress.Writers.Zip;
using Xunit;
@@ -599,7 +600,7 @@ public class ArchiveTests : ReaderTests
throwOnDispose: true
)
)
using (var archive = archiveFactory.Open(stream, readerOptions))
using (var archive = await archiveFactory.OpenAsync(new AsyncOnlyStream(stream), readerOptions))
{
try
{

View File

@@ -5,6 +5,7 @@ using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.IO;
using SharpCompress.Readers;
using SharpCompress.Test.Mocks;
using SharpCompress.Writers;
namespace SharpCompress.Test;
@@ -62,7 +63,7 @@ public class WriterTests : TestBase
CancellationToken cancellationToken = default
)
{
using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
using (Stream stream = new AsyncOnlyStream(File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))))
{
var writerOptions = new WriterOptions(compressionType) { LeaveStreamOpen = true };