Files
sharpcompress/tests/SharpCompress.Test/WriterTests.cs
Adam Hathcock 38203fb950 Fix async reader variable types - Remove double await on ReaderFactory.OpenAsync and use IAsyncReader
- Removed 'await' keyword before ReaderFactory.OpenAsync() calls since the method returns IAsyncReader directly (not Task)
- Changed ZipReader.Open() to ReaderFactory.OpenAsync() in Zip64AsyncTests.ReadForwardOnlyAsync()
- Changed TarReader.Open() to ReaderFactory.OpenAsync() in TarReaderAsyncTests.Tar_BZip2_Entry_Stream_Async()
- Fixed EntryStream disposal from 'await using' to 'using' since EntryStream doesn't implement IAsyncDisposable
- These changes fix compilation errors where async methods were being called on IReader (synchronous) instead of IAsyncReader (asynchronous)
2026-01-12 14:14:46 +00:00

109 lines
3.5 KiB
C#

using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.IO;
using SharpCompress.Readers;
using SharpCompress.Test.Mocks;
using SharpCompress.Writers;
namespace SharpCompress.Test;
public class WriterTests : TestBase
{
private readonly ArchiveType _type;
protected WriterTests(ArchiveType type) => _type = type;
protected void Write(
CompressionType compressionType,
string archive,
string archiveToVerifyAgainst,
Encoding? encoding = null
)
{
using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
{
var writerOptions = new WriterOptions(compressionType) { LeaveStreamOpen = true };
writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
using var writer = WriterFactory.Open(stream, _type, writerOptions);
writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
}
CompareArchivesByPath(
Path.Combine(SCRATCH2_FILES_PATH, archive),
Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst)
);
using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
{
var readerOptions = new ReaderOptions();
readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
using var reader = ReaderFactory.Open(
SharpCompressStream.Create(stream, leaveOpen: true),
readerOptions
);
reader.WriteAllToDirectory(
SCRATCH_FILES_PATH,
new ExtractionOptions { ExtractFullPath = true }
);
}
VerifyFiles();
}
protected async Task WriteAsync(
CompressionType compressionType,
string archive,
string archiveToVerifyAgainst,
Encoding? encoding = null,
CancellationToken cancellationToken = default
)
{
using (
Stream stream = new AsyncOnlyStream(
File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))
)
)
{
var writerOptions = new WriterOptions(compressionType) { LeaveStreamOpen = true };
writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
using var writer = WriterFactory.Open(stream, _type, writerOptions);
await writer.WriteAllAsync(
ORIGINAL_FILES_PATH,
"*",
SearchOption.AllDirectories,
cancellationToken
);
}
CompareArchivesByPath(
Path.Combine(SCRATCH2_FILES_PATH, archive),
Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst)
);
using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
{
var readerOptions = new ReaderOptions();
readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
await using var reader = ReaderFactory.OpenAsync(
new AsyncOnlyStream(SharpCompressStream.Create(stream, leaveOpen: true)),
readerOptions,
cancellationToken
);
await reader.WriteAllToDirectoryAsync(
SCRATCH_FILES_PATH,
new ExtractionOptions { ExtractFullPath = true },
cancellationToken
);
}
VerifyFiles();
}
}