From c6f339c7380a005cf601b5a45cbed701e1222e55 Mon Sep 17 00:00:00 2001 From: Patrick Stevens <3138005+Smaug123@users.noreply.github.com> Date: Thu, 6 Jun 2024 20:50:48 +0100 Subject: [PATCH] Set working directory for tests (#36) --- Consumer/TestAppDomain.fs | 9 ++++++++- TestRunner.Lib/Domain.fs | 5 ++++- TestRunner.Lib/SurfaceBaseline.txt | 8 +++++--- TestRunner.Lib/TestFixture.fs | 8 +++++++- TestRunner.Lib/version.json | 2 +- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Consumer/TestAppDomain.fs b/Consumer/TestAppDomain.fs index 8f588d3..dd59cb1 100644 --- a/Consumer/TestAppDomain.fs +++ b/Consumer/TestAppDomain.fs @@ -9,8 +9,15 @@ open NUnit.Framework module TestAppDomain = [] - let ``Can load config`` () = + let ``Can load config from app domain`` () = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "some-config.json") |> File.ReadAllText |> fun s -> s.Trim () |> shouldEqual """{"hi":"bye"}""" + + [] + let ``Can load config from working dir`` () = + "some-config.json" + |> File.ReadAllText + |> fun s -> s.Trim () + |> shouldEqual """{"hi":"bye"}""" diff --git a/TestRunner.Lib/Domain.fs b/TestRunner.Lib/Domain.fs index 69c940c..04295a2 100644 --- a/TestRunner.Lib/Domain.fs +++ b/TestRunner.Lib/Domain.fs @@ -57,6 +57,8 @@ type SingleTestMethod = /// each of which may run many times. type TestFixture = { + /// The assembly which contains this TestFixture, loaded into a separate context. + ContainingAssembly : Assembly /// Fully-qualified name of this fixture (e.g. MyThing.Test.Foo for `[] module Foo` in the /// `MyThing.Test` assembly). Name : string @@ -80,8 +82,9 @@ type TestFixture = } /// A test fixture about which we know nothing. No tests, no setup/teardown. - static member Empty (name : string) = + static member Empty (containingAssembly : Assembly) (name : string) = { + ContainingAssembly = containingAssembly Name = name OneTimeSetUp = None OneTimeTearDown = None diff --git a/TestRunner.Lib/SurfaceBaseline.txt b/TestRunner.Lib/SurfaceBaseline.txt index cbb0455..5bde67e 100644 --- a/TestRunner.Lib/SurfaceBaseline.txt +++ b/TestRunner.Lib/SurfaceBaseline.txt @@ -161,8 +161,10 @@ TestRunner.TestFailure.NewTearDownFailed [static method]: TestRunner.UserMethodF TestRunner.TestFailure.NewTestFailed [static method]: TestRunner.UserMethodFailure -> TestRunner.TestFailure TestRunner.TestFailure.Tag [property]: [read-only] int TestRunner.TestFixture inherit obj, implements TestRunner.TestFixture System.IEquatable, System.Collections.IStructuralEquatable -TestRunner.TestFixture..ctor [constructor]: (string, System.Reflection.MethodInfo option, System.Reflection.MethodInfo option, System.Reflection.MethodInfo list, System.Reflection.MethodInfo list, TestRunner.SingleTestMethod list) -TestRunner.TestFixture.Empty [static method]: string -> TestRunner.TestFixture +TestRunner.TestFixture..ctor [constructor]: (System.Reflection.Assembly, string, System.Reflection.MethodInfo option, System.Reflection.MethodInfo option, System.Reflection.MethodInfo list, System.Reflection.MethodInfo list, TestRunner.SingleTestMethod list) +TestRunner.TestFixture.ContainingAssembly [property]: [read-only] System.Reflection.Assembly +TestRunner.TestFixture.Empty [static method]: System.Reflection.Assembly -> string -> TestRunner.TestFixture +TestRunner.TestFixture.get_ContainingAssembly [method]: unit -> System.Reflection.Assembly TestRunner.TestFixture.get_Name [method]: unit -> string TestRunner.TestFixture.get_OneTimeSetUp [method]: unit -> System.Reflection.MethodInfo option TestRunner.TestFixture.get_OneTimeTearDown [method]: unit -> System.Reflection.MethodInfo option @@ -267,4 +269,4 @@ TestRunner.UserMethodFailure.IsThrew [property]: [read-only] bool TestRunner.UserMethodFailure.Name [property]: [read-only] string TestRunner.UserMethodFailure.NewReturnedNonUnit [static method]: (string, obj) -> TestRunner.UserMethodFailure TestRunner.UserMethodFailure.NewThrew [static method]: (string, System.Exception) -> TestRunner.UserMethodFailure -TestRunner.UserMethodFailure.Tag [property]: [read-only] int +TestRunner.UserMethodFailure.Tag [property]: [read-only] int \ No newline at end of file diff --git a/TestRunner.Lib/TestFixture.fs b/TestRunner.Lib/TestFixture.fs index a0f9100..c97403e 100644 --- a/TestRunner.Lib/TestFixture.fs +++ b/TestRunner.Lib/TestFixture.fs @@ -1,6 +1,7 @@ namespace TestRunner open System +open System.IO open System.Reflection open System.Threading open Microsoft.FSharp.Core @@ -281,6 +282,9 @@ module TestFixture = ) |> Option.toObj + let oldWorkDir = Environment.CurrentDirectory + Environment.CurrentDirectory <- FileInfo(tests.ContainingAssembly.Location).Directory.FullName + let setupResult = match tests.OneTimeSetUp with | Some su -> @@ -331,6 +335,8 @@ module TestFixture = Some (UserMethodFailure.Threw (td.Name, e)) | _ -> None + Environment.CurrentDirectory <- oldWorkDir + { Failed = testFailures |> Seq.toList SuccessCount = totalTestSuccess.Value @@ -346,7 +352,7 @@ module TestFixture = |> Seq.map (fun attr -> attr.ConstructorArguments |> Seq.exactlyOne |> _.Value |> unbox) |> Seq.toList - (TestFixture.Empty parentType.Name, parentType.GetRuntimeMethods ()) + (TestFixture.Empty parentType.Assembly parentType.Name, parentType.GetRuntimeMethods ()) ||> Seq.fold (fun state mi -> ((state, []), mi.CustomAttributes) ||> Seq.fold (fun (state, unrecognisedAttrs) attr -> diff --git a/TestRunner.Lib/version.json b/TestRunner.Lib/version.json index f57433b..b8c4ffc 100644 --- a/TestRunner.Lib/version.json +++ b/TestRunner.Lib/version.json @@ -1,5 +1,5 @@ { - "version": "0.2", + "version": "0.3", "publicReleaseRefSpec": null, "pathFilters": [ "./",