From 46fbb85a9eba3074dc987bf60ce6d6efd5bf2c83 Mon Sep 17 00:00:00 2001 From: Morten Nielsen Date: Mon, 6 Nov 2023 13:48:45 -0800 Subject: [PATCH] Improve attachments serialization --- src/MSTestX.Adapter/GlobalUsings.cs | 4 +++- .../TestAppRunner/TestResultSerializer.cs | 20 +++++++++++++++++++ .../TestAppRunner/ViewModels/TestRunnerVM.cs | 7 ++++++- src/UnitTests/AttachmentTests.cs | 4 ++-- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/MSTestX.Adapter/GlobalUsings.cs b/src/MSTestX.Adapter/GlobalUsings.cs index 057ac5d..930450a 100644 --- a/src/MSTestX.Adapter/GlobalUsings.cs +++ b/src/MSTestX.Adapter/GlobalUsings.cs @@ -1 +1,3 @@ -global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file +#if NET5_0_OR_GREATER +global using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter; +#endif \ No newline at end of file diff --git a/src/TestAppRunner/TestAppRunner/TestResultSerializer.cs b/src/TestAppRunner/TestAppRunner/TestResultSerializer.cs index 94ed7a4..c2a6791 100644 --- a/src/TestAppRunner/TestAppRunner/TestResultSerializer.cs +++ b/src/TestAppRunner/TestAppRunner/TestResultSerializer.cs @@ -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(); @@ -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(); diff --git a/src/TestAppRunner/TestAppRunner/ViewModels/TestRunnerVM.cs b/src/TestAppRunner/TestAppRunner/ViewModels/TestRunnerVM.cs index 7a67c29..2eeabd9 100644 --- a/src/TestAppRunner/TestAppRunner/ViewModels/TestRunnerVM.cs +++ b/src/TestAppRunner/TestAppRunner/ViewModels/TestRunnerVM.cs @@ -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 { diff --git a/src/UnitTests/AttachmentTests.cs b/src/UnitTests/AttachmentTests.cs index fa22402..5c2387b 100644 --- a/src/UnitTests/AttachmentTests.cs +++ b/src/UnitTests/AttachmentTests.cs @@ -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);