Skip to content

Commit 0cc74c4

Browse files
committed
fix: IO 测试 net471 兼容:添加 System.Memory,修复 File.ReadAllBytesAsync/WriteAllBytesAsync/Write/MemoryStream.Write/string.Replace API,NuGet 集成测试 #if NET 包裹
1 parent b0f1cbb commit 0cc74c4

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

tests/Magicodes.IE.IO.Tests/Magicodes.IE.IO.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
</ItemGroup>
2828

2929
<ItemGroup>
30+
<PackageReference Include="System.Memory" Condition="'$(TargetFramework)' == 'net471'" />
3031
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net471" Condition="'$(TargetFramework)' == 'net471'" PrivateAssets="all" />
3132
</ItemGroup>
3233

tests/Magicodes.IE.IO.Tests/NuGetConsumerIntegrationTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
using System.IO;
44
using System.IO.Compression;
55
using System.Linq;
6-
using System.Runtime.InteropServices;
76
using System.Threading.Tasks;
87
using Magicodes.IE.IO;
98
using Xunit;
109

10+
#if NET
1111
namespace Magicodes.IE.IO.Tests;
1212

1313
public sealed class NuGetConsumerIntegrationTests
@@ -46,7 +46,7 @@ private static void RunPackedConsumer(bool publishAot)
4646
var publishProperties = publishAot ? "<PublishAot>true</PublishAot><PublishTrimmed>true</PublishTrimmed>" : string.Empty;
4747
File.WriteAllText(Path.Combine(consumer, "Consumer.csproj"), $"<Project Sdk=\"Microsoft.NET.Sdk\"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net8.0</TargetFramework><Nullable>enable</Nullable>{publishProperties}</PropertyGroup><ItemGroup><PackageReference Include=\"Magicodes.IE.IO\" Version=\"{packageVersion}\" /></ItemGroup></Project>");
4848
File.WriteAllText(Path.Combine(consumer, "Program.cs"), "using System.IO; using System.Linq; using Magicodes.IE.IO; namespace Consumer; public sealed class MoneyConverter : CellConverter<decimal> { public override bool Read(string cell, out decimal value) => decimal.TryParse(cell, out value); } [XlsxExportable] public sealed class ConsumerRow { public string Name { get; set; } = \"\"; public decimal Amount { get; set; } } public static class Program { public static void Main() { var metadata = XlsxGeneratedTypeMetadataRegistry.TryGet<ConsumerRow>(); if (metadata is null) throw new System.Exception(\"metadata:null\"); using var ms = new MemoryStream(); Xlsx.Write(ms, new[] { new ConsumerRow { Name = \"ok\", Amount = 12.5m } }); ms.Position = 0; var options = new XlsxReadOptions<ConsumerRow>().WithConverter(new MoneyConverter()); var row = Xlsx.Read<ConsumerRow>(ms, options).Single(); if (row.Name != \"ok\" || row.Amount != 12.5m) throw new System.Exception($\"{row.Name}|{row.Amount}\"); } }");
49-
var rid = RuntimeInformation.RuntimeIdentifier;
49+
var rid = System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier;
5050
Run(dotnet, consumer, $"restore --no-cache --configfile NuGet.config{(publishAot ? $" -r {rid}" : string.Empty)} -v:minimal");
5151
if (!publishAot)
5252
{
@@ -58,7 +58,7 @@ private static void RunPackedConsumer(bool publishAot)
5858
var publishRoot = Path.Combine(consumer, "bin", "Release", "net8.0", rid, "publish");
5959
var executable = Directory.GetFiles(publishRoot, "*", SearchOption.TopDirectoryOnly)
6060
.SingleOrDefault(path =>
61-
string.Equals(Path.GetFileName(path), OperatingSystem.IsWindows() ? "Consumer.exe" : "Consumer", StringComparison.Ordinal));
61+
string.Equals(Path.GetFileName(path), System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows) ? "Consumer.exe" : "Consumer", StringComparison.Ordinal));
6262
if (executable is null)
6363
throw new FileNotFoundException($"NativeAOT executable was not found under {publishRoot}. Files: {string.Join(", ", Directory.Exists(publishRoot) ? Directory.GetFiles(publishRoot, "*", SearchOption.AllDirectories) : Array.Empty<string>())}");
6464
Run(executable, consumer, string.Empty);
@@ -94,3 +94,4 @@ private static string FindRepositoryRoot()
9494
return directory?.FullName ?? throw new DirectoryNotFoundException("Magicodes.IE repository root not found.");
9595
}
9696
}
97+
#endif

