Skip to content

Commit

Permalink
Improve attachments serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
dotMorten committed Nov 6, 2023
1 parent ce4e10e commit 46fbb85
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/MSTestX.Adapter/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
#if NET5_0_OR_GREATER
global using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;
#endif
20 changes: 20 additions & 0 deletions src/TestAppRunner/TestAppRunner/TestResultSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ private static void WriteType(BinaryWriter bw, object value)
{
bw.Write(t.Ticks);
}
else if (value is AttachmentSet attachmentSet) {
bw.Write(attachmentSet.Uri.OriginalString);
bw.Write(attachmentSet.DisplayName);
WriteType(bw, attachmentSet.Attachments);
}
else if (value is UriDataAttachment attachment)
{
bw.Write(attachment.Uri.OriginalString);
bw.Write(attachment.Description);
}
else if(value is System.Collections.IEnumerable enumerable)
{
var enumerator = enumerable.GetEnumerator();
Expand Down Expand Up @@ -187,6 +197,16 @@ private static object ReadValue(BinaryReader br, Type fieldType, object currentV
return TimeSpan.FromTicks(br.ReadInt64());
if (fieldType.IsEnum)
return br.ReadInt32();
if (fieldType == typeof(AttachmentSet))
{
var uri = br.ReadString();
Debug.WriteLine(uri);
var set = new AttachmentSet(new Uri(uri), br.ReadString());
ReadValue(br, set.Attachments.GetType(), set.Attachments);
return set;
}
if (fieldType == typeof(UriDataAttachment))
return new UriDataAttachment(new Uri(br.ReadString()), br.ReadString());
if (fieldType.GetInterface("System.Collections.IEnumerable") != null)
{
int count = br.ReadInt32();
Expand Down
7 changes: 6 additions & 1 deletion src/TestAppRunner/TestAppRunner/ViewModels/TestRunnerVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ await Task.Run(() =>
{
t.Result = MSTestX.UnitTestRunner.TestResultSerializer.Deserialize(previousResult.Result, t.Test);
}
catch { return; }
catch(System.Exception ex)
{
Debug.WriteLine($"Failed to deserialize {t.Test.FullyQualifiedName}: " + ex.Message);
Debug.WriteLine(ex.StackTrace);
return;
}
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/UnitTests/AttachmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public async Task TestImageAttachment()
ValidateDirectories();
var folder = TestContext.TestRunResultsDirectory;
HttpClient c = new HttpClient();
using (var stream = await c.GetStreamAsync("https://github.com/dotMorten/MSTestX/raw/master/src/TestAppRunner/TestAppRunner.iOS/Resources/Default.png"))
using (var stream = await c.GetStreamAsync("https://github.com/dotMorten/MSTestX/assets/1378165/979a60c1-2c88-4492-9a57-b2ba2cc6fbfe"))
{
folder = Path.Combine(folder, nameof(TestImageAttachment));
var di = new DirectoryInfo(folder);
if (!di.Exists) di.Create();
var filename = Path.Combine(folder, "Xamagon.png");
var filename = Path.Combine(folder, "AttachedImage.png");
using (var output = File.OpenWrite(filename))
{
await stream.CopyToAsync(output);
Expand Down

0 comments on commit 46fbb85

Please sign in to comment.