mirror of
https://github.com/Smaug123/unofficial-nunit-runner
synced 2025-10-08 18:48:40 +00:00
Set AppDomain (#15)
This commit is contained in:
@@ -10,6 +10,10 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="TestSetUp.fs" />
|
||||
<Compile Include="TestValues.fs" />
|
||||
<None Include="some-config.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<Compile Include="TestAppDomain.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
16
Consumer/TestAppDomain.fs
Normal file
16
Consumer/TestAppDomain.fs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace Consumer
|
||||
|
||||
open System
|
||||
open FsUnitTyped
|
||||
open System.IO
|
||||
open NUnit.Framework
|
||||
|
||||
[<TestFixture>]
|
||||
module TestAppDomain =
|
||||
|
||||
[<Test>]
|
||||
let ``Can load config`` () =
|
||||
Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "some-config.json")
|
||||
|> File.ReadAllText
|
||||
|> fun s -> s.Trim ()
|
||||
|> shouldEqual """{"hi":"bye"}"""
|
1
Consumer/some-config.json
Normal file
1
Consumer/some-config.json
Normal file
@@ -0,0 +1 @@
|
||||
{"hi":"bye"}
|
@@ -197,7 +197,7 @@ module TestFixture =
|
||||
| :? unit -> Ok ()
|
||||
| ret -> Error (TestReturnedNonUnit ret)
|
||||
with exc ->
|
||||
Error (TestThrew exc)
|
||||
Error (TestThrew exc.InnerException)
|
||||
|
||||
finally
|
||||
for tearDown in tearDown do
|
||||
@@ -472,7 +472,6 @@ module TestFixture =
|
||||
)
|
||||
|
||||
module Program =
|
||||
[<EntryPoint>]
|
||||
let main argv =
|
||||
let testDll, filter =
|
||||
match argv |> List.ofSeq with
|
||||
@@ -485,20 +484,42 @@ module Program =
|
||||
| Some filter -> TestFixture.shouldRun filter
|
||||
| None -> fun _ _ -> true
|
||||
|
||||
// Fix for https://github.com/Smaug123/unofficial-nunit-runner/issues/8
|
||||
// Set AppContext.BaseDirectory to where the test DLL is.
|
||||
// (This tells the DLL loader to look next to the test DLL for dependencies.)
|
||||
let oldBaseDir = AppContext.BaseDirectory
|
||||
AppContext.SetData ("APP_CONTEXT_BASE_DIRECTORY", testDll.Directory.FullName)
|
||||
let assy = Assembly.LoadFrom testDll.FullName
|
||||
|
||||
let anyFailures =
|
||||
try
|
||||
assy.ExportedTypes
|
||||
// TODO: NUnit nowadays doesn't care if you're a TestFixture or not
|
||||
|> Seq.filter (fun ty ->
|
||||
ty.CustomAttributes
|
||||
|> Seq.exists (fun attr -> attr.AttributeType.FullName = "NUnit.Framework.TestFixtureAttribute")
|
||||
)
|
||||
|> Seq.iter (fun ty ->
|
||||
|> Seq.fold
|
||||
(fun anyFailures ty ->
|
||||
let testFixture = TestFixture.parse ty
|
||||
|
||||
match TestFixture.run filter testFixture with
|
||||
| 0 -> ()
|
||||
| i -> eprintfn $"%i{i} tests failed"
|
||||
| 0 -> anyFailures
|
||||
| i ->
|
||||
eprintfn $"%i{i} tests failed"
|
||||
true
|
||||
)
|
||||
false
|
||||
finally
|
||||
AppContext.SetData ("APP_CONTEXT_BASE_DIRECTORY", oldBaseDir)
|
||||
|
||||
0
|
||||
if anyFailures then 1 else 0
|
||||
|
||||
[<EntryPoint>]
|
||||
let reallyMain argv =
|
||||
// Hack to make sure `finally`s get run.
|
||||
// (The runtime doesn't define which `finally`s, if any, run when an uncaught exception terminates execution.)
|
||||
try
|
||||
main argv
|
||||
with _ ->
|
||||
reraise ()
|
||||
|
Reference in New Issue
Block a user