tests/Magicodes.IE.IO.Tests/XlsxIO_Core_Tests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public async Task SaveAsync_PlainDto_WritesFile()
2525
{
2626
Xlsx.Write(path, new[] { new OrderDto { OrderNo = "A1", Amount = 10m, CreatedAt = new DateTime(2024, 1, 1) } });
2727
File.Exists(path).ShouldBeTrue();
28+
#if NET6_0_OR_GREATER
2829
var bytes = await File.ReadAllBytesAsync(path);
30+
#else
31+
var bytes = File.ReadAllBytes(path);
32+
#endif
2933
var rows = XlsxIO_TestSupport.ReadSheet(bytes);
3034
rows.Count.ShouldBe(2);
3135
rows[1][0].ShouldBe("A1");
@@ -415,7 +419,7 @@ public async Task WriteMultiSheetAsync_Stream_TwoSheets_BothWritten()
415419
var bytes = Xlsx.WriteWorkbookToBytes(
416420
new Sheet("First", new[] { new OrderDto { OrderNo = "A" } }),
417421
new Sheet("Second", new[] { new OrderDto { OrderNo = "B" } }));
418-
ms.Write(bytes);
422+
ms.Write(bytes, 0, bytes.Length);
419423
var xml = XlsxIO_TestSupport.ReadEntry(ms.ToArray(), "xl/workbook.xml");
420424
xml.ShouldContain("First");
421425
xml.ShouldContain("Second");
@@ -430,7 +434,11 @@ public async Task SaveMultiSheetAsync_Path_TwoSheets_BothWritten()
430434
var bytes = Xlsx.WriteWorkbookToBytes(
431435
new Sheet("First", new[] { new OrderDto { OrderNo = "A" } }),
432436
new Sheet("Second", new[] { new OrderDto { OrderNo = "B" } }));
437+
#if NET6_0_OR_GREATER
433438
await File.WriteAllBytesAsync(path, bytes);
439+
#else
440+
File.WriteAllBytes(path, bytes);
441+
#endif
434442
var xml = XlsxIO_TestSupport.ReadEntry(bytes, "xl/workbook.xml");
435443
xml.ShouldContain("First");
436444
xml.ShouldContain("Second");
@@ -621,7 +629,7 @@ public async Task WriteMultiSheetAsync_Stream_WritesValidWorkbook()
621629
var bytes = Xlsx.WriteWorkbookToBytes(
622630
new Sheet<OrderDto>("S1", new[] { new OrderDto { OrderNo = "A" } }),
623631
new Sheet<OrderDto>("S2", Array.Empty<OrderDto>()));
624-
ms.Write(bytes);
632+
ms.Write(bytes, 0, bytes.Length);
625633
var workbookXml = XlsxIO_TestSupport.ReadEntry(ms.ToArray(), "xl/workbook.xml");
626634
workbookXml.ShouldContain("S1");
627635
workbookXml.ShouldContain("S2");
@@ -654,7 +662,7 @@ async IAsyncEnumerable<OrderDto> Data()
654662
await Xlsx.WriteAsync(ms, Data(), p => p.Sheet("Orders"));
655663
var bytes = ms.ToArray();
656664
var workbookXml = XlsxIO_TestSupport.ReadEntry(bytes, "xl/workbook.xml");
657-
var sheetTagCount = (workbookXml.Length - workbookXml.Replace("<sheet ", string.Empty, StringComparison.Ordinal).Length) / "<sheet ".Length;
665+
var sheetTagCount = (workbookXml.Length - workbookXml.Replace("<sheet ", "").Length) / "<sheet ".Length;
658666

659667
sheetTagCount.ShouldBe(1);
660668
workbookXml.ShouldContain("Orders");

0 commit comments

Comments
 (0)