test: OpenZip
This commit is contained in:
1 parent
c5d8543323
commit
f8b2c0bbff
7 files changed
+62
-43
No files matched your search
@@ -11,6 +11,13 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="TestFiles\FileUtils\Corrupted.zip" />
|
||||||
|
<None Remove="TestFiles\FileUtils\GB Encoding.zip" />
|
||||||
|
<None Remove="TestFiles\FileUtils\Not zip.zip" />
|
||||||
|
<None Remove="TestFiles\FileUtils\UTF8 Encoding.zip" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||||
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
|
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
|
||||||
@@ -25,6 +32,21 @@
|
|||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="TestFiles\FileUtils\Corrupted.zip">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
<Content Include="TestFiles\FileUtils\GB Encoding.zip">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
<Content Include="TestFiles\FileUtils\UTF8 Encoding.zip">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
<Content Include="TestFiles\FileUtils\Not zip.zip">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Using Include="System.Diagnostics.CodeAnalysis" />
|
<Using Include="System.Diagnostics.CodeAnalysis" />
|
||||||
<Using Include="System.Collections" />
|
<Using Include="System.Collections" />
|
||||||
@@ -35,6 +57,7 @@
|
|||||||
<Using Include="System.Runtime.InteropServices" />
|
<Using Include="System.Runtime.InteropServices" />
|
||||||
<Using Include="MeloongCore" />
|
<Using Include="MeloongCore" />
|
||||||
<Using Include="MeloongCore.Extensions" />
|
<Using Include="MeloongCore.Extensions" />
|
||||||
|
<Using Include="MeloongCore.Tests" />
|
||||||
<Using Include="Xunit" />
|
<Using Include="Xunit" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
|
|||||||
|
[23:59:44] [WARN] [FabricLoader/Metadata]: Mod stutterfix uses the version mc1.20.1-0.2.0 which isn't compatible with Loader's extended semantic version format (Could not parse version number component 'mc1'!), SemVer is recommended for reliably evaluating dependencies and prioritizing newer version
|
||||||
|
测试内容
|
||||||
Binary file not shown.
@@ -0,0 +1,11 @@
|
|||||||
|
namespace MeloongCore.Tests;
|
||||||
|
public static class TestUtils {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取测试用文件的路径。
|
||||||
|
/// </summary>
|
||||||
|
public static string GetTestFile(string directoryName, string fileName) {
|
||||||
|
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", directoryName, fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,6 +3,32 @@ using System.IO.Compression;
|
|||||||
namespace MeloongCore.Tests;
|
namespace MeloongCore.Tests;
|
||||||
public class FileUtilsTest : IDisposable {
|
public class FileUtilsTest : IDisposable {
|
||||||
|
|
||||||
|
#region 压缩包
|
||||||
|
|
||||||
|
[Fact(DisplayName = nameof(OpenZip))]
|
||||||
|
public void OpenZip() {
|
||||||
|
// GB 编码
|
||||||
|
using var gbArchive = FileUtils.OpenZip(TestUtils.GetTestFile(nameof(FileUtils), "GB Encoding.zip"));
|
||||||
|
Assert.Contains(gbArchive.Entries, e => e.Name == "中文文件.txt");
|
||||||
|
Assert.Contains(gbArchive.Entries, e => e.Name == "fabricloader.log");
|
||||||
|
Assert.DoesNotContain(gbArchive.Entries, e => e.Name == "fabricloader.log2");
|
||||||
|
// UTF8 编码
|
||||||
|
using var utfArchive = FileUtils.OpenZip(TestUtils.GetTestFile(nameof(FileUtils), "UTF8 Encoding.zip"));
|
||||||
|
Assert.Contains(utfArchive.Entries, e => e.Name == "中文文件.txt");
|
||||||
|
Assert.Contains(utfArchive.Entries, e => e.Name == "fabricloader.log");
|
||||||
|
Assert.DoesNotContain(utfArchive.Entries, e => e.Name == "fabricloader.log2");
|
||||||
|
// 损坏的文件
|
||||||
|
Assert.Throws<InvalidDataException>(() => FileUtils.OpenZip(TestUtils.GetTestFile(nameof(FileUtils), "Corrupted.zip")));
|
||||||
|
// 非压缩文件
|
||||||
|
Assert.Throws<InvalidDataException>(() => FileUtils.OpenZip(TestUtils.GetTestFile(nameof(FileUtils), "Not zip.zip")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#region 辅助方法
|
#region 辅助方法
|
||||||
|
|
||||||
private readonly string _tempDir;
|
private readonly string _tempDir;
|
||||||
@@ -66,49 +92,6 @@ public class FileUtilsTest : IDisposable {
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region OpenZip
|
|
||||||
|
|
||||||
public void OpenZip_ValidUtf8Zip_OpensSuccessfully() {
|
|
||||||
string zipPath = CreateZipWithEncoding("utf8.zip", new UTF8Encoding(false, true),
|
|
||||||
("hello.txt", "world"));
|
|
||||||
using var archive = FileUtils.OpenZip(zipPath);
|
|
||||||
Assert.Single(archive.Entries);
|
|
||||||
Assert.Equal("hello.txt", archive.Entries[0].FullName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OpenZip_ValidUtf8Zip_EntryContentIsCorrect() {
|
|
||||||
const string expectedContent = "测试内容";
|
|
||||||
string zipPath = CreateZipWithEncoding("utf8content.zip", new UTF8Encoding(false, true),
|
|
||||||
("data.txt", expectedContent));
|
|
||||||
using var archive = FileUtils.OpenZip(zipPath);
|
|
||||||
using var entryStream = archive.Entries[0].Open();
|
|
||||||
using var reader = new StreamReader(entryStream, Encoding.UTF8);
|
|
||||||
Assert.Equal(expectedContent, reader.ReadToEnd());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OpenZip_MultipleEntries_AllEntriesAccessible() {
|
|
||||||
string zipPath = CreateZipWithEncoding("multi.zip", new UTF8Encoding(false, true),
|
|
||||||
("a.txt", "aaa"),
|
|
||||||
("b.txt", "bbb"),
|
|
||||||
("c.txt", "ccc"));
|
|
||||||
using var archive = FileUtils.OpenZip(zipPath);
|
|
||||||
Assert.Equal(3, archive.Entries.Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OpenZip_InvalidFile_ThrowsInvalidDataException() {
|
|
||||||
string invalidPath = Path.Combine(_tempDir, "invalid.zip");
|
|
||||||
File.WriteAllBytes(invalidPath, [0x00, 0x01, 0x02, 0x03]); // 非 zip 数据
|
|
||||||
Assert.Throws<InvalidDataException>(() => FileUtils.OpenZip(invalidPath));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OpenZip_EmptyZip_ReturnsEmptyEntries() {
|
|
||||||
string zipPath = CreateZipWithEncoding("empty.zip", new UTF8Encoding(false, true));
|
|
||||||
using var archive = FileUtils.OpenZip(zipPath);
|
|
||||||
Assert.Empty(archive.Entries);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region ExtractToDirectory 测试
|
#region ExtractToDirectory 测试
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user