Skip to content

Commit

Permalink
test proper workaround for haf/expecto#367
Browse files Browse the repository at this point in the history
  • Loading branch information
matthid committed Dec 17, 2019
1 parent d92525a commit 46411ce
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ Target.create "HostDocs" (fun _ ->

let runExpecto workDir dllPath resultsXml =
let processResult =
DotNet.exec (dtntWorkDir workDir) (sprintf "%s" dllPath) "--summary --debug --sequenced --no-spinner"
DotNet.exec (dtntWorkDir workDir) (sprintf "%s" dllPath) "--summary"

if processResult.ExitCode <> 0 then failwithf "Tests in %s failed." (Path.GetFileName dllPath)
Trace.publish (ImportData.Nunit NunitDataVersion.Nunit) (workDir </> resultsXml)
Expand Down
3 changes: 2 additions & 1 deletion src/test/Fake.Core.CommandLine.UnitTests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ let main argv =
let config =
defaultConfig.appendSummaryHandler writeResults
|> ExpectoHelpers.addTimeout (TimeSpan.FromMinutes(20.))
|> ExpectoHelpers.setFakePrinter

FakeExpecto.Tests.runTestsInAssembly { config with parallel = false } argv
Expecto.Tests.runTestsInAssembly { config with parallel = false } argv
3 changes: 2 additions & 1 deletion src/test/Fake.Core.IntegrationTests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ let main argv =
let config =
defaultConfig.appendSummaryHandler writeResults
|> ExpectoHelpers.addTimeout (TimeSpan.FromMinutes(20.))
FakeExpecto.Tests.runTestsInAssembly { config with parallel = false } argv
|> ExpectoHelpers.setFakePrinter
Expecto.Tests.runTestsInAssembly { config with parallel = false } argv
3 changes: 2 additions & 1 deletion src/test/Fake.Core.UnitTests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ let main argv =
let config =
defaultConfig.appendSummaryHandler writeResults
|> ExpectoHelpers.addTimeout (TimeSpan.FromMinutes(20.))
FakeExpecto.Tests.runTestsInAssembly config argv
|> ExpectoHelpers.setFakePrinter
Expecto.Tests.runTestsInAssembly config argv
3 changes: 2 additions & 1 deletion src/test/Fake.DotNet.Cli.IntegrationTests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ let main argv =
let config =
defaultConfig.appendSummaryHandler writeResults
|> ExpectoHelpers.addTimeout (TimeSpan.FromMinutes(20.))
FakeExpecto.Tests.runTestsInAssembly { config with parallel = false } argv
|> ExpectoHelpers.setFakePrinter
Expecto.Tests.runTestsInAssembly { config with parallel = false } argv
113 changes: 95 additions & 18 deletions src/test/Fake.ExpectoSupport/ExpectoHelpers.fs
Original file line number Diff line number Diff line change
@@ -1,47 +1,124 @@
namespace Fake.ExpectoSupport
open Expecto

open System
open System.Threading
open System.Threading.Tasks

module ExpectoHelpers =
let addFilter f (config:Impl.ExpectoConfig) =

let inline internal commaString (i:int) = i.ToString("#,##0")
// because of https://github.com/haf/expecto/issues/367
let fakeDefaultPrinter : Expecto.Impl.TestPrinters =
{ beforeRun = fun _tests ->
printfn "EXPECTO? Running tests..."
async.Zero()

beforeEach = fun n ->
printfn "EXPECTO? %s starting..." n
async.Zero()

info = fun s ->
printfn "EXPECTO? %s" s
async.Zero()

passed = fun n d ->
printfn "EXPECTO? %s passed in %O." n d
async.Zero()

ignored = fun n m ->
printfn "EXPECTO? %s was ignored. %s" n m
async.Zero()

failed = fun n m d ->
printfn "EXPECTO? %s failed in %O. %s" n d m
async.Zero()

exn = fun n e d ->
printfn "EXPECTO? %s errored in %O: %O" n d e
async.Zero()

summary = fun _config summary ->
let spirit =
if summary.successful then "Success!" else String.Empty
let commonAncestor =
let rec loop ancestor (descendants : string list) =
match descendants with
| [] -> ancestor
| hd::tl when hd.StartsWith(ancestor)->
loop ancestor tl
| _ ->
if ancestor.Contains("/") then
loop (ancestor.Substring(0, ancestor.LastIndexOf "/")) descendants
else
"miscellaneous"

let parentNames =
summary.results
|> List.map (fun (flatTest, _) ->
if flatTest.name.Contains("/") then
flatTest.name.Substring(0, flatTest.name.LastIndexOf "/")
else
flatTest.name )

match parentNames with
| [x] -> x
| hd::tl ->
loop hd tl
| _ -> "miscellaneous" //we can't get here
printfn "EXPECTO! %s tests run in %O for %s%s passed, %s ignored, %s failed, %s errored. %s"
(summary.results |> List.sumBy (fun (_,r) -> if r.result.isIgnored then 0 else r.count) |> commaString)
summary.duration
commonAncestor
(summary.passed |> List.sumBy (fun (_,r) -> r.count) |> commaString)
(summary.ignored |> List.sumBy (fun (_,r) -> r.count) |> commaString)
(summary.failed |> List.sumBy (fun (_,r) -> r.count) |> commaString)
(summary.errored |> List.sumBy (fun (_,r) -> r.count) |> commaString)
spirit
async.Zero()
}

let setPrinter printer (config:Expecto.Impl.ExpectoConfig) =
{ config with printer = printer }
let setFakePrinter (config:Expecto.Impl.ExpectoConfig) =
setPrinter fakeDefaultPrinter config

let addFilter f (config:Expecto.Impl.ExpectoConfig) =
{ config with
filter = fun test ->
let filteredTests = config.filter test
f filteredTests }

let withTimeout (timeout:TimeSpan) (labelPath:string) (test: TestCode) : TestCode =
let withTimeout (timeout:TimeSpan) (labelPath:string) (test: Expecto.TestCode) : Expecto.TestCode =
let timeoutAsync testAsync =
async {
let t = Async.StartAsTask(testAsync)
let delay = Task.Delay(timeout)
let! result = Task.WhenAny(t, delay) |> Async.AwaitTask
if result = delay then
Tests.failtestf "Test '%s' timed out" labelPath
Expecto.Tests.failtestf "Test '%s' timed out" labelPath
}

match test with
| Sync test -> async { test() } |> timeoutAsync |> Async
| SyncWithCancel test ->
SyncWithCancel (fun ct ->
| Expecto.Sync test -> async { test() } |> timeoutAsync |> Expecto.Async
| Expecto.SyncWithCancel test ->
Expecto.SyncWithCancel (fun ct ->
Async.StartImmediate(async { test ct } |> timeoutAsync)
)
| Async test -> timeoutAsync test |> Async
| AsyncFsCheck (testConfig, stressConfig, test) ->
AsyncFsCheck (testConfig, stressConfig, test >> timeoutAsync)
| Expecto.Async test -> timeoutAsync test |> Expecto.Async
| Expecto.AsyncFsCheck (testConfig, stressConfig, test) ->
Expecto.AsyncFsCheck (testConfig, stressConfig, test >> timeoutAsync)

let mapTest f test =
let rec recMapping labelPath test =
match test with
| TestCase (code:TestCode, state:FocusState) ->
TestCase(f labelPath code, state)
| TestList (tests:Test list, state:FocusState) ->
TestList (tests |> List.map (recMapping labelPath), state)
| TestLabel (label:string, test:Test, state:FocusState) ->
TestLabel(label, recMapping (labelPath + "/" + label) test, state)
| Test.Sequenced (sequenceMethod, test) ->
Test.Sequenced(sequenceMethod, recMapping labelPath test)
| Expecto.TestCase (code:Expecto.TestCode, state:Expecto.FocusState) ->
Expecto.TestCase(f labelPath code, state)
| Expecto.TestList (tests:Expecto.Test list, state:Expecto.FocusState) ->
Expecto.TestList (tests |> List.map (recMapping labelPath), state)
| Expecto.TestLabel (label:string, test:Expecto.Test, state:Expecto.FocusState) ->
Expecto.TestLabel(label, recMapping (labelPath + "/" + label) test, state)
| Expecto.Test.Sequenced (sequenceMethod, test) ->
Expecto.Test.Sequenced(sequenceMethod, recMapping labelPath test)

recMapping "" test

Expand Down
3 changes: 1 addition & 2 deletions src/test/Fake.ExpectoSupport/Fake.ExpectoSupport.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="FakeExpecto.fs" />
<None Include="paket.references" />
<Compile Include="ExpectoHelpers.fs" />
</ItemGroup>

<ItemGroup />
<Import Project="..\..\..\.paket\Paket.Restore.targets" />

</Project>

0 comments on commit 46411ce

Please sign in to comment.