From 8367ddee11e5c19dfc089857632682739dd0db0d Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Mon, 14 Dec 2020 16:15:32 +0800 Subject: [PATCH] Backend.Tests: fix remaining exception marshalling failures on Windows There are some differences between marshalled exceptions in Windows vs other platforms, so we need to account for them in the tests. As adding stuff at runtime is harder than remove it, then JSON files will reflect exactly what happens on Windows OS, and then tweak/remove stuff for Linux/macOS. I know this makes the tests kinda fragile and quircky but it is likely that we will not end up with this way of serializing exceptions soon, as I'm mostly in exploratory development mode at the moment. --- src/GWallet.Backend.Tests/MarshallingData.fs | 43 ++++++++++++++++--- .../data/basicException.json | 3 +- .../data/customException.json | 3 +- .../data/fullException.json | 10 +++-- .../data/innerException.json | 6 ++- .../data/realException.json | 7 +-- 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/GWallet.Backend.Tests/MarshallingData.fs b/src/GWallet.Backend.Tests/MarshallingData.fs index 541cf5f73..47b228f9c 100644 --- a/src/GWallet.Backend.Tests/MarshallingData.fs +++ b/src/GWallet.Backend.Tests/MarshallingData.fs @@ -29,17 +29,43 @@ module MarshallingData = let private InjectCurrentDir (jsonContent: string): string = jsonContent.Replace("{prjDirAbsolutePath}", prjPath.FullName.Replace("\\", "/")) + let private NormalizeExceptions (jsonContent: string): string = + if System.IO.Path.DirectorySeparatorChar <> '\\' then + let weirdWindowsProperty = "\"WatsonBuckets\": null" + Assert.That(jsonContent, Is.StringContaining weirdWindowsProperty) + let removeWindowsProperty = jsonContent.Replace(", " + weirdWindowsProperty, String.Empty) + .Replace(", " + weirdWindowsProperty, String.Empty) + + removeWindowsProperty.Replace(":line 35", ":34 ").Replace(":line 68", ":67 ") + .Replace("\"8\\ncan serialize real exceptions\\nGWallet.Backend.Tests, Version={version}, Culture=neutral, PublicKeyToken=null\\nGWallet.Backend.Tests.ExceptionMarshalling\\nVoid can serialize real exceptions()\"", + "null") + .Replace("\"8\\ncan serialize full exceptions (all previous features combined)\\nGWallet.Backend.Tests, Version={version}, Culture=neutral, PublicKeyToken=null\\nGWallet.Backend.Tests.ExceptionMarshalling\\nVoid can serialize full exceptions (all previous features combined)()\"", + "null") + else + jsonContent + let internal Sanitize = RemoveJsonFormatting >> InjectCurrentVersion >> InjectCurrentDir - let private ReadEmbeddedResource resourceName = + let internal SanitizeExceptions = + RemoveJsonFormatting >> NormalizeExceptions >> InjectCurrentVersion >> InjectCurrentDir + + let private ReadEmbeddedResourceInternal resourceName exceptions = + let sanitizeFunc = + if exceptions then + SanitizeExceptions + else + Sanitize let assembly = Assembly.GetExecutingAssembly() use stream = assembly.GetManifestResourceStream resourceName if (stream = null) then failwithf "Embedded resource %s not found" resourceName use reader = new StreamReader(stream) reader.ReadToEnd() - |> Sanitize + |> sanitizeFunc + + let private ReadEmbeddedResource resourceName = + ReadEmbeddedResourceInternal resourceName false let UnsignedSaiTransactionExampleInJson = ReadEmbeddedResource "unsignedAndFormattedSaiTransaction.json" @@ -47,20 +73,23 @@ module MarshallingData = let SignedSaiTransactionExampleInJson = ReadEmbeddedResource "signedAndFormattedSaiTransaction.json" + let private ReadException fileName = + ReadEmbeddedResourceInternal fileName true + let BasicExceptionExampleInJson = - ReadEmbeddedResource "basicException.json" + ReadException "basicException.json" let RealExceptionExampleInJson = - ReadEmbeddedResource "realException.json" + ReadException "realException.json" let InnerExceptionExampleInJson = - ReadEmbeddedResource "innerException.json" + ReadException "innerException.json" let CustomExceptionExampleInJson = - ReadEmbeddedResource "customException.json" + ReadException "customException.json" let FullExceptionExampleInJson = - ReadEmbeddedResource "fullException.json" + ReadException "fullException.json" let SerializedExceptionsAreSame jsonExString1 jsonExString2 = let stackTracePath = "Value.StackTraceString" diff --git a/src/GWallet.Backend.Tests/data/basicException.json b/src/GWallet.Backend.Tests/data/basicException.json index 0d9509485..2085bbe5f 100644 --- a/src/GWallet.Backend.Tests/data/basicException.json +++ b/src/GWallet.Backend.Tests/data/basicException.json @@ -12,6 +12,7 @@ "RemoteStackIndex": 0, "ExceptionMethod": null, "HResult": -2146233088, - "Source": null + "Source": null, + "WatsonBuckets": null } } \ No newline at end of file diff --git a/src/GWallet.Backend.Tests/data/customException.json b/src/GWallet.Backend.Tests/data/customException.json index 84a66f94d..7ff38cfa3 100644 --- a/src/GWallet.Backend.Tests/data/customException.json +++ b/src/GWallet.Backend.Tests/data/customException.json @@ -12,6 +12,7 @@ "RemoteStackIndex": 0, "ExceptionMethod": null, "HResult": -2146233088, - "Source": null + "Source": null, + "WatsonBuckets": null } } \ No newline at end of file diff --git a/src/GWallet.Backend.Tests/data/fullException.json b/src/GWallet.Backend.Tests/data/fullException.json index 7285e36ca..95cea8889 100644 --- a/src/GWallet.Backend.Tests/data/fullException.json +++ b/src/GWallet.Backend.Tests/data/fullException.json @@ -16,14 +16,16 @@ "RemoteStackIndex": 0, "ExceptionMethod": null, "HResult": -2146233088, - "Source": null + "Source": null, + "WatsonBuckets": null }, "HelpURL": null, - "StackTraceString": " at GWallet.Backend.Tests.ExceptionMarshalling.can serialize full exceptions (all previous features combined) () [0x00016] in {prjDirAbsolutePath}/ExceptionMarshalling.fs:67 ", + "StackTraceString": " at GWallet.Backend.Tests.ExceptionMarshalling.can serialize full exceptions (all previous features combined) () [0x00016] in {prjDirAbsolutePath}/ExceptionMarshalling.fs:line 68", "RemoteStackTraceString": null, "RemoteStackIndex": 0, - "ExceptionMethod": null, + "ExceptionMethod": "8\ncan serialize full exceptions (all previous features combined)\nGWallet.Backend.Tests, Version={version}, Culture=neutral, PublicKeyToken=null\nGWallet.Backend.Tests.ExceptionMarshalling\nVoid can serialize full exceptions (all previous features combined)()", "HResult": -2146233088, - "Source": "GWallet.Backend.Tests" + "Source": "GWallet.Backend.Tests", + "WatsonBuckets": null } } \ No newline at end of file diff --git a/src/GWallet.Backend.Tests/data/innerException.json b/src/GWallet.Backend.Tests/data/innerException.json index 5f41f4be2..d4572ef85 100644 --- a/src/GWallet.Backend.Tests/data/innerException.json +++ b/src/GWallet.Backend.Tests/data/innerException.json @@ -16,7 +16,8 @@ "RemoteStackIndex": 0, "ExceptionMethod": null, "HResult": -2146233088, - "Source": null + "Source": null, + "WatsonBuckets": null }, "HelpURL": null, "StackTraceString": null, @@ -24,6 +25,7 @@ "RemoteStackIndex": 0, "ExceptionMethod": null, "HResult": -2146233088, - "Source": null + "Source": null, + "WatsonBuckets": null } } \ No newline at end of file diff --git a/src/GWallet.Backend.Tests/data/realException.json b/src/GWallet.Backend.Tests/data/realException.json index 3a3711ecd..df37c96a8 100644 --- a/src/GWallet.Backend.Tests/data/realException.json +++ b/src/GWallet.Backend.Tests/data/realException.json @@ -7,11 +7,12 @@ "Data": null, "InnerException": null, "HelpURL": null, - "StackTraceString": " at GWallet.Backend.Tests.ExceptionMarshalling.can serialize real exceptions () [0x0000c] in {prjDirAbsolutePath}/ExceptionMarshalling.fs:34 ", + "StackTraceString": " at GWallet.Backend.Tests.ExceptionMarshalling.can serialize real exceptions () [0x0000c] in {prjDirAbsolutePath}/ExceptionMarshalling.fs:line 35", "RemoteStackTraceString": null, "RemoteStackIndex": 0, - "ExceptionMethod": null, + "ExceptionMethod": "8\ncan serialize real exceptions\nGWallet.Backend.Tests, Version={version}, Culture=neutral, PublicKeyToken=null\nGWallet.Backend.Tests.ExceptionMarshalling\nVoid can serialize real exceptions()", "HResult": -2146233088, - "Source": "GWallet.Backend.Tests" + "Source": "GWallet.Backend.Tests", + "WatsonBuckets": null } } \ No newline at end of file