Skip to content

Commit

Permalink
Backend.Tests: fix remaining exception marshalling failures on Windows
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
knocte committed Dec 14, 2020
1 parent 510b7ab commit 8367dde
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
43 changes: 36 additions & 7 deletions src/GWallet.Backend.Tests/MarshallingData.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,67 @@ 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"

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"
Expand Down
3 changes: 2 additions & 1 deletion src/GWallet.Backend.Tests/data/basicException.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2146233088,
"Source": null
"Source": null,
"WatsonBuckets": null
}
}
3 changes: 2 additions & 1 deletion src/GWallet.Backend.Tests/data/customException.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2146233088,
"Source": null
"Source": null,
"WatsonBuckets": null
}
}
10 changes: 6 additions & 4 deletions src/GWallet.Backend.Tests/data/fullException.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 4 additions & 2 deletions src/GWallet.Backend.Tests/data/innerException.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2146233088,
"Source": null
"Source": null,
"WatsonBuckets": null
},
"HelpURL": null,
"StackTraceString": null,
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2146233088,
"Source": null
"Source": null,
"WatsonBuckets": null
}
}
7 changes: 4 additions & 3 deletions src/GWallet.Backend.Tests/data/realException.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 8367dde

Please sign in to comment.