mirror of
https://github.com/ZuneDev/ZuneUIXTools.git
synced 2026-07-27 13:11:59 -07:00
Add TempFile helper
This commit is contained in:
@@ -41,22 +41,17 @@ main:
|
||||
[InlineData("text.uix")]
|
||||
[InlineData("testA.uix")]
|
||||
[InlineData("testB.uix")]
|
||||
public async Task Disassemble(string fileName)
|
||||
public async Task Disassemble(string testResourceName)
|
||||
{
|
||||
string tempFilePath = Path.GetTempFileName();
|
||||
string uri = "file://" + tempFilePath;
|
||||
using TempFile tempFile = new(testResourceName);
|
||||
await tempFile.InitAsync();
|
||||
|
||||
// Copy the test data to a temporary file because Iris can only load from a URI.
|
||||
var testFileBytes = (byte[])TestResources.ResourceManager.GetObject(fileName)!;
|
||||
await File.WriteAllBytesAsync(tempFilePath, testFileBytes);
|
||||
string uri = "file://" + tempFile.Path;
|
||||
|
||||
var sourceLoadResult = MarkupSystem.ResolveLoadResult(uri, MarkupSystem.RootIslandId);
|
||||
var dis = Disassembler.Load(sourceLoadResult as MarkupLoadResult);
|
||||
|
||||
var disText = dis.Write();
|
||||
output.WriteLine(disText);
|
||||
|
||||
// Clean up temporary file
|
||||
File.Delete(tempFilePath);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
global using UIX.Test.Helpers;
|
||||
global using Xunit;
|
||||
@@ -0,0 +1,48 @@
|
||||
using OwlCore.ComponentModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace UIX.Test.Helpers;
|
||||
|
||||
internal class TempFile : IDisposable, IAsyncInit
|
||||
{
|
||||
private readonly Func<Task> _init;
|
||||
|
||||
public TempFile(System.Resources.ResourceManager resourceManager, string resourceName)
|
||||
: this((byte[])resourceManager.GetObject(resourceName)!)
|
||||
{
|
||||
}
|
||||
|
||||
public TempFile(string resourceName) : this(TestResources.ResourceManager, resourceName)
|
||||
{
|
||||
}
|
||||
|
||||
public TempFile(byte[] data)
|
||||
{
|
||||
_init = async delegate
|
||||
{
|
||||
// Copy the test data to a temporary file because Iris can only load from a URI.
|
||||
Path = System.IO.Path.GetTempFileName();
|
||||
await File.WriteAllBytesAsync(Path, data);
|
||||
};
|
||||
}
|
||||
|
||||
public string? Path { get; private set; }
|
||||
|
||||
[MemberNotNullWhen(true, nameof(Path))]
|
||||
public bool IsInitialized { get; private set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!IsInitialized) return;
|
||||
|
||||
File.Delete(Path);
|
||||
}
|
||||
|
||||
[MemberNotNull(nameof(Path))]
|
||||
public async Task InitAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
#pragma warning disable CS8774 // Member must have a non-null value when exiting.
|
||||
await _init();
|
||||
#pragma warning restore CS8774 // Member must have a non-null value when exiting.
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
|
||||
<PackageReference Include="OwlCore.ComponentModel" Version="0.6.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
Reference in New Issue
Block a user